Failed Conditions
Push — release-2.1 ( 8d1977...8da17b )
by Rick
06:19
created

ssi_queryMembers()   B

Complexity

Conditions 10
Paths 35

Size

Total Lines 65
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 31
nop 5
dl 0
loc 65
rs 7.6666
c 0
b 0
f 0
nc 35

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * Simple Machines Forum (SMF)
5
 *
6
 * @package SMF
7
 * @author Simple Machines https://www.simplemachines.org
8
 * @copyright 2020 Simple Machines and individual contributors
9
 * @license https://www.simplemachines.org/about/smf/license.php BSD
10
 *
11
 * @version 2.1 RC2
12
 */
13
14
// Don't do anything if SMF is already loaded.
15
if (defined('SMF'))
16
	return true;
17
18
define('SMF', 'SSI');
19
define('SMF_VERSION', '2.1 RC2');
20
define('SMF_FULL_VERSION', 'SMF ' . SMF_VERSION);
21
define('SMF_SOFTWARE_YEAR', '2020');
22
define('JQUERY_VERSION', '3.4.1');
23
define('POSTGRE_TITLE', 'PostgreSQL');
24
define('MYSQL_TITLE', 'MySQL');
25
define('SMF_USER_AGENT', 'Mozilla/5.0 (' . php_uname('s') . ' ' . php_uname('m') . ') AppleWebKit/605.1.15 (KHTML, like Gecko)  SMF/' . strtr(SMF_VERSION, ' ', '.'));
26
27
28
// We're going to want a few globals... these are all set later.
29
global $maintenance, $msubject, $mmessage, $mbname, $language;
30
global $boardurl, $boarddir, $sourcedir, $webmaster_email, $cookiename;
31
global $db_type, $db_server, $db_name, $db_user, $db_prefix, $db_persist, $db_error_send, $db_last_error, $db_show_debug;
32
global $db_connection, $db_port, $modSettings, $context, $sc, $user_info, $topic, $board, $txt;
33
global $smcFunc, $ssi_db_user, $scripturl, $ssi_db_passwd, $db_passwd, $cache_enable, $cachedir;
34
global $auth_secret;
35
36
if (!defined('TIME_START'))
37
	define('TIME_START', microtime(true));
38
39
// Just being safe...
40
foreach (array('db_character_set', 'cachedir') as $variable)
41
	unset($GLOBALS[$variable]);
42
43
// Get the forum's settings for database and file paths.
44
require_once(dirname(__FILE__) . '/Settings.php');
45
46
// Make absolutely sure the cache directory is defined and writable.
47
if (empty($cachedir) || !is_dir($cachedir) || !is_writable($cachedir))
48
{
49
	if (is_dir($boarddir . '/cache') && is_writable($boarddir . '/cache'))
50
		$cachedir = $boarddir . '/cache';
51
	else
52
	{
53
		$cachedir = sys_get_temp_dir() . '/smf_cache_' . md5($boarddir);
54
		@mkdir($cachedir, 0750);
55
	}
56
}
57
58
$ssi_error_reporting = error_reporting(!empty($db_show_debug) ? E_ALL : E_ALL & ~E_DEPRECATED);
59
/* Set this to one of three values depending on what you want to happen in the case of a fatal error.
60
	false:	Default, will just load the error sub template and die - not putting any theme layers around it.
61
	true:	Will load the error sub template AND put the SMF layers around it (Not useful if on total custom pages).
62
	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.
63
*/
64
$ssi_on_error_method = false;
65
66
// Don't do john didley if the forum's been shut down completely.
67
if ($maintenance == 2 && (!isset($ssi_maintenance_off) || $ssi_maintenance_off !== true))
68
	die($mmessage);
69
70
// Fix for using the current directory as a path.
71
if (substr($sourcedir, 0, 1) == '.' && substr($sourcedir, 1, 1) != '.')
72
	$sourcedir = dirname(__FILE__) . substr($sourcedir, 1);
73
74
// Load the important includes.
75
require_once($sourcedir . '/QueryString.php');
76
require_once($sourcedir . '/Session.php');
77
require_once($sourcedir . '/Subs.php');
78
require_once($sourcedir . '/Errors.php');
79
require_once($sourcedir . '/Logging.php');
80
require_once($sourcedir . '/Load.php');
81
require_once($sourcedir . '/Security.php');
82
require_once($sourcedir . '/Class-BrowserDetect.php');
83
require_once($sourcedir . '/Subs-Auth.php');
84
85
// Create a variable to store some SMF specific functions in.
86
$smcFunc = array();
87
88
// Initiate the database connection and define some database functions to use.
89
loadDatabase();
90
91
// Load installed 'Mods' settings.
92
reloadSettings();
93
// Clean the request variables.
94
cleanRequest();
95
96
// Seed the random generator?
97
if (empty($modSettings['rand_seed']) || mt_rand(1, 250) == 69)
98
	smf_seed_generator();
99
100
// Check on any hacking attempts.
101
if (isset($_REQUEST['GLOBALS']) || isset($_COOKIE['GLOBALS']))
102
	die('No direct access...');
103
elseif (isset($_REQUEST['ssi_theme']) && (int) $_REQUEST['ssi_theme'] == (int) $ssi_theme)
104
	die('No direct access...');
105
elseif (isset($_COOKIE['ssi_theme']) && (int) $_COOKIE['ssi_theme'] == (int) $ssi_theme)
106
	die('No direct access...');
107
elseif (isset($_REQUEST['ssi_layers'], $ssi_layers) && (@get_magic_quotes_gpc() ? stripslashes($_REQUEST['ssi_layers']) : $_REQUEST['ssi_layers']) == $ssi_layers)
108
	die('No direct access...');
109
if (isset($_REQUEST['context']))
110
	die('No direct access...');
111
112
// Gzip output? (because it must be boolean and true, this can't be hacked.)
113
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', '>='))
114
	ob_start('ob_gzhandler');
115
else
116
	$modSettings['enableCompressedOutput'] = '0';
117
118
/**
119
 * An autoloader for certain classes.
120
 *
121
 * @param string $class The fully-qualified class name.
122
 */
123
spl_autoload_register(function($class) use ($sourcedir)
124
{
125
	$classMap = array(
126
		'ReCaptcha\\' => 'ReCaptcha/',
127
		'MatthiasMullie\\Minify\\' => 'minify/src/',
128
		'MatthiasMullie\\PathConverter\\' => 'minify/path-converter/src/',
129
	);
130
131
	// Do any third-party scripts want in on the fun?
132
	call_integration_hook('integrate_autoload', array(&$classMap));
133
134
	foreach ($classMap as $prefix => $dirName)
135
	{
136
		// does the class use the namespace prefix?
137
		$len = strlen($prefix);
138
		if (strncmp($prefix, $class, $len) !== 0)
139
		{
140
			continue;
141
		}
142
143
		// get the relative class name
144
		$relativeClass = substr($class, $len);
145
146
		// replace the namespace prefix with the base directory, replace namespace
147
		// separators with directory separators in the relative class name, append
148
		// with .php
149
		$fileName = $dirName . strtr($relativeClass, '\\', '/') . '.php';
150
151
		// if the file exists, require it
152
		if (file_exists($fileName = $sourcedir . '/' . $fileName))
153
		{
154
			require_once $fileName;
155
156
			return;
157
		}
158
	}
159
});
160
161
// Primarily, this is to fix the URLs...
162
ob_start('ob_sessrewrite');
163
164
// Start the session... known to scramble SSI includes in cases...
165
if (!headers_sent())
166
	loadSession();
167
else
168
{
169
	if (isset($_COOKIE[session_name()]) || isset($_REQUEST[session_name()]))
170
	{
171
		// Make a stab at it, but ignore the E_WARNINGs generated because we can't send headers.
172
		$temp = error_reporting(error_reporting() & !E_WARNING);
173
		loadSession();
174
		error_reporting($temp);
175
	}
176
177
	if (!isset($_SESSION['session_value']))
178
	{
179
		$_SESSION['session_var'] = substr(md5($smcFunc['random_int']() . session_id() . $smcFunc['random_int']()), 0, rand(7, 12));
180
		$_SESSION['session_value'] = md5(session_id() . $smcFunc['random_int']());
181
	}
182
	$sc = $_SESSION['session_value'];
183
}
184
185
// Get rid of $board and $topic... do stuff loadBoard would do.
186
unset($board, $topic);
187
$user_info['is_mod'] = false;
188
$context['user']['is_mod'] = &$user_info['is_mod'];
189
$context['linktree'] = array();
190
191
// Load the user and their cookie, as well as their settings.
192
loadUserSettings();
193
194
// Load the current user's permissions....
195
loadPermissions();
196
197
// Load the current or SSI theme. (just use $ssi_theme = id_theme;)
198
loadTheme(isset($ssi_theme) ? (int) $ssi_theme : 0);
199
200
// @todo: probably not the best place, but somewhere it should be set...
201
if (!headers_sent())
202
	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']));
203
204
// Take care of any banning that needs to be done.
205
if (isset($_REQUEST['ssi_ban']) || (isset($ssi_ban) && $ssi_ban === true))
206
	is_not_banned();
207
208
// Do we allow guests in here?
209
if (empty($ssi_guest_access) && empty($modSettings['allow_guestAccess']) && $user_info['is_guest'] && basename($_SERVER['PHP_SELF']) != 'SSI.php')
210
{
211
	require_once($sourcedir . '/Subs-Auth.php');
212
	KickGuest();
213
	obExit(null, true);
214
}
215
216
// Load the stuff like the menu bar, etc.
217
if (isset($ssi_layers))
218
{
219
	$context['template_layers'] = $ssi_layers;
220
	template_header();
221
}
222
else
223
	setupThemeContext();
224
225
// Make sure they didn't muss around with the settings... but only if it's not cli.
226
if (isset($_SERVER['REMOTE_ADDR']) && !isset($_SERVER['is_cli']) && session_id() == '')
227
	trigger_error($txt['ssi_session_broken'], E_USER_NOTICE);
228
229
// Without visiting the forum this session variable might not be set on submit.
230
if (!isset($_SESSION['USER_AGENT']) && (!isset($_GET['ssi_function']) || $_GET['ssi_function'] !== 'pollVote'))
231
	$_SESSION['USER_AGENT'] = $_SERVER['HTTP_USER_AGENT'];
232
233
// Have the ability to easily add functions to SSI.
234
call_integration_hook('integrate_SSI');
235
236
// Ignore a call to ssi_* functions if we are not accessing SSI.php directly.
237
if (basename($_SERVER['PHP_SELF']) == 'SSI.php')
238
{
239
	// You shouldn't just access SSI.php directly by URL!!
240
	if (!isset($_GET['ssi_function']))
241
		die(sprintf($txt['ssi_not_direct'], $user_info['is_admin'] ? '\'' . addslashes(__FILE__) . '\'' : '\'SSI.php\''));
242
	// Call a function passed by GET.
243
	if (function_exists('ssi_' . $_GET['ssi_function']) && (!empty($modSettings['allow_guestAccess']) || !$user_info['is_guest']))
244
		call_user_func('ssi_' . $_GET['ssi_function']);
245
	exit;
246
}
247
248
// To avoid side effects later on.
249
unset($_GET['ssi_function']);
250
251
error_reporting($ssi_error_reporting);
252
253
return true;
254
255
/**
256
 * This shuts down the SSI and shows the footer.
257
 *
258
 * @return void
259
 */
260
function ssi_shutdown()
261
{
262
	if (!isset($_GET['ssi_function']) || $_GET['ssi_function'] != 'shutdown')
263
		template_footer();
264
}
265
266
/**
267
 * Show the SMF version.
268
 */
269
function ssi_version($output_method = 'echo')
270
{
271
	if ($output_method == 'echo')
272
		echo SMF_VERSION;
273
	else
274
		return SMF_VERSION;
275
}
276
277
/**
278
 * Show the full SMF version string.
279
 */
280
function ssi_full_version($output_method = 'echo')
281
{
282
	if ($output_method == 'echo')
283
		echo SMF_FULL_VERSION;
284
	else
285
		return SMF_FULL_VERSION;
286
}
287
288
/**
289
 * Show the SMF software year.
290
 */
291
function ssi_software_year($output_method = 'echo')
292
{
293
	if ($output_method == 'echo')
294
		echo SMF_SOFTWARE_YEAR;
295
	else
296
		return SMF_SOFTWARE_YEAR;
297
}
298
299
/**
300
 * Show the forum copyright. Only used in our ssi_examples files.
301
 */
302
function ssi_copyright($output_method = 'echo')
303
{
304
	global $forum_copyright;
305
306
	if ($output_method == 'echo')
307
		printf($forum_copyright, SMF_FULL_VERSION, SMF_SOFTWARE_YEAR);
308
	else
309
		return sprintf($forum_copyright, SMF_FULL_VERSION, SMF_SOFTWARE_YEAR);
310
}
311
312
/**
313
 * Display a welcome message, like: Hey, User, you have 0 messages, 0 are new.
314
 *
315
 * @param string $output_method The output method. If 'echo', will display everything. Otherwise returns an array of user info.
316
 * @return void|array Displays a welcome message or returns an array of user data depending on output_method.
317
 */
318
function ssi_welcome($output_method = 'echo')
319
{
320
	global $context, $txt, $scripturl;
321
322
	if ($output_method == 'echo')
323
	{
324
		if ($context['user']['is_guest'])
325
			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');
326
		else
327
			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'])))) : '';
328
	}
329
	// Don't echo... then do what?!
330
	else
331
		return $context['user'];
332
}
333
334
/**
335
 * Display a menu bar, like is displayed at the top of the forum.
336
 *
337
 * @param string $output_method The output method. If 'echo', will display the menu, otherwise returns an array of menu data.
338
 * @return void|array Displays the menu or returns an array of menu data depending on output_method.
339
 */
340
function ssi_menubar($output_method = 'echo')
341
{
342
	global $context;
343
344
	if ($output_method == 'echo')
345
		template_menu();
346
	// What else could this do?
347
	else
348
		return $context['menu_buttons'];
349
}
350
351
/**
352
 * Show a logout link.
353
 *
354
 * @param string $redirect_to A URL to redirect the user to after they log out.
355
 * @param string $output_method The output method. If 'echo', shows a logout link, otherwise returns the HTML for it.
356
 * @return void|string Displays a logout link or returns its HTML depending on output_method.
357
 */
