/SSI.php
PHP | 2224 lines | 1581 code | 279 blank | 364 comment | 318 complexity | c895f673599ada9447625bfc98d70822 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-3.0
Large files files are truncated, but you can click here to view the full file
- <?php
- /**
- * @name ElkArte Forum
- * @copyright ElkArte Forum contributors
- * @license BSD http://opensource.org/licenses/BSD-3-Clause
- *
- * This software is a derived product, based on:
- *
- * Simple Machines Forum (SMF)
- * copyright: 2011 Simple Machines (http://www.simplemachines.org)
- * license: BSD, See included LICENSE.TXT for terms and conditions.
- *
- * @version 1.0 Alpha
- */
- // Don't do anything if ELKARTE is already loaded.
- if (defined('ELKARTE'))
- return true;
- define('ELKARTE', 'SSI');
- // We're going to want a few globals... these are all set later.
- global $time_start, $maintenance, $msubject, $mmessage, $mbname, $language;
- global $boardurl, $webmaster_email, $cookiename;
- global $db_server, $db_name, $db_user, $db_prefix, $db_persist, $db_error_send, $db_last_error;
- global $db_connection, $modSettings, $context, $sc, $user_info, $topic, $board, $txt;
- global $smcFunc, $ssi_db_user, $scripturl, $ssi_db_passwd, $db_passwd;
- // Remember the current configuration so it can be set back.
- $ssi_magic_quotes_runtime = function_exists('get_magic_quotes_gpc') && get_magic_quotes_runtime();
- if (function_exists('set_magic_quotes_runtime'))
- @set_magic_quotes_runtime(0);
- $time_start = microtime(true);
- // Just being safe...
- foreach (array('db_character_set', 'cachedir') as $variable)
- if (isset($GLOBALS[$variable]))
- unset($GLOBALS[$variable]);
- // Get the forum's settings for database and file paths.
- require_once(dirname(__FILE__) . '/Settings.php');
- // Fix for using the current directory as a path.
- if (substr($sourcedir, 0, 1) == '.' && substr($sourcedir, 1, 1) != '.')
- $sourcedir = dirname(__FILE__) . substr($sourcedir, 1);
- // Make absolutely sure the new directories are defined.
- if ((empty($cachedir) || !file_exists($cachedir)) && file_exists($boarddir . '/cache'))
- $cachedir = $boarddir . '/cache';
- // Time to forget about variables and go with constants!
- DEFINE('BOARDDIR', $boarddir);
- DEFINE('CACHEDIR', $cachedir);
- DEFINE('EXTDIR', $extdir);
- DEFINE('LANGUAGEDIR', $languagedir);
- DEFINE('SOURCEDIR', $sourcedir);
- DEFINE('ADMINDIR', $sourcedir . '/admin');
- DEFINE('CONTROLLERDIR', $sourcedir . '/controllers');
- DEFINE('SUBSDIR', $sourcedir . '/subs');
- unset($boarddir, $cachedir, $sourcedir);
- $ssi_error_reporting = error_reporting(defined('E_STRICT') ? E_ALL | E_STRICT : E_ALL);
- /* Set this to one of three values depending on what you want to happen in the case of a fatal error.
- false: Default, will just load the error sub template and die - not putting any theme layers around it.
- true: Will load the error sub template AND put the template layers around it (Not useful if on total custom pages).
- string: Name of a callback function to call in the event of an error to allow you to define your own methods. Will die after function returns.
- */
- $ssi_on_error_method = false;
- // Don't do john didley if the forum's been shut down competely.
- if ($maintenance == 2 && (!isset($ssi_maintenance_off) || $ssi_maintenance_off !== true))
- die($mmessage);
- // Load the important includes.
- require_once(SOURCEDIR . '/QueryString.php');
- require_once(SOURCEDIR . '/Session.php');
- require_once(SOURCEDIR . '/Subs.php');
- require_once(SOURCEDIR . '/Errors.php');
- require_once(SOURCEDIR . '/Logging.php');
- require_once(SOURCEDIR . '/Load.php');
- require_once(SUBSDIR . '/Cache.subs.php');
- require_once(SOURCEDIR . '/Security.php');
- require_once(SOURCEDIR . '/BrowserDetect.class.php');
- // Create a variable to store some specific functions in.
- $smcFunc = array();
- // Initate the database connection and define some database functions to use.
- loadDatabase();
- // Load installed 'Mods' settings.
- reloadSettings();
- // Clean the request variables.
- cleanRequest();
- // Seed the random generator?
- if (empty($modSettings['rand_seed']) || mt_rand(1, 250) == 69)
- elk_seed_generator();
- // Check on any hacking attempts.
- if (isset($_REQUEST['GLOBALS']) || isset($_COOKIE['GLOBALS']))
- die('Hacking attempt...');
- elseif (isset($_REQUEST['ssi_theme']) && (int) $_REQUEST['ssi_theme'] == (int) $ssi_theme)
- die('Hacking attempt...');
- elseif (isset($_COOKIE['ssi_theme']) && (int) $_COOKIE['ssi_theme'] == (int) $ssi_theme)
- die('Hacking attempt...');
- elseif (isset($_REQUEST['ssi_layers'], $ssi_layers) && (@get_magic_quotes_gpc() ? stripslashes($_REQUEST['ssi_layers']) : $_REQUEST['ssi_layers']) == $ssi_layers)
- die('Hacking attempt...');
- if (isset($_REQUEST['context']))
- die('Hacking attempt...');
- // Gzip output? (because it must be boolean and true, this can't be hacked.)
- if (isset($ssi_gzip) && $ssi_gzip === true && ini_get('zlib.output_compression') != '1' && ini_get('output_handler') != 'ob_gzhandler' && version_compare(PHP_VERSION, '4.2.0', '>='))
- ob_start('ob_gzhandler');
- else
- $modSettings['enableCompressedOutput'] = '0';
- // Primarily, this is to fix the URLs...
- ob_start('ob_sessrewrite');
- // Start the session... known to scramble SSI includes in cases...
- if (!headers_sent())
- loadSession();
- else
- {
- if (isset($_COOKIE[session_name()]) || isset($_REQUEST[session_name()]))
- {
- // Make a stab at it, but ignore the E_WARNINGs generated because we can't send headers.
- $temp = error_reporting(error_reporting() & !E_WARNING);
- loadSession();
- error_reporting($temp);
- }
- if (!isset($_SESSION['session_value']))
- {
- $_SESSION['session_var'] = substr(md5(mt_rand() . session_id() . mt_rand()), 0, rand(7, 12));
- $_SESSION['session_value'] = md5(session_id() . mt_rand());
- }
- $sc = $_SESSION['session_value'];
- }
- // Get rid of $board and $topic... do stuff loadBoard would do.
- unset($board, $topic);
- $user_info['is_mod'] = false;
- $context['user']['is_mod'] = &$user_info['is_mod'];
- $context['linktree'] = array();
- // Load the user and their cookie, as well as their settings.
- loadUserSettings();
- // Load the current user's permissions....
- loadPermissions();
- // Load BadBehavior functions
- loadBadBehavior();
- // Load the current or SSI theme. (just use $ssi_theme = id_theme;)
- loadTheme(isset($ssi_theme) ? (int) $ssi_theme : 0);
- // @todo: probably not the best place, but somewhere it should be set...
- if (!headers_sent())
- header('Content-Type: text/html; charset=UTF-8');
- // Take care of any banning that needs to be done.
- if (isset($_REQUEST['ssi_ban']) || (isset($ssi_ban) && $ssi_ban === true))
- is_not_banned();
- // Do we allow guests in here?
- if (empty($ssi_guest_access) && empty($modSettings['allow_guestAccess']) && $user_info['is_guest'] && basename($_SERVER['PHP_SELF']) != 'SSI.php')
- {
- require_once(SUBSDIR . '/Auth.subs.php');
- KickGuest();
- obExit(null, true);
- }
- // Load the stuff like the menu bar, etc.
- if (isset($ssi_layers))
- {
- $context['template_layers'] = $ssi_layers;
- template_header();
- }
- else
- setupThemeContext();
- // Make sure they didn't muss around with the settings... but only if it's not cli.
- if (isset($_SERVER['REMOTE_ADDR']) && !isset($_SERVER['is_cli']) && session_id() == '')
- trigger_error($txt['ssi_session_broken'], E_USER_NOTICE);
- // Without visiting the forum this session variable might not be set on submit.
- if (!isset($_SESSION['USER_AGENT']) && (!isset($_GET['ssi_function']) || $_GET['ssi_function'] !== 'pollVote'))
- $_SESSION['USER_AGENT'] = $_SERVER['HTTP_USER_AGENT'];
- // Have the ability to easily add functions to SSI.
- call_integration_hook('integrate_SSI');
- // Call a function passed by GET.
- if (isset($_GET['ssi_function']) && function_exists('ssi_' . $_GET['ssi_function']) && (!empty($modSettings['allow_guestAccess']) || !$user_info['is_guest']))
- {
- call_user_func('ssi_' . $_GET['ssi_function']);
- exit;
- }
- if (isset($_GET['ssi_function']))
- exit;
- // You shouldn't just access SSI.php directly by URL!!
- elseif (basename($_SERVER['PHP_SELF']) == 'SSI.php')
- die(sprintf($txt['ssi_not_direct'], $user_info['is_admin'] ? '\'' . addslashes(__FILE__) . '\'' : '\'SSI.php\''));
- error_reporting($ssi_error_reporting);
- if (function_exists('set_magic_quotes_runtime'))
- @set_magic_quotes_runtime($ssi_magic_quotes_runtime);
- return true;
- /**
- * This shuts down the SSI and shows the footer.
- */
- function ssi_shutdown()
- {
- if (!isset($_GET['ssi_function']) || $_GET['ssi_function'] != 'shutdown')
- template_footer();
- }
- /**
- * Display a welcome message, like:
- * "Hey, User, you have 0 messages, 0 are new."
- *
- * @param string $output_method
- */
- function ssi_welcome($output_method = 'echo')
- {
- global $context, $txt, $scripturl;
- if ($output_method == 'echo')
- {
- if ($context['user']['is_guest'])
- echo sprintf($txt[$context['can_register'] ? 'welcome_guest_register' : 'welcome_guest'], $txt['guest_title'], $scripturl . '?action=login');
- else
- echo $txt['hello_member'], ' <strong>', $context['user']['name'], '</strong>', allowedTo('pm_read') ? ', ' . (empty($context['user']['messages']) ? $txt['msg_alert_no_messages'] : (($context['user']['messages'] == 1 ? sprintf($txt['msg_alert_one_message'], $scripturl . '?action=pm') : sprintf($txt['msg_alert_many_message'], $scripturl . '?action=pm', $context['user']['messages'])) . ', ' . ($context['user']['unread_messages'] == 1 ? $txt['msg_alert_one_new'] : sprintf($txt['msg_alert_many_new'], $context['user']['unread_messages'])))) : '';
- }
- // Don't echo... then do what?!
- else
- return $context['user'];
- }
- /**
- * Display a menu bar, like is displayed at the top of the forum.
- *
- * @param string $output_method
- */
- function ssi_menubar($output_method = 'echo')
- {
- global $context;
- if ($output_method == 'echo')
- template_menu();
- // What else could this do?
- else
- return $context['menu_buttons'];
- }
- /**
- * Show a logout link.
- *
- * @param string $redirect_to
- * @param string $output_method = 'echo'
- */
- function ssi_logout($redirect_to = '', $output_method = 'echo')
- {
- global $context, $txt, $scripturl;
- if ($redirect_to != '')
- $_SESSION['logout_url'] = $redirect_to;
- // Guests can't log out.
- if ($context['user']['is_guest'])
- return false;
- $link = '<a href="' . $scripturl . '?action=logout;' . $context['session_var'] . '=' . $context['session_id'] . '">' . $txt['logout'] . '</a>';
- if ($output_method == 'echo')
- echo $link;
- else
- return $link;
- }
- /**
- * Recent post list:
- * [board] Subject by Poster Date
- *
- * @param int $num_recent
- * @param array $exclude_boards
- * @param array $include_boards
- * @param string $output_method
- * @param bool $limit_body
- */
- function ssi_recentPosts($num_recent = 8, $exclude_boards = null, $include_boards = null, $output_method = 'echo', $limit_body = true)
- {
- global $context, $settings, $scripturl, $txt, $db_prefix, $user_info;
- global $modSettings, $smcFunc;
- // Excluding certain boards...
- if ($exclude_boards === null && !empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] > 0)
- $exclude_boards = array($modSettings['recycle_board']);
- else
- $exclude_boards = empty($exclude_boards) ? array() : (is_array($exclude_boards) ? $exclude_boards : array($exclude_boards));
- // What about including certain boards - note we do some protection here as pre-2.0 didn't have this parameter.
- if (is_array($include_boards) || (int) $include_boards === $include_boards)
- {
- $include_boards = is_array($include_boards) ? $include_boards : array($include_boards);
- }
- elseif ($include_boards != null)
- {
- $include_boards = array();
- }
- // Let's restrict the query boys (and girls)
- $query_where = '
- m.id_msg >= {int:min_message_id}
- ' . (empty($exclude_boards) ? '' : '
- AND b.id_board NOT IN ({array_int:exclude_boards})') . '
- ' . ($include_boards === null ? '' : '
- AND b.id_board IN ({array_int:include_boards})') . '
- AND {query_wanna_see_board}' . ($modSettings['postmod_active'] ? '
- AND m.approved = {int:is_approved}' : '');
- $query_where_params = array(
- 'is_approved' => 1,
- 'include_boards' => $include_boards === null ? '' : $include_boards,
- 'exclude_boards' => empty($exclude_boards) ? '' : $exclude_boards,
- 'min_message_id' => $modSettings['maxMsgID'] - 25 * min($num_recent, 5),
- );
- // Past to this simpleton of a function...
- return ssi_queryPosts($query_where, $query_where_params, $num_recent, 'm.id_msg DESC', $output_method, $limit_body);
- }
- /**
- * 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.
- *
- * @param array $post_ids
- * @param bool $override_permissions
- * @param string $output_method = 'echo'
- */
- function ssi_fetchPosts($post_ids = array(), $override_permissions = false, $output_method = 'echo')
- {
- global $user_info, $modSettings;
- if (empty($post_ids))
- return;
- // Allow the user to request more than one - why not?
- $post_ids = is_array($post_ids) ? $post_ids : array($post_ids);
- // Restrict the posts required...
- $query_where = '
- m.id_msg IN ({array_int:message_list})' . ($override_permissions ? '' : '
- AND {query_wanna_see_board}') . ($modSettings['postmod_active'] ? '
- AND m.approved = {int:is_approved}' : '');
- $query_where_params = array(
- 'message_list' => $post_ids,
- 'is_approved' => 1,
- );
- // Then make the query and dump the data.
- return ssi_queryPosts($query_where, $query_where_params, '', 'm.id_msg DESC', $output_method);
- }
- /**
- * This removes code duplication in other queries
- * - don't call it direct unless you really know what you're up to.
- *
- * @param string $query_where
- * @param array $query_where_params
- * @param int $query_limit
- * @param string $query_order
- * @param string $output_method = 'echo'
- * @param bool $limit_body
- * @param bool $override_permissions
- */
- function ssi_queryPosts($query_where = '', $query_where_params = array(), $query_limit = 10, $query_order = 'm.id_msg DESC', $output_method = 'echo', $limit_body = false, $override_permissions = false)
- {
- global $context, $settings, $scripturl, $txt, $db_prefix, $user_info;
- global $modSettings, $smcFunc;
- // Find all the posts. Newer ones will have higher IDs.
- $request = $smcFunc['db_query']('substring', '
- SELECT
- m.poster_time, m.subject, m.id_topic, m.id_member, m.id_msg, m.id_board, b.name AS board_name,
- IFNULL(mem.real_name, m.poster_name) AS poster_name, ' . ($user_info['is_guest'] ? '1 AS is_read, 0 AS new_from' : '
- IFNULL(lt.id_msg, IFNULL(lmr.id_msg, 0)) >= m.id_msg_modified AS is_read,
- IFNULL(lt.id_msg, IFNULL(lmr.id_msg, -1)) + 1 AS new_from') . ', ' . ($limit_body ? 'SUBSTRING(m.body, 1, 384) AS body' : 'm.body') . ', m.smileys_enabled
- FROM {db_prefix}messages AS m
- INNER JOIN {db_prefix}boards AS b ON (b.id_board = m.id_board)
- LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member)' . (!$user_info['is_guest'] ? '
- LEFT JOIN {db_prefix}log_topics AS lt ON (lt.id_topic = m.id_topic AND lt.id_member = {int:current_member})
- LEFT JOIN {db_prefix}log_mark_read AS lmr ON (lmr.id_board = m.id_board AND lmr.id_member = {int:current_member})' : '') . '
- WHERE 1=1 ' . ($override_permissions ? '' : '
- AND {query_wanna_see_board}') . ($modSettings['postmod_active'] ? '
- AND m.approved = {int:is_approved}' : '') . '
- ' . (empty($query_where) ? '' : 'AND ' . $query_where) . '
- ORDER BY ' . $query_order . '
- ' . ($query_limit == '' ? '' : 'LIMIT ' . $query_limit),
- array_merge($query_where_params, array(
- 'current_member' => $user_info['id'],
- 'is_approved' => 1,
- ))
- );
- $posts = array();
- while ($row = $smcFunc['db_fetch_assoc']($request))
- {
- $row['body'] = parse_bbc($row['body'], $row['smileys_enabled'], $row['id_msg']);
- // Censor it!
- censorText($row['subject']);
- censorText($row['body']);
- $preview = strip_tags(strtr($row['body'], array('<br />' => ' ')));
- // Build the array.
- $posts[] = array(
- 'id' => $row['id_msg'],
- 'board' => array(
- 'id' => $row['id_board'],
- 'name' => $row['board_name'],
- 'href' => $scripturl . '?board=' . $row['id_board'] . '.0',
- 'link' => '<a href="' . $scripturl . '?board=' . $row['id_board'] . '.0">' . $row['board_name'] . '</a>'
- ),
- 'topic' => $row['id_topic'],
- 'poster' => array(
- 'id' => $row['id_member'],
- 'name' => $row['poster_name'],
- 'href' => empty($row['id_member']) ? '' : $scripturl . '?action=profile;u=' . $row['id_member'],
- 'link' => empty($row['id_member']) ? $row['poster_name'] : '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['poster_name'] . '</a>'
- ),
- 'subject' => $row['subject'],
- 'short_subject' => shorten_subject($row['subject'], 25),
- 'preview' => $smcFunc['strlen']($preview) > 128 ? $smcFunc['substr']($preview, 0, 128) . '...' : $preview,
- 'body' => $row['body'],
- 'time' => timeformat($row['poster_time']),
- 'timestamp' => forum_time(true, $row['poster_time']),
- 'href' => $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . ';topicseen#new',
- 'link' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . '#msg' . $row['id_msg'] . '" rel="nofollow">' . $row['subject'] . '</a>',
- 'new' => !empty($row['is_read']),
- 'is_new' => empty($row['is_read']),
- 'new_from' => $row['new_from'],
- );
- }
- $smcFunc['db_free_result']($request);
- // Just return it.
- if ($output_method != 'echo' || empty($posts))
- return $posts;
- echo '
- <table border="0" class="ssi_table">';
- foreach ($posts as $post)
- echo '
- <tr>
- <td align="right" valign="top" nowrap="nowrap">
- [', $post['board']['link'], ']
- </td>
- <td valign="top">
- <a href="', $post['href'], '">', $post['subject'], '</a>
- ', $txt['by'], ' ', $post['poster']['link'], '
- ', $post['is_new'] ? '<a href="' . $scripturl . '?topic=' . $post['topic'] . '.msg' . $post['new_from'] . ';topicseen#new" rel="nofollow"><span class="new_posts">' . $txt['new'] . '</span></a>' : '', '
- </td>
- <td align="right" nowrap="nowrap">
- ', $post['time'], '
- </td>
- </tr>';
- echo '
- </table>';
- }
- /**
- * Recent topic list:
- * [board] Subject by Poster Date
- *
- * @param int $num_recent
- * @param array $exclude_boards
- * @param bool $include_boards
- * @param string $output_method = 'echo'
- */
- function ssi_recentTopics($num_recent = 8, $exclude_boards = null, $include_boards = null, $output_method = 'echo')
- {
- global $context, $settings, $scripturl, $txt, $db_prefix, $user_info;
- global $modSettings, $smcFunc;
- if ($exclude_boards === null && !empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] > 0)
- $exclude_boards = array($modSettings['recycle_board']);
- else
- $exclude_boards = empty($exclude_boards) ? array() : (is_array($exclude_boards) ? $exclude_boards : array($exclude_boards));
- // Only some boards?.
- if (is_array($include_boards) || (int) $include_boards === $include_boards)
- {
- $include_boards = is_array($include_boards) ? $include_boards : array($include_boards);
- }
- elseif ($include_boards != null)
- {
- $output_method = $include_boards;
- $include_boards = array();
- }
- $stable_icons = array('xx', 'thumbup', 'thumbdown', 'exclamation', 'question', 'lamp', 'smiley', 'angry', 'cheesy', 'grin', 'sad', 'wink', 'poll', 'moved', 'recycled', 'wireless');
- $icon_sources = array();
- foreach ($stable_icons as $icon)
- $icon_sources[$icon] = 'images_url';
- // Find all the posts in distinct topics. Newer ones will have higher IDs.
- $request = $smcFunc['db_query']('substring', '
- SELECT
- 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,
- IFNULL(mem.real_name, m.poster_name) AS poster_name, ' . ($user_info['is_guest'] ? '1 AS is_read, 0 AS new_from' : '
- IFNULL(lt.id_msg, IFNULL(lmr.id_msg, 0)) >= m.id_msg_modified AS is_read,
- IFNULL(lt.id_msg, IFNULL(lmr.id_msg, -1)) + 1 AS new_from') . ', SUBSTRING(m.body, 1, 384) AS body, m.smileys_enabled, m.icon
- FROM {db_prefix}topics AS t
- INNER JOIN {db_prefix}messages AS m ON (m.id_msg = t.id_last_msg)
- INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board)
- INNER JOIN {db_prefix}messages AS ms ON (ms.id_msg = t.id_first_msg)
- LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member)' . (!$user_info['is_guest'] ? '
- LEFT JOIN {db_prefix}log_topics AS lt ON (lt.id_topic = t.id_topic AND lt.id_member = {int:current_member})
- LEFT JOIN {db_prefix}log_mark_read AS lmr ON (lmr.id_board = b.id_board AND lmr.id_member = {int:current_member})' : '') . '
- WHERE t.id_last_msg >= {int:min_message_id}
- ' . (empty($exclude_boards) ? '' : '
- AND b.id_board NOT IN ({array_int:exclude_boards})') . '
- ' . (empty($include_boards) ? '' : '
- AND b.id_board IN ({array_int:include_boards})') . '
- AND {query_wanna_see_board}' . ($modSettings['postmod_active'] ? '
- AND t.approved = {int:is_approved}
- AND m.approved = {int:is_approved}' : '') . '
- ORDER BY t.id_last_msg DESC
- LIMIT ' . $num_recent,
- array(
- 'current_member' => $user_info['id'],
- 'include_boards' => empty($include_boards) ? '' : $include_boards,
- 'exclude_boards' => empty($exclude_boards) ? '' : $exclude_boards,
- 'min_message_id' => $modSettings['maxMsgID'] - 35 * min($num_recent, 5),
- 'is_approved' => 1,
- )
- );
- $posts = array();
- while ($row = $smcFunc['db_fetch_assoc']($request))
- {
- $row['body'] = strip_tags(strtr(parse_bbc($row['body'], $row['smileys_enabled'], $row['id_msg']), array('<br />' => ' ')));
- if ($smcFunc['strlen']($row['body']) > 128)
- $row['body'] = $smcFunc['substr']($row['body'], 0, 128) . '...';
- // Censor the subject.
- censorText($row['subject']);
- censorText($row['body']);
- if (!empty($modSettings['messageIconChecks_enable']) && !isset($icon_sources[$row['icon']]))
- $icon_sources[$row['icon']] = file_exists($settings['theme_dir'] . '/images/post/' . $row['icon'] . '.png') ? 'images_url' : 'default_images_url';
- // Build the array.
- $posts[] = array(
- 'board' => array(
- 'id' => $row['id_board'],
- 'name' => $row['board_name'],
- 'href' => $scripturl . '?board=' . $row['id_board'] . '.0',
- 'link' => '<a href="' . $scripturl . '?board=' . $row['id_board'] . '.0">' . $row['board_name'] . '</a>'
- ),
- 'topic' => $row['id_topic'],
- 'poster' => array(
- 'id' => $row['id_member'],
- 'name' => $row['poster_name'],
- 'href' => empty($row['id_member']) ? '' : $scripturl . '?action=profile;u=' . $row['id_member'],
- 'link' => empty($row['id_member']) ? $row['poster_name'] : '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['poster_name'] . '</a>'
- ),
- 'subject' => $row['subject'],
- 'replies' => $row['num_replies'],
- 'views' => $row['num_views'],
- 'short_subject' => shorten_subject($row['subject'], 25),
- 'preview' => $row['body'],
- 'time' => timeformat($row['poster_time']),
- 'timestamp' => forum_time(true, $row['poster_time']),
- 'href' => $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . ';topicseen#new',
- 'link' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . '#new" rel="nofollow">' . $row['subject'] . '</a>',
- // Retained for compatibility - is technically incorrect!
- 'new' => !empty($row['is_read']),
- 'is_new' => empty($row['is_read']),
- 'new_from' => $row['new_from'],
- 'icon' => '<img src="' . $settings[$icon_sources[$row['icon']]] . '/post/' . $row['icon'] . '.png" align="middle" alt="' . $row['icon'] . '" />',
- );
- }
- $smcFunc['db_free_result']($request);
- // Just return it.
- if ($output_method != 'echo' || empty($posts))
- return $posts;
- echo '
- <table border="0" class="ssi_table">';
- foreach ($posts as $post)
- echo '
- <tr>
- <td align="right" valign="top" nowrap="nowrap">
- [', $post['board']['link'], ']
- </td>
- <td valign="top">
- <a href="', $post['href'], '">', $post['subject'], '</a>
- ', $txt['by'], ' ', $post['poster']['link'], '
- ', !$post['is_new'] ? '' : '<a href="' . $scripturl . '?topic=' . $post['topic'] . '.msg' . $post['new_from'] . ';topicseen#new" rel="nofollow"><span class="new_posts">' . $txt['new'] . '</span></a>', '
- </td>
- <td align="right" nowrap="nowrap">
- ', $post['time'], '
- </td>
- </tr>';
- echo '
- </table>';
- }
- /**
- * Show the top poster's name and profile link.
- *
- * @param int $topNumber
- * @param string $output_method = 'echo'
- */
- function ssi_topPoster($topNumber = 1, $output_method = 'echo')
- {
- global $db_prefix, $scripturl, $smcFunc;
- // Find the latest poster.
- $request = $smcFunc['db_query']('', '
- SELECT id_member, real_name, posts
- FROM {db_prefix}members
- ORDER BY posts DESC
- LIMIT ' . $topNumber,
- array(
- )
- );
- $return = array();
- while ($row = $smcFunc['db_fetch_assoc']($request))
- $return[] = array(
- 'id' => $row['id_member'],
- 'name' => $row['real_name'],
- 'href' => $scripturl . '?action=profile;u=' . $row['id_member'],
- 'link' => '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['real_name'] . '</a>',
- 'posts' => $row['posts']
- );
- $smcFunc['db_free_result']($request);
- // Just return all the top posters.
- if ($output_method != 'echo')
- return $return;
- // Make a quick array to list the links in.
- $temp_array = array();
- foreach ($return as $member)
- $temp_array[] = $member['link'];
- echo implode(', ', $temp_array);
- }
- /**
- * Show boards by activity.
- *
- * @param int $num_top
- * @param string $output_method = 'echo'
- */
- function ssi_topBoards($num_top = 10, $output_method = 'echo')
- {
- global $context, $settings, $db_prefix, $txt, $scripturl, $user_info, $modSettings, $smcFunc;
- // Find boards with lots of posts.
- $request = $smcFunc['db_query']('', '
- SELECT
- b.name, b.num_topics, b.num_posts, b.id_board,' . (!$user_info['is_guest'] ? ' 1 AS is_read' : '
- (IFNULL(lb.id_msg, 0) >= b.id_last_msg) AS is_read') . '
- FROM {db_prefix}boards AS b
- LEFT JOIN {db_prefix}log_boards AS lb ON (lb.id_board = b.id_board AND lb.id_member = {int:current_member})
- WHERE {query_wanna_see_board}' . (!empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] > 0 ? '
- AND b.id_board != {int:recycle_board}' : '') . '
- ORDER BY b.num_posts DESC
- LIMIT ' . $num_top,
- array(
- 'current_member' => $user_info['id'],
- 'recycle_board' => (int) $modSettings['recycle_board'],
- )
- );
- $boards = array();
- while ($row = $smcFunc['db_fetch_assoc']($request))
- $boards[] = array(
- 'id' => $row['id_board'],
- 'num_posts' => $row['num_posts'],
- 'num_topics' => $row['num_topics'],
- 'name' => $row['name'],
- 'new' => empty($row['is_read']),
- 'href' => $scripturl . '?board=' . $row['id_board'] . '.0',
- 'link' => '<a href="' . $scripturl . '?board=' . $row['id_board'] . '.0">' . $row['name'] . '</a>'
- );
- $smcFunc['db_free_result']($request);
- // If we shouldn't output or have nothing to output, just jump out.
- if ($output_method != 'echo' || empty($boards))
- return $boards;
- echo '
- <table class="ssi_table">
- <tr>
- <th align="left">', $txt['board'], '</th>
- <th align="left">', $txt['board_topics'], '</th>
- <th align="left">', $txt['posts'], '</th>
- </tr>';
- foreach ($boards as $board)
- echo '
- <tr>
- <td>', $board['link'], $board['new'] ? ' <a href="' . $board['href'] . '"><span class="new_posts">' . $txt['new'] . '</span></a>' : '', '</td>
- <td align="right">', comma_format($board['num_topics']), '</td>
- <td align="right">', comma_format($board['num_posts']), '</td>
- </tr>';
- echo '
- </table>';
- }
- /**
- * Shows the top topics.
- *
- * @param string $type
- * @param 10 $num_topics
- * @param string $output_method = 'echo'
- */
- function ssi_topTopics($type = 'replies', $num_topics = 10, $output_method = 'echo')
- {
- global $db_prefix, $txt, $scripturl, $user_info, $modSettings, $smcFunc, $context;
- if ($modSettings['totalMessages'] > 100000)
- {
- // @todo Why don't we use {query(_wanna)_see_board}?
- $request = $smcFunc['db_query']('', '
- SELECT id_topic
- FROM {db_prefix}topics
- WHERE num_' . ($type != 'replies' ? 'views' : 'replies') . ' != 0' . ($modSettings['postmod_active'] ? '
- AND approved = {int:is_approved}' : '') . '
- ORDER BY num_' . ($type != 'replies' ? 'views' : 'replies') . ' DESC
- LIMIT {int:limit}',
- array(
- 'is_approved' => 1,
- 'limit' => $num_topics > 100 ? ($num_topics + ($num_topics / 2)) : 100,
- )
- );
- $topic_ids = array();
- while ($row = $smcFunc['db_fetch_assoc']($request))
- $topic_ids[] = $row['id_topic'];
- $smcFunc['db_free_result']($request);
- }
- else
- $topic_ids = array();
- $request = $smcFunc['db_query']('', '
- SELECT m.subject, m.id_topic, t.num_views, t.num_replies
- FROM {db_prefix}topics AS t
- INNER JOIN {db_prefix}messages AS m ON (m.id_msg = t.id_first_msg)
- INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board)
- WHERE {query_wanna_see_board}' . ($modSettings['postmod_active'] ? '
- AND t.approved = {int:is_approved}' : '') . (!empty($topic_ids) ? '
- AND t.id_topic IN ({array_int:topic_list})' : '') . (!empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] > 0 ? '
- AND b.id_board != {int:recycle_enable}' : '') . '
- ORDER BY t.num_' . ($type != 'replies' ? 'views' : 'replies') . ' DESC
- LIMIT {int:limit}',
- array(
- 'topic_list' => $topic_ids,
- 'is_approved' => 1,
- 'recycle_enable' => $modSettings['recycle_board'],
- 'limit' => $num_topics,
- )
- );
- $topics = array();
- while ($row = $smcFunc['db_fetch_assoc']($request))
- {
- censorText($row['subject']);
- $topics[] = array(
- 'id' => $row['id_topic'],
- 'subject' => $row['subject'],
- 'num_replies' => $row['num_replies'],
- 'num_views' => $row['num_views'],
- 'href' => $scripturl . '?topic=' . $row['id_topic'] . '.0',
- 'link' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.0">' . $row['subject'] . '</a>',
- );
- }
- $smcFunc['db_free_result']($request);
- if ($output_method != 'echo' || empty($topics))
- return $topics;
- echo '
- <table class="ssi_table">
- <tr>
- <th align="left"></th>
- <th align="left">', $txt['views'], '</th>
- <th align="left">', $txt['replies'], '</th>
- </tr>';
- foreach ($topics as $topic)
- echo '
- <tr>
- <td align="left">
- ', $topic['link'], '
- </td>
- <td align="right">', comma_format($topic['num_views']), '</td>
- <td align="right">', comma_format($topic['num_replies']), '</td>
- </tr>';
- echo '
- </table>';
- }
- /**
- * Shows the top topics, by replies.
- *
- * @param int $num_topics = 10
- * @param string $output_method = 'echo'
- */
- function ssi_topTopicsReplies($num_topics = 10, $output_method = 'echo')
- {
- return ssi_topTopics('replies', $num_topics, $output_method);
- }
- /**
- * Shows the top topics, by views.
- *
- * @param int $num_topics = 10
- * @param string $output_method = 'echo'
- */
- function ssi_topTopicsViews($num_topics = 10, $output_method = 'echo')
- {
- return ssi_topTopics('views', $num_topics, $output_method);
- }
- /**
- * Show a link to the latest member:
- * Please welcome, Someone, out latest member.
- *
- * @param string $output_method = 'echo'
- */
- function ssi_latestMember($output_method = 'echo')
- {
- global $db_prefix, $txt, $scripturl, $context;
- if ($output_method == 'echo')
- echo '
- ', sprintf($txt['welcome_newest_member'], $context['common_stats']['latest_member']['link']), '<br />';
- else
- return $context['common_stats']['latest_member'];
- }
- /**
- * Fetch a random member - if type set to 'day' will only change once a day!
- *
- * @param string $random_type = ''
- * @param string $output_method = 'echo'
- */
- function ssi_randomMember($random_type = '', $output_method = 'echo')
- {
- global $modSettings;
- // If we're looking for something to stay the same each day then seed the generator.
- if ($random_type == 'day')
- {
- // Set the seed to change only once per day.
- mt_srand(floor(time() / 86400));
- }
- // Get the lowest ID we're interested in.
- $member_id = mt_rand(1, $modSettings['latestMember']);
- $where_query = '
- id_member >= {int:selected_member}
- AND is_activated = {int:is_activated}';
- $query_where_params = array(
- 'selected_member' => $member_id,
- 'is_activated' => 1,
- );
- $result = ssi_queryMembers($where_query, $query_where_params, 1, 'id_member ASC', $output_method);
- // If we got nothing do the reverse - in case of unactivated members.
- if (empty($result))
- {
- $where_query = '
- id_member <= {int:selected_member}
- AND is_activated = {int:is_activated}';
- $query_where_params = array(
- 'selected_member' => $member_id,
- 'is_activated' => 1,
- );
- $result = ssi_queryMembers($where_query, $query_where_params, 1, 'id_member DESC', $output_method);
- }
- // Just to be sure put the random generator back to something... random.
- if ($random_type != '')
- mt_srand(time());
- return $result;
- }
- /**
- * Fetch a specific member.
- *
- * @param array $member_ids = array()
- * @param string $output_method = 'echo'
- */
- function ssi_fetchMember($member_ids = array(), $output_method = 'echo')
- {
- if (empty($member_ids))
- return;
- // Can have more than one member if you really want...
- $member_ids = is_array($member_ids) ? $member_ids : array($member_ids);
- // Restrict it right!
- $query_where = '
- id_member IN ({array_int:member_list})';
- $query_where_params = array(
- 'member_list' => $member_ids,
- );
- // Then make the query and dump the data.
- return ssi_queryMembers($query_where, $query_where_params, '', 'id_member', $output_method);
- }
- /**
- * Fetch a specific member.
- *
- * @param null $group_id
- * @param string $output_method = 'echo'
- */
- function ssi_fetchGroupMembers($group_id = null, $output_method = 'echo')
- {
- if ($group_id === null)
- return;
- $query_where = '
- id_group = {int:id_group}
- OR id_post_group = {int:id_group}
- OR FIND_IN_SET({int:id_group}, additional_groups)';
- $query_where_params = array(
- 'id_group' => $group_id,
- );
- return ssi_queryMembers($query_where, $query_where_params, '', 'real_name', $output_method);
- }
- /**
- * Fetch some member data!
- *
- * @param string $query_where
- * @param string $query_where_params
- * @param string $query_limit
- * @param string $query_order
- * @param string $output_method
- */
- function ssi_queryMembers($query_where = null, $query_where_params = array(), $query_limit = '', $query_order = 'id_member DESC', $output_method = 'echo')
- {
- global $context, $settings, $scripturl, $txt, $db_prefix, $user_info;
- global $modSettings, $smcFunc, $memberContext;
- if ($query_where === null)
- return;
- // Fetch the members in question.
- $request = $smcFunc['db_query']('', '
- SELECT id_member
- FROM {db_prefix}members
- WHERE ' . $query_where . '
- ORDER BY ' . $query_order . '
- ' . ($query_limit == '' ? '' : 'LIMIT ' . $query_limit),
- array_merge($query_where_params, array(
- ))
- );
- $members = array();
- while ($row = $smcFunc['db_fetch_assoc']($request))
- $members[] = $row['id_member'];
- $smcFunc['db_free_result']($request);
- if (empty($members))
- return array();
- // Load the members.
- loadMemberData($members);
- // Draw the table!
- if ($output_method == 'echo')
- echo '
- <table border="0" class="ssi_table">';
- $query_members = array();
- foreach ($members as $member)
- {
- // Load their context data.
- if (!loadMemberContext($member))
- continue;
- // Store this member's information.
- $query_members[$member] = $memberContext[$member];
- // Only do something if we're echo'ing.
- if ($output_method == 'echo')
- echo '
- <tr>
- <td align="right" valign="top" nowrap="nowrap">
- ', $query_members[$member]['link'], '
- <br />', $query_members[$member]['blurb'], '
- <br />', $query_members[$member]['avatar']['image'], '
- </td>
- </tr>';
- }
- // End the table if appropriate.
- if ($output_method == 'echo')
- echo '
- </table>';
- // Send back the data.
- return $query_members;
- }
- /**
- * Show some basic stats: Total This: XXXX, etc.
- *
- * @param string $output_method
- */
- function ssi_boardStats($output_method = 'echo')
- {
- global $db_prefix, $txt, $scripturl, $modSettings, $smcFunc;
- if (!allowedTo('view_stats'))
- return;
- $totals = array(
- 'members' => $modSettings['totalMembers'],
- 'posts' => $modSettings['totalMessages'],
- 'topics' => $modSettings['totalTopics']
- );
- $result = $smcFunc['db_query']('', '
- SELECT COUNT(*)
- FROM {db_prefix}boards',
- array(
- )
- );
- list ($totals['boards']) = $smcFunc['db_fetch_row']($result);
- $smcFunc['db_free_result']($result);
- $result = $smcFunc['db_query']('', '
- SELECT COUNT(*)
- FROM {db_prefix}categories',
- array(
- )
- );
- list ($totals['categories']) = $smcFunc['db_fetch_row']($result);
- $smcFunc['db_free_result']($result);
- if ($output_method != 'echo')
- return $totals;
- echo '
- ', $txt['total_members'], ': <a href="', $scripturl . '?action=memberlist">', comma_format($totals['members']), '</a><br />
- ', $txt['total_posts'], ': ', comma_format($totals['posts']), '<br />
- ', $txt['total_topics'], ': ', comma_format($totals['topics']), ' <br />
- ', $txt['total_cats'], ': ', comma_format($totals['categories']), '<br />
- ', $txt['total_boards'], ': ', comma_format($totals['boards']);
- }
- /**
- * Shows a list of online users:
- * YY Guests, ZZ Users and then a list...
- *
- * @param string $output_method
- */
- function ssi_whosOnline($output_method = 'echo')
- {
- global $user_info, $txt, $settings, $modSettings;
- require_once(SUBSDIR . '/MembersOnline.subs.php');
- $membersOnlineOptions = array(
- 'show_hidden' => allowedTo('moderate_forum'),
- );
- $return = getMembersOnlineStats($membersOnlineOptions);
- // Add some redundancy for backwards compatibility reasons.
- if ($output_method != 'echo')
- return $return + array(
- 'users' => $return['users_online'],
- 'guests' => $return['num_guests'],
- 'hidden' => $return['num_users_hidden'],
- 'buddies' => $return['num_buddies'],
- 'num_users' => $return['num_users_online'],
- 'total_users' => $return['num_users_online'] + $return['num_guests'] + $return['num_spiders'],
- );
- echo '
- ', 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'];
- $bracketList = array();
- if (!empty($user_info['buddies']))
- $bracketList[] = comma_format($return['num_buddies']) . ' ' . ($return['num_buddies'] == 1 ? $txt['buddy'] : $txt['buddies']);
- if (!empty($return['num_spiders']))
- $bracketList[] = comma_format($return['num_spiders']) . ' ' . ($return['num_spiders'] == 1 ? $txt['spider'] : $txt['spiders']);
- if (!empty($return['num_users_hidden']))
- $bracketList[] = comma_format($return['num_users_hidden']) . ' ' . $txt['hidden'];
- if (!empty($bracketList))
- echo ' (' . implode(', ', $bracketList) . ')';
- echo '<br />
- ', implode(', ', $return['list_users_online']);
- // Showing membergroups?
- if (!empty($settings['show_group_key']) && !empty($return['membergroups']))
- echo '<br />
- [' . implode('] [', $return['membergroups']) . ']';
- }
- /**
- * Just like whosOnline except it also logs the online presence.
- *
- * @param string $output_method
- */
- function ssi_logOnline($output_method = 'echo')
- {
- writeLog();
- if ($output_method != 'echo')
- return ssi_whosOnline($output_method);
- else
- ssi_whosOnline($output_method);
- }
- /**
- * Shows a login box.
- *
- * @param string $redirect_to = ''
- * @param string $output_method = 'echo'
- */
- function ssi_login($redirect_to = '', $output_method = 'echo')
- {
- global $scripturl, $txt, $user_info, $context, $modSettings;
- if ($redirect_to != '')
- $_SESSION['login_url'] = $redirect_to;
- if ($output_method != 'echo' || !$user_info['is_guest'])
- return $user_info['is_guest'];
- echo '
- <form action="', $scripturl, '?action=login2" method="post" accept-charset="UTF-8">
- <table border="0" cellspacing="1" cellpadding="0" class="ssi_table">
- <tr>
- <td align="right"><label for="user">', $txt['username'], ':</label> </td>
- <td><input type="text" id="user" name="user" size="9" value="', $user_info['username'], '" class="input_text" /></td>
- </tr><tr>
- <td align="right"><label for="passwrd">', $txt['password'], ':</label> </td>
- <td><input type="password" name="passwrd" id="passwrd" size="9" class="input_password" /></td>
- </tr>';
- // Open ID?
- if (!empty($modSettings['enableOpenID']))
- echo '<tr>
- <td colspan="2" align="center"><strong>—', $txt['or'], '—</strong></td>
- </tr><tr>
- <td align="right"><label for="openid_url">', $txt['openid'], ':</label> </td>
- <td><input type="text" name="openid_identifier" id="openid_url" class="input_text openid_login" size="17" /></td>
- </tr>';
- echo '<tr>
- <td><input type="hidden" name="cookielength" value="-1" /></td>
- <td><input type="submit" value="', $txt['login'], '" class="button_submit" /></td>
- </tr>
- </table>
- </form>';
- }
- /**
- * Show the most-voted-in poll.
- *
- * @param string $output_method = 'echo'
- */
- function ssi_topPoll($output_method = 'echo')
- {
- // Just use recentPoll, no need to duplicate code...
- return ssi_recentPoll(true, $output_method);
- }
- /**
- * Show the most recently posted poll.
- *
- * @param bool $topPollInstead = false
- * @param string $output_method = string
- */
- function ssi_recentPoll($topPollInstead = false, $output_method = 'echo')
- {
- global $db_prefix, $txt, $settings, $boardurl, $user_info, $context, $smcFunc, $modSettings;
- $boardsAllowed = array_intersect(boardsAllowedTo('poll_view'), boardsAllowedTo('poll_vote'));
- if (empty($boardsAllowed))
- return array();
- $request = $smcFunc['db_query']('', '
- SELECT p.id_poll, p.question, t.id_topic, p.max_votes, p.guest_vote, p.hide_results, p.expire_time
- FROM {db_prefix}polls AS p
- INNER JOIN {db_prefix}topics AS t ON (t.id_poll = p.id_poll' . ($modSettings['postmod_active'] ? ' AND t.approved = {int:is_approved}' : '') . ')
- INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board)' . ($topPollInstead ? '
- INNER JOIN {db_prefix}poll_choices AS pc ON (pc.id_poll = p.id_poll)' : '') . '
- 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})
- WHERE p.voting_locked = {int:voting_opened}
- AND (p.expire_time = {int:no_expiration} OR {int:current_time} < p.expire_time)
- AND ' . ($user_info['is_guest'] ? 'p.guest_vote = {int:guest_vote_allowed}' : 'lp.id_choice IS NULL') . '
- AND {query_wanna_see_board}' . (!in_array(0, $boardsAllowed) ? '
- AND b.id_board IN ({array_int:boards_allowed_list})' : '') . (!empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] > 0 ? '
- AND b.id_board != {int:recycle_enable}' : '') . '
- ORDER BY ' . ($topPollInstead ? 'pc.votes' : 'p.id_poll') . ' DESC
- LIMIT 1',
- array(
- 'current_member' => $user_info['id'],
- 'boards_allowed_list' => $boardsAllowed,
- 'is_approved' => 1,
- 'guest_vote_allowed' => 1,
- 'no_member' => 0,
- 'voting_opened' => 0,
- 'no_expiration' => 0,
- 'current_time' => time(),
- 'recycle_enable' => $modSettings['recycle_board'],
- )
- );
- $row = $smcFunc['db_fetch_assoc']($request);
- $smcFunc['db_free_result']($request);
- // This user has voted on all the polls.
- if ($row === false)
- return array();
- // If this is a guest who's voted we'll through ourselves to show poll to show the results.
- if ($user_info['is_guest'] && (!$row['guest_vote'] || (isset($_COOKIE['guest_poll_vote']) && in_array($row['id_poll'], explode(',', $_COOKIE['guest_poll_vote'])))))
- return ssi_showPoll($row['id_topic'], $output_method);
- $request = $smcFunc['db_query']('', '
- SELECT COUNT(DISTINCT id_member)
- FROM {db_prefix}log_polls
- WHERE id_poll = {int:current_poll}',
- array(
- 'current_poll' => $row['id_poll'],
- )
- );
- list ($total) = $smcFunc['db_fetch_row']($request);
- $smcFunc['db_free_result']($request);
- $request = $smcFunc['db_query']('', '
- SELECT id_choice, label, votes
- FROM {db_prefix}poll_choices
- WHERE id_poll = {int:current_poll}',
- array(
- 'current_poll' => $row['id_poll'],
- )
- );
- $options = array();
- while ($rowChoice = $smcFunc['db_fetch_assoc']($request))
- {
- censorText($rowChoice['label']);
- $options[$rowChoice['id_choice']] = array($rowChoice['label'], $rowChoice['votes']);
- }
- $smcFunc['db_free_result']($request);
- // Can they view it?
- $is_expired = !empty($row['expire_time']) && $row['expire_time'] < time();
- $allow_view_results = allowedTo('moderate_board') || $row['hide_results'] == 0 || $is_expired;
- $return = array(
- 'id' => $row['id_poll'],
- 'image' => 'poll',
- 'question' => $row['question'],
- 'total_votes' => $total,
- 'is_locked' => false,
- 'topic' => $row['id_topic'],
- 'allow_view_results' => $allow_view_results,
- 'options' => array()
- );
- // Calculate the percentages and bar lengths...
- $divisor = $return['total_votes'] == 0 ? 1 : $return['total_votes'];
- foreach ($options as $i => $option)
- {
- $bar = floor(($option[1] * 100) / $divisor);
- $barWide = $bar == 0 ? 1 : floor(($bar * 5) / 3);
- $return['options'][$i] = array(
- 'id' => 'options-' . ($topPollInstead ? 'top-' : 'recent-') . $i,
- 'percent' => $bar,
- 'votes' => $option[1],
- 'bar' => '<span style="white-space: nowrap;"><img src="' . $settings['images_url'] . '/poll_' . ($context['right_to_left'] ? 'right' : 'left') . '.png" alt="" /><img src="' . $settings['images_url'] . '/poll_middle.png" width="' . $barWide . '" height="12" alt="-" /><img src="' . $settings['images_url'] . '/poll_' . ($context['right_to_left'] ? 'left' : 'right') . '.png" alt="" /></span>',
- 'option' => parse_bbc($option[0]),
- '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') . '" />'
- );
- }
- $return['allowed_warning'] = $row['max_votes'] > 1 ? sprintf($txt['poll_options6'], min(count($options), $row['max_votes'])) : '';
- if ($output_method != 'echo')
- return $return;
- if ($allow_view_results)
- {
- echo '
- <form class="ssi_poll" action="', $boardurl, '/SSI.php?ssi_function=pollVote" method="post" accept-charset="UTF-8">
- <strong>', $return['question'], '</strong><br />
- ', !empty($return['allowed_warning']) ? $return['allowed_warning'] . '<br />' : '';
- foreach ($return['options'] as $option)
- echo '
- <label for="', $option['id'], '">', $option['vote_button'], ' ', $option['option'], '</label><br />';
- echo '
- <input type="submit" value="', $txt['poll_vote'], '" class="button_submit" />
- <input type="hidden" name="poll" value="', $return['id'], '" />
- <input type="hidden" name="', $context['session_var'], '" value="', $context['session_id'], '" />
- </form>';
- }
- else
- echo $txt['poll_cannot_see'];
- }
- /**
- * Show a poll.
- *
- * @param int $topic = null
- * @param string $output_method = 'echo'
- */
- function ssi_showPoll($topic = null, $output_method = 'echo')
- {
- global $db_prefix, $txt, $settings, $boardurl, $user_info, $context, $smcFunc, $modSettings;
- $boardsAllowed = boardsAllowedTo('poll_view');
- if (empty($boardsAllowed))
- return array();
- if ($topic === null && isset($_REQUEST['ssi_topic']))
- $topic = (int) $_REQUEST['ssi_topic'];
- else
- $topic = (int) $topic;
- $request = $smcFunc['db_query']('', '
- SELECT
- p.id_poll, p.question, p.voting_locked, p.hide_results, p.expire_time, p.max_votes, p.guest_vote, b.id_board
- FROM {db_prefix}topics AS t
- INNER JOIN {db_prefix}polls AS p ON (p.id_poll = t.id_poll)
- INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board)
- WHERE t.id_topic = {int:current_topic}
- AND {query_see_board}' . (!in_array(0, $boardsAllowed) ? '
- AND b.id_board IN ({array_int:boards_allowed_see})' : '') . ($modSettings['postmod_active'] ? '
- AND t.approved = {int:is_approved}' : '') . '
- LIMIT 1',
- array(
- 'current_topic' => $topic,
- 'boards_allowed_see' => $boardsAllowed,
- 'is_approved' => 1,
- )
- );
- // Either this topic has no poll, or the user cannot view it.
- if ($smcFunc['db_num_rows']($request) == 0)
- return array();
- $row = $smcFunc['db_fetch_assoc']($request);
- $smcFunc['db_free_result']($request);
- // Check if they can vote.
- if (!empty($row['expire_time']) && $row['expire_time'] < time())
- $allow_vote = false;
- elseif ($user_info['is_guest'] && $row['guest_vote'] && (!isset($_COOKIE['guest_poll_vote']) || !in_array($row['id_poll'], explode(',', $_COOKIE['guest_poll_vote']))))
- $allow_vote = true;
- elseif ($user_info['is_guest'])
- $allow_vote = false;
- elseif (!empty($row['voting_locked']) || !allowedTo('poll_vote', $row['id_board']))
- $allow_vote = false;
- else
- {
- $request = $smcFunc['db_query']('', '
- SELECT id_member
- FROM {db_prefix}log_polls
- WHERE id_poll = {int:current_poll}
- AND id_member = {int:current_member}
- LIMIT 1',
- array(
- 'current_member' => $user_info['id'],
- 'current_poll' => $row['id_poll'],
- )
- );
- $allow_vote = $smcFunc['db_num_rows']($request) == 0;
- $smcFunc['db_free_result']($request);
- }
- // Can they view?
- $is_expired = !empty($row['expire_time']) && $row['expire_time'] < time();
- $allow_view_results = allowedTo('moderate_board') || $row['hide_results'] == 0 || ($row['hide_results'] == 1 && !$allow_vote) || $is_expired;
- $request = $smcFunc['db_query']('', '
- SELECT COUNT(DISTINCT id_member)
- FROM {db_prefix}log_polls
- WHERE id_poll = {int:current_poll}',
- array(
- 'current_poll' => $row['id_poll'],
- )
- );
- list ($total) = $smcFunc['db_fetch_row']($request);
- $smcFunc['db_free_result']($request);
- $request = $smcFunc['db_query']('', '
- SELECT id_choice, label, votes
- FROM {db_prefix}poll_choices
- WHERE id_poll = {int:current_poll}',
- array(
- 'current_poll' => $row['id_poll'],
- )
- );
- $options = array();
- $total_votes = 0;
- while ($rowChoice = $smcFunc['db_fetch_assoc']($request))
- {
- censorText($rowChoice['label']);
- $options[$rowChoice['id_choice']] = array($rowChoice['label'], $rowChoice['votes']);
- $total_votes += $rowChoice['votes'];
- }
- $smcFunc['db_free_result']($request);
- $return = array(
- 'id' => $row['id_poll'],
- 'image' => empty($row['voting_locked']) ? 'poll' : 'locked_poll',
- 'question' => $row['question'],
- 'total_votes' => $total,
- 'is_locked' => !empty($row['voting_locked']),
- 'allow_…
Large files files are truncated, but you can click here to view the full file