comparison forum/SSI.php @ 76:e3e11437ecea website

Add forum code
author Chris Cannam
date Sun, 07 Jul 2013 11:25:48 +0200
parents
children
comparison
equal deleted inserted replaced
75:72f59aa7e503 76:e3e11437ecea
1 <?php
2
3 /**
4 * Simple Machines Forum (SMF)
5 *
6 * @package SMF
7 * @author Simple Machines http://www.simplemachines.org
8 * @copyright 2011 Simple Machines
9 * @license http://www.simplemachines.org/about/smf/license.php BSD
10 *
11 * @version 2.0.4
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
20 // We're going to want a few globals... these are all set later.
21 global $time_start, $maintenance, $msubject, $mmessage, $mbname, $language;
22 global $boardurl, $boarddir, $sourcedir, $webmaster_email, $cookiename;
23 global $db_server, $db_name, $db_user, $db_prefix, $db_persist, $db_error_send, $db_last_error;
24 global $db_connection, $modSettings, $context, $sc, $user_info, $topic, $board, $txt;
25 global $smcFunc, $ssi_db_user, $scripturl, $ssi_db_passwd, $db_passwd, $cachedir;
26
27 // Remember the current configuration so it can be set back.
28 $ssi_magic_quotes_runtime = function_exists('get_magic_quotes_gpc') && get_magic_quotes_runtime();
29 if (function_exists('set_magic_quotes_runtime'))
30 @set_magic_quotes_runtime(0);
31 $time_start = microtime();
32
33 // Just being safe...
34 foreach (array('db_character_set', 'cachedir') as $variable)
35 if (isset($GLOBALS[$variable]))
36 unset($GLOBALS[$variable]);
37
38 // Get the forum's settings for database and file paths.
39 require_once(dirname(__FILE__) . '/Settings.php');
40
41 // Make absolutely sure the cache directory is defined.
42 if ((empty($cachedir) || !file_exists($cachedir)) && file_exists($boarddir . '/cache'))
43 $cachedir = $boarddir . '/cache';
44
45 $ssi_error_reporting = error_reporting(defined('E_STRICT') ? E_ALL | E_STRICT : E_ALL);
46 /* Set this to one of three values depending on what you want to happen in the case of a fatal error.
47 false: Default, will just load the error sub template and die - not putting any theme layers around it.
48 true: Will load the error sub template AND put the SMF layers around it (Not useful if on total custom pages).
49 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.
50 */
51 $ssi_on_error_method = false;
52
53 // Don't do john didley if the forum's been shut down competely.
54 if ($maintenance == 2 && (!isset($ssi_maintenance_off) || $ssi_maintenance_off !== true))
55 die($mmessage);
56
57 // Fix for using the current directory as a path.
58 if (substr($sourcedir, 0, 1) == '.' && substr($sourcedir, 1, 1) != '.')
59 $sourcedir = dirname(__FILE__) . substr($sourcedir, 1);
60
61 // Load the important includes.
62 require_once($sourcedir . '/QueryString.php');
63 require_once($sourcedir . '/Subs.php');
64 require_once($sourcedir . '/Errors.php');
65 require_once($sourcedir . '/Load.php');
66 require_once($sourcedir . '/Security.php');
67
68 // Using an pre-PHP 5.1 version?
69 if (@version_compare(PHP_VERSION, '5.1') == -1)
70 require_once($sourcedir . '/Subs-Compat.php');
71
72 // Create a variable to store some SMF specific functions in.
73 $smcFunc = array();
74
75 // Initate the database connection and define some database functions to use.
76 loadDatabase();
77
78 // Load installed 'Mods' settings.
79 reloadSettings();
80 // Clean the request variables.
81 cleanRequest();
82
83 // Seed the random generator?
84 if (empty($modSettings['rand_seed']) || mt_rand(1, 250) == 69)
85 smf_seed_generator();
86
87 // Check on any hacking attempts.
88 if (isset($_REQUEST['GLOBALS']) || isset($_COOKIE['GLOBALS']))
89 die('Hacking attempt...');
90 elseif (isset($_REQUEST['ssi_theme']) && (int) $_REQUEST['ssi_theme'] == (int) $ssi_theme)
91 die('Hacking attempt...');
92 elseif (isset($_COOKIE['ssi_theme']) && (int) $_COOKIE['ssi_theme'] == (int) $ssi_theme)
93 die('Hacking attempt...');
94 elseif (isset($_REQUEST['ssi_layers'], $ssi_layers) && (@get_magic_quotes_gpc() ? stripslashes($_REQUEST['ssi_layers']) : $_REQUEST['ssi_layers']) == $ssi_layers)
95 die('Hacking attempt...');
96 if (isset($_REQUEST['context']))
97 die('Hacking attempt...');
98
99 // Make sure wireless is always off.
100 define('WIRELESS', false);
101
102 // Gzip output? (because it must be boolean and true, this can't be hacked.)
103 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') != -1)
104 ob_start('ob_gzhandler');
105 else
106 $modSettings['enableCompressedOutput'] = '0';
107
108 // Primarily, this is to fix the URLs...
109 ob_start('ob_sessrewrite');
110
111 // Start the session... known to scramble SSI includes in cases...
112 if (!headers_sent())
113 loadSession();
114 else
115 {
116 if (isset($_COOKIE[session_name()]) || isset($_REQUEST[session_name()]))
117 {
118 // Make a stab at it, but ignore the E_WARNINGs generated because we can't send headers.
119 $temp = error_reporting(error_reporting() & !E_WARNING);
120 loadSession();
121 error_reporting($temp);
122 }
123
124 if (!isset($_SESSION['session_value']))
125 {
126 $_SESSION['session_var'] = substr(md5(mt_rand() . session_id() . mt_rand()), 0, rand(7, 12));
127 $_SESSION['session_value'] = md5(session_id() . mt_rand());
128 }
129 $sc = $_SESSION['session_value'];
130 }
131
132 // Get rid of $board and $topic... do stuff loadBoard would do.
133 unset($board, $topic);
134 $user_info['is_mod'] = false;
135 $context['user']['is_mod'] = &$user_info['is_mod'];
136 $context['linktree'] = array();
137
138 // Load the user and their cookie, as well as their settings.
139 loadUserSettings();
140
141 // Load the current user's permissions....
142 loadPermissions();
143
144 // Load the current or SSI theme. (just use $ssi_theme = id_theme;)
145 loadTheme(isset($ssi_theme) ? (int) $ssi_theme : 0);
146
147 // Take care of any banning that needs to be done.
148 if (isset($_REQUEST['ssi_ban']) || (isset($ssi_ban) && $ssi_ban === true))
149 is_not_banned();
150
151 // Do we allow guests in here?
152 if (empty($ssi_guest_access) && empty($modSettings['allow_guestAccess']) && $user_info['is_guest'] && basename($_SERVER['PHP_SELF']) != 'SSI.php')
153 {
154 require_once($sourcedir . '/Subs-Auth.php');
155 KickGuest();
156 obExit(null, true);
157 }
158
159 // Load the stuff like the menu bar, etc.
160 if (isset($ssi_layers))
161 {
162 $context['template_layers'] = $ssi_layers;
163 template_header();
164 }
165 else
166 setupThemeContext();
167
168 // Make sure they didn't muss around with the settings... but only if it's not cli.
169 if (isset($_SERVER['REMOTE_ADDR']) && !isset($_SERVER['is_cli']) && session_id() == '')
170 trigger_error($txt['ssi_session_broken'], E_USER_NOTICE);
171
172 // Without visiting the forum this session variable might not be set on submit.
173 if (!isset($_SESSION['USER_AGENT']) && (!isset($_GET['ssi_function']) || $_GET['ssi_function'] !== 'pollVote'))
174 $_SESSION['USER_AGENT'] = $_SERVER['HTTP_USER_AGENT'];
175
176 // Call a function passed by GET.
177 if (isset($_GET['ssi_function']) && function_exists('ssi_' . $_GET['ssi_function']) && (!empty($modSettings['allow_guestAccess']) || !$user_info['is_guest']))
178 {
179 call_user_func('ssi_' . $_GET['ssi_function']);
180 exit;
181 }
182 if (isset($_GET['ssi_function']))
183 exit;
184 // You shouldn't just access SSI.php directly by URL!!
185 elseif (basename($_SERVER['PHP_SELF']) == 'SSI.php')
186 die(sprintf($txt['ssi_not_direct'], $user_info['is_admin'] ? '\'' . addslashes(__FILE__) . '\'' : '\'SSI.php\''));
187
188 error_reporting($ssi_error_reporting);
189 if (function_exists('set_magic_quotes_runtime'))
190 @set_magic_quotes_runtime($ssi_magic_quotes_runtime);
191
192 return true;
193
194 // This shuts down the SSI and shows the footer.
195 function ssi_shutdown()
196 {
197 if (!isset($_GET['ssi_function']) || $_GET['ssi_function'] != 'shutdown')
198 template_footer();
199 }
200
201 // Display a welcome message, like: Hey, User, you have 0 messages, 0 are new.
202 function ssi_welcome($output_method = 'echo')
203 {
204 global $context, $txt, $scripturl;
205
206 if ($output_method == 'echo')
207 {
208 if ($context['user']['is_guest'])
209 echo sprintf($txt['welcome_guest'], $txt['guest_title']);
210 else
211 echo $txt['hello_member'], ' <strong>', $context['user']['name'], '</strong>', allowedTo('pm_read') ? ', ' . $txt['msg_alert_you_have'] . ' <a href="' . $scripturl . '?action=pm">' . $context['user']['messages'] . ' ' . ($context['user']['messages'] == '1' ? $txt['message_lowercase'] : $txt['msg_alert_messages']) . '</a>' . $txt['newmessages4'] . ' ' . $context['user']['unread_messages'] . ' ' . ($context['user']['unread_messages'] == '1' ? $txt['newmessages0'] : $txt['newmessages1']) : '', '.';
212 }
213 // Don't echo... then do what?!
214 else
215 return $context['user'];
216 }
217
218 // Display a menu bar, like is displayed at the top of the forum.
219 function ssi_menubar($output_method = 'echo')
220 {
221 global $context;
222
223 if ($output_method == 'echo')
224 template_menu();
225 // What else could this do?
226 else
227 return $context['menu_buttons'];
228 }
229
230 // Show a logout link.
231 function ssi_logout($redirect_to = '', $output_method = 'echo')
232 {
233 global $context, $txt, $scripturl;
234
235 if ($redirect_to != '')
236 $_SESSION['logout_url'] = $redirect_to;
237
238 // Guests can't log out.
239 if ($context['user']['is_guest'])
240 return false;
241
242 $link = '<a href="' . $scripturl . '?action=logout;' . $context['session_var'] . '=' . $context['session_id'] . '">' . $txt['logout'] . '</a>';
243
244 if ($output_method == 'echo')
245 echo $link;
246 else
247 return $link;
248 }
249
250 // Recent post list: [board] Subject by Poster Date
251 function ssi_recentPosts($num_recent = 8, $exclude_boards = null, $include_boards = null, $output_method = 'echo', $limit_body = true)
252 {
253 global $context, $settings, $scripturl, $txt, $db_prefix, $user_info;
254 global $modSettings, $smcFunc;
255
256 // Excluding certain boards...
257 if ($exclude_boards === null && !empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] > 0)
258 $exclude_boards = array($modSettings['recycle_board']);
259 else
260 $exclude_boards = empty($exclude_boards) ? array() : (is_array($exclude_boards) ? $exclude_boards : array($exclude_boards));
261
262 // What about including certain boards - note we do some protection here as pre-2.0 didn't have this parameter.
263 if (is_array($include_boards) || (int) $include_boards === $include_boards)
264 {
265 $include_boards = is_array($include_boards) ? $include_boards : array($include_boards);
266 }
267 elseif ($include_boards != null)
268 {
269 $include_boards = array();
270 }
271
272 // Let's restrict the query boys (and girls)
273 $query_where = '
274 m.id_msg >= {int:min_message_id}
275 ' . (empty($exclude_boards) ? '' : '
276 AND b.id_board NOT IN ({array_int:exclude_boards})') . '
277 ' . ($include_boards === null ? '' : '
278 AND b.id_board IN ({array_int:include_boards})') . '
279 AND {query_wanna_see_board}' . ($modSettings['postmod_active'] ? '
280 AND m.approved = {int:is_approved}' : '');
281
282 $query_where_params = array(
283 'is_approved' => 1,
284 'include_boards' => $include_boards === null ? '' : $include_boards,
285 'exclude_boards' => empty($exclude_boards) ? '' : $exclude_boards,
286 'min_message_id' => $modSettings['maxMsgID'] - 25 * min($num_recent, 5),
287 );
288
289 // Past to this simpleton of a function...
290 return ssi_queryPosts($query_where, $query_where_params, $num_recent, 'm.id_msg DESC', $output_method, $limit_body);
291 }
292
293 // Fetch a post with a particular ID. By default will only show if you have permission to the see the board in question - this can be overriden.
294 function ssi_fetchPosts($post_ids = array(), $override_permissions = false, $output_method = 'echo')
295 {
296 global $user_info, $modSettings;
297
298 if (empty($post_ids))
299 return;
300
301 // Allow the user to request more than one - why not?
302 $post_ids = is_array($post_ids) ? $post_ids : array($post_ids);
303
304 // Restrict the posts required...
305 $query_where = '
306 m.id_msg IN ({array_int:message_list})' . ($override_permissions ? '' : '
307 AND {query_wanna_see_board}') . ($modSettings['postmod_active'] ? '
308 AND m.approved = {int:is_approved}' : '');
309 $query_where_params = array(
310 'message_list' => $post_ids,
311 'is_approved' => 1,
312 );
313
314 // Then make the query and dump the data.
315 return ssi_queryPosts($query_where, $query_where_params, '', 'm.id_msg DESC', $output_method);
316 }
317
318 // This removes code duplication in other queries - don't call it direct unless you really know what you're up to.
319 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)
320 {
321 global $context, $settings, $scripturl, $txt, $db_prefix, $user_info;
322 global $modSettings, $smcFunc;
323
324 // Find all the posts. Newer ones will have higher IDs.
325 $request = $smcFunc['db_query']('substring', '
326 SELECT
327 m.poster_time, m.subject, m.id_topic, m.id_member, m.id_msg, m.id_board, b.name AS board_name,
328 IFNULL(mem.real_name, m.poster_name) AS poster_name, ' . ($user_info['is_guest'] ? '1 AS is_read, 0 AS new_from' : '
329 IFNULL(lt.id_msg, IFNULL(lmr.id_msg, 0)) >= m.id_msg_modified AS is_read,
330 IFNULL(lt.id_msg, IFNULL(lmr.id_msg, -1)) + 1 AS new_from') . ', ' . ($limit_body ? 'SUBSTRING(m.body, 1, 384) AS body' : 'm.body') . ', m.smileys_enabled
331 FROM {db_prefix}messages AS m
332 INNER JOIN {db_prefix}boards AS b ON (b.id_board = m.id_board)
333 LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member)' . (!$user_info['is_guest'] ? '
334 LEFT JOIN {db_prefix}log_topics AS lt ON (lt.id_topic = m.id_topic AND lt.id_member = {int:current_member})
335 LEFT JOIN {db_prefix}log_mark_read AS lmr ON (lmr.id_board = m.id_board AND lmr.id_member = {int:current_member})' : '') . '
336 WHERE 1=1 ' . ($override_permissions ? '' : '
337 AND {query_wanna_see_board}') . ($modSettings['postmod_active'] ? '
338 AND m.approved = {int:is_approved}' : '') . '
339 ' . (empty($query_where) ? '' : 'AND ' . $query_where) . '
340 ORDER BY ' . $query_order . '
341 ' . ($query_limit == '' ? '' : 'LIMIT ' . $query_limit),
342 array_merge($query_where_params, array(
343 'current_member' => $user_info['id'],
344 'is_approved' => 1,
345 ))
346 );
347 $posts = array();
348 while ($row = $smcFunc['db_fetch_assoc']($request))
349 {
350 $row['body'] = parse_bbc($row['body'], $row['smileys_enabled'], $row['id_msg']);
351
352 // Censor it!
353 censorText($row['subject']);
354 censorText($row['body']);
355
356 $preview = strip_tags(strtr($row['body'], array('<br />' => '&#10;')));
357
358 // Build the array.
359 $posts[] = array(
360 'id' => $row['id_msg'],
361 'board' => array(
362 'id' => $row['id_board'],
363 'name' => $row['board_name'],
364 'href' => $scripturl . '?board=' . $row['id_board'] . '.0',
365 'link' => '<a href="' . $scripturl . '?board=' . $row['id_board'] . '.0">' . $row['board_name'] . '</a>'
366 ),
367 'topic' => $row['id_topic'],
368 'poster' => array(
369 'id' => $row['id_member'],
370 'name' => $row['poster_name'],
371 'href' => empty($row['id_member']) ? '' : $scripturl . '?action=profile;u=' . $row['id_member'],
372 'link' => empty($row['id_member']) ? $row['poster_name'] : '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['poster_name'] . '</a>'
373 ),
374 'subject' => $row['subject'],
375 'short_subject' => shorten_subject($row['subject'], 25),
376 'preview' => $smcFunc['strlen']($preview) > 128 ? $smcFunc['substr']($preview, 0, 128) . '...' : $preview,
377 'body' => $row['body'],
378 'time' => timeformat($row['poster_time']),
379 'timestamp' => forum_time(true, $row['poster_time']),
380 'href' => $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . ';topicseen#new',
381 'link' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . '#msg' . $row['id_msg'] . '" rel="nofollow">' . $row['subject'] . '</a>',
382 'new' => !empty($row['is_read']),
383 'is_new' => empty($row['is_read']),
384 'new_from' => $row['new_from'],
385 );
386 }
387 $smcFunc['db_free_result']($request);
388
389 // Just return it.
390 if ($output_method != 'echo' || empty($posts))
391 return $posts;
392
393 echo '
394 <table border="0" class="ssi_table">';
395 foreach ($posts as $post)
396 echo '
397 <tr>
398 <td align="right" valign="top" nowrap="nowrap">
399 [', $post['board']['link'], ']
400 </td>
401 <td valign="top">
402 <a href="', $post['href'], '">', $post['subject'], '</a>
403 ', $txt['by'], ' ', $post['poster']['link'], '
404 ', $post['is_new'] ? '<a href="' . $scripturl . '?topic=' . $post['topic'] . '.msg' . $post['new_from'] . ';topicseen#new" rel="nofollow"><img src="' . $settings['lang_images_url'] . '/new.gif" alt="' . $txt['new'] . '" /></a>' : '', '
405 </td>
406 <td align="right" nowrap="nowrap">
407 ', $post['time'], '
408 </td>
409 </tr>';
410 echo '
411 </table>';
412 }
413
414 // Recent topic list: [board] Subject by Poster Date
415 function ssi_recentTopics($num_recent = 8, $exclude_boards = null, $include_boards = null, $output_method = 'echo')
416 {
417 global $context, $settings, $scripturl, $txt, $db_prefix, $user_info;
418 global $modSettings, $smcFunc;
419
420 if ($exclude_boards === null && !empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] > 0)
421 $exclude_boards = array($modSettings['recycle_board']);
422 else
423 $exclude_boards = empty($exclude_boards) ? array() : (is_array($exclude_boards) ? $exclude_boards : array($exclude_boards));
424
425 // Only some boards?.
426 if (is_array($include_boards) || (int) $include_boards === $include_boards)
427 {
428 $include_boards = is_array($include_boards) ? $include_boards : array($include_boards);
429 }
430 elseif ($include_boards != null)
431 {
432 $output_method = $include_boards;
433 $include_boards = array();
434 }
435
436 $stable_icons = array('xx', 'thumbup', 'thumbdown', 'exclamation', 'question', 'lamp', 'smiley', 'angry', 'cheesy', 'grin', 'sad', 'wink', 'moved', 'recycled', 'wireless');
437 $icon_sources = array();
438 foreach ($stable_icons as $icon)
439 $icon_sources[$icon] = 'images_url';
440
441 // Find all the posts in distinct topics. Newer ones will have higher IDs.
442 $request = $smcFunc['db_query']('substring', '
443 SELECT
444 m.poster_time, ms.subject, m.id_topic, m.id_member, m.id_msg, b.id_board, b.name AS board_name, t.num_replies, t.num_views,
445 IFNULL(mem.real_name, m.poster_name) AS poster_name, ' . ($user_info['is_guest'] ? '1 AS is_read, 0 AS new_from' : '
446 IFNULL(lt.id_msg, IFNULL(lmr.id_msg, 0)) >= m.id_msg_modified AS is_read,
447 IFNULL(lt.id_msg, IFNULL(lmr.id_msg, -1)) + 1 AS new_from') . ', SUBSTRING(m.body, 1, 384) AS body, m.smileys_enabled, m.icon
448 FROM {db_prefix}topics AS t
449 INNER JOIN {db_prefix}messages AS m ON (m.id_msg = t.id_last_msg)
450 INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board)
451 INNER JOIN {db_prefix}messages AS ms ON (ms.id_msg = t.id_first_msg)
452 LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member)' . (!$user_info['is_guest'] ? '
453 LEFT JOIN {db_prefix}log_topics AS lt ON (lt.id_topic = t.id_topic AND lt.id_member = {int:current_member})
454 LEFT JOIN {db_prefix}log_mark_read AS lmr ON (lmr.id_board = b.id_board AND lmr.id_member = {int:current_member})' : '') . '
455 WHERE t.id_last_msg >= {int:min_message_id}
456 ' . (empty($exclude_boards) ? '' : '
457 AND b.id_board NOT IN ({array_int:exclude_boards})') . '
458 ' . (empty($include_boards) ? '' : '
459 AND b.id_board IN ({array_int:include_boards})') . '
460 AND {query_wanna_see_board}' . ($modSettings['postmod_active'] ? '
461 AND t.approved = {int:is_approved}
462 AND m.approved = {int:is_approved}' : '') . '
463 ORDER BY t.id_last_msg DESC
464 LIMIT ' . $num_recent,
465 array(
466 'current_member' => $user_info['id'],
467 'include_boards' => empty($include_boards) ? '' : $include_boards,
468 'exclude_boards' => empty($exclude_boards) ? '' : $exclude_boards,
469 'min_message_id' => $modSettings['maxMsgID'] - 35 * min($num_recent, 5),
470 'is_approved' => 1,
471 )
472 );
473 $posts = array();
474 while ($row = $smcFunc['db_fetch_assoc']($request))
475 {
476 $row['body'] = strip_tags(strtr(parse_bbc($row['body'], $row['smileys_enabled'], $row['id_msg']), array('<br />' => '&#10;')));
477 if ($smcFunc['strlen']($row['body']) > 128)
478 $row['body'] = $smcFunc['substr']($row['body'], 0, 128) . '...';
479
480 // Censor the subject.
481 censorText($row['subject']);
482 censorText($row['body']);
483
484 if (empty($modSettings['messageIconChecks_disable']) && !isset($icon_sources[$row['icon']]))
485 $icon_sources[$row['icon']] = file_exists($settings['theme_dir'] . '/images/post/' . $row['icon'] . '.gif') ? 'images_url' : 'default_images_url';
486
487 // Build the array.
488 $posts[] = array(
489 'board' => array(
490 'id' => $row['id_board'],
491 'name' => $row['board_name'],
492 'href' => $scripturl . '?board=' . $row['id_board'] . '.0',
493 'link' => '<a href="' . $scripturl . '?board=' . $row['id_board'] . '.0">' . $row['board_name'] . '</a>'
494 ),
495 'topic' => $row['id_topic'],
496 'poster' => array(
497 'id' => $row['id_member'],
498 'name' => $row['poster_name'],
499 'href' => empty($row['id_member']) ? '' : $scripturl . '?action=profile;u=' . $row['id_member'],
500 'link' => empty($row['id_member']) ? $row['poster_name'] : '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['poster_name'] . '</a>'
501 ),
502 'subject' => $row['subject'],
503 'replies' => $row['num_replies'],
504 'views' => $row['num_views'],
505 'short_subject' => shorten_subject($row['subject'], 25),
506 'preview' => $row['body'],
507 'time' => timeformat($row['poster_time']),
508 'timestamp' => forum_time(true, $row['poster_time']),
509 'href' => $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . ';topicseen#new',
510 'link' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . '#new" rel="nofollow">' . $row['subject'] . '</a>',
511 // Retained for compatibility - is technically incorrect!
512 'new' => !empty($row['is_read']),
513 'is_new' => empty($row['is_read']),
514 'new_from' => $row['new_from'],
515 'icon' => '<img src="' . $settings[$icon_sources[$row['icon']]] . '/post/' . $row['icon'] . '.gif" align="middle" alt="' . $row['icon'] . '" />',
516 );
517 }
518 $smcFunc['db_free_result']($request);
519
520 // Just return it.
521 if ($output_method != 'echo' || empty($posts))
522 return $posts;
523
524 echo '
525 <table border="0" class="ssi_table">';
526 foreach ($posts as $post)
527 echo '
528 <tr>
529 <td align="right" valign="top" nowrap="nowrap">
530 [', $post['board']['link'], ']
531 </td>
532 <td valign="top">
533 <a href="', $post['href'], '">', $post['subject'], '</a>
534 ', $txt['by'], ' ', $post['poster']['link'], '
535 ', !$post['is_new'] ? '' : '<a href="' . $scripturl . '?topic=' . $post['topic'] . '.msg' . $post['new_from'] . ';topicseen#new" rel="nofollow"><img src="' . $settings['lang_images_url'] . '/new.gif" alt="' . $txt['new'] . '" /></a>', '
536 </td>
537 <td align="right" nowrap="nowrap">
538 ', $post['time'], '
539 </td>
540 </tr>';
541 echo '
542 </table>';
543 }
544
545 // Show the top poster's name and profile link.
546 function ssi_topPoster($topNumber = 1, $output_method = 'echo')
547 {
548 global $db_prefix, $scripturl, $smcFunc;
549
550 // Find the latest poster.
551 $request = $smcFunc['db_query']('', '
552 SELECT id_member, real_name, posts
553 FROM {db_prefix}members
554 ORDER BY posts DESC
555 LIMIT ' . $topNumber,
556 array(
557 )
558 );
559 $return = array();
560 while ($row = $smcFunc['db_fetch_assoc']($request))
561 $return[] = array(
562 'id' => $row['id_member'],
563 'name' => $row['real_name'],
564 'href' => $scripturl . '?action=profile;u=' . $row['id_member'],
565 'link' => '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['real_name'] . '</a>',
566 'posts' => $row['posts']
567 );
568 $smcFunc['db_free_result']($request);
569
570 // Just return all the top posters.
571 if ($output_method != 'echo')
572 return $return;
573
574 // Make a quick array to list the links in.
575 $temp_array = array();
576 foreach ($return as $member)
577 $temp_array[] = $member['link'];
578
579 echo implode(', ', $temp_array);
580 }
581
582 // Show boards by activity.
583 function ssi_topBoards($num_top = 10, $output_method = 'echo')
584 {
585 global $context, $settings, $db_prefix, $txt, $scripturl, $user_info, $modSettings, $smcFunc;
586
587 // Find boards with lots of posts.
588 $request = $smcFunc['db_query']('', '
589 SELECT
590 b.name, b.num_topics, b.num_posts, b.id_board,' . (!$user_info['is_guest'] ? ' 1 AS is_read' : '
591 (IFNULL(lb.id_msg, 0) >= b.id_last_msg) AS is_read') . '
592 FROM {db_prefix}boards AS b
593 LEFT JOIN {db_prefix}log_boards AS lb ON (lb.id_board = b.id_board AND lb.id_member = {int:current_member})
594 WHERE {query_wanna_see_board}' . (!empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] > 0 ? '
595 AND b.id_board != {int:recycle_board}' : '') . '
596 ORDER BY b.num_posts DESC
597 LIMIT ' . $num_top,
598 array(
599 'current_member' => $user_info['id'],
600 'recycle_board' => (int) $modSettings['recycle_board'],
601 )
602 );
603 $boards = array();
604 while ($row = $smcFunc['db_fetch_assoc']($request))
605 $boards[] = array(
606 'id' => $row['id_board'],
607 'num_posts' => $row['num_posts'],
608 'num_topics' => $row['num_topics'],
609 'name' => $row['name'],
610 'new' => empty($row['is_read']),
611 'href' => $scripturl . '?board=' . $row['id_board'] . '.0',
612 'link' => '<a href="' . $scripturl . '?board=' . $row['id_board'] . '.0">' . $row['name'] . '</a>'
613 );
614 $smcFunc['db_free_result']($request);
615
616 // If we shouldn't output or have nothing to output, just jump out.
617 if ($output_method != 'echo' || empty($boards))
618 return $boards;
619
620 echo '
621 <table class="ssi_table">
622 <tr>
623 <th align="left">', $txt['board'], '</th>
624 <th align="left">', $txt['board_topics'], '</th>
625 <th align="left">', $txt['posts'], '</th>
626 </tr>';
627 foreach ($boards as $board)
628 echo '
629 <tr>
630 <td>', $board['link'], $board['new'] ? ' <a href="' . $board['href'] . '"><img src="' . $settings['lang_images_url'] . '/new.gif" alt="' . $txt['new'] . '" /></a>' : '', '</td>
631 <td align="right">', comma_format($board['num_topics']), '</td>
632 <td align="right">', comma_format($board['num_posts']), '</td>
633 </tr>';
634 echo '
635 </table>';
636 }
637
638 // Shows the top topics.
639 function ssi_topTopics($type = 'replies', $num_topics = 10, $output_method = 'echo')
640 {
641 global $db_prefix, $txt, $scripturl, $user_info, $modSettings, $smcFunc, $context;
642
643 if ($modSettings['totalMessages'] > 100000)
644 {
645 // !!! Why don't we use {query(_wanna)_see_board}?
646 $request = $smcFunc['db_query']('', '
647 SELECT id_topic
648 FROM {db_prefix}topics
649 WHERE num_' . ($type != 'replies' ? 'views' : 'replies') . ' != 0' . ($modSettings['postmod_active'] ? '
650 AND approved = {int:is_approved}' : '') . '
651 ORDER BY num_' . ($type != 'replies' ? 'views' : 'replies') . ' DESC
652 LIMIT {int:limit}',
653 array(
654 'is_approved' => 1,
655 'limit' => $num_topics > 100 ? ($num_topics + ($num_topics / 2)) : 100,
656 )
657 );
658 $topic_ids = array();
659 while ($row = $smcFunc['db_fetch_assoc']($request))
660 $topic_ids[] = $row['id_topic'];
661 $smcFunc['db_free_result']($request);
662 }
663 else
664 $topic_ids = array();
665
666 $request = $smcFunc['db_query']('', '
667 SELECT m.subject, m.id_topic, t.num_views, t.num_replies
668 FROM {db_prefix}topics AS t
669 INNER JOIN {db_prefix}messages AS m ON (m.id_msg = t.id_first_msg)
670 INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board)
671 WHERE {query_wanna_see_board}' . ($modSettings['postmod_active'] ? '
672 AND t.approved = {int:is_approved}' : '') . (!empty($topic_ids) ? '
673 AND t.id_topic IN ({array_int:topic_list})' : '') . (!empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] > 0 ? '
674 AND b.id_board != {int:recycle_enable}' : '') . '
675 ORDER BY t.num_' . ($type != 'replies' ? 'views' : 'replies') . ' DESC
676 LIMIT {int:limit}',
677 array(
678 'topic_list' => $topic_ids,
679 'is_approved' => 1,
680 'recycle_enable' => $modSettings['recycle_board'],
681 'limit' => $num_topics,
682 )
683 );
684 $topics = array();
685 while ($row = $smcFunc['db_fetch_assoc']($request))
686 {
687 censorText($row['subject']);
688
689 $topics[] = array(
690 'id' => $row['id_topic'],
691 'subject' => $row['subject'],
692 'num_replies' => $row['num_replies'],
693 'num_views' => $row['num_views'],
694 'href' => $scripturl . '?topic=' . $row['id_topic'] . '.0',
695 'link' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.0">' . $row['subject'] . '</a>',
696 );
697 }
698 $smcFunc['db_free_result']($request);
699
700 if ($output_method != 'echo' || empty($topics))
701 return $topics;
702
703 echo '
704 <table class="ssi_table">
705 <tr>
706 <th align="left"></th>
707 <th align="left">', $txt['views'], '</th>
708 <th align="left">', $txt['replies'], '</th>
709 </tr>';
710 foreach ($topics as $topic)
711 echo '
712 <tr>
713 <td align="left">
714 ', $topic['link'], '
715 </td>
716 <td align="right">', comma_format($topic['num_views']), '</td>
717 <td align="right">', comma_format($topic['num_replies']), '</td>
718 </tr>';
719 echo '
720 </table>';
721 }
722
723 // Shows the top topics, by replies.
724 function ssi_topTopicsReplies($num_topics = 10, $output_method = 'echo')
725 {
726 return ssi_topTopics('replies', $num_topics, $output_method);
727 }
728
729 // Shows the top topics, by views.
730 function ssi_topTopicsViews($num_topics = 10, $output_method = 'echo')
731 {
732 return ssi_topTopics('views', $num_topics, $output_method);
733 }
734
735 // Show a link to the latest member: Please welcome, Someone, out latest member.
736 function ssi_latestMember($output_method = 'echo')
737 {
738 global $db_prefix, $txt, $scripturl, $context;
739
740 if ($output_method == 'echo')
741 echo '
742 ', $txt['welcome_member'], ' ', $context['common_stats']['latest_member']['link'], '', $txt['newest_member'], '<br />';
743 else
744 return $context['common_stats']['latest_member'];
745 }
746
747 // Fetch a random member - if type set to 'day' will only change once a day!
748 function ssi_randomMember($random_type = '', $output_method = 'echo')
749 {
750 global $modSettings;
751
752 // If we're looking for something to stay the same each day then seed the generator.
753 if ($random_type == 'day')
754 {
755 // Set the seed to change only once per day.
756 mt_srand(floor(time() / 86400));
757 }
758
759 // Get the lowest ID we're interested in.
760 $member_id = mt_rand(1, $modSettings['latestMember']);
761
762 $where_query = '
763 id_member >= {int:selected_member}
764 AND is_activated = {int:is_activated}';
765
766 $query_where_params = array(
767 'selected_member' => $member_id,
768 'is_activated' => 1,
769 );
770
771 $result = ssi_queryMembers($where_query, $query_where_params, 1, 'id_member ASC', $output_method);
772
773 // If we got nothing do the reverse - in case of unactivated members.
774 if (empty($result))
775 {
776 $where_query = '
777 id_member <= {int:selected_member}
778 AND is_activated = {int:is_activated}';
779
780 $query_where_params = array(
781 'selected_member' => $member_id,
782 'is_activated' => 1,
783 );
784
785 $result = ssi_queryMembers($where_query, $query_where_params, 1, 'id_member DESC', $output_method);
786 }
787
788 // Just to be sure put the random generator back to something... random.
789 if ($random_type != '')
790 mt_srand(time());
791
792 return $result;
793 }
794
795 // Fetch a specific member.
796 function ssi_fetchMember($member_ids = array(), $output_method = 'echo')
797 {
798 if (empty($member_ids))
799 return;
800
801 // Can have more than one member if you really want...
802 $member_ids = is_array($member_ids) ? $member_ids : array($member_ids);
803
804 // Restrict it right!
805 $query_where = '
806 id_member IN ({array_int:member_list})';
807
808 $query_where_params = array(
809 'member_list' => $member_ids,
810 );
811
812 // Then make the query and dump the data.
813 return ssi_queryMembers($query_where, $query_where_params, '', 'id_member', $output_method);
814 }
815
816 // Get all members of a group.
817 function ssi_fetchGroupMembers($group_id = null, $output_method = 'echo')
818 {
819 if ($group_id === null)
820 return;
821
822 $query_where = '
823 id_group = {int:id_group}
824 OR id_post_group = {int:id_group}
825 OR FIND_IN_SET({int:id_group}, additional_groups)';
826
827 $query_where_params = array(
828 'id_group' => $group_id,
829 );
830
831 return ssi_queryMembers($query_where, $query_where_params, '', 'real_name', $output_method);
832 }
833
834 // Fetch some member data!
835 function ssi_queryMembers($query_where = null, $query_where_params = array(), $query_limit = '', $query_order = 'id_member DESC', $output_method = 'echo')
836 {
837 global $context, $settings, $scripturl, $txt, $db_prefix, $user_info;
838 global $modSettings, $smcFunc, $memberContext;
839
840 if ($query_where === null)
841 return;
842
843 // Fetch the members in question.
844 $request = $smcFunc['db_query']('', '
845 SELECT id_member
846 FROM {db_prefix}members
847 WHERE ' . $query_where . '
848 ORDER BY ' . $query_order . '
849 ' . ($query_limit == '' ? '' : 'LIMIT ' . $query_limit),
850 array_merge($query_where_params, array(
851 ))
852 );
853 $members = array();
854 while ($row = $smcFunc['db_fetch_assoc']($request))
855 $members[] = $row['id_member'];
856 $smcFunc['db_free_result']($request);
857
858 if (empty($members))
859 return array();
860
861 // Load the members.
862 loadMemberData($members);
863
864 // Draw the table!
865 if ($output_method == 'echo')
866 echo '
867 <table border="0" class="ssi_table">';
868
869 $query_members = array();
870 foreach ($members as $member)
871 {
872 // Load their context data.
873 if (!loadMemberContext($member))
874 continue;
875
876 // Store this member's information.
877 $query_members[$member] = $memberContext[$member];
878
879 // Only do something if we're echo'ing.
880 if ($output_method == 'echo')
881 echo '
882 <tr>
883 <td align="right" valign="top" nowrap="nowrap">
884 ', $query_members[$member]['link'], '
885 <br />', $query_members[$member]['blurb'], '
886 <br />', $query_members[$member]['avatar']['image'], '
887 </td>
888 </tr>';
889 }
890
891 // End the table if appropriate.
892 if ($output_method == 'echo')
893 echo '
894 </table>';
895
896 // Send back the data.
897 return $query_members;
898 }
899
900 // Show some basic stats: Total This: XXXX, etc.
901 function ssi_boardStats($output_method = 'echo')
902 {
903 global $db_prefix, $txt, $scripturl, $modSettings, $smcFunc;
904
905 $totals = array(
906 'members' => $modSettings['totalMembers'],
907 'posts' => $modSettings['totalMessages'],
908 'topics' => $modSettings['totalTopics']
909 );
910
911 $result = $smcFunc['db_query']('', '
912 SELECT COUNT(*)
913 FROM {db_prefix}boards',
914 array(
915 )
916 );
917 list ($totals['boards']) = $smcFunc['db_fetch_row']($result);
918 $smcFunc['db_free_result']($result);
919
920 $result = $smcFunc['db_query']('', '
921 SELECT COUNT(*)
922 FROM {db_prefix}categories',
923 array(
924 )
925 );
926 list ($totals['categories']) = $smcFunc['db_fetch_row']($result);
927 $smcFunc['db_free_result']($result);
928
929 if ($output_method != 'echo')
930 return $totals;
931
932 echo '
933 ', $txt['total_members'], ': <a href="', $scripturl . '?action=mlist">', comma_format($totals['members']), '</a><br />
934 ', $txt['total_posts'], ': ', comma_format($totals['posts']), '<br />
935 ', $txt['total_topics'], ': ', comma_format($totals['topics']), ' <br />
936 ', $txt['total_cats'], ': ', comma_format($totals['categories']), '<br />
937 ', $txt['total_boards'], ': ', comma_format($totals['boards']);
938 }
939
940 // Shows a list of online users: YY Guests, ZZ Users and then a list...
941 function ssi_whosOnline($output_method = 'echo')
942 {
943 global $user_info, $txt, $sourcedir, $settings, $modSettings;
944
945 require_once($sourcedir . '/Subs-MembersOnline.php');
946 $membersOnlineOptions = array(
947 'show_hidden' => allowedTo('moderate_forum'),
948 'sort' => 'log_time',
949 'reverse_sort' => true,
950 );
951 $return = getMembersOnlineStats($membersOnlineOptions);
952
953 // Add some redundancy for backwards compatibility reasons.
954 if ($output_method != 'echo')
955 return $return + array(
956 'users' => $return['users_online'],
957 'guests' => $return['num_guests'],
958 'hidden' => $return['num_users_hidden'],
959 'buddies' => $return['num_buddies'],
960 'num_users' => $return['num_users_online'],
961 'total_users' => $return['num_users_online'] + $return['num_guests'] + $return['num_spiders'],
962 );
963
964 echo '
965 ', 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'];
966
967 $bracketList = array();
968 if (!empty($user_info['buddies']))
969 $bracketList[] = comma_format($return['num_buddies']) . ' ' . ($return['num_buddies'] == 1 ? $txt['buddy'] : $txt['buddies']);
970 if (!empty($return['num_spiders']))
971 $bracketList[] = comma_format($return['num_spiders']) . ' ' . ($return['num_spiders'] == 1 ? $txt['spider'] : $txt['spiders']);
972 if (!empty($return['num_users_hidden']))
973 $bracketList[] = comma_format($return['num_users_hidden']) . ' ' . $txt['hidden'];
974
975 if (!empty($bracketList))
976 echo ' (' . implode(', ', $bracketList) . ')';
977
978 echo '<br />
979 ', implode(', ', $return['list_users_online']);
980
981 // Showing membergroups?
982 if (!empty($settings['show_group_key']) && !empty($return['membergroups']))
983 echo '<br />
984 [' . implode(']&nbsp;&nbsp;[', $return['membergroups']) . ']';
985 }
986
987 // Just like whosOnline except it also logs the online presence.
988 function ssi_logOnline($output_method = 'echo')
989 {
990 writeLog();
991
992 if ($output_method != 'echo')
993 return ssi_whosOnline($output_method);
994 else
995 ssi_whosOnline($output_method);
996 }
997
998 // Shows a login box.
999 function ssi_login($redirect_to = '', $output_method = 'echo')
1000 {
1001 global $scripturl, $txt, $user_info, $context, $modSettings;
1002
1003 if ($redirect_to != '')
1004 $_SESSION['login_url'] = $redirect_to;
1005
1006 if ($output_method != 'echo' || !$user_info['is_guest'])
1007 return $user_info['is_guest'];
1008
1009 echo '
1010 <form action="', $scripturl, '?action=login2" method="post" accept-charset="', $context['character_set'], '">
1011 <table border="0" cellspacing="1" cellpadding="0" class="ssi_table">
1012 <tr>
1013 <td align="right"><label for="user">', $txt['username'], ':</label>&nbsp;</td>
1014 <td><input type="text" id="user" name="user" size="9" value="', $user_info['username'], '" class="input_text" /></td>
1015 </tr><tr>
1016 <td align="right"><label for="passwrd">', $txt['password'], ':</label>&nbsp;</td>
1017 <td><input type="password" name="passwrd" id="passwrd" size="9" class="input_password" /></td>
1018 </tr>';
1019
1020 // Open ID?
1021 if (!empty($modSettings['enableOpenID']))
1022 echo '<tr>
1023 <td colspan="2" align="center"><strong>&mdash;', $txt['or'], '&mdash;</strong></td>
1024 </tr><tr>
1025 <td align="right"><label for="openid_url">', $txt['openid'], ':</label>&nbsp;</td>
1026 <td><input type="text" name="openid_identifier" id="openid_url" class="input_text openid_login" size="17" /></td>
1027 </tr>';
1028
1029 echo '<tr>
1030 <td><input type="hidden" name="cookielength" value="-1" /></td>
1031 <td><input type="submit" value="', $txt['login'], '" class="button_submit" /></td>
1032 </tr>
1033 </table>
1034 </form>';
1035
1036 }
1037
1038 // Show the most-voted-in poll.
1039 function ssi_topPoll($output_method = 'echo')
1040 {
1041 // Just use recentPoll, no need to duplicate code...
1042 return ssi_recentPoll(true, $output_method);
1043 }
1044
1045 // Show the most recently posted poll.
1046 function ssi_recentPoll($topPollInstead = false, $output_method = 'echo')
1047 {
1048 global $db_prefix, $txt, $settings, $boardurl, $user_info, $context, $smcFunc, $modSettings;
1049
1050 $boardsAllowed = array_intersect(boardsAllowedTo('poll_view'), boardsAllowedTo('poll_vote'));
1051
1052 if (empty($boardsAllowed))
1053 return array();
1054
1055 $request = $smcFunc['db_query']('', '
1056 SELECT p.id_poll, p.question, t.id_topic, p.max_votes, p.guest_vote, p.hide_results, p.expire_time
1057 FROM {db_prefix}polls AS p
1058 INNER JOIN {db_prefix}topics AS t ON (t.id_poll = p.id_poll' . ($modSettings['postmod_active'] ? ' AND t.approved = {int:is_approved}' : '') . ')
1059 INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board)' . ($topPollInstead ? '
1060 INNER JOIN {db_prefix}poll_choices AS pc ON (pc.id_poll = p.id_poll)' : '') . '
1061 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})
1062 WHERE p.voting_locked = {int:voting_opened}
1063 AND (p.expire_time = {int:no_expiration} OR {int:current_time} < p.expire_time)
1064 AND ' . ($user_info['is_guest'] ? 'p.guest_vote = {int:guest_vote_allowed}' : 'lp.id_choice IS NULL') . '
1065 AND {query_wanna_see_board}' . (!in_array(0, $boardsAllowed) ? '
1066 AND b.id_board IN ({array_int:boards_allowed_list})' : '') . (!empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] > 0 ? '
1067 AND b.id_board != {int:recycle_enable}' : '') . '
1068 ORDER BY ' . ($topPollInstead ? 'pc.votes' : 'p.id_poll') . ' DESC
1069 LIMIT 1',
1070 array(
1071 'current_member' => $user_info['id'],
1072 'boards_allowed_list' => $boardsAllowed,
1073 'is_approved' => 1,
1074 'guest_vote_allowed' => 1,
1075 'no_member' => 0,
1076 'voting_opened' => 0,
1077 'no_expiration' => 0,
1078 'current_time' => time(),
1079 'recycle_enable' => $modSettings['recycle_board'],
1080 )
1081 );
1082 $row = $smcFunc['db_fetch_assoc']($request);
1083 $smcFunc['db_free_result']($request);
1084
1085 // This user has voted on all the polls.
1086 if ($row === false)
1087 return array();
1088
1089 // If this is a guest who's voted we'll through ourselves to show poll to show the results.
1090 if ($user_info['is_guest'] && (!$row['guest_vote'] || (isset($_COOKIE['guest_poll_vote']) && in_array($row['id_poll'], explode(',', $_COOKIE['guest_poll_vote'])))))
1091 return ssi_showPoll($row['id_topic'], $output_method);
1092
1093 $request = $smcFunc['db_query']('', '
1094 SELECT COUNT(DISTINCT id_member)
1095 FROM {db_prefix}log_polls
1096 WHERE id_poll = {int:current_poll}',
1097 array(
1098 'current_poll' => $row['id_poll'],
1099 )
1100 );
1101 list ($total) = $smcFunc['db_fetch_row']($request);
1102 $smcFunc['db_free_result']($request);
1103
1104 $request = $smcFunc['db_query']('', '
1105 SELECT id_choice, label, votes
1106 FROM {db_prefix}poll_choices
1107 WHERE id_poll = {int:current_poll}',
1108 array(
1109 'current_poll' => $row['id_poll'],
1110 )
1111 );
1112 $options = array();
1113 while ($rowChoice = $smcFunc['db_fetch_assoc']($request))
1114 {
1115 censorText($rowChoice['label']);
1116
1117 $options[$rowChoice['id_choice']] = array($rowChoice['label'], $rowChoice['votes']);
1118 }
1119 $smcFunc['db_free_result']($request);
1120
1121 // Can they view it?
1122 $is_expired = !empty($row['expire_time']) && $row['expire_time'] < time();
1123 $allow_view_results = allowedTo('moderate_board') || $row['hide_results'] == 0 || $is_expired;
1124
1125 $return = array(
1126 'id' => $row['id_poll'],
1127 'image' => 'poll',
1128 'question' => $row['question'],
1129 'total_votes' => $total,
1130 'is_locked' => false,
1131 'topic' => $row['id_topic'],
1132 'allow_view_results' => $allow_view_results,
1133 'options' => array()
1134 );
1135
1136 // Calculate the percentages and bar lengths...
1137 $divisor = $return['total_votes'] == 0 ? 1 : $return['total_votes'];
1138 foreach ($options as $i => $option)
1139 {
1140 $bar = floor(($option[1] * 100) / $divisor);
1141 $barWide = $bar == 0 ? 1 : floor(($bar * 5) / 3);
1142 $return['options'][$i] = array(
1143 'id' => 'options-' . ($topPollInstead ? 'top-' : 'recent-') . $i,
1144 'percent' => $bar,
1145 'votes' => $option[1],
1146 'bar' => '<span style="white-space: nowrap;"><img src="' . $settings['images_url'] . '/poll_' . ($context['right_to_left'] ? 'right' : 'left') . '.gif" alt="" /><img src="' . $settings['images_url'] . '/poll_middle.gif" width="' . $barWide . '" height="12" alt="-" /><img src="' . $settings['images_url'] . '/poll_' . ($context['right_to_left'] ? 'left' : 'right') . '.gif" alt="" /></span>',
1147 'option' => parse_bbc($option[0]),
1148 'vote_button' => '<input type="' . ($row['max_votes'] > 1 ? 'checkbox' : 'radio') . '" name="options[]" id="options-' . ($topPollInstead ? 'top-' : 'recent-') . $i . '" value="' . $i . '" class="input_' . ($row['max_votes'] > 1 ? 'check' : 'radio') . '" />'
1149 );
1150 }
1151
1152 $return['allowed_warning'] = $row['max_votes'] > 1 ? sprintf($txt['poll_options6'], min(count($options), $row['max_votes'])) : '';
1153
1154 if ($output_method != 'echo')
1155 return $return;
1156
1157 if ($allow_view_results)
1158 {
1159 echo '
1160 <form class="ssi_poll" action="', $boardurl, '/SSI.php?ssi_function=pollVote" method="post" accept-charset="', $context['character_set'], '">
1161 <strong>', $return['question'], '</strong><br />
1162 ', !empty($return['allowed_warning']) ? $return['allowed_warning'] . '<br />' : '';
1163
1164 foreach ($return['options'] as $option)
1165 echo '
1166 <label for="', $option['id'], '">', $option['vote_button'], ' ', $option['option'], '</label><br />';
1167
1168 echo '
1169 <input type="submit" value="', $txt['poll_vote'], '" class="button_submit" />
1170 <input type="hidden" name="poll" value="', $return['id'], '" />
1171 <input type="hidden" name="', $context['session_var'], '" value="', $context['session_id'], '" />
1172 </form>';
1173 }
1174 else
1175 echo $txt['poll_cannot_see'];
1176 }
1177
1178 function ssi_showPoll($topic = null, $output_method = 'echo')
1179 {
1180 global $db_prefix, $txt, $settings, $boardurl, $user_info, $context, $smcFunc, $modSettings;
1181
1182 $boardsAllowed = boardsAllowedTo('poll_view');
1183
1184 if (empty($boardsAllowed))
1185 return array();
1186
1187 if ($topic === null && isset($_REQUEST['ssi_topic']))
1188 $topic = (int) $_REQUEST['ssi_topic'];
1189 else
1190 $topic = (int) $topic;
1191
1192 $request = $smcFunc['db_query']('', '
1193 SELECT
1194 p.id_poll, p.question, p.voting_locked, p.hide_results, p.expire_time, p.max_votes, p.guest_vote, b.id_board
1195 FROM {db_prefix}topics AS t
1196 INNER JOIN {db_prefix}polls AS p ON (p.id_poll = t.id_poll)
1197 INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board)
1198 WHERE t.id_topic = {int:current_topic}
1199 AND {query_see_board}' . (!in_array(0, $boardsAllowed) ? '
1200 AND b.id_board IN ({array_int:boards_allowed_see})' : '') . ($modSettings['postmod_active'] ? '
1201 AND t.approved = {int:is_approved}' : '') . '
1202 LIMIT 1',
1203 array(
1204 'current_topic' => $topic,
1205 'boards_allowed_see' => $boardsAllowed,
1206 'is_approved' => 1,
1207 )
1208 );
1209
1210 // Either this topic has no poll, or the user cannot view it.
1211 if ($smcFunc['db_num_rows']($request) == 0)
1212 return array();
1213
1214 $row = $smcFunc['db_fetch_assoc']($request);
1215 $smcFunc['db_free_result']($request);
1216
1217 // Check if they can vote.
1218 if (!empty($row['expire_time']) && $row['expire_time'] < time())
1219 $allow_vote = false;
1220 elseif ($user_info['is_guest'] && $row['guest_vote'] && (!isset($_COOKIE['guest_poll_vote']) || !in_array($row['id_poll'], explode(',', $_COOKIE['guest_poll_vote']))))
1221 $allow_vote = true;
1222 elseif ($user_info['is_guest'])
1223 $allow_vote = false;
1224 elseif (!empty($row['voting_locked']) || !allowedTo('poll_vote', $row['id_board']))
1225 $allow_vote = false;
1226 else
1227 {
1228 $request = $smcFunc['db_query']('', '
1229 SELECT id_member
1230 FROM {db_prefix}log_polls
1231 WHERE id_poll = {int:current_poll}
1232 AND id_member = {int:current_member}
1233 LIMIT 1',
1234 array(
1235 'current_member' => $user_info['id'],
1236 'current_poll' => $row['id_poll'],
1237 )
1238 );
1239 $allow_vote = $smcFunc['db_num_rows']($request) == 0;
1240 $smcFunc['db_free_result']($request);
1241 }
1242
1243 // Can they view?
1244 $is_expired = !empty($row['expire_time']) && $row['expire_time'] < time();
1245 $allow_view_results = allowedTo('moderate_board') || $row['hide_results'] == 0 || ($row['hide_results'] == 1 && !$allow_vote) || $is_expired;
1246
1247 $request = $smcFunc['db_query']('', '
1248 SELECT COUNT(DISTINCT id_member)
1249 FROM {db_prefix}log_polls
1250 WHERE id_poll = {int:current_poll}',
1251 array(
1252 'current_poll' => $row['id_poll'],
1253 )
1254 );
1255 list ($total) = $smcFunc['db_fetch_row']($request);
1256 $smcFunc['db_free_result']($request);
1257
1258 $request = $smcFunc['db_query']('', '
1259 SELECT id_choice, label, votes
1260 FROM {db_prefix}poll_choices
1261 WHERE id_poll = {int:current_poll}',
1262 array(
1263 'current_poll' => $row['id_poll'],
1264 )
1265 );
1266 $options = array();
1267 $total_votes = 0;
1268 while ($rowChoice = $smcFunc['db_fetch_assoc']($request))
1269 {
1270 censorText($rowChoice['label']);
1271
1272 $options[$rowChoice['id_choice']] = array($rowChoice['label'], $rowChoice['votes']);
1273 $total_votes += $rowChoice['votes'];
1274 }
1275 $smcFunc['db_free_result']($request);
1276
1277 $return = array(
1278 'id' => $row['id_poll'],
1279 'image' => empty($pollinfo['voting_locked']) ? 'poll' : 'locked_poll',
1280 'question' => $row['question'],
1281 'total_votes' => $total,
1282 'is_locked' => !empty($pollinfo['voting_locked']),
1283 'allow_vote' => $allow_vote,
1284 'allow_view_results' => $allow_view_results,
1285 'topic' => $topic
1286 );
1287
1288 // Calculate the percentages and bar lengths...
1289 $divisor = $total_votes == 0 ? 1 : $total_votes;
1290 foreach ($options as $i => $option)
1291 {
1292 $bar = floor(($option[1] * 100) / $divisor);
1293 $barWide = $bar == 0 ? 1 : floor(($bar * 5) / 3);
1294 $return['options'][$i] = array(
1295 'id' => 'options-' . $i,
1296 'percent' => $bar,
1297 'votes' => $option[1],
1298 'bar' => '<span style="white-space: nowrap;"><img src="' . $settings['images_url'] . '/poll_' . ($context['right_to_left'] ? 'right' : 'left') . '.gif" alt="" /><img src="' . $settings['images_url'] . '/poll_middle.gif" width="' . $barWide . '" height="12" alt="-" /><img src="' . $settings['images_url'] . '/poll_' . ($context['right_to_left'] ? 'left' : 'right') . '.gif" alt="" /></span>',
1299 'option' => parse_bbc($option[0]),
1300 'vote_button' => '<input type="' . ($row['max_votes'] > 1 ? 'checkbox' : 'radio') . '" name="options[]" id="options-' . $i . '" value="' . $i . '" class="input_' . ($row['max_votes'] > 1 ? 'check' : 'radio') . '" />'
1301 );
1302 }
1303
1304 $return['allowed_warning'] = $row['max_votes'] > 1 ? sprintf($txt['poll_options6'], min(count($options), $row['max_votes'])) : '';
1305
1306 if ($output_method != 'echo')
1307 return $return;
1308
1309 if ($return['allow_vote'])
1310 {
1311 echo '
1312 <form class="ssi_poll" action="', $boardurl, '/SSI.php?ssi_function=pollVote" method="post" accept-charset="', $context['character_set'], '">
1313 <strong>', $return['question'], '</strong><br />
1314 ', !empty($return['allowed_warning']) ? $return['allowed_warning'] . '<br />' : '';
1315
1316 foreach ($return['options'] as $option)
1317 echo '
1318 <label for="', $option['id'], '">', $option['vote_button'], ' ', $option['option'], '</label><br />';
1319
1320 echo '
1321 <input type="submit" value="', $txt['poll_vote'], '" class="button_submit" />
1322 <input type="hidden" name="poll" value="', $return['id'], '" />
1323 <input type="hidden" name="', $context['session_var'], '" value="', $context['session_id'], '" />
1324 </form>';
1325 }
1326 elseif ($return['allow_view_results'])
1327 {
1328 echo '
1329 <div class="ssi_poll">
1330 <strong>', $return['question'], '</strong>
1331 <dl>';
1332
1333 foreach ($return['options'] as $option)
1334 echo '
1335 <dt>', $option['option'], '</dt>
1336 <dd>
1337 <div class="ssi_poll_bar" style="border: 1px solid #666; height: 1em">
1338 <div class="ssi_poll_bar_fill" style="background: #ccf; height: 1em; width: ', $option['percent'], '%;">
1339 </div>
1340 </div>
1341 ', $option['votes'], ' (', $option['percent'], '%)
1342 </dd>';
1343 echo '
1344 </dl>
1345 <strong>', $txt['poll_total_voters'], ': ', $return['total_votes'], '</strong>
1346 </div>';
1347 }
1348 // Cannot see it I'm afraid!
1349 else
1350 echo $txt['poll_cannot_see'];
1351 }
1352
1353 // Takes care of voting - don't worry, this is done automatically.
1354 function ssi_pollVote()
1355 {
1356 global $context, $db_prefix, $user_info, $sc, $smcFunc, $sourcedir, $modSettings;
1357
1358 if (!isset($_POST[$context['session_var']]) || $_POST[$context['session_var']] != $sc || empty($_POST['options']) || !isset($_POST['poll']))
1359 {
1360 echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
1361 <html>
1362 <head>
1363 <script type="text/javascript"><!-- // --><![CDATA[
1364 history.go(-1);
1365 // ]]></script>
1366 </head>
1367 <body>&laquo;</body>
1368 </html>';
1369 return;
1370 }
1371
1372 // This can cause weird errors! (ie. copyright missing.)
1373 checkSession();
1374
1375 $_POST['poll'] = (int) $_POST['poll'];
1376
1377 // Check if they have already voted, or voting is locked.
1378 $request = $smcFunc['db_query']('', '
1379 SELECT
1380 p.id_poll, p.voting_locked, p.expire_time, p.max_votes, p.guest_vote,
1381 t.id_topic,
1382 IFNULL(lp.id_choice, -1) AS selected
1383 FROM {db_prefix}polls AS p
1384 INNER JOIN {db_prefix}topics AS t ON (t.id_poll = {int:current_poll})
1385 INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board)
1386 LEFT JOIN {db_prefix}log_polls AS lp ON (lp.id_poll = p.id_poll AND lp.id_member = {int:current_member})
1387 WHERE p.id_poll = {int:current_poll}
1388 AND {query_see_board}' . ($modSettings['postmod_active'] ? '
1389 AND t.approved = {int:is_approved}' : '') . '
1390 LIMIT 1',
1391 array(
1392 'current_member' => $user_info['id'],
1393 'current_poll' => $_POST['poll'],
1394 'is_approved' => 1,
1395 )
1396 );
1397 if ($smcFunc['db_num_rows']($request) == 0)
1398 die;
1399 $row = $smcFunc['db_fetch_assoc']($request);
1400 $smcFunc['db_free_result']($request);
1401
1402 if (!empty($row['voting_locked']) || ($row['selected'] != -1 && !$user_info['is_guest']) || (!empty($row['expire_time']) && time() > $row['expire_time']))
1403 redirectexit('topic=' . $row['id_topic'] . '.0');
1404
1405 // Too many options checked?
1406 if (count($_REQUEST['options']) > $row['max_votes'])
1407 redirectexit('topic=' . $row['id_topic'] . '.0');
1408
1409 // It's a guest who has already voted?
1410 if ($user_info['is_guest'])
1411 {
1412 // Guest voting disabled?
1413 if (!$row['guest_vote'])
1414 redirectexit('topic=' . $row['id_topic'] . '.0');
1415 // Already voted?
1416 elseif (isset($_COOKIE['guest_poll_vote']) && in_array($row['id_poll'], explode(',', $_COOKIE['guest_poll_vote'])))
1417 redirectexit('topic=' . $row['id_topic'] . '.0');
1418 }
1419
1420 $options = array();
1421 $inserts = array();
1422 foreach ($_REQUEST['options'] as $id)
1423 {
1424 $id = (int) $id;
1425
1426 $options[] = $id;
1427 $inserts[] = array($_POST['poll'], $user_info['id'], $id);
1428 }
1429
1430 // Add their vote in to the tally.
1431 $smcFunc['db_insert']('insert',
1432 $db_prefix . 'log_polls',
1433 array('id_poll' => 'int', 'id_member' => 'int', 'id_choice' => 'int'),
1434 $inserts,
1435 array('id_poll', 'id_member', 'id_choice')
1436 );
1437 $smcFunc['db_query']('', '
1438 UPDATE {db_prefix}poll_choices
1439 SET votes = votes + 1
1440 WHERE id_poll = {int:current_poll}
1441 AND id_choice IN ({array_int:option_list})',
1442 array(
1443 'option_list' => $options,
1444 'current_poll' => $_POST['poll'],
1445 )
1446 );
1447
1448 // Track the vote if a guest.
1449 if ($user_info['is_guest'])
1450 {
1451 $_COOKIE['guest_poll_vote'] = !empty($_COOKIE['guest_poll_vote']) ? ($_COOKIE['guest_poll_vote'] . ',' . $row['id_poll']) : $row['id_poll'];
1452
1453 require_once($sourcedir . '/Subs-Auth.php');
1454 $cookie_url = url_parts(!empty($modSettings['localCookies']), !empty($modSettings['globalCookies']));
1455 setcookie('guest_poll_vote', $_COOKIE['guest_poll_vote'], time() + 2500000, $cookie_url[1], $cookie_url[0], 0);
1456 }
1457
1458 redirectexit('topic=' . $row['id_topic'] . '.0');
1459 }
1460
1461 // Show a search box.
1462 function ssi_quickSearch($output_method = 'echo')
1463 {
1464 global $scripturl, $txt, $context;
1465
1466 if ($output_method != 'echo')
1467 return $scripturl . '?action=search';
1468
1469 echo '
1470 <form action="', $scripturl, '?action=search2" method="post" accept-charset="', $context['character_set'], '">
1471 <input type="hidden" name="advanced" value="0" /><input type="text" name="search" size="30" class="input_text" /> <input type="submit" name="submit" value="', $txt['search'], '" class="button_submit" />
1472 </form>';
1473 }
1474
1475 // Show what would be the forum news.
1476 function ssi_news($output_method = 'echo')
1477 {
1478 global $context;
1479
1480 if ($output_method != 'echo')
1481 return $context['random_news_line'];
1482
1483 echo $context['random_news_line'];
1484 }
1485
1486 // Show today's birthdays.
1487 function ssi_todaysBirthdays($output_method = 'echo')
1488 {
1489 global $scripturl, $modSettings, $user_info;
1490
1491 if (empty($modSettings['cal_enabled']) || !allowedTo('calendar_view') || !allowedTo('profile_view_any'))
1492 return;
1493
1494 $eventOptions = array(
1495 'include_birthdays' => true,
1496 'num_days_shown' => empty($modSettings['cal_days_for_index']) || $modSettings['cal_days_for_index'] < 1 ? 1 : $modSettings['cal_days_for_index'],
1497 );
1498 $return = cache_quick_get('calendar_index_offset_' . ($user_info['time_offset'] + $modSettings['time_offset']), 'Subs-Calendar.php', 'cache_getRecentEvents', array($eventOptions));
1499
1500 if ($output_method != 'echo')
1501 return $return['calendar_birthdays'];
1502
1503 foreach ($return['calendar_birthdays'] as $member)
1504 echo '
1505 <a href="', $scripturl, '?action=profile;u=', $member['id'], '">' . $member['name'] . (isset($member['age']) ? ' (' . $member['age'] . ')' : '') . '</a>' . (!$member['is_last'] ? ', ' : '');
1506 }
1507
1508 // Show today's holidays.
1509 function ssi_todaysHolidays($output_method = 'echo')
1510 {
1511 global $modSettings, $user_info;
1512
1513 if (empty($modSettings['cal_enabled']) || !allowedTo('calendar_view'))
1514 return;
1515
1516 $eventOptions = array(
1517 'include_holidays' => true,
1518 'num_days_shown' => empty($modSettings['cal_days_for_index']) || $modSettings['cal_days_for_index'] < 1 ? 1 : $modSettings['cal_days_for_index'],
1519 );
1520 $return = cache_quick_get('calendar_index_offset_' . ($user_info['time_offset'] + $modSettings['time_offset']), 'Subs-Calendar.php', 'cache_getRecentEvents', array($eventOptions));
1521
1522 if ($output_method != 'echo')
1523 return $return['calendar_holidays'];
1524
1525 echo '
1526 ', implode(', ', $return['calendar_holidays']);
1527 }
1528
1529 // Show today's events.
1530 function ssi_todaysEvents($output_method = 'echo')
1531 {
1532 global $modSettings, $user_info;
1533
1534 if (empty($modSettings['cal_enabled']) || !allowedTo('calendar_view'))
1535 return;
1536
1537 $eventOptions = array(
1538 'include_events' => true,
1539 'num_days_shown' => empty($modSettings['cal_days_for_index']) || $modSettings['cal_days_for_index'] < 1 ? 1 : $modSettings['cal_days_for_index'],
1540 );
1541 $return = cache_quick_get('calendar_index_offset_' . ($user_info['time_offset'] + $modSettings['time_offset']), 'Subs-Calendar.php', 'cache_getRecentEvents', array($eventOptions));
1542
1543 if ($output_method != 'echo')
1544 return $return['calendar_events'];
1545
1546 foreach ($return['calendar_events'] as $event)
1547 {
1548 if ($event['can_edit'])
1549 echo '
1550 <a href="' . $event['modify_href'] . '" style="color: #ff0000;">*</a> ';
1551 echo '
1552 ' . $event['link'] . (!$event['is_last'] ? ', ' : '');
1553 }
1554 }
1555
1556 // Show all calendar entires for today. (birthdays, holodays, and events.)
1557 function ssi_todaysCalendar($output_method = 'echo')
1558 {
1559 global $modSettings, $txt, $scripturl, $user_info;
1560
1561 $eventOptions = array(
1562 'include_birthdays' => allowedTo('profile_view_any'),
1563 'include_holidays' => true,
1564 'include_events' => true,
1565 'num_days_shown' => empty($modSettings['cal_days_for_index']) || $modSettings['cal_days_for_index'] < 1 ? 1 : $modSettings['cal_days_for_index'],
1566 );
1567 $return = cache_quick_get('calendar_index_offset_' . ($user_info['time_offset'] + $modSettings['time_offset']), 'Subs-Calendar.php', 'cache_getRecentEvents', array($eventOptions));
1568
1569 if ($output_method != 'echo')
1570 return $return;
1571
1572 if (!empty($return['calendar_holidays']))
1573 echo '
1574 <span class="holiday">' . $txt['calendar_prompt'] . ' ' . implode(', ', $return['calendar_holidays']) . '<br /></span>';
1575 if (!empty($return['calendar_birthdays']))
1576 {
1577 echo '
1578 <span class="birthday">' . $txt['birthdays_upcoming'] . '</span> ';
1579 foreach ($return['calendar_birthdays'] as $member)
1580 echo '
1581 <a href="', $scripturl, '?action=profile;u=', $member['id'], '">', $member['name'], isset($member['age']) ? ' (' . $member['age'] . ')' : '', '</a>', !$member['is_last'] ? ', ' : '';
1582 echo '
1583 <br />';
1584 }
1585 if (!empty($return['calendar_events']))
1586 {
1587 echo '
1588 <span class="event">' . $txt['events_upcoming'] . '</span> ';
1589 foreach ($return['calendar_events'] as $event)
1590 {
1591 if ($event['can_edit'])
1592 echo '
1593 <a href="' . $event['modify_href'] . '" style="color: #ff0000;">*</a> ';
1594 echo '
1595 ' . $event['link'] . (!$event['is_last'] ? ', ' : '');
1596 }
1597 }
1598 }
1599
1600 // Show the latest news, with a template... by board.
1601 function ssi_boardNews($board = null, $limit = null, $start = null, $length = null, $output_method = 'echo')
1602 {
1603 global $scripturl, $db_prefix, $txt, $settings, $modSettings, $context;
1604 global $smcFunc;
1605
1606 loadLanguage('Stats');
1607
1608 // Must be integers....
1609 if ($limit === null)
1610 $limit = isset($_GET['limit']) ? (int) $_GET['limit'] : 5;
1611 else
1612 $limit = (int) $limit;
1613
1614 if ($start === null)
1615 $start = isset($_GET['start']) ? (int) $_GET['start'] : 0;
1616 else
1617 $start = (int) $start;
1618
1619 if ($board !== null)
1620 $board = (int) $board;
1621 elseif (isset($_GET['board']))
1622 $board = (int) $_GET['board'];
1623
1624 if ($length === null)
1625 $length = isset($_GET['length']) ? (int) $_GET['length'] : 0;
1626 else
1627 $length = (int) $length;
1628
1629 $limit = max(0, $limit);
1630 $start = max(0, $start);
1631
1632 // Make sure guests can see this board.
1633 $request = $smcFunc['db_query']('', '
1634 SELECT id_board
1635 FROM {db_prefix}boards
1636 WHERE ' . ($board === null ? '' : 'id_board = {int:current_board}
1637 AND ') . 'FIND_IN_SET(-1, member_groups)
1638 LIMIT 1',
1639 array(
1640 'current_board' => $board,
1641 )
1642 );
1643 if ($smcFunc['db_num_rows']($request) == 0)
1644 {
1645 if ($output_method == 'echo')
1646 die($txt['ssi_no_guests']);
1647 else
1648 return array();
1649 }
1650 list ($board) = $smcFunc['db_fetch_row']($request);
1651 $smcFunc['db_free_result']($request);
1652
1653 // Load the message icons - the usual suspects.
1654 $stable_icons = array('xx', 'thumbup', 'thumbdown', 'exclamation', 'question', 'lamp', 'smiley', 'angry', 'cheesy', 'grin', 'sad', 'wink', 'moved', 'recycled', 'wireless');
1655 $icon_sources = array();
1656 foreach ($stable_icons as $icon)
1657 $icon_sources[$icon] = 'images_url';
1658
1659 // Find the post ids.
1660 $request = $smcFunc['db_query']('', '
1661 SELECT t.id_first_msg
1662 FROM {db_prefix}topics as t
1663 LEFT JOIN {db_prefix}boards as b ON (b.id_board = t.id_board)
1664 WHERE t.id_board = {int:current_board}' . ($modSettings['postmod_active'] ? '
1665 AND t.approved = {int:is_approved}' : '') . '
1666 AND {query_see_board}
1667 ORDER BY t.id_first_msg DESC
1668 LIMIT ' . $start . ', ' . $limit,
1669 array(
1670 'current_board' => $board,
1671 'is_approved' => 1,
1672 )
1673 );
1674 $posts = array();
1675 while ($row = $smcFunc['db_fetch_assoc']($request))
1676 $posts[] = $row['id_first_msg'];
1677 $smcFunc['db_free_result']($request);
1678
1679 if (empty($posts))
1680 return array();
1681
1682 // Find the posts.
1683 $request = $smcFunc['db_query']('', '
1684 SELECT
1685 m.icon, m.subject, m.body, IFNULL(mem.real_name, m.poster_name) AS poster_name, m.poster_time,
1686 t.num_replies, t.id_topic, m.id_member, m.smileys_enabled, m.id_msg, t.locked, t.id_last_msg
1687 FROM {db_prefix}topics AS t
1688 INNER JOIN {db_prefix}messages AS m ON (m.id_msg = t.id_first_msg)
1689 LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member)
1690 WHERE t.id_first_msg IN ({array_int:post_list})
1691 ORDER BY t.id_first_msg DESC
1692 LIMIT ' . count($posts),
1693 array(
1694 'post_list' => $posts,
1695 )
1696 );
1697 $return = array();
1698 while ($row = $smcFunc['db_fetch_assoc']($request))
1699 {
1700 // If we want to limit the length of the post.
1701 if (!empty($length) && $smcFunc['strlen']($row['body']) > $length)
1702 {
1703 $row['body'] = $smcFunc['substr']($row['body'], 0, $length);
1704
1705 // The first space or line break. (<br />, etc.)
1706 $cutoff = max(strrpos($row['body'], ' '), strrpos($row['body'], '<'));
1707
1708 if ($cutoff !== false)
1709 $row['body'] = $smcFunc['substr']($row['body'], 0, $cutoff);
1710 $row['body'] .= '...';
1711 }
1712
1713 $row['body'] = parse_bbc($row['body'], $row['smileys_enabled'], $row['id_msg']);
1714
1715 // Check that this message icon is there...
1716 if (empty($modSettings['messageIconChecks_disable']) && !isset($icon_sources[$row['icon']]))
1717 $icon_sources[$row['icon']] = file_exists($settings['theme_dir'] . '/images/post/' . $row['icon'] . '.gif') ? 'images_url' : 'default_images_url';
1718
1719 censorText($row['subject']);
1720 censorText($row['body']);
1721
1722 $return[] = array(
1723 'id' => $row['id_topic'],
1724 'message_id' => $row['id_msg'],
1725 'icon' => '<img src="' . $settings[$icon_sources[$row['icon']]] . '/post/' . $row['icon'] . '.gif" alt="' . $row['icon'] . '" />',
1726 'subject' => $row['subject'],
1727 'time' => timeformat($row['poster_time']),
1728 'timestamp' => forum_time(true, $row['poster_time']),
1729 'body' => $row['body'],
1730 'href' => $scripturl . '?topic=' . $row['id_topic'] . '.0',
1731 'link' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.0">' . $row['num_replies'] . ' ' . ($row['num_replies'] == 1 ? $txt['ssi_comment'] : $txt['ssi_comments']) . '</a>',
1732 'replies' => $row['num_replies'],
1733 'comment_href' => !empty($row['locked']) ? '' : $scripturl . '?action=post;topic=' . $row['id_topic'] . '.' . $row['num_replies'] . ';last_msg=' . $row['id_last_msg'],
1734 '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>',
1735 'new_comment' => !empty($row['locked']) ? '' : '<a href="' . $scripturl . '?action=post;topic=' . $row['id_topic'] . '.' . $row['num_replies'] . '">' . $txt['ssi_write_comment'] . '</a>',
1736 'poster' => array(
1737 'id' => $row['id_member'],
1738 'name' => $row['poster_name'],
1739 'href' => !empty($row['id_member']) ? $scripturl . '?action=profile;u=' . $row['id_member'] : '',
1740 'link' => !empty($row['id_member']) ? '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['poster_name'] . '</a>' : $row['poster_name']
1741 ),
1742 'locked' => !empty($row['locked']),
1743 'is_last' => false
1744 );
1745 }
1746 $smcFunc['db_free_result']($request);
1747
1748 if (empty($return))
1749 return $return;
1750
1751 $return[count($return) - 1]['is_last'] = true;
1752
1753 if ($output_method != 'echo')
1754 return $return;
1755
1756 foreach ($return as $news)
1757 {
1758 echo '
1759 <div class="news_item">
1760 <h3 class="news_header">
1761 ', $news['icon'], '
1762 <a href="', $news['href'], '">', $news['subject'], '</a>
1763 </h3>
1764 <div class="news_timestamp">', $news['time'], ' ', $txt['by'], ' ', $news['poster']['link'], '</div>
1765 <div class="news_body" style="padding: 2ex 0;">', $news['body'], '</div>
1766 ', $news['link'], $news['locked'] ? '' : ' | ' . $news['comment_link'], '
1767 </div>';
1768
1769 if (!$news['is_last'])
1770 echo '
1771 <hr />';
1772 }
1773 }
1774
1775 // Show the most recent events.
1776 function ssi_recentEvents($max_events = 7, $output_method = 'echo')
1777 {
1778 global $db_prefix, $user_info, $scripturl, $modSettings, $txt, $context, $smcFunc;
1779
1780 if (empty($modSettings['cal_enabled']) || !allowedTo('calendar_view'))
1781 return;
1782
1783 // Find all events which are happening in the near future that the member can see.
1784 $request = $smcFunc['db_query']('', '
1785 SELECT
1786 cal.id_event, cal.start_date, cal.end_date, cal.title, cal.id_member, cal.id_topic,
1787 cal.id_board, t.id_first_msg, t.approved
1788 FROM {db_prefix}calendar AS cal
1789 LEFT JOIN {db_prefix}boards AS b ON (b.id_board = cal.id_board)
1790 LEFT JOIN {db_prefix}topics AS t ON (t.id_topic = cal.id_topic)
1791 WHERE cal.start_date <= {date:current_date}
1792 AND cal.end_date >= {date:current_date}
1793 AND (cal.id_board = {int:no_board} OR {query_wanna_see_board})
1794 ORDER BY cal.start_date DESC
1795 LIMIT ' . $max_events,
1796 array(
1797 'current_date' => strftime('%Y-%m-%d', forum_time(false)),
1798 'no_board' => 0,
1799 )
1800 );
1801 $return = array();
1802 $duplicates = array();
1803 while ($row = $smcFunc['db_fetch_assoc']($request))
1804 {
1805 // 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.
1806 if (!empty($duplicates[$row['title'] . $row['id_topic']]))
1807 continue;
1808
1809 // Censor the title.
1810 censorText($row['title']);
1811
1812 if ($row['start_date'] < strftime('%Y-%m-%d', forum_time(false)))
1813 $date = strftime('%Y-%m-%d', forum_time(false));
1814 else
1815 $date = $row['start_date'];
1816
1817 // If the topic it is attached to is not approved then don't link it.
1818 if (!empty($row['id_first_msg']) && !$row['approved'])
1819 $row['id_board'] = $row['id_topic'] = $row['id_first_msg'] = 0;
1820
1821 $return[$date][] = array(
1822 'id' => $row['id_event'],
1823 'title' => $row['title'],
1824 'can_edit' => allowedTo('calendar_edit_any') || ($row['id_member'] == $user_info['id'] && allowedTo('calendar_edit_own')),
1825 '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'],
1826 'href' => $row['id_board'] == 0 ? '' : $scripturl . '?topic=' . $row['id_topic'] . '.0',
1827 'link' => $row['id_board'] == 0 ? $row['title'] : '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.0">' . $row['title'] . '</a>',
1828 'start_date' => $row['start_date'],
1829 'end_date' => $row['end_date'],
1830 'is_last' => false
1831 );
1832
1833 // Let's not show this one again, huh?
1834 $duplicates[$row['title'] . $row['id_topic']] = true;
1835 }
1836 $smcFunc['db_free_result']($request);
1837
1838 foreach ($return as $mday => $array)
1839 $return[$mday][count($array) - 1]['is_last'] = true;
1840
1841 if ($output_method != 'echo' || empty($return))
1842 return $return;
1843
1844 // Well the output method is echo.
1845 echo '
1846 <span class="event">' . $txt['events'] . '</span> ';
1847 foreach ($return as $mday => $array)
1848 foreach ($array as $event)
1849 {
1850 if ($event['can_edit'])
1851 echo '
1852 <a href="' . $event['modify_href'] . '" style="color: #ff0000;">*</a> ';
1853
1854 echo '
1855 ' . $event['link'] . (!$event['is_last'] ? ', ' : '');
1856 }
1857 }
1858
1859 // Check the passed id_member/password. If $is_username is true, treats $id as a username.
1860 function ssi_checkPassword($id = null, $password = null, $is_username = false)
1861 {
1862 global $db_prefix, $sourcedir, $smcFunc;
1863
1864 // If $id is null, this was most likely called from a query string and should do nothing.
1865 if ($id === null)
1866 return;
1867
1868 $request = $smcFunc['db_query']('', '
1869 SELECT passwd, member_name, is_activated
1870 FROM {db_prefix}members
1871 WHERE ' . ($is_username ? 'member_name' : 'id_member') . ' = {string:id}
1872 LIMIT 1',
1873 array(
1874 'id' => $id,
1875 )
1876 );
1877 list ($pass, $user, $active) = $smcFunc['db_fetch_row']($request);
1878 $smcFunc['db_free_result']($request);
1879
1880 return sha1(strtolower($user) . $password) == $pass && $active == 1;
1881 }
1882
1883 // We want to show the recent attachments outside of the forum.
1884 function ssi_recentAttachments($num_attachments = 10, $attachment_ext = array(), $output_method = 'echo')
1885 {
1886 global $smcFunc, $context, $modSettings, $scripturl, $txt, $settings;
1887
1888 // We want to make sure that we only get attachments for boards that we can see *if* any.
1889 $attachments_boards = boardsAllowedTo('view_attachments');
1890
1891 // No boards? Adios amigo.
1892 if (empty($attachments_boards))
1893 return array();
1894
1895 // Is it an array?
1896 if (!is_array($attachment_ext))
1897 $attachment_ext = array($attachment_ext);
1898
1899 // Lets build the query.
1900 $request = $smcFunc['db_query']('', '
1901 SELECT
1902 att.id_attach, att.id_msg, att.filename, IFNULL(att.size, 0) AS filesize, att.downloads, mem.id_member,
1903 IFNULL(mem.real_name, m.poster_name) AS poster_name, m.id_topic, m.subject, t.id_board, m.poster_time,
1904 att.width, att.height' . (empty($modSettings['attachmentShowImages']) || empty($modSettings['attachmentThumbnails']) ? '' : ', IFNULL(thumb.id_attach, 0) AS id_thumb, thumb.width AS thumb_width, thumb.height AS thumb_height') . '
1905 FROM {db_prefix}attachments AS att
1906 INNER JOIN {db_prefix}messages AS m ON (m.id_msg = att.id_msg)
1907 INNER JOIN {db_prefix}topics AS t ON (t.id_topic = m.id_topic)
1908 LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member)' . (empty($modSettings['attachmentShowImages']) || empty($modSettings['attachmentThumbnails']) ? '' : '
1909 LEFT JOIN {db_prefix}attachments AS thumb ON (thumb.id_attach = att.id_thumb)') . '
1910 WHERE att.attachment_type = 0' . ($attachments_boards === array(0) ? '' : '
1911 AND m.id_board IN ({array_int:boards_can_see})') . (!empty($attachment_ext) ? '
1912 AND att.fileext IN ({array_string:attachment_ext})' : '') .
1913 (!$modSettings['postmod_active'] || allowedTo('approve_posts') ? '' : '
1914 AND t.approved = {int:is_approved}
1915 AND m.approved = {int:is_approved}
1916 AND att.approved = {int:is_approved}') . '
1917 ORDER BY att.id_attach DESC
1918 LIMIT {int:num_attachments}',
1919 array(
1920 'boards_can_see' => $attachments_boards,
1921 'attachment_ext' => $attachment_ext,
1922 'num_attachments' => $num_attachments,
1923 'is_approved' => 1,
1924 )
1925 );
1926
1927 // We have something.
1928 $attachments = array();
1929 while ($row = $smcFunc['db_fetch_assoc']($request))
1930 {
1931 $filename = preg_replace('~&amp;#(\\d{1,7}|x[0-9a-fA-F]{1,6});~', '&#\\1;', htmlspecialchars($row['filename']));
1932
1933 // Is it an image?
1934 $attachments[$row['id_attach']] = array(
1935 'member' => array(
1936 'id' => $row['id_member'],
1937 'name' => $row['poster_name'],
1938 'link' => empty($row['id_member']) ? $row['poster_name'] : '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['poster_name'] . '</a>',
1939 ),
1940 'file' => array(
1941 'filename' => $filename,
1942 'filesize' => round($row['filesize'] /1024, 2) . $txt['kilobyte'],
1943 'downloads' => $row['downloads'],
1944 'href' => $scripturl . '?action=dlattach;topic=' . $row['id_topic'] . '.0;attach=' . $row['id_attach'],
1945 'link' => '<img src="' . $settings['images_url'] . '/icons/clip.gif" alt="" /> <a href="' . $scripturl . '?action=dlattach;topic=' . $row['id_topic'] . '.0;attach=' . $row['id_attach'] . '">' . $filename . '</a>',
1946 'is_image' => !empty($row['width']) && !empty($row['height']) && !empty($modSettings['attachmentShowImages']),
1947 ),
1948 'topic' => array(
1949 'id' => $row['id_topic'],
1950 'subject' => $row['subject'],
1951 'href' => $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . '#msg' . $row['id_msg'],
1952 'link' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . '#msg' . $row['id_msg'] . '">' . $row['subject'] . '</a>',
1953 'time' => timeformat($row['poster_time']),
1954 ),
1955 );
1956
1957 // Images.
1958 if ($attachments[$row['id_attach']]['file']['is_image'])
1959 {
1960 $id_thumb = empty($row['id_thumb']) ? $row['id_attach'] : $row['id_thumb'];
1961 $attachments[$row['id_attach']]['file']['image'] = array(
1962 'id' => $id_thumb,
1963 'width' => $row['width'],
1964 'height' => $row['height'],
1965 'img' => '<img src="' . $scripturl . '?action=dlattach;topic=' . $row['id_topic'] . '.0;attach=' . $row['id_attach'] . ';image" alt="' . $filename . '" />',
1966 'thumb' => '<img src="' . $scripturl . '?action=dlattach;topic=' . $row['id_topic'] . '.0;attach=' . $id_thumb . ';image" alt="' . $filename . '" />',
1967 'href' => $scripturl . '?action=dlattach;topic=' . $row['id_topic'] . '.0;attach=' . $id_thumb . ';image',
1968 '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>',
1969 );
1970 }
1971 }
1972 $smcFunc['db_free_result']($request);
1973
1974 // So you just want an array? Here you can have it.
1975 if ($output_method == 'array' || empty($attachments))
1976 return $attachments;
1977
1978 // Give them the default.
1979 echo '
1980 <table class="ssi_downloads" cellpadding="2">
1981 <tr>
1982 <th align="left">', $txt['file'], '</th>
1983 <th align="left">', $txt['posted_by'], '</th>
1984 <th align="left">', $txt['downloads'], '</th>
1985 <th align="left">', $txt['filesize'], '</th>
1986 </tr>';
1987 foreach ($attachments as $attach)
1988 echo '
1989 <tr>
1990 <td>', $attach['file']['link'], '</td>
1991 <td>', $attach['member']['link'], '</td>
1992 <td align="center">', $attach['file']['downloads'], '</td>
1993 <td>', $attach['file']['filesize'], '</td>
1994 </tr>';
1995 echo '
1996 </table>';
1997 }
1998
1999 ?>