358
function ssi_logout($redirect_to = '', $output_method = 'echo')
359
{
360
	global $context, $txt, $scripturl;
361
362
	if ($redirect_to != '')
363
		$_SESSION['logout_url'] = $redirect_to;
364
365
	// Guests can't log out.
366
	if ($context['user']['is_guest'])
367
		return false;
368
369
	$link = '<a href="' . $scripturl . '?action=logout;' . $context['session_var'] . '=' . $context['session_id'] . '">' . $txt['logout'] . '</a>';
370
371
	if ($output_method == 'echo')
372
		echo $link;
373
	else
374
		return $link;
375
}
376
377
/**
378
 * Recent post list:   [board] Subject by Poster    Date
379
 *
380
 * @param int $num_recent How many recent posts to display
381
 * @param null|array $exclude_boards If set, doesn't show posts from the specified boards
382
 * @param null|array $include_boards If set, only includes posts from the specified boards
383
 * @param string $output_method The output method. If 'echo', displays the posts, otherwise returns an array of information about them.
384
 * @param bool $limit_body Whether or not to only show the first 384 characters of each post
385
 * @return void|array Displays a list of recent posts or returns an array of information about them depending on output_method.
386
 */
387
function ssi_recentPosts($num_recent = 8, $exclude_boards = null, $include_boards = null, $output_method = 'echo', $limit_body = true)
388
{
389
	global $modSettings, $context;
390
391
	// Excluding certain boards...
392
	if ($exclude_boards === null && !empty($modSettings['recycle_enable']) && !empty($modSettings['recycle_board']))
393
		$exclude_boards = array($modSettings['recycle_board']);
394
	else
395
		$exclude_boards = empty($exclude_boards) ? array() : (is_array($exclude_boards) ? $exclude_boards : array($exclude_boards));
0 ignored issues
show
introduced by
The condition is_array($exclude_boards) is always true.
Loading history...
396
397
	// What about including certain boards - note we do some protection here as pre-2.0 didn't have this parameter.
398
	if (is_array($include_boards) || (int) $include_boards === $include_boards)
399
	{
400
		$include_boards = is_array($include_boards) ? $include_boards : array($include_boards);
0 ignored issues
show
introduced by
The condition is_array($include_boards) is always true.
Loading history...
401
	}
402
	elseif ($include_boards != null)
0 ignored issues
show
introduced by
The condition $include_boards != null is always false.
Loading history...
403
	{
404
		$include_boards = array();
405
	}
406
407
	// Let's restrict the query boys (and girls)
408
	$query_where = '
409
		m.id_msg >= {int:min_message_id}
410
		' . (empty($exclude_boards) ? '' : '
411
		AND b.id_board NOT IN ({array_int:exclude_boards})') . '
412
		' . ($include_boards === null ? '' : '
413
		AND b.id_board IN ({array_int:include_boards})') . '
414
		AND {query_wanna_see_board}' . ($modSettings['postmod_active'] ? '
415
		AND m.approved = {int:is_approved}' : '');
416
417
	$query_where_params = array(
418
		'is_approved' => 1,
419
		'include_boards' => $include_boards === null ? '' : $include_boards,
420
		'exclude_boards' => empty($exclude_boards) ? '' : $exclude_boards,
421
		'min_message_id' => $modSettings['maxMsgID'] - (!empty($context['min_message_posts']) ? $context['min_message_posts'] : 25) * min($num_recent, 5),
422
	);
423
424
	// Past to this simpleton of a function...
425
	return ssi_queryPosts($query_where, $query_where_params, $num_recent, 'm.id_msg DESC', $output_method, $limit_body);
426
}
427
428
/**
429
 * Fetches one or more posts by ID.
430
 *
431
 * @param array $post_ids An array containing the IDs of the posts to show
432
 * @param bool $override_permissions Whether to ignore permissions. If true, will show posts even if the user doesn't have permission to see them.
433
 * @param string $output_method The output method. If 'echo', displays the posts, otherwise returns an array of info about them
434
 * @return void|array Displays the specified posts or returns an array of info about them, depending on output_method.
435
 */
436
function ssi_fetchPosts($post_ids = array(), $override_permissions = false, $output_method = 'echo')
437
{
438
	global $modSettings;
439
440
	if (empty($post_ids))
441
		return;
442
443
	// Allow the user to request more than one - why not?
444
	$post_ids = is_array($post_ids) ? $post_ids : array($post_ids);
0 ignored issues
show
introduced by
The condition is_array($post_ids) is always true.
Loading history...
445
446
	// Restrict the posts required...
447
	$query_where = '
448
		m.id_msg IN ({array_int:message_list})' . ($override_permissions ? '' : '
449
			AND {query_wanna_see_board}') . ($modSettings['postmod_active'] ? '
450
			AND m.approved = {int:is_approved}' : '');
451
	$query_where_params = array(
452
		'message_list' => $post_ids,
453
		'is_approved' => 1,
454
	);
455
456
	// Then make the query and dump the data.
457
	return ssi_queryPosts($query_where, $query_where_params, '', 'm.id_msg DESC', $output_method, false, $override_permissions);
0 ignored issues
show
Bug introduced by
'' of type string is incompatible with the type integer expected by parameter $query_limit of ssi_queryPosts(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

457
	return ssi_queryPosts($query_where, $query_where_params, /** @scrutinizer ignore-type */ '', 'm.id_msg DESC', $output_method, false, $override_permissions);
Loading history...
458
}
459
460
/**
461
 * This handles actually pulling post info. Called from other functions to eliminate duplication.
462
 *
463
 * @param string $query_where The WHERE clause for the query
464
 * @param array $query_where_params An array of parameters for the WHERE clause
465
 * @param int $query_limit The maximum number of rows to return
466
 * @param string $query_order The ORDER BY clause for the query
467
 * @param string $output_method The output method. If 'echo', displays the posts, otherwise returns an array of info about them.
468
 * @param bool $limit_body If true, will only show the first 384 characters of the post rather than all of it
469
 * @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
470
 * @return void|array Displays the posts or returns an array of info about them, depending on output_method
471
 */
472
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)
473
{
474
	global $scripturl, $txt, $user_info;
475
	global $modSettings, $smcFunc, $context;
476
477
	if (!empty($modSettings['enable_likes']))
478
		$context['can_like'] = allowedTo('likes_like');
479
480
	// Find all the posts. Newer ones will have higher IDs.
481
	$request = $smcFunc['db_query']('substring', '
482
		SELECT
483
			m.poster_time, m.subject, m.id_topic, m.id_member, m.id_msg, m.id_board, m.likes, b.name AS board_name,
484
			COALESCE(mem.real_name, m.poster_name) AS poster_name, ' . ($user_info['is_guest'] ? '1 AS is_read, 0 AS new_from' : '
485
			COALESCE(lt.id_msg, lmr.id_msg, 0) >= m.id_msg_modified AS is_read,
486
			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
487
		FROM {db_prefix}messages AS m
488
			INNER JOIN {db_prefix}boards AS b ON (b.id_board = m.id_board)' . ($modSettings['postmod_active'] ? '
489
			INNER JOIN {db_prefix}topics AS t ON (t.id_topic = m.id_topic)' : '') . '
490
			LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member)' . (!$user_info['is_guest'] ? '
491
			LEFT JOIN {db_prefix}log_topics AS lt ON (lt.id_topic = m.id_topic AND lt.id_member = {int:current_member})
492
			LEFT JOIN {db_prefix}log_mark_read AS lmr ON (lmr.id_board = m.id_board AND lmr.id_member = {int:current_member})' : '') . '
493
		WHERE 1=1 ' . ($override_permissions ? '' : '
494
			AND {query_wanna_see_board}') . ($modSettings['postmod_active'] ? '
495
			AND m.approved = {int:is_approved}
496
			AND t.approved = {int:is_approved}' : '') . '
497
		' . (empty($query_where) ? '' : 'AND ' . $query_where) . '
498
		ORDER BY ' . $query_order . '
499
		' . ($query_limit == '' ? '' : 'LIMIT ' . $query_limit),
500
		array_merge($query_where_params, array(
501
			'current_member' => $user_info['id'],
502
			'is_approved' => 1,
503
		))
504
	);
505
	$posts = array();
506
	while ($row = $smcFunc['db_fetch_assoc']($request))
507
	{
508
		$row['body'] = parse_bbc($row['body'], $row['smileys_enabled'], $row['id_msg']);
509
510
		// Censor it!
511
		censorText($row['subject']);
512
		censorText($row['body']);
513
514
		$preview = strip_tags(strtr($row['body'], array('<br>' => '&#10;')));
515
516
		// Build the array.
517
		$posts[$row['id_msg']] = array(
518
			'id' => $row['id_msg'],
519
			'board' => array(
520
				'id' => $row['id_board'],
521
				'name' => $row['board_name'],
522
				'href' => $scripturl . '?board=' . $row['id_board'] . '.0',
523
				'link' => '<a href="' . $scripturl . '?board=' . $row['id_board'] . '.0">' . $row['board_name'] . '</a>'
524
			),
525
			'topic' => $row['id_topic'],
526
			'poster' => array(
527
				'id' => $row['id_member'],
528
				'name' => $row['poster_name'],
529
				'href' => empty($row['id_member']) ? '' : $scripturl . '?action=profile;u=' . $row['id_member'],
530
				'link' => empty($row['id_member']) ? $row['poster_name'] : '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['poster_name'] . '</a>'
531
			),
532
			'subject' => $row['subject'],
533
			'short_subject' => shorten_subject($row['subject'], 25),
534
			'preview' => $smcFunc['strlen']($preview) > 128 ? $smcFunc['substr']($preview, 0, 128) . '...' : $preview,
535
			'body' => $row['body'],
536
			'time' => timeformat($row['poster_time']),
537
			'timestamp' => forum_time(true, $row['poster_time']),
538
			'href' => $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . ';topicseen#new',
539
			'link' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . '#msg' . $row['id_msg'] . '" rel="nofollow">' . $row['subject'] . '</a>',
540
			'new' => !empty($row['is_read']),
541
			'is_new' => empty($row['is_read']),
542
			'new_from' => $row['new_from'],
543
		);
544
545
		// Get the likes for each message.
546
		if (!empty($modSettings['enable_likes']))
547
			$posts[$row['id_msg']]['likes'] = array(
548
				'count' => $row['likes'],
549
				'you' => in_array($row['id_msg'], prepareLikesContext($row['id_topic'])),
550
				'can_like' => !$context['user']['is_guest'] && $row['id_member'] != $context['user']['id'] && !empty($context['can_like']),
551
			);
552
	}
553
	$smcFunc['db_free_result']($request);
554
555
	// If mods want to do something with this list of posts, let them do that now.
556
	call_integration_hook('integrate_ssi_queryPosts', array(&$posts));
557
558
	// Just return it.
559
	if ($output_method != 'echo' || empty($posts))
560
		return $posts;
561
562
	echo '
563
		<table style="border: none" class="ssi_table">';
564
	foreach ($posts as $post)
565
		echo '
566
			<tr>
567
				<td style="text-align: right; vertical-align: top; white-space: nowrap">
568
					[', $post['board']['link'], ']
569
				</td>
570
				<td style="vertical-align: top">
571
					<a href="', $post['href'], '">', $post['subject'], '</a>
572
					', $txt['by'], ' ', $post['poster']['link'], '
573
					', $post['is_new'] ? '<a href="' . $scripturl . '?topic=' . $post['topic'] . '.msg' . $post['new_from'] . ';topicseen#new" rel="nofollow" class="new_posts">' . $txt['new'] . '</a>' : '', '
574
				</td>
575
				<td style="text-align: right; white-space: nowrap">
576
					', $post['time'], '
577
				</td>
578
			</tr>';
579
	echo '
580
		</table>';
581
}
582
583
/**
584
 * Recent topic list:   [board] Subject by Poster   Date
585
 *
586
 * @param int $num_recent How many recent topics to show
587
 * @param null|array $exclude_boards If set, exclude topics from the specified board(s)
588
 * @param null|array $include_boards If set, only include topics from the specified board(s)
589
 * @param string $output_method The output method. If 'echo', displays a list of topics, otherwise returns an array of info about them
590
 * @return void|array Either displays a list of topics or returns an array of info about them, depending on output_method.
591
 */
592
function ssi_recentTopics($num_recent = 8, $exclude_boards = null, $include_boards = null, $output_method = 'echo')
593
{
594
	global $settings, $scripturl, $txt, $user_info;
595
	global $modSettings, $smcFunc, $context;
596
597
	if ($exclude_boards === null && !empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] > 0)
598
		$exclude_boards = array($modSettings['recycle_board']);
599
	else
600
		$exclude_boards = empty($exclude_boards) ? array() : (is_array($exclude_boards) ? $exclude_boards : array($exclude_boards));
0 ignored issues
show
introduced by
The condition is_array($exclude_boards) is always true.
Loading history...
601
602
	// Only some boards?.
603
	if (is_array($include_boards) || (int) $include_boards === $include_boards)
604
	{
605
		$include_boards = is_array($include_boards) ? $include_boards : array($include_boards);
0 ignored issues
show
introduced by
The condition is_array($include_boards) is always true.
Loading history...
606
	}
607
	elseif ($include_boards != null)
0 ignored issues
show
introduced by
The condition $include_boards != null is always false.
Loading history...
608
	{
609
		$output_method = $include_boards;
610
		$include_boards = array();
611
	}
612
613
	$icon_sources = array();
614
	foreach ($context['stable_icons'] as $icon)
615
		$icon_sources[$icon] = 'images_url';
616
617
	// Find all the posts in distinct topics.  Newer ones will have higher IDs.
618
	$request = $smcFunc['db_query']('substring', '
619
		SELECT
620
			t.id_topic, b.id_board, b.name AS board_name
621
		FROM {db_prefix}topics AS t
622
			INNER JOIN {db_prefix}messages AS ml ON (ml.id_msg = t.id_last_msg)
623
			LEFT JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board)
624
		WHERE t.id_last_msg >= {int:min_message_id}' . (empty($exclude_boards) ? '' : '
625
			AND b.id_board NOT IN ({array_int:exclude_boards})') . '' . (empty($include_boards) ? '' : '
626
			AND b.id_board IN ({array_int:include_boards})') . '
627
			AND {query_wanna_see_board}' . ($modSettings['postmod_active'] ? '
628
			AND t.approved = {int:is_approved}
629
			AND ml.approved = {int:is_approved}' : '') . '
630
		ORDER BY t.id_last_msg DESC
631
		LIMIT ' . $num_recent,
632
		array(
633
			'include_boards' => empty($include_boards) ? '' : $include_boards,
634
			'exclude_boards' => empty($exclude_boards) ? '' : $exclude_boards,
635
			'min_message_id' => $modSettings['maxMsgID'] - (!empty($context['min_message_topics']) ? $context['min_message_topics'] : 35) * min($num_recent, 5),
636
			'is_approved' => 1,
637
		)
638
	);
639
	$topics = array();
640
	while ($row = $smcFunc['db_fetch_assoc']($request))
641
		$topics[$row['id_topic']] = $row;
642
	$smcFunc['db_free_result']($request);
643
644
	// Did we find anything? If not, bail.
645
	if (empty($topics))
646
		return array();
647
648
	$recycle_board = !empty($modSettings['recycle_enable']) && !empty($modSettings['recycle_board']) ? (int) $modSettings['recycle_board'] : 0;
649
650
	// Find all the posts in distinct topics.  Newer ones will have higher IDs.
651
	$request = $smcFunc['db_query']('substring', '
652
		SELECT
653
			mf.poster_time, mf.subject, ml.id_topic, mf.id_member, ml.id_msg, t.num_replies, t.num_views, mg.online_color, t.id_last_msg,
654
			COALESCE(mem.real_name, mf.poster_name) AS poster_name, ' . ($user_info['is_guest'] ? '1 AS is_read, 0 AS new_from' : '
655
			COALESCE(lt.id_msg, lmr.id_msg, 0) >= ml.id_msg_modified AS is_read,
656
			COALESCE(lt.id_msg, lmr.id_msg, -1) + 1 AS new_from') . ', SUBSTRING(mf.body, 1, 384) AS body, mf.smileys_enabled, mf.icon
657
		FROM {db_prefix}topics AS t
658
			INNER JOIN {db_prefix}messages AS ml ON (ml.id_msg = t.id_last_msg)
659
			INNER JOIN {db_prefix}messages AS mf ON (mf.id_msg = t.id_last_msg)
660
			LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = mf.id_member)' . (!$user_info['is_guest'] ? '
661
			LEFT JOIN {db_prefix}log_topics AS lt ON (lt.id_topic = t.id_topic AND lt.id_member = {int:current_member})
662
			LEFT JOIN {db_prefix}log_mark_read AS lmr ON (lmr.id_board = t.id_board AND lmr.id_member = {int:current_member})' : '') . '
663
			LEFT JOIN {db_prefix}membergroups AS mg ON (mg.id_group = mem.id_group)
664
		WHERE t.id_topic IN ({array_int:topic_list})
665
		ORDER BY t.id_last_msg DESC',
666
		array(
667
			'current_member' => $user_info['id'],
668
			'topic_list' => array_keys($topics),
669
		)
670
	);
671
	$posts = array();
672
	while ($row = $smcFunc['db_fetch_assoc']($request))
673
	{
674
		$row['body'] = strip_tags(strtr(parse_bbc($row['body'], $row['smileys_enabled'], $row['id_msg']), array('<br>' => '&#10;')));
675
		if ($smcFunc['strlen']($row['body']) > 128)
676
			$row['body'] = $smcFunc['substr']($row['body'], 0, 128) . '...';
677
678
		// Censor the subject.
679
		censorText($row['subject']);
680
		censorText($row['body']);
681
682
		// Recycled icon
683
		if (!empty($recycle_board) && $topics[$row['id_topic']]['id_board'] == $recycle_board)
684
			$row['icon'] = 'recycled';
685
686
		if (!empty($modSettings['messageIconChecks_enable']) && !isset($icon_sources[$row['icon']]))
687
			$icon_sources[$row['icon']] = file_exists($settings['theme_dir'] . '/images/post/' . $row['icon'] . '.png') ? 'images_url' : 'default_images_url';
688
		elseif (!isset($icon_sources[$row['icon']]))
689
			$icon_sources[$row['icon']] = 'images_url';
690
691
		// Build the array.
692
		$posts[] = array(
693
			'board' => array(
694
				'id' => $topics[$row['id_topic']]['id_board'],
695
				'name' => $topics[$row['id_topic']]['board_name'],
696
				'href' => $scripturl . '?board=' . $topics[$row['id_topic']]['id_board'] . '.0',
697
				'link' => '<a href="' . $scripturl . '?board=' . $topics[$row['id_topic']]['id_board'] . '.0">' . $topics[$row['id_topic']]['board_name'] . '</a>',
698
			),
699
			'topic' => $row['id_topic'],
700
			'poster' => array(
701
				'id' => $row['id_member'],
702
				'name' => $row['poster_name'],
703
				'href' => empty($row['id_member']) ? '' : $scripturl . '?action=profile;u=' . $row['id_member'],
704
				'link' => empty($row['id_member']) ? $row['poster_name'] : '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['poster_name'] . '</a>'
705
			),
706
			'subject' => $row['subject'],
707
			'replies' => $row['num_replies'],
708
			'views' => $row['num_views'],
709
			'short_subject' => shorten_subject($row['subject'], 25),
710
			'preview' => $row['body'],
711
			'time' => timeformat($row['poster_time']),
712
			'timestamp' => forum_time(true, $row['poster_time']),
713
			'href' => $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . ';topicseen#new',
714
			'link' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . '#new" rel="nofollow">' . $row['subject'] . '</a>',
715
			// Retained for compatibility - is technically incorrect!
716
			'new' => !empty($row['is_read']),
717
			'is_new' => empty($row['is_read']),
718
			'new_from' => $row['new_from'],
719
			'icon' => '<img src="' . $settings[$icon_sources[$row['icon']]] . '/post/' . $row['icon'] . '.png" style="vertical-align:middle;" alt="' . $row['icon'] . '">',
720
		);
721
	}
722
	$smcFunc['db_free_result']($request);
723
724
	// If mods want to do somthing with this list of topics, let them do that now.
725
	call_integration_hook('integrate_ssi_recentTopics', array(&$posts));
726
727
	// Just return it.
728
	if ($output_method != 'echo' || empty($posts))
729
		return $posts;
730
731
	echo '
732
		<table style="border: none" class="ssi_table">';
733
	foreach ($posts as $post)
734
		echo '
735
			<tr>
736
				<td style="text-align: right; vertical-align: top; white-space: nowrap">
737
					[', $post['board']['link'], ']
738
				</td>
739
				<td style="vertical-align: top">
740
					<a href="', $post['href'], '">', $post['subject'], '</a>
741
					', $txt['by'], ' ', $post['poster']['link'], '
742
					', !$post['is_new'] ? '' : '<a href="' . $scripturl . '?topic=' . $post['topic'] . '.msg' . $post['new_from'] . ';topicseen#new" rel="nofollow" class="new_posts">' . $txt['new'] . '</a>', '
743
				</td>
744
				<td style="text-align: right; white-space: nowrap">
745
					', $post['time'], '
746
				</td>
747
			</tr>';
748
	echo '
749
		</table>';
750
}
751
752
/**
753
 * Shows a list of top posters
754
 *
755
 * @param int $topNumber How many top posters to list
756
 * @param string $output_method The output method. If 'echo', will display a list of users, otherwise returns an array of info about them.
757
 * @return void|array Either displays a list of users or returns an array of info about them, depending on output_method.
758
 */
759
function ssi_topPoster($topNumber = 1, $output_method = 'echo')
760
{
761
	global $scripturl, $smcFunc;
762
763
	// Find the latest poster.
764
	$request = $smcFunc['db_query']('', '
765
		SELECT id_member, real_name, posts
766
		FROM {db_prefix}members
767
		ORDER BY posts DESC
768
		LIMIT ' . $topNumber,
769
		array(
770
		)
771
	);
772
	$return = array();
773
	while ($row = $smcFunc['db_fetch_assoc']($request))
774
		$return[] = array(
775
			'id' => $row['id_member'],
776
			'name' => $row['real_name'],
777
			'href' => $scripturl . '?action=profile;u=' . $row['id_member'],
778
			'link' => '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['real_name'] . '</a>',
779
			'posts' => $row['posts']
780
		);
781
	$smcFunc['db_free_result']($request);
782
783
	// If mods want to do somthing with this list of members, let them do that now.
784
	call_integration_hook('integrate_ssi_topPoster', array(&$return));
785
786
	// Just return all the top posters.
787
	if ($output_method != 'echo')
788
		return $return;
789
790
	// Make a quick array to list the links in.
791
	$temp_array = array();
792
	foreach ($return as $member)
793
		$temp_array[] = $member['link'];
794
795
	echo implode(', ', $temp_array);
796
}
797
798
/**
799
 * Shows a list of top boards based on activity
800
 *
801
 * @param int $num_top How many boards to display
802
 * @param string $output_method The output method. If 'echo', displays a list of boards, otherwise returns an array of info about them.
803
 * @return void|array Displays a list of the top boards or returns an array of info about them, depending on output_method.
804
 */
805
function ssi_topBoards($num_top = 10, $output_method = 'echo')
806
{
807
	global $txt, $scripturl, $user_info, $modSettings, $smcFunc;
808
809
	// Find boards with lots of posts.
810
	$request = $smcFunc['db_query']('', '
811
		SELECT
812
			b.name, b.num_topics, b.num_posts, b.id_board,' . (!$user_info['is_guest'] ? ' 1 AS is_read' : '
813
			(COALESCE(lb.id_msg, 0) >= b.id_last_msg) AS is_read') . '
814
		FROM {db_prefix}boards AS b
815
			LEFT JOIN {db_prefix}log_boards AS lb ON (lb.id_board = b.id_board AND lb.id_member = {int:current_member})
816
		WHERE {query_wanna_see_board}' . (!empty($modSettings['recycle_enable']) && !empty($modSettings['recycle_board']) ? '
817
			AND b.id_board != {int:recycle_board}' : '') . '
818
		ORDER BY b.num_posts DESC
819
		LIMIT ' . $num_top,
820
		array(
821
			'current_member' => $user_info['id'],
822
			'recycle_board' => !empty($modSettings['recycle_board']) ? (int) $modSettings['recycle_board'] : null,
823
		)
824
	);
825
	$boards = array();
826
	while ($row = $smcFunc['db_fetch_assoc']($request))
827
		$boards[] = array(
828
			'id' => $row['id_board'],
829
			'num_posts' => $row['num_posts'],
830
			'num_topics' => $row['num_topics'],
831
			'name' => $row['name'],
832
			'new' => empty($row['is_read']),
833
			'href' => $scripturl . '?board=' . $row['id_board'] . '.0',
834
			'link' => '<a href="' . $scripturl . '?board=' . $row['id_board'] . '.0">' . $row['name'] . '</a>'
835
		);
836
	$smcFunc['db_free_result']($request);
837
838
	// If mods want to do somthing with this list of boards, let them do that now.
839
	call_integration_hook('integrate_ssi_topBoards', array(&$boards));
840
841
	// If we shouldn't output or have nothing to output, just jump out.
842
	if ($output_method != 'echo' || empty($boards))
843
		return $boards;
844
845
	echo '
846
		<table class="ssi_table">
847
			<tr>
848
				<th style="text-align: left">', $txt['board'], '</th>
849
				<th style="text-align: left">', $txt['board_topics'], '</th>
850
				<th style="text-align: left">', $txt['posts'], '</th>
851
			</tr>';
852
	foreach ($boards as $sBoard)
853
		echo '
854
			<tr>
855
				<td>', $sBoard['link'], $sBoard['new'] ? ' <a href="' . $sBoard['href'] . '" class="new_posts">' . $txt['new'] . '</a>' : '', '</td>
856
				<td style="text-align: right">', comma_format($sBoard['num_topics']), '</td>
857
				<td style="text-align: right">', comma_format($sBoard['num_posts']), '</td>
858
			</tr>';
859
	echo '
860
		</table>';
861
}
862
863
// Shows the top topics.
864
/**
865
 * Shows a list of top topics based on views or replies
866
 *
867
 * @param string $type Can be either replies or views
868
 * @param int $num_topics How many topics to display
869
 * @param string $output_method The output method. If 'echo', displays a list of topics, otherwise returns an array of info about them.
870
 * @return void|array Either displays a list of topics or returns an array of info about them, depending on output_method.
871
 */
872
function ssi_topTopics($type = 'replies', $num_topics = 10, $output_method = 'echo')
873
{
874
	global $txt, $scripturl, $modSettings, $smcFunc;
875
876
	if ($modSettings['totalMessages'] > 100000)
877
	{
878
		// @todo Why don't we use {query(_wanna)_see_board}?
879
		$request = $smcFunc['db_query']('', '
880
			SELECT id_topic
881
			FROM {db_prefix}topics
882
			WHERE num_' . ($type != 'replies' ? 'views' : 'replies') . ' != 0' . ($modSettings['postmod_active'] ? '
883
				AND approved = {int:is_approved}' : '') . '
884
			ORDER BY num_' . ($type != 'replies' ? 'views' : 'replies') . ' DESC
885
			LIMIT {int:limit}',
886
			array(
887
				'is_approved' => 1,
888
				'limit' => $num_topics > 100 ? ($num_topics + ($num_topics / 2)) : 100,
889
			)
890
		);
891
		$topic_ids = array();
892
		while ($row = $smcFunc['db_fetch_assoc']($request))
893
			$topic_ids[] = $row['id_topic'];
894
		$smcFunc['db_free_result']($request);
895
	}
896
	else
897
		$topic_ids = array();
898
899
	$request = $smcFunc['db_query']('', '
900
		SELECT m.subject, m.id_topic, t.num_views, t.num_replies
901
		FROM {db_prefix}topics AS t
902
			INNER JOIN {db_prefix}messages AS m ON (m.id_msg = t.id_first_msg)
903
			INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board)
904
		WHERE {query_wanna_see_board}' . ($modSettings['postmod_active'] ? '
905
			AND t.approved = {int:is_approved}' : '') . (!empty($topic_ids) ? '
906
			AND t.id_topic IN ({array_int:topic_list})' : '') . (!empty($modSettings['recycle_enable']) && !empty($modSettings['recycle_board']) ? '
907
			AND b.id_board != {int:recycle_board}' : '') . '
908
		ORDER BY t.num_' . ($type != 'replies' ? 'views' : 'replies') . ' DESC
909
		LIMIT {int:limit}',
910
		array(
911
			'topic_list' => $topic_ids,
912
			'is_approved' => 1,
913
			'recycle_board' => !empty($modSettings['recycle_board']) ? (int) $modSettings['recycle_board'] : null,
914
			'limit' => $num_topics,
915
		)
916
	);
917
	$topics = array();
918
	while ($row = $smcFunc['db_fetch_assoc']($request))
919
	{
920
		censorText($row['subject']);
921
922
		$topics[] = array(
923
			'id' => $row['id_topic'],
924
			'subject' => $row['subject'],
925
			'num_replies' => $row['num_replies'],
926
			'num_views' => $row['num_views'],
927
			'href' => $scripturl . '?topic=' . $row['id_topic'] . '.0',
928
			'link' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.0">' . $row['subject'] . '</a>',
929
		);
930
	}
931
	$smcFunc['db_free_result']($request);
932
933
	// If mods want to do somthing with this list of topics, let them do that now.
934
	call_integration_hook('integrate_ssi_topTopics', array(&$topics, $type));
935
936
	if ($output_method != 'echo' || empty($topics))
937
		return $topics;
938
939
	echo '
940
		<table class="ssi_table">
941
			<tr>
942
				<th style="text-align: left"></th>
943
				<th style="text-align: left">', $txt['views'], '</th>
944
				<th style="text-align: left">', $txt['replies'], '</th>
945
			</tr>';
946
	foreach ($topics as $sTopic)
947
		echo '
948
			<tr>
949
				<td style="text-align: left">
950
					', $sTopic['link'], '
951
				</td>
952
				<td style="text-align: right">', comma_format($sTopic['num_views']), '</td>
953
				<td style="text-align: right">', comma_format($sTopic['num_replies']), '</td>
954
			</tr>';
955
	echo '
956
		</table>';
957
}
958
959
/**
960
 * Top topics based on replies
961
 *
962
 * @param int $num_topics How many topics to show
963
 * @param string $output_method The output method. If 'echo', displays a list of topics, otherwise returns an array of info about them
964
 * @return void|array Either displays a list of top topics or returns an array of info about them, depending on output_method.
965
 */
966
function ssi_topTopicsReplies($num_topics = 10, $output_method = 'echo')
967
{
968
	return ssi_topTopics('replies', $num_topics, $output_method);
969
}
970
971
/**
972
 * Top topics based on views
973
 *
974
 * @param int $num_topics How many topics to show
975
 * @param string $output_method The output method. If 'echo', displays a list of topics, otherwise returns an array of info about them
976
 * @return void|array Either displays a list of top topics or returns an array of info about them, depending on output_method.
977
 */
978
function ssi_topTopicsViews($num_topics = 10, $output_method = 'echo')
979
{
980
	return ssi_topTopics('views', $num_topics, $output_method);
981
}
982
983
/**
984
 * Show a link to the latest member: Please welcome, Someone, our latest member.
985
 *
986
 * @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.
987
 * @return void|array Displays a "welcome" message for the latest member or returns an array of info about them, depending on output_method.
988
 */
989
function ssi_latestMember($output_method = 'echo')
990
{
991
	global $txt, $context;
992
993
	if ($output_method == 'echo')
994
		echo '
995
	', sprintf($txt['welcome_newest_member'], $context['common_stats']['latest_member']['link']), '<br>';
996
	else
997
		return $context['common_stats']['latest_member'];
998
}
999
1000
/**
1001
 * Fetches a random member.
1002
 *
1003
 * @param string $random_type If 'day', only fetches a new random member once a day.
1004
 * @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.
1005
 * @return void|array Displays a link to a random member's profile or returns an array of info about them depending on output_method.
1006
 */
1007
function ssi_randomMember($random_type = '', $output_method = 'echo')
1008
{
1009
	global $modSettings;
1010
1011
	// If we're looking for something to stay the same each day then seed the generator.
1012
	if ($random_type == 'day')
1013
	{
1014
		// Set the seed to change only once per day.
1015
		mt_srand(floor(time() / 86400));
0 ignored issues
show
Bug introduced by
floor(time() / 86400) of type double is incompatible with the type integer expected by parameter $seed of mt_srand(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

1015
		mt_srand(/** @scrutinizer ignore-type */ floor(time() / 86400));
Loading history...
1016
	}
1017
1018
	// Get the lowest ID we're interested in.
1019
	$member_id = mt_rand(1, $modSettings['latestMember']);
1020
1021
	$where_query = '
1022
		id_member >= {int:selected_member}
1023
		AND is_activated = {int:is_activated}';
1024
1025
	$query_where_params = array(
1026
		'selected_member' => $member_id,
1027
		'is_activated' => 1,
1028
	);
1029
1030
	$result = ssi_queryMembers($where_query, $query_where_params, 1, 'id_member ASC', $output_method);
1031
1032
	// If we got nothing do the reverse - in case of unactivated members.
1033
	if (empty($result))
1034
	{
1035
		$where_query = '
1036
			id_member <= {int:selected_member}
1037
			AND is_activated = {int:is_activated}';
1038
1039
		$query_where_params = array(
1040
			'selected_member' => $member_id,
1041
			'is_activated' => 1,
1042
		);
1043
1044
		$result = ssi_queryMembers($where_query, $query_where_params, 1, 'id_member DESC', $output_method);
1045
	}
1046
1047
	// Just to be sure put the random generator back to something... random.
1048
	if ($random_type != '')
1049
		mt_srand(time());
1050
1051
	return $result;
1052
}
1053
1054
/**
1055
 * Fetch specific members
1056
 *
1057
 * @param array $member_ids The IDs of the members to fetch
1058
 * @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.
1059
 * @return void|array Displays links to the specified members' profiles or returns an array of info about them, depending on output_method.
1060
 */
1061
function ssi_fetchMember($member_ids = array(), $output_method = 'echo')
1062
{
1063
	if (empty($member_ids))
1064
		return;
1065
1066
	// Can have more than one member if you really want...
1067
	$member_ids = is_array($member_ids) ? $member_ids : array($member_ids);
0 ignored issues
show
introduced by
The condition is_array($member_ids) is always true.
Loading history...
1068
1069
	// Restrict it right!
1070
	$query_where = '
1071
		id_member IN ({array_int:member_list})';
1072
1073
	$query_where_params = array(
1074
		'member_list' => $member_ids,
1075
	);
1076
1077
	// Then make the query and dump the data.
1078
	return ssi_queryMembers($query_where, $query_where_params, '', 'id_member', $output_method);
1079
}
1080
1081
/**
1082
 * Get al members in the specified group
1083
 *
1084
 * @param int $group_id The ID of the group to get members from
1085
 * @param string $output_method The output method. If 'echo', returns a list of group members, otherwise returns an array of info about them.
1086
 * @return void|array Displays a list of group members or returns an array of info about them, depending on output_method.
1087
 */
1088
function ssi_fetchGroupMembers($group_id = null, $output_method = 'echo')
1089
{
1090
	if ($group_id === null)
1091
		return;
1092
1093
	$query_where = '
1094
		id_group = {int:id_group}
1095
		OR id_post_group = {int:id_group}
1096
		OR FIND_IN_SET({int:id_group}, additional_groups) != 0';
1097
1098
	$query_where_params = array(
1099
		'id_group' => $group_id,
1100
	);
1101
1102
	return ssi_queryMembers($query_where, $query_where_params, '', 'real_name', $output_method);
1103
}
1104
1105
/**
1106
 * Pulls info about members based on the specified parameters. Used by other functions to eliminate duplication.
1107
 *
1108
 * @param string $query_where The info for the WHERE clause of the query
1109
 * @param array $query_where_params The parameters for the WHERE clause
1110
 * @param string|int $query_limit The number of rows to return or an empty string to return all
1111
 * @param string $query_order The info for the ORDER BY clause of the query
1112
 * @param string $output_method The output method. If 'echo', displays a list of members, otherwise returns an array of info about them
1113
 * @return void|array Displays a list of members or returns an array of info about them, depending on output_method.
1114
 */
1115
function ssi_queryMembers($query_where = null, $query_where_params = array(), $query_limit = '', $query_order = 'id_member DESC', $output_method = 'echo')
1116
{
1117
	global $smcFunc, $memberContext;
1118
1119
	if ($query_where === null)
1120
		return;
1121
1122
	// Fetch the members in question.
1123
	$request = $smcFunc['db_query']('', '
1124
		SELECT id_member
1125
		FROM {db_prefix}members
1126
		WHERE ' . $query_where . '
1127
		ORDER BY ' . $query_order . '
1128
		' . ($query_limit == '' ? '' : 'LIMIT ' . $query_limit),
1129
		array_merge($query_where_params, array(
1130
		))
1131
	);
1132
	$members = array();
1133
	while ($row = $smcFunc['db_fetch_assoc']($request))
1134
		$members[] = $row['id_member'];
1135
	$smcFunc['db_free_result']($request);
1136
1137
	if (empty($members))
1138
		return array();
1139
1140
	// If mods want to do somthing with this list of members, let them do that now.
1141
	call_integration_hook('integrate_ssi_queryMembers', array(&$members));
1142
1143
	// Load the members.
1144
	loadMemberData($members);
1145
1146
	// Draw the table!
1147
	if ($output_method == 'echo')
1148
		echo '
1149
		<table style="border: none" class="ssi_table">';
1150
1151
	$query_members = array();
1152
	foreach ($members as $member)
1153
	{
1154
		// Load their context data.
1155
		if (!loadMemberContext($member))
1156
			continue;
1157
1158
		// Store this member's information.
1159
		$query_members[$member] = $memberContext[$member];
1160
1161
		// Only do something if we're echo'ing.
1162
		if ($output_method == 'echo')
1163
			echo '
1164
			<tr>
1165
				<td style="text-align: right; vertical-align: top; white-space: nowrap">
1166
					', $query_members[$member]['link'], '
1167
					<br>', $query_members[$member]['blurb'], '
1168
					<br>', $query_members[$member]['avatar']['image'], '
1169
				</td>
1170
			</tr>';
1171
	}
1172
1173
	// End the table if appropriate.
1174
	if ($output_method == 'echo')
1175
		echo '
1176
		</table>';
1177
1178
	// Send back the data.
1179
	return $query_members;
1180
}
1181
1182
/**
1183
 * Show some basic stats:   Total This: XXXX, etc.
1184
 *
1185
 * @param string $output_method The output method. If 'echo', displays the stats, otherwise returns an array of info about them
1186
 * @return void|array Doesn't return anything if the user can't view stats. Otherwise either displays the stats or returns an array of info about them, depending on output_method.
1187
 */
1188
function ssi_boardStats($output_method = 'echo')
1189
{
1190
	global $txt, $scripturl, $modSettings, $smcFunc;
1191
1192
	if (!allowedTo('view_stats'))
1193
		return;
1194
1195
	$totals = array(
1196
		'members' => $modSettings['totalMembers'],
1197
		'posts' => $modSettings['totalMessages'],
1198
		'topics' => $modSettings['totalTopics']
1199
	);
1200
1201
	$result = $smcFunc['db_query']('', '
1202
		SELECT COUNT(*)
1203
		FROM {db_prefix}boards',
1204
		array(
1205
		)
1206
	);
1207
	list ($totals['boards']) = $smcFunc['db_fetch_row']($result);
1208
	$smcFunc['db_free_result']($result);
1209
1210
	$result = $smcFunc['db_query']('', '
1211
		SELECT COUNT(*)
1212
		FROM {db_prefix}categories',
1213
		array(
1214
		)
1215
	);
1216
	list ($totals['categories']) = $smcFunc['db_fetch_row']($result);
1217
	$smcFunc['db_free_result']($result);
1218
1219
	// If mods want to do somthing with the board stats, let them do that now.
1220
	call_integration_hook('integrate_ssi_boardStats', array(&$totals));
1221
1222
	if ($output_method != 'echo')
1223
		return $totals;
1224
1225
	echo '
1226
		', $txt['total_members'], ': <a href="', $scripturl . '?action=mlist">', comma_format($totals['members']), '</a><br>
1227
		', $txt['total_posts'], ': ', comma_format($totals['posts']), '<br>
1228
		', $txt['total_topics'], ': ', comma_format($totals['topics']), ' <br>
1229
		', $txt['total_cats'], ': ', comma_format($totals['categories']), '<br>
1230
		', $txt['total_boards'], ': ', comma_format($totals['boards']);
1231
}
1232
1233
/**
1234
 * Shows a list of online users:  YY Guests, ZZ Users and then a list...
1235
 *
1236
 * @param string $output_method The output method. If 'echo', displays a list, otherwise returns an array of info about the online users.
1237
 * @return void|array Either displays a list of online users or returns an array of info about them, depending on output_method.
1238
 */
1239
function ssi_whosOnline($output_method = 'echo')
1240
{
1241
	global $user_info, $txt, $sourcedir, $settings;
1242
1243
	require_once($sourcedir . '/Subs-MembersOnline.php');
1244
	$membersOnlineOptions = array(
1245
		'show_hidden' => allowedTo('moderate_forum'),
1246
	);
1247
	$return = getMembersOnlineStats($membersOnlineOptions);
1248
1249
	// If mods want to do somthing with the list of who is online, let them do that now.
1250
	call_integration_hook('integrate_ssi_whosOnline', array(&$return));
1251
1252
	// Add some redundancy for backwards compatibility reasons.
1253
	if ($output_method != 'echo')
1254
		return $return + array(
1255
			'users' => $return['users_online'],
1256
			'guests' => $return['num_guests'],
1257
			'hidden' => $return['num_users_hidden'],
1258
			'buddies' => $return['num_buddies'],
1259
			'num_users' => $return['num_users_online'],
1260
			'total_users' => $return['num_users_online'] + $return['num_guests'],
1261
		);
1262
1263
	echo '
1264
		', comma_format($return['num_guests']), ' ', $return['num_guests'] == 1 ? $txt['guest'] : $txt['guests'], ', ', comma_format($return['num_users_online']), ' ', $return['num_users_online'] == 1 ? $txt['user'] : $txt['users'];
1265
1266
	$bracketList = array();
1267
	if (!empty($user_info['buddies']))
1268
		$bracketList[] = comma_format($return['num_buddies']) . ' ' . ($return['num_buddies'] == 1 ? $txt['buddy'] : $txt['buddies']);
1269
	if (!empty($return['num_spiders']))
1270
		$bracketList[] = comma_format($return['num_spiders']) . ' ' . ($return['num_spiders'] == 1 ? $txt['spider'] : $txt['spiders']);
1271
	if (!empty($return['num_users_hidden']))
1272
		$bracketList[] = comma_format($return['num_users_hidden']) . ' ' . $txt['hidden'];
1273
1274
	if (!empty($bracketList))
1275
		echo ' (' . implode(', ', $bracketList) . ')';
1276
1277
	echo '<br>
1278
			', implode(', ', $return['list_users_online']);
1279
1280
	// Showing membergroups?
1281
	if (!empty($settings['show_group_key']) && !empty($return['membergroups']))
1282
		echo '<br>
1283
			[' . implode(']&nbsp;&nbsp;[', $return['membergroups']) . ']';
1284
}
1285
1286
/**
1287
 * Just like whosOnline except it also logs the online presence.
1288
 *
1289
 * @param string $output_method The output method. If 'echo', displays a list, otherwise returns an array of info about the online users.
1290
 * @return void|array Either displays a list of online users or returns an aray of info about them, depending on output_method.
1291
 */
1292
function ssi_logOnline($output_method = 'echo')
1293
{
1294
	writeLog();
1295
1296
	if ($output_method != 'echo')
1297
		return ssi_whosOnline($output_method);
1298
	else
1299
		ssi_whosOnline($output_method);
1300
}
1301
1302
// Shows a login box.
1303
/**
1304
 * Shows a login box
1305
 *
1306
 * @param string $redirect_to The URL to redirect the user to after they login
1307
 * @param string $output_method The output method. If 'echo' and the user is a guest, displays a login box, otherwise returns whether the user is a guest
1308
 * @return void|bool Either displays a login box or returns whether the user is a guest, depending on whether the user is logged in and output_method.
1309
 */
1310
function ssi_login($redirect_to = '', $output_method = 'echo')
1311
{
1312
	global $scripturl, $txt, $user_info, $context;
1313
1314
	if ($redirect_to != '')
1315
		$_SESSION['login_url'] = $redirect_to;
1316
1317
	if ($output_method != 'echo' || !$user_info['is_guest'])
1318
		return $user_info['is_guest'];
1319
1320
	// Create a login token
1321
	createToken('login');
1322
1323
	echo '
1324
		<form action="', $scripturl, '?action=login2" method="post" accept-charset="', $context['character_set'], '">
1325
			<table style="border: none" class="ssi_table">
1326
				<tr>
1327
					<td style="text-align: right; border-spacing: 1"><label for="user">', $txt['username'], ':</label>&nbsp;</td>
1328
					<td><input type="text" id="user" name="user" size="9" value="', $user_info['username'], '"></td>
1329
				</tr><tr>
1330
					<td style="text-align: right; border-spacing: 1"><label for="passwrd">', $txt['password'], ':</label>&nbsp;</td>
1331
					<td><input type="password" name="passwrd" id="passwrd" size="9"></td>
1332
				</tr>
1333
				<tr>
1334
					<td>
1335
						<input type="hidden" name="cookielength" value="-1">
1336
						<input type="hidden" name="', $context['session_var'], '" value="', $context['session_id'], '" />
1337
						<input type="hidden" name="', $context['login_token_var'], '" value="', $context['login_token'], '">
1338
					</td>
1339
					<td><input type="submit" value="', $txt['login'], '" class="button"></td>
1340
				</tr>
1341
			</table>
1342
		</form>';
1343
1344
}
1345
1346
/**
1347
 * Show the top poll based on votes
1348
 *
1349
 * @param string $output_method The output method. If 'echo', displays the poll, otherwise returns an array of info about it
1350
 * @return void|array Either shows the top poll or returns an array of info about it, depending on output_method.
1351
 */
1352
function ssi_topPoll($output_method = 'echo')
1353
{
1354
	// Just use recentPoll, no need to duplicate code...
1355
	return ssi_recentPoll(true, $output_method);
1356
}
1357
1358
// Show the most recently posted poll.
1359
/**
1360
 * Shows the most recent poll
1361
 *
1362
 * @param bool $topPollInstead Whether to show the top poll (based on votes) instead of the most recent one
1363
 * @param string $output_method The output method. If 'echo', displays the poll, otherwise returns an array of info about it.
1364
 * @return void|array Either shows the poll or returns an array of info about it, depending on output_method.
1365
 */
1366
function ssi_recentPoll($topPollInstead = false, $output_method = 'echo')
1367
{
1368
	global $txt, $boardurl, $user_info, $context, $smcFunc, $modSettings;
1369
1370
	$boardsAllowed = array_intersect(boardsAllowedTo('poll_view'), boardsAllowedTo('poll_vote'));
1371
1372
	if (empty($boardsAllowed))
1373
		return array();
1374
1375
	$request = $smcFunc['db_query']('', '
1376
		SELECT p.id_poll, p.question, t.id_topic, p.max_votes, p.guest_vote, p.hide_results, p.expire_time
1377
		FROM {db_prefix}polls AS p
1378
			INNER JOIN {db_prefix}topics AS t ON (t.id_poll = p.id_poll' . ($modSettings['postmod_active'] ? ' AND t.approved = {int:is_approved}' : '') . ')
1379
			INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board)' . ($topPollInstead ? '
1380
			INNER JOIN {db_prefix}poll_choices AS pc ON (pc.id_poll = p.id_poll)' : '') . '
1381
			LEFT JOIN {db_prefix}log_polls AS lp ON (lp.id_poll = p.id_poll AND lp.id_member > {int:no_member} AND lp.id_member = {int:current_member})
1382
		WHERE p.voting_locked = {int:voting_opened}
1383
			AND (p.expire_time = {int:no_expiration} OR {int:current_time} < p.expire_time)
1384
			AND ' . ($user_info['is_guest'] ? 'p.guest_vote = {int:guest_vote_allowed}' : 'lp.id_choice IS NULL') . '
1385
			AND {query_wanna_see_board}' . (!in_array(0, $boardsAllowed) ? '
1386
			AND b.id_board IN ({array_int:boards_allowed_list})' : '') . (!empty($modSettings['recycle_enable']) && !empty($modSettings['recycle_board']) ? '
1387
			AND b.id_board != {int:recycle_board}' : '') . '
1388
		ORDER BY ' . ($topPollInstead ? 'pc.votes' : 'p.id_poll') . ' DESC
1389
		LIMIT 1',
1390
		array(
1391
			'current_member' => $user_info['id'],
1392
			'boards_allowed_list' => $boardsAllowed,
1393
			'is_approved' => 1,
1394
			'guest_vote_allowed' => 1,
1395
			'no_member' => 0,
1396
			'voting_opened' => 0,
1397
			'no_expiration' => 0,
1398
			'current_time' => time(),
1399
			'recycle_board' => !empty($modSettings['recycle_board']) ? (int) $modSettings['recycle_board'] : null,
1400
		)
1401
	);
1402
	$row = $smcFunc['db_fetch_assoc']($request);
1403
	$smcFunc['db_free_result']($request);
1404
1405
	// This user has voted on all the polls.
1406
	if (empty($row) || !is_array($row))
1407
		return array();
1408
1409
	// If this is a guest who's voted we'll through ourselves to show poll to show the results.
1410
	if ($user_info['is_guest'] && (!$row['guest_vote'] || (isset($_COOKIE['guest_poll_vote']) && in_array($row['id_poll'], explode(',', $_COOKIE['guest_poll_vote'])))))
1411
		return ssi_showPoll($row['id_topic'], $output_method);
1412
1413
	$request = $smcFunc['db_query']('', '
1414
		SELECT COUNT(DISTINCT id_member)
1415
		FROM {db_prefix}log_polls
1416
		WHERE id_poll = {int:current_poll}',
1417
		array(
1418
			'current_poll' => $row['id_poll'],
1419
		)
1420
	);
1421
	list ($total) = $smcFunc['db_fetch_row']($request);
1422
	$smcFunc['db_free_result']($request);
1423
1424
	$request = $smcFunc['db_query']('', '
1425
		SELECT id_choice, label, votes
1426
		FROM {db_prefix}poll_choices
1427
		WHERE id_poll = {int:current_poll}',
1428
		array(
1429
			'current_poll' => $row['id_poll'],
1430
		)
1431
	);
1432
	$sOptions = array();
1433
	while ($rowChoice = $smcFunc['db_fetch_assoc']($request))
1434
	{
1435
		censorText($rowChoice['label']);
1436
1437
		$sOptions[$rowChoice['id_choice']] = array($rowChoice['label'], $rowChoice['votes']);
1438
	}
1439
	$smcFunc['db_free_result']($request);
1440
1441
	// Can they view it?
1442
	$is_expired = !empty($row['expire_time']) && $row['expire_time'] < time();
1443
	$allow_view_results = allowedTo('moderate_board') || $row['hide_results'] == 0 || $is_expired;
1444
1445
	$return = array(
1446
		'id' => $row['id_poll'],
1447
		'image' => 'poll',
1448
		'question' => $row['question'],
1449
		'total_votes' => $total,
1450
		'is_locked' => false,
1451
		'topic' => $row['id_topic'],
1452
		'allow_view_results' => $allow_view_results,
1453
		'options' => array()
1454
	);
1455
1456
	// Calculate the percentages and bar lengths...
1457
	$divisor = $return['total_votes'] == 0 ? 1 : $return['total_votes'];
1458
	foreach ($sOptions as $i => $option)
1459
	{
1460
		$bar = floor(($option[1] * 100) / $divisor);
1461
		$return['options'][$i] = array(
1462
			'id' => 'options-' . ($topPollInstead ? 'top-' : 'recent-') . $i,
1463
			'percent' => $bar,
1464
			'votes' => $option[1],
1465
			'option' => parse_bbc($option[0]),
1466
			'vote_button' => '<input type="' . ($row['max_votes'] > 1 ? 'checkbox' : 'radio') . '" name="options[]" id="options-' . ($topPollInstead ? 'top-' : 'recent-') . $i . '" value="' . $i . '">'
1467
		);
1468
	}
1469
1470
	$return['allowed_warning'] = $row['max_votes'] > 1 ? sprintf($txt['poll_options_limit'], min(count($sOptions), $row['max_votes'])) : '';
1471
1472
	// If mods want to do somthing with this list of polls, let them do that now.
1473
	call_integration_hook('integrate_ssi_recentPoll', array(&$return, $topPollInstead));
1474
1475
	if ($output_method != 'echo')
1476
		return $return;
1477
1478
	if ($allow_view_results)
1479
	{
1480
		echo '
1481
		<form class="ssi_poll" action="', $boardurl, '/SSI.php?ssi_function=pollVote" method="post" accept-charset="', $context['character_set'], '">
1482
			<strong>', $return['question'], '</strong><br>
1483
			', !empty($return['allowed_warning']) ? $return['allowed_warning'] . '<br>' : '';
1484
1485
		foreach ($return['options'] as $option)
1486
			echo '
1487
			<label for="', $option['id'], '">', $option['vote_button'], ' ', $option['option'], '</label><br>';
1488
1489
		echo '
1490
			<input type="submit" value="', $txt['poll_vote'], '" class="button">
1491
			<input type="hidden" name="poll" value="', $return['id'], '">
1492
			<input type="hidden" name="', $context['session_var'], '" value="', $context['session_id'], '">
1493
		</form>';
1494
	}
1495
	else
1496
		echo $txt['poll_cannot_see'];
1497
}
1498
1499
/**
1500
 * Shows the poll from the specified topic
1501
 *
1502
 * @param null|int $topic The topic to show the poll from. If null, $_REQUEST['ssi_topic'] will be used instead.
1503
 * @param string $output_method The output method. If 'echo', displays the poll, otherwise returns an array of info about it.
1504
 * @return void|array Either displays the poll or returns an array of info about it, depending on output_method.
1505
 */
1506
function ssi_showPoll($topic = null, $output_method = 'echo')
1507
{
1508
	global $txt, $boardurl, $user_info, $context, $smcFunc, $modSettings;
1509
1510
	$boardsAllowed = boardsAllowedTo('poll_view');
1511
1512
	if (empty($boardsAllowed))
1513
		return array();
1514
1515
	if ($topic === null && isset($_REQUEST['ssi_topic']))
1516
		$topic = (int) $_REQUEST['ssi_topic'];
1517
	else
1518
		$topic = (int) $topic;
1519
1520
	$request = $smcFunc['db_query']('', '
1521
		SELECT
1522
			p.id_poll, p.question, p.voting_locked, p.hide_results, p.expire_time, p.max_votes, p.guest_vote, b.id_board
1523
		FROM {db_prefix}topics AS t
1524
			INNER JOIN {db_prefix}polls AS p ON (p.id_poll = t.id_poll)
1525
			INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board)
1526
		WHERE t.id_topic = {int:current_topic}
1527
			AND {query_see_board}' . (!in_array(0, $boardsAllowed) ? '
1528
			AND b.id_board IN ({array_int:boards_allowed_see})' : '') . ($modSettings['postmod_active'] ? '
1529
			AND t.approved = {int:is_approved}' : '') . '
1530
		LIMIT 1',
1531
		array(
1532
			'current_topic' => $topic,
1533
			'boards_allowed_see' => $boardsAllowed,
1534
			'is_approved' => 1,
1535
		)
1536
	);
1537
1538
	// Either this topic has no poll, or the user cannot view it.
1539
	if ($smcFunc['db_num_rows']($request) == 0)
1540
		return array();
1541
1542
	$row = $smcFunc['db_fetch_assoc']($request);
1543
	$smcFunc['db_free_result']($request);
1544
1545
	// Check if they can vote.
1546
	$already_voted = false;
1547
	if (!empty($row['expire_time']) && $row['expire_time'] < time())
1548
		$allow_vote = false;
1549
	elseif ($user_info['is_guest'])
1550
	{
1551
		// There's a difference between "allowed to vote" and "already voted"...
1552
		$allow_vote = $row['guest_vote'];
1553
1554
		// Did you already vote?
1555
		if (isset($_COOKIE['guest_poll_vote']) && in_array($row['id_poll'], explode(',', $_COOKIE['guest_poll_vote'])))
1556
		{
1557
			$already_voted = true;
1558
		}
1559
	}
1560
	elseif (!empty($row['voting_locked']) || !allowedTo('poll_vote', $row['id_board']))
1561
		$allow_vote = false;
1562
	else
1563
	{
1564
		$request = $smcFunc['db_query']('', '
1565
			SELECT id_member
1566
			FROM {db_prefix}log_polls
1567
			WHERE id_poll = {int:current_poll}
1568
				AND id_member = {int:current_member}
1569
			LIMIT 1',
1570
			array(
1571
				'current_member' => $user_info['id'],
1572
				'current_poll' => $row['id_poll'],
1573
			)
1574
		);
1575
		$allow_vote = $smcFunc['db_num_rows']($request) == 0;
1576
		$already_voted = $allow_vote;
1577
		$smcFunc['db_free_result']($request);
1578
	}
1579
1580
	// Can they view?
1581
	$is_expired = !empty($row['expire_time']) && $row['expire_time'] < time();
1582
	$allow_view_results = allowedTo('moderate_board') || $row['hide_results'] == 0 || ($row['hide_results'] == 1 && $already_voted) || $is_expired;
1583
1584
	$request = $smcFunc['db_query']('', '
1585
		SELECT COUNT(DISTINCT id_member)
1586
		FROM {db_prefix}log_polls
1587
		WHERE id_poll = {int:current_poll}',
1588
		array(
1589
			'current_poll' => $row['id_poll'],
1590
		)
1591
	);
1592
	list ($total) = $smcFunc['db_fetch_row']($request);
1593
	$smcFunc['db_free_result']($request);
1594
1595
	$request = $smcFunc['db_query']('', '
1596
		SELECT id_choice, label, votes
1597
		FROM {db_prefix}poll_choices
1598
		WHERE id_poll = {int:current_poll}',
1599
		array(
1600
			'current_poll' => $row['id_poll'],
1601
		)
1602
	);
1603
	$sOptions = array();
1604
	$total_votes = 0;
1605
	while ($rowChoice = $smcFunc['db_fetch_assoc']($request))
1606
	{
1607
		censorText($rowChoice['label']);
1608
1609
		$sOptions[$rowChoice['id_choice']] = array($rowChoice['label'], $rowChoice['votes']);
1610
		$total_votes += $rowChoice['votes'];
1611
	}
1612
	$smcFunc['db_free_result']($request);
1613
1614
	$return = array(
1615
		'id' => $row['id_poll'],
1616
		'image' => empty($row['voting_locked']) ? 'poll' : 'locked_poll',
1617
		'question' => $row['question'],
1618
		'total_votes' => $total,
1619
		'is_locked' => !empty($row['voting_locked']),
1620
		'allow_vote' => $allow_vote,
1621
		'allow_view_results' => $allow_view_results,
1622
		'topic' => $topic
1623
	);
1624
1625
	// Calculate the percentages and bar lengths...
1626
	$divisor = $total_votes == 0 ? 1 : $total_votes;
0 ignored issues
show
introduced by
The condition $total_votes == 0 is always true.
Loading history...
1627
	foreach ($sOptions as $i => $option)
1628
	{
1629
		$bar = floor(($option[1] * 100) / $divisor);
1630
		$return['options'][$i] = array(
1631
			'id' => 'options-' . $i,
1632
			'percent' => $bar,
1633
			'votes' => $option[1],
1634
			'option' => parse_bbc($option[0]),
1635
			'vote_button' => '<input type="' . ($row['max_votes'] > 1 ? 'checkbox' : 'radio') . '" name="options[]" id="options-' . $i . '" value="' . $i . '">'
1636
		);
1637
	}
1638
1639
	$return['allowed_warning'] = $row['max_votes'] > 1 ? sprintf($txt['poll_options_limit'], min(count($sOptions), $row['max_votes'])) : '';
1640
1641
	// If mods want to do somthing with this poll, let them do that now.
1642
	call_integration_hook('integrate_ssi_showPoll', array(&$return));
1643
1644
	if ($output_method != 'echo')
1645
		return $return;
1646
1647
	if ($return['allow_vote'])
1648
	{
1649
		echo '
1650
			<form class="ssi_poll" action="', $boardurl, '/SSI.php?ssi_function=pollVote" method="post" accept-charset="', $context['character_set'], '">
1651
				<strong>', $return['question'], '</strong><br>
1652
				', !empty($return['allowed_warning']) ? $return['allowed_warning'] . '<br>' : '';
1653
1654
		foreach ($return['options'] as $option)
1655
			echo '
1656
				<label for="', $option['id'], '">', $option['vote_button'], ' ', $option['option'], '</label><br>';
1657
1658
		echo '
1659
				<input type="submit" value="', $txt['poll_vote'], '" class="button">
1660
				<input type="hidden" name="poll" value="', $return['id'], '">
1661
				<input type="hidden" name="', $context['session_var'], '" value="', $context['session_id'], '">
1662
			</form>';
1663
	}
1664
	else
1665
	{
1666
		echo '
1667
			<div class="ssi_poll">
1668
				<strong>', $return['question'], '</strong>
1669
				<dl>';
1670
1671
		foreach ($return['options'] as $option)
1672
		{
1673
			echo '
1674
					<dt>', $option['option'], '</dt>
1675
					<dd>';
1676
1677
			if ($return['allow_view_results'])
1678
			{
1679
				echo '
1680
						<div class="ssi_poll_bar" style="border: 1px solid #666; height: 1em">
1681
							<div class="ssi_poll_bar_fill" style="background: #ccf; height: 1em; width: ', $option['percent'], '%;">
1682
							</div>
1683
						</div>
1684
						', $option['votes'], ' (', $option['percent'], '%)';
1685
			}
1686
1687
			echo '
1688
					</dd>';
1689
		}
1690
1691
		echo '
1692
				</dl>', ($return['allow_view_results'] ? '
1693
				<strong>' . $txt['poll_total_voters'] . ': ' . $return['total_votes'] . '</strong>' : ''), '
1694
			</div>';
1695
	}
1696
}
1697
1698
/**
1699
 * Handles voting in a poll (done automatically)
1700
 */
1701
function ssi_pollVote()
1702
{
1703
	global $context, $db_prefix, $user_info, $sc, $smcFunc, $sourcedir, $modSettings;
1704
1705
	if (!isset($_POST[$context['session_var']]) || $_POST[$context['session_var']] != $sc || empty($_POST['options']) || !isset($_POST['poll']))
1706
	{
1707
		echo '<!DOCTYPE html>
1708
<html>
1709
<head>
1710
	<script>
1711
		history.go(-1);
1712
	</script>
1713
</head>
1714
<body>&laquo;</body>
1715
</html>';
1716
		return;
1717
	}
1718
1719
	// This can cause weird errors! (ie. copyright missing.)
1720
	checkSession();
1721
1722
	$_POST['poll'] = (int) $_POST['poll'];
1723
1724
	// Check if they have already voted, or voting is locked.
1725
	$request = $smcFunc['db_query']('', '
1726
		SELECT
1727
			p.id_poll, p.voting_locked, p.expire_time, p.max_votes, p.guest_vote,
1728
			t.id_topic,
1729
			COALESCE(lp.id_choice, -1) AS selected
1730
		FROM {db_prefix}polls AS p
1731
			INNER JOIN {db_prefix}topics AS t ON (t.id_poll = {int:current_poll})
1732
			INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board)
1733
			LEFT JOIN {db_prefix}log_polls AS lp ON (lp.id_poll = p.id_poll AND lp.id_member = {int:current_member})
1734
		WHERE p.id_poll = {int:current_poll}
1735
			AND {query_see_board}' . ($modSettings['postmod_active'] ? '
1736
			AND t.approved = {int:is_approved}' : '') . '
1737
		LIMIT 1',
1738
		array(
1739
			'current_member' => $user_info['id'],
1740
			'current_poll' => $_POST['poll'],
1741
			'is_approved' => 1,
1742
		)
1743
	);
1744
	if ($smcFunc['db_num_rows']($request) == 0)
1745
		die;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
1746
	$row = $smcFunc['db_fetch_assoc']($request);
1747
	$smcFunc['db_free_result']($request);
1748
1749
	if (!empty($row['voting_locked']) || ($row['selected'] != -1 && !$user_info['is_guest']) || (!empty($row['expire_time']) && time() > $row['expire_time']))
1750
		redirectexit('topic=' . $row['id_topic'] . '.0');
1751
1752
	// Too many options checked?
1753
	if (count($_REQUEST['options']) > $row['max_votes'])
1754
		redirectexit('topic=' . $row['id_topic'] . '.0');
1755
1756
	// It's a guest who has already voted?
1757
	if ($user_info['is_guest'])
1758
	{
1759
		// Guest voting disabled?
1760
		if (!$row['guest_vote'])
1761
			redirectexit('topic=' . $row['id_topic'] . '.0');
1762
		// Already voted?
1763
		elseif (isset($_COOKIE['guest_poll_vote']) && in_array($row['id_poll'], explode(',', $_COOKIE['guest_poll_vote'])))
1764
			redirectexit('topic=' . $row['id_topic'] . '.0');
1765
	}
1766
1767
	$sOptions = array();
1768
	$inserts = array();
1769
	foreach ($_REQUEST['options'] as $id)
1770
	{
1771
		$id = (int) $id;
1772
1773
		$sOptions[] = $id;
1774
		$inserts[] = array($_POST['poll'], $user_info['id'], $id);
1775
	}
1776
1777
	// Add their vote in to the tally.
1778
	$smcFunc['db_insert']('insert',
1779
		$db_prefix . 'log_polls',
1780
		array('id_poll' => 'int', 'id_member' => 'int', 'id_choice' => 'int'),
1781
		$inserts,
1782
		array('id_poll', 'id_member', 'id_choice')
1783
	);
1784
	$smcFunc['db_query']('', '
1785
		UPDATE {db_prefix}poll_choices
1786
		SET votes = votes + 1
1787
		WHERE id_poll = {int:current_poll}
1788
			AND id_choice IN ({array_int:option_list})',
1789
		array(
1790
			'option_list' => $sOptions,
1791
			'current_poll' => $_POST['poll'],
1792
		)
1793
	);
1794
1795
	// Track the vote if a guest.
1796
	if ($user_info['is_guest'])
1797
	{
1798
		$_COOKIE['guest_poll_vote'] = !empty($_COOKIE['guest_poll_vote']) ? ($_COOKIE['guest_poll_vote'] . ',' . $row['id_poll']) : $row['id_poll'];
1799
1800
		require_once($sourcedir . '/Subs-Auth.php');
1801
		$cookie_url = url_parts(!empty($modSettings['localCookies']), !empty($modSettings['globalCookies']));
1802
		smf_setcookie('guest_poll_vote', $_COOKIE['guest_poll_vote'], time() + 2500000, $cookie_url[1], $cookie_url[0], false, false);
1803
	}
1804
1805
	redirectexit('topic=' . $row['id_topic'] . '.0');
1806
}
1807
1808
// Show a search box.
1809
/**
1810
 * Shows a search box
1811
 *
1812
 * @param string $output_method The output method. If 'echo', displays a search box, otherwise returns the URL of the search page.
1813
 * @return void|string Displays a search box or returns the URL to the search page depending on output_method. If you don't have permission to search, the function won't return anything.
1814
 */
1815
function ssi_quickSearch($output_method = 'echo')
1816
{
1817
	global $scripturl, $txt, $context;
1818
1819
	if (!allowedTo('search_posts'))
1820
		return;
1821
1822
	if ($output_method != 'echo')
1823
		return $scripturl . '?action=search';
1824
1825
	echo '
1826
		<form action="', $scripturl, '?action=search2" method="post" accept-charset="', $context['character_set'], '">
1827
			<input type="hidden" name="advanced" value="0"><input type="text" name="ssi_search" size="30"> <input type="submit" value="', $txt['search'], '" class="button">
1828
		</form>';
1829
}
1830
1831
/**
1832
 * Show a random forum news item
1833
 *
1834
 * @param string $output_method The output method. If 'echo', shows the news item, otherwise returns it.
1835
 * @return void|string Shows or returns a random forum news item, depending on output_method.
1836
 */
1837
function ssi_news($output_method = 'echo')
1838
{
1839
	global $context;
1840
1841
	$context['random_news_line'] = !empty($context['news_lines']) ? $context['news_lines'][mt_rand(0, count($context['news_lines']) - 1)] : '';
1842
1843
	// If mods want to do somthing with the news, let them do that now. Don't need to pass the news line itself, since it is already in $context.
1844
	call_integration_hook('integrate_ssi_news');
1845
1846
	if ($output_method != 'echo')
1847
		return $context['random_news_line'];
1848
1849
	echo $context['random_news_line'];
1850
}
1851
1852
/**
1853
 * Show today's birthdays.
1854
 *
1855
 * @param string $output_method The output method. If 'echo', displays a list of users, otherwise returns an array of info about them.
1856
 * @return void|array Displays a list of users or returns an array of info about them depending on output_method.
1857
 */
1858
function ssi_todaysBirthdays($output_method = 'echo')
1859
{
1860
	global $scripturl, $modSettings, $user_info;
1861
1862
	if (empty($modSettings['cal_enabled']) || !allowedTo('calendar_view') || !allowedTo('profile_view'))
1863
		return;
1864
1865
	$eventOptions = array(
1866
		'include_birthdays' => true,
1867
		'num_days_shown' => empty($modSettings['cal_days_for_index']) || $modSettings['cal_days_for_index'] < 1 ? 1 : $modSettings['cal_days_for_index'],
1868
	);
1869
	$return = cache_quick_get('calendar_index_offset_' . ($user_info['time_offset'] + $modSettings['time_offset']), 'Subs-Calendar.php', 'cache_getRecentEvents', array($eventOptions));
1870
1871
	// The ssi_todaysCalendar variants all use the same hook and just pass on $eventOptions so the hooked code can distinguish different cases if necessary
1872
	call_integration_hook('integrate_ssi_calendar', array(&$return, $eventOptions));
1873
1874
	if ($output_method != 'echo')
1875
		return $return['calendar_birthdays'];
1876
1877
	foreach ($return['calendar_birthdays'] as $member)
1878
		echo '
1879
			<a href="', $scripturl, '?action=profile;u=', $member['id'], '"><span class="fix_rtl_names">' . $member['name'] . '</span>' . (isset($member['age']) ? ' (' . $member['age'] . ')' : '') . '</a>' . (!$member['is_last'] ? ', ' : '');
1880
}
1881
1882
/**
1883
 * Shows today's holidays.
1884
 *
1885
 * @param string $output_method The output method. If 'echo', displays a list of holidays, otherwise returns an array of info about them.
1886
 * @return void|array Displays a list of holidays or returns an array of info about them depending on output_method
1887
 */
1888
function ssi_todaysHolidays($output_method = 'echo')
1889
{
1890
	global $modSettings, $user_info;
1891
1892
	if (empty($modSettings['cal_enabled']) || !allowedTo('calendar_view'))
1893
		return;
1894
1895
	$eventOptions = array(
1896
		'include_holidays' => true,
1897
		'num_days_shown' => empty($modSettings['cal_days_for_index']) || $modSettings['cal_days_for_index'] < 1 ? 1 : $modSettings['cal_days_for_index'],
1898
	);
1899
	$return = cache_quick_get('calendar_index_offset_' . ($user_info['time_offset'] + $modSettings['time_offset']), 'Subs-Calendar.php', 'cache_getRecentEvents', array($eventOptions));
1900
1901
	// The ssi_todaysCalendar variants all use the same hook and just pass on $eventOptions so the hooked code can distinguish different cases if necessary
1902
	call_integration_hook('integrate_ssi_calendar', array(&$return, $eventOptions));
1903
1904
	if ($output_method != 'echo')
1905
		return $return['calendar_holidays'];
1906
1907
	echo '
1908
		', implode(', ', $return['calendar_holidays']);
1909
}
1910
1911
/**
1912
 * @param string $output_method The output method. If 'echo', displays a list of events, otherwise returns an array of info about them.
1913
 * @return void|array Displays a list of events or returns an array of info about them depending on output_method
1914
 */
1915
function ssi_todaysEvents($output_method = 'echo')
1916
{
1917
	global $modSettings, $user_info;
1918
1919
	if (empty($modSettings['cal_enabled']) || !allowedTo('calendar_view'))
1920
		return;
1921
1922
	$eventOptions = array(
1923
		'include_events' => true,
1924
		'num_days_shown' => empty($modSettings['cal_days_for_index']) || $modSettings['cal_days_for_index'] < 1 ? 1 : $modSettings['cal_days_for_index'],
1925
	);
1926
	$return = cache_quick_get('calendar_index_offset_' . ($user_info['time_offset'] + $modSettings['time_offset']), 'Subs-Calendar.php', 'cache_getRecentEvents', array($eventOptions));
1927
1928
	// The ssi_todaysCalendar variants all use the same hook and just pass on $eventOptions so the hooked code can distinguish different cases if necessary
1929
	call_integration_hook('integrate_ssi_calendar', array(&$return, $eventOptions));
1930
1931
	if ($output_method != 'echo')
1932
		return $return['calendar_events'];
1933
1934
	foreach ($return['calendar_events'] as $event)
1935
	{
1936
		if ($event['can_edit'])
1937
			echo '
1938
	<a href="' . $event['modify_href'] . '" style="color: #ff0000;">*</a> ';
1939
		echo '
1940
	' . $event['link'] . (!$event['is_last'] ? ', ' : '');
1941
	}
1942
}
1943
1944
/**
1945
 * Shows today's calendar items (events, birthdays and holidays)
1946
 *
1947
 * @param string $output_method The output method. If 'echo', displays a list of calendar items, otherwise returns an array of info about them.
1948
 * @return void|array Displays a list of calendar items or returns an array of info about them depending on output_method
1949
 */
1950
function ssi_todaysCalendar($output_method = 'echo')
1951
{
1952
	global $modSettings, $txt, $scripturl, $user_info;
1953
1954
	if (empty($modSettings['cal_enabled']) || !allowedTo('calendar_view'))
1955
		return;
1956
1957
	$eventOptions = array(
1958
		'include_birthdays' => allowedTo('profile_view'),
1959
		'include_holidays' => true,
1960
		'include_events' => true,
1961
		'num_days_shown' => empty($modSettings['cal_days_for_index']) || $modSettings['cal_days_for_index'] < 1 ? 1 : $modSettings['cal_days_for_index'],
1962
	);
1963
	$return = cache_quick_get('calendar_index_offset_' . ($user_info['time_offset'] + $modSettings['time_offset']), 'Subs-Calendar.php', 'cache_getRecentEvents', array($eventOptions));
1964
1965
	// The ssi_todaysCalendar variants all use the same hook and just pass on $eventOptions so the hooked code can distinguish different cases if necessary
1966
	call_integration_hook('integrate_ssi_calendar', array(&$return, $eventOptions));
1967
1968
	if ($output_method != 'echo')
1969
		return $return;
1970
1971
	if (!empty($return['calendar_holidays']))
1972
		echo '
1973
			<span class="holiday">' . $txt['calendar_prompt'] . ' ' . implode(', ', $return['calendar_holidays']) . '<br></span>';
1974
	if (!empty($return['calendar_birthdays']))
1975
	{
1976
		echo '
1977
			<span class="birthday">' . $txt['birthdays_upcoming'] . '</span> ';
1978
		foreach ($return['calendar_birthdays'] as $member)
1979
			echo '
1980
			<a href="', $scripturl, '?action=profile;u=', $member['id'], '"><span class="fix_rtl_names">', $member['name'], '</span>', isset($member['age']) ? ' (' . $member['age'] . ')' : '', '</a>', !$member['is_last'] ? ', ' : '';
1981
		echo '
1982
			<br>';
1983
	}
1984
	if (!empty($return['calendar_events']))
1985
	{
1986
		echo '
1987
			<span class="event">' . $txt['events_upcoming'] . '</span> ';
1988
		foreach ($return['calendar_events'] as $event)
1989
		{
1990
			if ($event['can_edit'])
1991
				echo '
1992
			<a href="' . $event['modify_href'] . '" style="color: #ff0000;">*</a> ';
1993
			echo '
1994
			' . $event['link'] . (!$event['is_last'] ? ', ' : '');
1995
		}
1996
	}
1997
}
1998
1999
/**
2000
 * Show the latest news, with a template... by board.
2001
 *
2002
 * @param null|int $board The ID of the board to get the info from. Defaults to $board or $_GET['board'] if not set.
2003
 * @param null|int $limit How many items to show. Defaults to $_GET['limit'] or 5 if not set.
2004
 * @param null|int $start Start with the specified item. Defaults to $_GET['start'] or 0 if not set.
2005
 * @param null|int $length How many characters to show from each post. Defaults to $_GET['length'] or 0 (no limit) if not set.
2006
 * @param string $output_method The output method. If 'echo', displays the news items, otherwise returns an array of info about them.
2007
 * @return void|array Displays the news items or returns an array of info about them, depending on output_method.
2008
 */
2009
function ssi_boardNews($board = null, $limit = null, $start = null, $length = null, $output_method = 'echo')
2010
{
2011
	global $scripturl, $txt, $settings, $modSettings, $context;
2012
	global $smcFunc;
2013
2014
	loadLanguage('Stats');
2015
2016
	// Must be integers....
2017
	if ($limit === null)
2018
		$limit = isset($_GET['limit']) ? (int) $_GET['limit'] : 5;
2019
	else
2020
		$limit = (int) $limit;
2021
2022
	if ($start === null)
2023
		$start = isset($_GET['start']) ? (int) $_GET['start'] : 0;
2024
	else
2025
		$start = (int) $start;
2026
2027
	if ($board !== null)
2028
		$board = (int) $board;
2029
	elseif (isset($_GET['board']))
2030
		$board = (int) $_GET['board'];
2031
2032
	if ($length === null)
2033
		$length = isset($_GET['length']) ? (int) $_GET['length'] : 0;
2034
	else
2035
		$length = (int) $length;
2036
2037
	$limit = max(0, $limit);
2038
	$start = max(0, $start);
2039
2040
	// Make sure guests can see this board.
2041
	$request = $smcFunc['db_query']('', '
2042
		SELECT id_board
2043
		FROM {db_prefix}boards
2044
		WHERE ' . ($board === null ? '' : 'id_board = {int:current_board}
2045
			AND ') . 'FIND_IN_SET(-1, member_groups) != 0
2046
		LIMIT 1',
2047
		array(
2048
			'current_board' => $board,
2049
		)
2050
	);
2051
	if ($smcFunc['db_num_rows']($request) == 0)
2052
	{
2053
		if ($output_method == 'echo')
2054
			die($txt['ssi_no_guests']);
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
2055
		else
2056
			return array();
2057
	}
2058
	list ($board) = $smcFunc['db_fetch_row']($request);
2059
	$smcFunc['db_free_result']($request);
2060
2061
	$icon_sources = array();
2062
	foreach ($context['stable_icons'] as $icon)
2063
		$icon_sources[$icon] = 'images_url';
2064
2065
	if (!empty($modSettings['enable_likes']))
2066
	{
2067
		$context['can_like'] = allowedTo('likes_like');
2068
	}
2069
2070
	// Find the post ids.
2071
	$request = $smcFunc['db_query']('', '
2072
		SELECT t.id_first_msg
2073
		FROM {db_prefix}topics as t
2074
			LEFT JOIN {db_prefix}boards as b ON (b.id_board = t.id_board)
2075
		WHERE t.id_board = {int:current_board}' . ($modSettings['postmod_active'] ? '
2076
			AND t.approved = {int:is_approved}' : '') . '
2077
			AND {query_see_board}
2078
		ORDER BY t.id_first_msg DESC
2079
		LIMIT ' . $start . ', ' . $limit,
2080
		array(
2081
			'current_board' => $board,
2082
			'is_approved' => 1,
2083
		)
2084
	);
2085
	$posts = array();
2086
	while ($row = $smcFunc['db_fetch_assoc']($request))
2087
		$posts[] = $row['id_first_msg'];
2088
	$smcFunc['db_free_result']($request);
2089
2090
	if (empty($posts))
2091
		return array();
2092
2093
	// Find the posts.
2094
	$request = $smcFunc['db_query']('', '
2095
		SELECT
2096
			m.icon, m.subject, m.body, COALESCE(mem.real_name, m.poster_name) AS poster_name, m.poster_time, m.likes,
2097
			t.num_replies, t.id_topic, m.id_member, m.smileys_enabled, m.id_msg, t.locked, t.id_last_msg, m.id_board
2098
		FROM {db_prefix}topics AS t
2099
			INNER JOIN {db_prefix}messages AS m ON (m.id_msg = t.id_first_msg)
2100
			LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member)
2101
		WHERE t.id_first_msg IN ({array_int:post_list})
2102
		ORDER BY t.id_first_msg DESC
2103
		LIMIT ' . count($posts),
2104
		array(
2105
			'post_list' => $posts,
2106
		)
2107
	);
2108
	$return = array();
2109
	$recycle_board = !empty($modSettings['recycle_enable']) && !empty($modSettings['recycle_board']) ? (int) $modSettings['recycle_board'] : 0;
2110
	while ($row = $smcFunc['db_fetch_assoc']($request))
2111
	{
2112
		// If we want to limit the length of the post.
2113
		if (!empty($length) && $smcFunc['strlen']($row['body']) > $length)
2114
		{
2115
			$row['body'] = $smcFunc['substr']($row['body'], 0, $length);
2116
			$cutoff = false;
2117
2118
			$last_space = strrpos($row['body'], ' ');
2119
			$last_open = strrpos($row['body'], '<');
2120
			$last_close = strrpos($row['body'], '>');
2121
			if (empty($last_space) || ($last_space == $last_open + 3 && (empty($last_close) || (!empty($last_close) && $last_close < $last_open))) || $last_space < $last_open || $last_open == $length - 6)
2122
				$cutoff = $last_open;
2123
			elseif (empty($last_close) || $last_close < $last_open)
2124
				$cutoff = $last_space;
2125
2126
			if ($cutoff !== false)
2127
				$row['body'] = $smcFunc['substr']($row['body'], 0, $cutoff);
2128
			$row['body'] .= '...';
2129
		}
2130
2131
		$row['body'] = parse_bbc($row['body'], $row['smileys_enabled'], $row['id_msg']);
2132
2133
		if (!empty($recycle_board) && $row['id_board'] == $recycle_board)
2134
			$row['icon'] = 'recycled';
2135
2136
		// Check that this message icon is there...
2137
		if (!empty($modSettings['messageIconChecks_enable']) && !isset($icon_sources[$row['icon']]))
2138
			$icon_sources[$row['icon']] = file_exists($settings['theme_dir'] . '/images/post/' . $row['icon'] . '.png') ? 'images_url' : 'default_images_url';
2139
		elseif (!isset($icon_sources[$row['icon']]))
2140
			$icon_sources[$row['icon']] = 'images_url';
2141
2142
		censorText($row['subject']);
2143
		censorText($row['body']);
2144
2145
		$return[] = array(
2146
			'id' => $row['id_topic'],
2147
			'message_id' => $row['id_msg'],
2148
			'icon' => '<img src="' . $settings[$icon_sources[$row['icon']]] . '/post/' . $row['icon'] . '.png" alt="' . $row['icon'] . '">',
2149
			'subject' => $row['subject'],
2150
			'time' => timeformat($row['poster_time']),
2151
			'timestamp' => forum_time(true, $row['poster_time']),
2152
			'body' => $row['body'],
2153
			'href' => $scripturl . '?topic=' . $row['id_topic'] . '.0',
2154
			'link' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.0">' . $row['num_replies'] . ' ' . ($row['num_replies'] == 1 ? $txt['ssi_comment'] : $txt['ssi_comments']) . '</a>',
2155
			'replies' => $row['num_replies'],
2156
			'comment_href' => !empty($row['locked']) ? '' : $scripturl . '?action=post;topic=' . $row['id_topic'] . '.' . $row['num_replies'] . ';last_msg=' . $row['id_last_msg'],
2157
			'comment_link' => !empty($row['locked']) ? '' : '<a href="' . $scripturl . '?action=post;topic=' . $row['id_topic'] . '.' . $row['num_replies'] . ';last_msg=' . $row['id_last_msg'] . '">' . $txt['ssi_write_comment'] . '</a>',
2158
			'new_comment' => !empty($row['locked']) ? '' : '<a href="' . $scripturl . '?action=post;topic=' . $row['id_topic'] . '.' . $row['num_replies'] . '">' . $txt['ssi_write_comment'] . '</a>',
2159
			'poster' => array(
2160
				'id' => $row['id_member'],
2161
				'name' => $row['poster_name'],
2162
				'href' => !empty($row['id_member']) ? $scripturl . '?action=profile;u=' . $row['id_member'] : '',
2163
				'link' => !empty($row['id_member']) ? '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['poster_name'] . '</a>' : $row['poster_name']
2164
			),
2165
			'locked' => !empty($row['locked']),
2166
			'is_last' => false,
2167
			// Nasty ternary for likes not messing around the "is_last" check.
2168
			'likes' => !empty($modSettings['enable_likes']) ? array(
2169
				'count' => $row['likes'],
2170
				'you' => in_array($row['id_msg'], prepareLikesContext((int) $row['id_topic'])),
2171
				'can_like' => !$context['user']['is_guest'] && $row['id_member'] != $context['user']['id'] && !empty($context['can_like']),
2172
			) : array(),
2173
		);
2174
	}
2175
	$smcFunc['db_free_result']($request);
2176
2177
	if (empty($return))
2178
		return $return;
2179
2180
	$return[count($return) - 1]['is_last'] = true;
2181
2182
	// If mods want to do somthing with this list of posts, let them do that now.
2183
	call_integration_hook('integrate_ssi_boardNews', array(&$return));
2184
2185
	if ($output_method != 'echo')
2186
		return $return;
2187
2188
	foreach ($return as $news)
2189
	{
2190
		echo '
2191
			<div class="news_item">
2192
				<h3 class="news_header">
2193
					', $news['icon'], '
2194
					<a href="', $news['href'], '">', $news['subject'], '</a>
2195
				</h3>
2196
				<div class="news_timestamp">', $news['time'], ' ', $txt['by'], ' ', $news['poster']['link'], '</div>
2197
				<div class="news_body" style="padding: 2ex 0;">', $news['body'], '</div>
2198
				', $news['link'], $news['locked'] ? '' : ' | ' . $news['comment_link'], '';
2199
2200
		// Is there any likes to show?
2201
		if (!empty($modSettings['enable_likes']))
2202
		{
2203
			echo '
2204
					<ul>';
2205
2206
			if (!empty($news['likes']['can_like']))
2207
			{
2208
				echo '
2209
						<li class="smflikebutton" id="msg_', $news['message_id'], '_likes"><a href="', $scripturl, '?action=likes;ltype=msg;sa=like;like=', $news['message_id'], ';', $context['session_var'], '=', $context['session_id'], '" class="msg_like"><span class="', $news['likes']['you'] ? 'unlike' : 'like', '"></span>', $news['likes']['you'] ? $txt['unlike'] : $txt['like'], '</a></li>';
2210
			}
2211
2212
			if (!empty($news['likes']['count']))
2213
			{
2214
				$context['some_likes'] = true;
2215
				$count = $news['likes']['count'];
2216
				$base = 'likes_';
2217
				if ($news['likes']['you'])
2218
				{
2219
					$base = 'you_' . $base;
2220
					$count--;
2221
				}
2222
				$base .= (isset($txt[$base . $count])) ? $count : 'n';
2223
2224
				echo '
2225
						<li class="like_count smalltext">', sprintf($txt[$base], $scripturl . '?action=likes;sa=view;ltype=msg;like=' . $news['message_id'] . ';' . $context['session_var'] . '=' . $context['session_id'], comma_format($count)), '</li>';
2226
			}
2227
2228
			echo '
2229
					</ul>';
2230
		}
2231
2232
		// Close the main div.
2233
		echo '
2234
			</div>';
2235
2236
		if (!$news['is_last'])
2237
			echo '
2238
			<hr>';
2239
	}
2240
}
2241
2242
/**
2243
 * Show the most recent events
2244
 *
2245
 * @param int $max_events The maximum number of events to show
2246
 * @param string $output_method The output method. If 'echo', displays the events, otherwise returns an array of info about them.
2247
 * @return void|array Displays the events or returns an array of info about them, depending on output_method.
2248
 */
2249
function ssi_recentEvents($max_events = 7, $output_method = 'echo')
2250
{
2251
	global $user_info, $scripturl, $modSettings, $txt, $context, $smcFunc;
2252
2253
	if (empty($modSettings['cal_enabled']) || !allowedTo('calendar_view'))
2254
		return;
2255
2256
	// Find all events which are happening in the near future that the member can see.
2257
	$request = $smcFunc['db_query']('', '
2258
		SELECT
2259
			cal.id_event, cal.start_date, cal.end_date, cal.title, cal.id_member, cal.id_topic,
2260
			cal.start_time, cal.end_time, cal.timezone, cal.location,
2261
			cal.id_board, t.id_first_msg, t.approved
2262
		FROM {db_prefix}calendar AS cal
2263
			LEFT JOIN {db_prefix}boards AS b ON (b.id_board = cal.id_board)
2264
			LEFT JOIN {db_prefix}topics AS t ON (t.id_topic = cal.id_topic)
2265
		WHERE cal.start_date <= {date:current_date}
2266
			AND cal.end_date >= {date:current_date}
2267
			AND (cal.id_board = {int:no_board} OR {query_wanna_see_board})
2268
		ORDER BY cal.start_date DESC
2269
		LIMIT ' . $max_events,
2270
		array(
2271
			'current_date' => strftime('%Y-%m-%d', forum_time(false)),
2272
			'no_board' => 0,
2273
		)
2274
	);
2275
	$return = array();
2276
	$duplicates = array();
2277
	while ($row = $smcFunc['db_fetch_assoc']($request))
2278
	{
2279
		// Check if we've already come by an event linked to this same topic with the same title... and don't display it if we have.
2280
		if (!empty($duplicates[$row['title'] . $row['id_topic']]))
2281
			continue;
2282
2283
		// Censor the title.
2284
		censorText($row['title']);
2285
2286
		if ($row['start_date'] < strftime('%Y-%m-%d', forum_time(false)))
2287
			$date = strftime('%Y-%m-%d', forum_time(false));
2288
		else
2289
			$date = $row['start_date'];
2290
2291
		// If the topic it is attached to is not approved then don't link it.
2292
		if (!empty($row['id_first_msg']) && !$row['approved'])
2293
			$row['id_board'] = $row['id_topic'] = $row['id_first_msg'] = 0;
2294
2295
		$allday = (empty($row['start_time']) || empty($row['end_time']) || empty($row['timezone']) || !in_array($row['timezone'], timezone_identifiers_list(DateTimeZone::ALL_WITH_BC))) ? true : false;
0 ignored issues
show
Bug introduced by
It seems like timezone_identifiers_lis...eTimeZone::ALL_WITH_BC) can also be of type false; however, parameter $haystack of in_array() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

2295
		$allday = (empty($row['start_time']) || empty($row['end_time']) || empty($row['timezone']) || !in_array($row['timezone'], /** @scrutinizer ignore-type */ timezone_identifiers_list(DateTimeZone::ALL_WITH_BC))) ? true : false;
Loading history...
2296
2297
		$return[$date][] = array(
2298
			'id' => $row['id_event'],
2299
			'title' => $row['title'],
2300
			'location' => $row['location'],
2301
			'can_edit' => allowedTo('calendar_edit_any') || ($row['id_member'] == $user_info['id'] && allowedTo('calendar_edit_own')),
2302
			'modify_href' => $scripturl . '?action=' . ($row['id_board'] == 0 ? 'calendar;sa=post;' : 'post;msg=' . $row['id_first_msg'] . ';topic=' . $row['id_topic'] . '.0;calendar;') . 'eventid=' . $row['id_event'] . ';' . $context['session_var'] . '=' . $context['session_id'],
2303
			'href' => $row['id_board'] == 0 ? '' : $scripturl . '?topic=' . $row['id_topic'] . '.0',
2304
			'link' => $row['id_board'] == 0 ? $row['title'] : '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.0">' . $row['title'] . '</a>',
2305
			'start_date' => $row['start_date'],
2306
			'end_date' => $row['end_date'],
2307
			'start_time' => !$allday ? $row['start_time'] : null,
2308
			'end_time' => !$allday ? $row['end_time'] : null,
2309
			'tz' => !$allday ? $row['timezone'] : null,
2310
			'allday' => $allday,
2311
			'is_last' => false
2312
		);
2313
2314
		// Let's not show this one again, huh?
2315
		$duplicates[$row['title'] . $row['id_topic']] = true;
2316
	}
2317
	$smcFunc['db_free_result']($request);
2318
2319
	foreach ($return as $mday => $array)
2320
		$return[$mday][count($array) - 1]['is_last'] = true;
2321
2322
	// If mods want to do somthing with this list of events, let them do that now.
2323
	call_integration_hook('integrate_ssi_recentEvents', array(&$return));
2324
2325
	if ($output_method != 'echo' || empty($return))
2326
		return $return;
2327
2328
	// Well the output method is echo.
2329
	echo '
2330
			<span class="event">' . $txt['events'] . '</span> ';
2331
	foreach ($return as $mday => $array)
2332
		foreach ($array as $event)
2333
		{
2334
			if ($event['can_edit'])
2335
				echo '
2336
				<a href="' . $event['modify_href'] . '" style="color: #ff0000;">*</a> ';
2337
2338
			echo '
2339
				' . $event['link'] . (!$event['is_last'] ? ', ' : '');
2340
		}
2341
}
2342
2343
/**
2344
 * Checks whether the specified password is correct for the specified user.
2345
 *
2346
 * @param int|string $id The ID or username of a user
2347
 * @param string $password The password to check
2348
 * @param bool $is_username If true, treats $id as a username rather than a user ID
2349
 * @return bool Whether or not the password is correct.
2350
 */
2351
function ssi_checkPassword($id = null, $password = null, $is_username = false)
2352
{
2353
	global $smcFunc;
2354
2355
	// If $id is null, this was most likely called from a query string and should do nothing.
2356
	if ($id === null)
2357
		return;
2358
2359
	$request = $smcFunc['db_query']('', '
2360
		SELECT passwd, member_name, is_activated
2361
		FROM {db_prefix}members
2362
		WHERE ' . ($is_username ? 'member_name' : 'id_member') . ' = {string:id}
2363
		LIMIT 1',
2364
		array(
2365
			'id' => $id,
2366
		)
2367
	);
2368
	list ($pass, $user, $active) = $smcFunc['db_fetch_row']($request);
2369
	$smcFunc['db_free_result']($request);
2370
2371
	return hash_verify_password($user, $password, $pass) && $active == 1;
2372
}
2373
2374
/**
2375
 * Shows the most recent attachments that the user can see
2376
 *
2377
 * @param int $num_attachments How many to show
2378
 * @param array $attachment_ext Only shows attachments with the specified extensions ('jpg', 'gif', etc.) if set
2379
 * @param string $output_method The output method. If 'echo', displays a table with links/info, otherwise returns an array with information about the attachments
2380
 * @return void|array Displays a table of attachment info or returns an array containing info about the attachments, depending on output_method.
2381
 */
2382
function ssi_recentAttachments($num_attachments = 10, $attachment_ext = array(), $output_method = 'echo')
2383
{
2384
	global $smcFunc, $modSettings, $scripturl, $txt, $settings;
2385
2386
	// We want to make sure that we only get attachments for boards that we can see *if* any.
2387
	$attachments_boards = boardsAllowedTo('view_attachments');
2388
2389
	// No boards?  Adios amigo.
2390
	if (empty($attachments_boards))
2391
		return array();
2392
2393
	// Is it an array?
2394
	$attachment_ext = (array) $attachment_ext;
2395
2396
	// Lets build the query.
2397
	$request = $smcFunc['db_query']('', '
2398
		SELECT
2399
			att.id_attach, att.id_msg, att.filename, COALESCE(att.size, 0) AS filesize, att.downloads, mem.id_member,
2400
			COALESCE(mem.real_name, m.poster_name) AS poster_name, m.id_topic, m.subject, t.id_board, m.poster_time,
2401
			att.width, att.height' . (empty($modSettings['attachmentShowImages']) || empty($modSettings['attachmentThumbnails']) ? '' : ', COALESCE(thumb.id_attach, 0) AS id_thumb, thumb.width AS thumb_width, thumb.height AS thumb_height') . '
2402
		FROM {db_prefix}attachments AS att
2403
			INNER JOIN {db_prefix}messages AS m ON (m.id_msg = att.id_msg)
2404
			INNER JOIN {db_prefix}topics AS t ON (t.id_topic = m.id_topic)
2405
			LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member)' . (empty($modSettings['attachmentShowImages']) || empty($modSettings['attachmentThumbnails']) ? '' : '
2406
			LEFT JOIN {db_prefix}attachments AS thumb ON (thumb.id_attach = att.id_thumb)') . '
2407
		WHERE att.attachment_type = 0' . ($attachments_boards === array(0) ? '' : '
2408
			AND m.id_board IN ({array_int:boards_can_see})') . (!empty($attachment_ext) ? '
2409
			AND att.fileext IN ({array_string:attachment_ext})' : '') .
2410
			(!$modSettings['postmod_active'] || allowedTo('approve_posts') ? '' : '
2411
			AND t.approved = {int:is_approved}
2412
			AND m.approved = {int:is_approved}
2413
			AND att.approved = {int:is_approved}') . '
2414
		ORDER BY att.id_attach DESC
2415
		LIMIT {int:num_attachments}',
2416
		array(
2417
			'boards_can_see' => $attachments_boards,
2418
			'attachment_ext' => $attachment_ext,
2419
			'num_attachments' => $num_attachments,
2420
			'is_approved' => 1,
2421
		)
2422
	);
2423
2424
	// We have something.
2425
	$attachments = array();
2426
	while ($row = $smcFunc['db_fetch_assoc']($request))
2427
	{
2428
		$filename = preg_replace('~&amp;#(\\d{1,7}|x[0-9a-fA-F]{1,6});~', '&#\\1;', htmlspecialchars($row['filename']));
2429
2430
		// Is it an image?
2431
		$attachments[$row['id_attach']] = array(
2432
			'member' => array(
2433
				'id' => $row['id_member'],
2434
				'name' => $row['poster_name'],
2435
				'link' => empty($row['id_member']) ? $row['poster_name'] : '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['poster_name'] . '</a>',
2436
			),
2437
			'file' => array(
2438
				'filename' => $filename,
2439
				'filesize' => round($row['filesize'] / 1024, 2) . $txt['kilobyte'],
2440
				'downloads' => $row['downloads'],
2441
				'href' => $scripturl . '?action=dlattach;topic=' . $row['id_topic'] . '.0;attach=' . $row['id_attach'],
2442
				'link' => '<img src="' . $settings['images_url'] . '/icons/clip.png" alt=""> <a href="' . $scripturl . '?action=dlattach;topic=' . $row['id_topic'] . '.0;attach=' . $row['id_attach'] . '">' . $filename . '</a>',
2443
				'is_image' => !empty($row['width']) && !empty($row['height']) && !empty($modSettings['attachmentShowImages']),
2444
			),
2445
			'topic' => array(
2446
				'id' => $row['id_topic'],
2447
				'subject' => $row['subject'],
2448
				'href' => $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . '#msg' . $row['id_msg'],
2449
				'link' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . '#msg' . $row['id_msg'] . '">' . $row['subject'] . '</a>',
2450
				'time' => timeformat($row['poster_time']),
2451
			),
2452
		);
2453
2454
		// Images.
2455
		if ($attachments[$row['id_attach']]['file']['is_image'])
2456
		{
2457
			$id_thumb = empty($row['id_thumb']) ? $row['id_attach'] : $row['id_thumb'];
2458
			$attachments[$row['id_attach']]['file']['image'] = array(
2459
				'id' => $id_thumb,
2460
				'width' => $row['width'],
2461
				'height' => $row['height'],
2462
				'img' => '<img src="' . $scripturl . '?action=dlattach;topic=' . $row['id_topic'] . '.0;attach=' . $row['id_attach'] . ';image" alt="' . $filename . '">',
2463
				'thumb' => '<img src="' . $scripturl . '?action=dlattach;topic=' . $row['id_topic'] . '.0;attach=' . $id_thumb . ';image" alt="' . $filename . '">',
2464
				'href' => $scripturl . '?action=dlattach;topic=' . $row['id_topic'] . '.0;attach=' . $id_thumb . ';image',
2465
				'link' => '<a href="' . $scripturl . '?action=dlattach;topic=' . $row['id_topic'] . '.0;attach=' . $row['id_attach'] . ';image"><img src="' . $scripturl . '?action=dlattach;topic=' . $row['id_topic'] . '.0;attach=' . $id_thumb . ';image" alt="' . $filename . '"></a>',
2466
			);
2467
		}
2468
	}
2469
	$smcFunc['db_free_result']($request);
2470
2471
	// If mods want to do somthing with this list of attachments, let them do that now.
2472
	call_integration_hook('integrate_ssi_recentAttachments', array(&$attachments));
2473
2474
	// So you just want an array?  Here you can have it.
2475
	if ($output_method == 'array' || empty($attachments))
2476
		return $attachments;
2477
2478
	// Give them the default.
2479
	echo '
2480
		<table class="ssi_downloads">
2481
			<tr>
2482
				<th style="text-align: left; padding: 2">', $txt['file'], '</th>
2483
				<th style="text-align: left; padding: 2">', $txt['posted_by'], '</th>
2484
				<th style="text-align: left; padding: 2">', $txt['downloads'], '</th>
2485
				<th style="text-align: left; padding: 2">', $txt['filesize'], '</th>
2486
			</tr>';
2487
	foreach ($attachments as $attach)
2488
		echo '
2489
			<tr>
2490
				<td>', $attach['file']['link'], '</td>
2491
				<td>', $attach['member']['link'], '</td>
2492
				<td style="text-align: center">', $attach['file']['downloads'], '</td>
2493
				<td>', $attach['file']['filesize'], '</td>
2494
			</tr>';
2495
	echo '
2496
		</table>';
2497
}
2498
2499
?>