PageRenderTime 57ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/php/SSI.php

https://github.com/dekoza/openshift-smf-2.0.7
PHP | 1999 lines | 1557 code | 273 blank | 169 comment | 306 complexity | 53c5c669d3eb1b9ac0f89451a22e891a MD5 | raw file
Possible License(s): BSD-3-Clause

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**
  3. * Simple Machines Forum (SMF)
  4. *
  5. * @package SMF
  6. * @author Simple Machines http://www.simplemachines.org
  7. * @copyright 2011 Simple Machines
  8. * @license http://www.simplemachines.org/about/smf/license.php BSD
  9. *
  10. * @version 2.0.7
  11. */
  12. // Don't do anything if SMF is already loaded.
  13. if (defined('SMF'))
  14. return true;
  15. define('SMF', 'SSI');
  16. // We're going to want a few globals... these are all set later.
  17. global $time_start, $maintenance, $msubject, $mmessage, $mbname, $language;
  18. global $boardurl, $boarddir, $sourcedir, $webmaster_email, $cookiename;
  19. global $db_server, $db_name, $db_user, $db_prefix, $db_persist, $db_error_send, $db_last_error;
  20. global $db_connection, $modSettings, $context, $sc, $user_info, $topic, $board, $txt;
  21. global $smcFunc, $ssi_db_user, $scripturl, $ssi_db_passwd, $db_passwd, $cachedir;
  22. // Remember the current configuration so it can be set back.
  23. $ssi_magic_quotes_runtime = function_exists('get_magic_quotes_gpc') && get_magic_quotes_runtime();
  24. if (function_exists('set_magic_quotes_runtime'))
  25. @set_magic_quotes_runtime(0);
  26. $time_start = microtime();
  27. // Just being safe...
  28. foreach (array('db_character_set', 'cachedir') as $variable)
  29. if (isset($GLOBALS[$variable]))
  30. unset($GLOBALS[$variable]);
  31. // Get the forum's settings for database and file paths.
  32. require_once(dirname(__FILE__) . '/Settings.php');
  33. // Make absolutely sure the cache directory is defined.
  34. if ((empty($cachedir) || !file_exists($cachedir)) && file_exists($boarddir . '/cache'))
  35. $cachedir = $boarddir . '/cache';
  36. $ssi_error_reporting = error_reporting(defined('E_STRICT') ? E_ALL | E_STRICT : E_ALL);
  37. /* Set this to one of three values depending on what you want to happen in the case of a fatal error.
  38. false: Default, will just load the error sub template and die - not putting any theme layers around it.
  39. true: Will load the error sub template AND put the SMF layers around it (Not useful if on total custom pages).
  40. 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.
  41. */
  42. $ssi_on_error_method = false;
  43. // Don't do john didley if the forum's been shut down competely.
  44. if ($maintenance == 2 && (!isset($ssi_maintenance_off) || $ssi_maintenance_off !== true))
  45. die($mmessage);
  46. // Fix for using the current directory as a path.
  47. if (substr($sourcedir, 0, 1) == '.' && substr($sourcedir, 1, 1) != '.')
  48. $sourcedir = dirname(__FILE__) . substr($sourcedir, 1);
  49. // Load the important includes.
  50. require_once($sourcedir . '/QueryString.php');
  51. require_once($sourcedir . '/Subs.php');
  52. require_once($sourcedir . '/Errors.php');
  53. require_once($sourcedir . '/Load.php');
  54. require_once($sourcedir . '/Security.php');
  55. // Using an pre-PHP 5.1 version?
  56. if (@version_compare(PHP_VERSION, '5.1') == -1)
  57. require_once($sourcedir . '/Subs-Compat.php');
  58. // Create a variable to store some SMF specific functions in.
  59. $smcFunc = array();
  60. // Initate the database connection and define some database functions to use.
  61. loadDatabase();
  62. // Load installed 'Mods' settings.
  63. reloadSettings();
  64. // Clean the request variables.
  65. cleanRequest();
  66. // Seed the random generator?
  67. if (empty($modSettings['rand_seed']) || mt_rand(1, 250) == 69)
  68. smf_seed_generator();
  69. // Check on any hacking attempts.
  70. if (isset($_REQUEST['GLOBALS']) || isset($_COOKIE['GLOBALS']))
  71. die('Hacking attempt...');
  72. elseif (isset($_REQUEST['ssi_theme']) && (int) $_REQUEST['ssi_theme'] == (int) $ssi_theme)
  73. die('Hacking attempt...');
  74. elseif (isset($_COOKIE['ssi_theme']) && (int) $_COOKIE['ssi_theme'] == (int) $ssi_theme)
  75. die('Hacking attempt...');
  76. elseif (isset($_REQUEST['ssi_layers'], $ssi_layers) && (@get_magic_quotes_gpc() ? stripslashes($_REQUEST['ssi_layers']) : $_REQUEST['ssi_layers']) == $ssi_layers)
  77. die('Hacking attempt...');
  78. if (isset($_REQUEST['context']))
  79. die('Hacking attempt...');
  80. // Make sure wireless is always off.
  81. define('WIRELESS', false);
  82. // Gzip output? (because it must be boolean and true, this can't be hacked.)
  83. 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)
  84. ob_start('ob_gzhandler');
  85. else
  86. $modSettings['enableCompressedOutput'] = '0';
  87. // Primarily, this is to fix the URLs...
  88. ob_start('ob_sessrewrite');
  89. // Start the session... known to scramble SSI includes in cases...
  90. if (!headers_sent())
  91. loadSession();
  92. else
  93. {
  94. if (isset($_COOKIE[session_name()]) || isset($_REQUEST[session_name()]))
  95. {
  96. // Make a stab at it, but ignore the E_WARNINGs generated because we can't send headers.
  97. $temp = error_reporting(error_reporting() & !E_WARNING);
  98. loadSession();
  99. error_reporting($temp);
  100. }
  101. if (!isset($_SESSION['session_value']))
  102. {
  103. $_SESSION['session_var'] = substr(md5(mt_rand() . session_id() . mt_rand()), 0, rand(7, 12));
  104. $_SESSION['session_value'] = md5(session_id() . mt_rand());
  105. }
  106. $sc = $_SESSION['session_value'];
  107. }
  108. // Get rid of $board and $topic... do stuff loadBoard would do.
  109. unset($board, $topic);
  110. $user_info['is_mod'] = false;
  111. $context['user']['is_mod'] = &$user_info['is_mod'];
  112. $context['linktree'] = array();
  113. // Load the user and their cookie, as well as their settings.
  114. loadUserSettings();
  115. // Load the current user's permissions....
  116. loadPermissions();
  117. // Load the current or SSI theme. (just use $ssi_theme = id_theme;)
  118. loadTheme(isset($ssi_theme) ? (int) $ssi_theme : 0);
  119. // Take care of any banning that needs to be done.
  120. if (isset($_REQUEST['ssi_ban']) || (isset($ssi_ban) && $ssi_ban === true))
  121. is_not_banned();
  122. // Do we allow guests in here?
  123. if (empty($ssi_guest_access) && empty($modSettings['allow_guestAccess']) && $user_info['is_guest'] && basename($_SERVER['PHP_SELF']) != 'SSI.php')
  124. {
  125. require_once($sourcedir . '/Subs-Auth.php');
  126. KickGuest();
  127. obExit(null, true);
  128. }
  129. // Load the stuff like the menu bar, etc.
  130. if (isset($ssi_layers))
  131. {
  132. $context['template_layers'] = $ssi_layers;
  133. template_header();
  134. }
  135. else
  136. setupThemeContext();
  137. // Make sure they didn't muss around with the settings... but only if it's not cli.
  138. if (isset($_SERVER['REMOTE_ADDR']) && !isset($_SERVER['is_cli']) && session_id() == '')
  139. trigger_error($txt['ssi_session_broken'], E_USER_NOTICE);
  140. // Without visiting the forum this session variable might not be set on submit.
  141. if (!isset($_SESSION['USER_AGENT']) && (!isset($_GET['ssi_function']) || $_GET['ssi_function'] !== 'pollVote'))
  142. $_SESSION['USER_AGENT'] = $_SERVER['HTTP_USER_AGENT'];
  143. // Call a function passed by GET.
  144. if (isset($_GET['ssi_function']) && function_exists('ssi_' . $_GET['ssi_function']) && (!empty($modSettings['allow_guestAccess']) || !$user_info['is_guest']))
  145. {
  146. call_user_func('ssi_' . $_GET['ssi_function']);
  147. exit;
  148. }
  149. if (isset($_GET['ssi_function']))
  150. exit;
  151. // You shouldn't just access SSI.php directly by URL!!
  152. elseif (basename($_SERVER['PHP_SELF']) == 'SSI.php')
  153. die(sprintf($txt['ssi_not_direct'], $user_info['is_admin'] ? '\'' . addslashes(__FILE__) . '\'' : '\'SSI.php\''));
  154. error_reporting($ssi_error_reporting);
  155. if (function_exists('set_magic_quotes_runtime'))
  156. @set_magic_quotes_runtime($ssi_magic_quotes_runtime);
  157. return true;
  158. // This shuts down the SSI and shows the footer.
  159. function ssi_shutdown()
  160. {
  161. if (!isset($_GET['ssi_function']) || $_GET['ssi_function'] != 'shutdown')
  162. template_footer();
  163. }
  164. // Display a welcome message, like: Hey, User, you have 0 messages, 0 are new.
  165. function ssi_welcome($output_method = 'echo')
  166. {
  167. global $context, $txt, $scripturl;
  168. if ($output_method == 'echo')
  169. {
  170. if ($context['user']['is_guest'])
  171. echo sprintf($txt['welcome_guest'], $txt['guest_title']);
  172. else
  173. 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']) : '', '.';
  174. }
  175. // Don't echo... then do what?!
  176. else
  177. return $context['user'];
  178. }
  179. // Display a menu bar, like is displayed at the top of the forum.
  180. function ssi_menubar($output_method = 'echo')
  181. {
  182. global $context;
  183. if ($output_method == 'echo')
  184. template_menu();
  185. // What else could this do?
  186. else
  187. return $context['menu_buttons'];
  188. }
  189. // Show a logout link.
  190. function ssi_logout($redirect_to = '', $output_method = 'echo')
  191. {
  192. global $context, $txt, $scripturl;
  193. if ($redirect_to != '')
  194. $_SESSION['logout_url'] = $redirect_to;
  195. // Guests can't log out.
  196. if ($context['user']['is_guest'])
  197. return false;
  198. $link = '<a href="' . $scripturl . '?action=logout;' . $context['session_var'] . '=' . $context['session_id'] . '">' . $txt['logout'] . '</a>';
  199. if ($output_method == 'echo')
  200. echo $link;
  201. else
  202. return $link;
  203. }
  204. // Recent post list: [board] Subject by Poster Date
  205. function ssi_recentPosts($num_recent = 8, $exclude_boards = null, $include_boards = null, $output_method = 'echo', $limit_body = true)
  206. {
  207. global $context, $settings, $scripturl, $txt, $db_prefix, $user_info;
  208. global $modSettings, $smcFunc;
  209. // Excluding certain boards...
  210. if ($exclude_boards === null && !empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] > 0)
  211. $exclude_boards = array($modSettings['recycle_board']);
  212. else
  213. $exclude_boards = empty($exclude_boards) ? array() : (is_array($exclude_boards) ? $exclude_boards : array($exclude_boards));
  214. // What about including certain boards - note we do some protection here as pre-2.0 didn't have this parameter.
  215. if (is_array($include_boards) || (int) $include_boards === $include_boards)
  216. {
  217. $include_boards = is_array($include_boards) ? $include_boards : array($include_boards);
  218. }
  219. elseif ($include_boards != null)
  220. {
  221. $include_boards = array();
  222. }
  223. // Let's restrict the query boys (and girls)
  224. $query_where = '
  225. m.id_msg >= {int:min_message_id}
  226. ' . (empty($exclude_boards) ? '' : '
  227. AND b.id_board NOT IN ({array_int:exclude_boards})') . '
  228. ' . ($include_boards === null ? '' : '
  229. AND b.id_board IN ({array_int:include_boards})') . '
  230. AND {query_wanna_see_board}' . ($modSettings['postmod_active'] ? '
  231. AND m.approved = {int:is_approved}' : '');
  232. $query_where_params = array(
  233. 'is_approved' => 1,
  234. 'include_boards' => $include_boards === null ? '' : $include_boards,
  235. 'exclude_boards' => empty($exclude_boards) ? '' : $exclude_boards,
  236. 'min_message_id' => $modSettings['maxMsgID'] - 25 * min($num_recent, 5),
  237. );
  238. // Past to this simpleton of a function...
  239. return ssi_queryPosts($query_where, $query_where_params, $num_recent, 'm.id_msg DESC', $output_method, $limit_body);
  240. }
  241. // 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.
  242. function ssi_fetchPosts($post_ids = array(), $override_permissions = false, $output_method = 'echo')
  243. {
  244. global $user_info, $modSettings;
  245. if (empty($post_ids))
  246. return;
  247. // Allow the user to request more than one - why not?
  248. $post_ids = is_array($post_ids) ? $post_ids : array($post_ids);
  249. // Restrict the posts required...
  250. $query_where = '
  251. m.id_msg IN ({array_int:message_list})' . ($override_permissions ? '' : '
  252. AND {query_wanna_see_board}') . ($modSettings['postmod_active'] ? '
  253. AND m.approved = {int:is_approved}' : '');
  254. $query_where_params = array(
  255. 'message_list' => $post_ids,
  256. 'is_approved' => 1,
  257. );
  258. // Then make the query and dump the data.
  259. return ssi_queryPosts($query_where, $query_where_params, '', 'm.id_msg DESC', $output_method, false, $override_permissions);
  260. }
  261. // This removes code duplication in other queries - don't call it direct unless you really know what you're up to.
  262. 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)
  263. {
  264. global $context, $settings, $scripturl, $txt, $db_prefix, $user_info;
  265. global $modSettings, $smcFunc;
  266. // Find all the posts. Newer ones will have higher IDs.
  267. $request = $smcFunc['db_query']('substring', '
  268. SELECT
  269. m.poster_time, m.subject, m.id_topic, m.id_member, m.id_msg, m.id_board, b.name AS board_name,
  270. IFNULL(mem.real_name, m.poster_name) AS poster_name, ' . ($user_info['is_guest'] ? '1 AS is_read, 0 AS new_from' : '
  271. IFNULL(lt.id_msg, IFNULL(lmr.id_msg, 0)) >= m.id_msg_modified AS is_read,
  272. 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
  273. FROM {db_prefix}messages AS m
  274. INNER JOIN {db_prefix}boards AS b ON (b.id_board = m.id_board)
  275. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member)' . (!$user_info['is_guest'] ? '
  276. LEFT JOIN {db_prefix}log_topics AS lt ON (lt.id_topic = m.id_topic AND lt.id_member = {int:current_member})
  277. LEFT JOIN {db_prefix}log_mark_read AS lmr ON (lmr.id_board = m.id_board AND lmr.id_member = {int:current_member})' : '') . '
  278. WHERE 1=1 ' . ($override_permissions ? '' : '
  279. AND {query_wanna_see_board}') . ($modSettings['postmod_active'] ? '
  280. AND m.approved = {int:is_approved}' : '') . '
  281. ' . (empty($query_where) ? '' : 'AND ' . $query_where) . '
  282. ORDER BY ' . $query_order . '
  283. ' . ($query_limit == '' ? '' : 'LIMIT ' . $query_limit),
  284. array_merge($query_where_params, array(
  285. 'current_member' => $user_info['id'],
  286. 'is_approved' => 1,
  287. ))
  288. );
  289. $posts = array();
  290. while ($row = $smcFunc['db_fetch_assoc']($request))
  291. {
  292. $row['body'] = parse_bbc($row['body'], $row['smileys_enabled'], $row['id_msg']);
  293. // Censor it!
  294. censorText($row['subject']);
  295. censorText($row['body']);
  296. $preview = strip_tags(strtr($row['body'], array('<br />' => '&#10;')));
  297. // Build the array.
  298. $posts[] = array(
  299. 'id' => $row['id_msg'],
  300. 'board' => array(
  301. 'id' => $row['id_board'],
  302. 'name' => $row['board_name'],
  303. 'href' => $scripturl . '?board=' . $row['id_board'] . '.0',
  304. 'link' => '<a href="' . $scripturl . '?board=' . $row['id_board'] . '.0">' . $row['board_name'] . '</a>'
  305. ),
  306. 'topic' => $row['id_topic'],
  307. 'poster' => array(
  308. 'id' => $row['id_member'],
  309. 'name' => $row['poster_name'],
  310. 'href' => empty($row['id_member']) ? '' : $scripturl . '?action=profile;u=' . $row['id_member'],
  311. 'link' => empty($row['id_member']) ? $row['poster_name'] : '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['poster_name'] . '</a>'
  312. ),
  313. 'subject' => $row['subject'],
  314. 'short_subject' => shorten_subject($row['subject'], 25),
  315. 'preview' => $smcFunc['strlen']($preview) > 128 ? $smcFunc['substr']($preview, 0, 128) . '...' : $preview,
  316. 'body' => $row['body'],
  317. 'time' => timeformat($row['poster_time']),
  318. 'timestamp' => forum_time(true, $row['poster_time']),
  319. 'href' => $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . ';topicseen#new',
  320. 'link' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . '#msg' . $row['id_msg'] . '" rel="nofollow">' . $row['subject'] . '</a>',
  321. 'new' => !empty($row['is_read']),
  322. 'is_new' => empty($row['is_read']),
  323. 'new_from' => $row['new_from'],
  324. );
  325. }
  326. $smcFunc['db_free_result']($request);
  327. // Just return it.
  328. if ($output_method != 'echo' || empty($posts))
  329. return $posts;
  330. echo '
  331. <table border="0" class="ssi_table">';
  332. foreach ($posts as $post)
  333. echo '
  334. <tr>
  335. <td align="right" valign="top" nowrap="nowrap">
  336. [', $post['board']['link'], ']
  337. </td>
  338. <td valign="top">
  339. <a href="', $post['href'], '">', $post['subject'], '</a>
  340. ', $txt['by'], ' ', $post['poster']['link'], '
  341. ', $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>' : '', '
  342. </td>
  343. <td align="right" nowrap="nowrap">
  344. ', $post['time'], '
  345. </td>
  346. </tr>';
  347. echo '
  348. </table>';
  349. }
  350. // Recent topic list: [board] Subject by Poster Date
  351. function ssi_recentTopics($num_recent = 8, $exclude_boards = null, $include_boards = null, $output_method = 'echo')
  352. {
  353. global $context, $settings, $scripturl, $txt, $db_prefix, $user_info;
  354. global $modSettings, $smcFunc;
  355. if ($exclude_boards === null && !empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] > 0)
  356. $exclude_boards = array($modSettings['recycle_board']);
  357. else
  358. $exclude_boards = empty($exclude_boards) ? array() : (is_array($exclude_boards) ? $exclude_boards : array($exclude_boards));
  359. // Only some boards?.
  360. if (is_array($include_boards) || (int) $include_boards === $include_boards)
  361. {
  362. $include_boards = is_array($include_boards) ? $include_boards : array($include_boards);
  363. }
  364. elseif ($include_boards != null)
  365. {
  366. $output_method = $include_boards;
  367. $include_boards = array();
  368. }
  369. $stable_icons = array('xx', 'thumbup', 'thumbdown', 'exclamation', 'question', 'lamp', 'smiley', 'angry', 'cheesy', 'grin', 'sad', 'wink', 'moved', 'recycled', 'wireless');
  370. $icon_sources = array();
  371. foreach ($stable_icons as $icon)
  372. $icon_sources[$icon] = 'images_url';
  373. // Find all the posts in distinct topics. Newer ones will have higher IDs.
  374. $request = $smcFunc['db_query']('substring', '
  375. SELECT
  376. 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,
  377. IFNULL(mem.real_name, m.poster_name) AS poster_name, ' . ($user_info['is_guest'] ? '1 AS is_read, 0 AS new_from' : '
  378. IFNULL(lt.id_msg, IFNULL(lmr.id_msg, 0)) >= m.id_msg_modified AS is_read,
  379. 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
  380. FROM {db_prefix}topics AS t
  381. INNER JOIN {db_prefix}messages AS m ON (m.id_msg = t.id_last_msg)
  382. INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board)
  383. INNER JOIN {db_prefix}messages AS ms ON (ms.id_msg = t.id_first_msg)
  384. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member)' . (!$user_info['is_guest'] ? '
  385. LEFT JOIN {db_prefix}log_topics AS lt ON (lt.id_topic = t.id_topic AND lt.id_member = {int:current_member})
  386. LEFT JOIN {db_prefix}log_mark_read AS lmr ON (lmr.id_board = b.id_board AND lmr.id_member = {int:current_member})' : '') . '
  387. WHERE t.id_last_msg >= {int:min_message_id}
  388. ' . (empty($exclude_boards) ? '' : '
  389. AND b.id_board NOT IN ({array_int:exclude_boards})') . '
  390. ' . (empty($include_boards) ? '' : '
  391. AND b.id_board IN ({array_int:include_boards})') . '
  392. AND {query_wanna_see_board}' . ($modSettings['postmod_active'] ? '
  393. AND t.approved = {int:is_approved}
  394. AND m.approved = {int:is_approved}' : '') . '
  395. ORDER BY t.id_last_msg DESC
  396. LIMIT ' . $num_recent,
  397. array(
  398. 'current_member' => $user_info['id'],
  399. 'include_boards' => empty($include_boards) ? '' : $include_boards,
  400. 'exclude_boards' => empty($exclude_boards) ? '' : $exclude_boards,
  401. 'min_message_id' => $modSettings['maxMsgID'] - 35 * min($num_recent, 5),
  402. 'is_approved' => 1,
  403. )
  404. );
  405. $posts = array();
  406. while ($row = $smcFunc['db_fetch_assoc']($request))
  407. {
  408. $row['body'] = strip_tags(strtr(parse_bbc($row['body'], $row['smileys_enabled'], $row['id_msg']), array('<br />' => '&#10;')));
  409. if ($smcFunc['strlen']($row['body']) > 128)
  410. $row['body'] = $smcFunc['substr']($row['body'], 0, 128) . '...';
  411. // Censor the subject.
  412. censorText($row['subject']);
  413. censorText($row['body']);
  414. if (empty($modSettings['messageIconChecks_disable']) && !isset($icon_sources[$row['icon']]))
  415. $icon_sources[$row['icon']] = file_exists($settings['theme_dir'] . '/images/post/' . $row['icon'] . '.gif') ? 'images_url' : 'default_images_url';
  416. // Build the array.
  417. $posts[] = array(
  418. 'board' => array(
  419. 'id' => $row['id_board'],
  420. 'name' => $row['board_name'],
  421. 'href' => $scripturl . '?board=' . $row['id_board'] . '.0',
  422. 'link' => '<a href="' . $scripturl . '?board=' . $row['id_board'] . '.0">' . $row['board_name'] . '</a>'
  423. ),
  424. 'topic' => $row['id_topic'],
  425. 'poster' => array(
  426. 'id' => $row['id_member'],
  427. 'name' => $row['poster_name'],
  428. 'href' => empty($row['id_member']) ? '' : $scripturl . '?action=profile;u=' . $row['id_member'],
  429. 'link' => empty($row['id_member']) ? $row['poster_name'] : '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['poster_name'] . '</a>'
  430. ),
  431. 'subject' => $row['subject'],
  432. 'replies' => $row['num_replies'],
  433. 'views' => $row['num_views'],
  434. 'short_subject' => shorten_subject($row['subject'], 25),
  435. 'preview' => $row['body'],
  436. 'time' => timeformat($row['poster_time']),
  437. 'timestamp' => forum_time(true, $row['poster_time']),
  438. 'href' => $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . ';topicseen#new',
  439. 'link' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . '#new" rel="nofollow">' . $row['subject'] . '</a>',
  440. // Retained for compatibility - is technically incorrect!
  441. 'new' => !empty($row['is_read']),
  442. 'is_new' => empty($row['is_read']),
  443. 'new_from' => $row['new_from'],
  444. 'icon' => '<img src="' . $settings[$icon_sources[$row['icon']]] . '/post/' . $row['icon'] . '.gif" align="middle" alt="' . $row['icon'] . '" />',
  445. );
  446. }
  447. $smcFunc['db_free_result']($request);
  448. // Just return it.
  449. if ($output_method != 'echo' || empty($posts))
  450. return $posts;
  451. echo '
  452. <table border="0" class="ssi_table">';
  453. foreach ($posts as $post)
  454. echo '
  455. <tr>
  456. <td align="right" valign="top" nowrap="nowrap">
  457. [', $post['board']['link'], ']
  458. </td>
  459. <td valign="top">
  460. <a href="', $post['href'], '">', $post['subject'], '</a>
  461. ', $txt['by'], ' ', $post['poster']['link'], '
  462. ', !$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>', '
  463. </td>
  464. <td align="right" nowrap="nowrap">
  465. ', $post['time'], '
  466. </td>
  467. </tr>';
  468. echo '
  469. </table>';
  470. }
  471. // Show the top poster's name and profile link.
  472. function ssi_topPoster($topNumber = 1, $output_method = 'echo')
  473. {
  474. global $db_prefix, $scripturl, $smcFunc;
  475. // Find the latest poster.
  476. $request = $smcFunc['db_query']('', '
  477. SELECT id_member, real_name, posts
  478. FROM {db_prefix}members
  479. ORDER BY posts DESC
  480. LIMIT ' . $topNumber,
  481. array(
  482. )
  483. );
  484. $return = array();
  485. while ($row = $smcFunc['db_fetch_assoc']($request))
  486. $return[] = array(
  487. 'id' => $row['id_member'],
  488. 'name' => $row['real_name'],
  489. 'href' => $scripturl . '?action=profile;u=' . $row['id_member'],
  490. 'link' => '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['real_name'] . '</a>',
  491. 'posts' => $row['posts']
  492. );
  493. $smcFunc['db_free_result']($request);
  494. // Just return all the top posters.
  495. if ($output_method != 'echo')
  496. return $return;
  497. // Make a quick array to list the links in.
  498. $temp_array = array();
  499. foreach ($return as $member)
  500. $temp_array[] = $member['link'];
  501. echo implode(', ', $temp_array);
  502. }
  503. // Show boards by activity.
  504. function ssi_topBoards($num_top = 10, $output_method = 'echo')
  505. {
  506. global $context, $settings, $db_prefix, $txt, $scripturl, $user_info, $modSettings, $smcFunc;
  507. // Find boards with lots of posts.
  508. $request = $smcFunc['db_query']('', '
  509. SELECT
  510. b.name, b.num_topics, b.num_posts, b.id_board,' . (!$user_info['is_guest'] ? ' 1 AS is_read' : '
  511. (IFNULL(lb.id_msg, 0) >= b.id_last_msg) AS is_read') . '
  512. FROM {db_prefix}boards AS b
  513. LEFT JOIN {db_prefix}log_boards AS lb ON (lb.id_board = b.id_board AND lb.id_member = {int:current_member})
  514. WHERE {query_wanna_see_board}' . (!empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] > 0 ? '
  515. AND b.id_board != {int:recycle_board}' : '') . '
  516. ORDER BY b.num_posts DESC
  517. LIMIT ' . $num_top,
  518. array(
  519. 'current_member' => $user_info['id'],
  520. 'recycle_board' => (int) $modSettings['recycle_board'],
  521. )
  522. );
  523. $boards = array();
  524. while ($row = $smcFunc['db_fetch_assoc']($request))
  525. $boards[] = array(
  526. 'id' => $row['id_board'],
  527. 'num_posts' => $row['num_posts'],
  528. 'num_topics' => $row['num_topics'],
  529. 'name' => $row['name'],
  530. 'new' => empty($row['is_read']),
  531. 'href' => $scripturl . '?board=' . $row['id_board'] . '.0',
  532. 'link' => '<a href="' . $scripturl . '?board=' . $row['id_board'] . '.0">' . $row['name'] . '</a>'
  533. );
  534. $smcFunc['db_free_result']($request);
  535. // If we shouldn't output or have nothing to output, just jump out.
  536. if ($output_method != 'echo' || empty($boards))
  537. return $boards;
  538. echo '
  539. <table class="ssi_table">
  540. <tr>
  541. <th align="left">', $txt['board'], '</th>
  542. <th align="left">', $txt['board_topics'], '</th>
  543. <th align="left">', $txt['posts'], '</th>
  544. </tr>';
  545. foreach ($boards as $board)
  546. echo '
  547. <tr>
  548. <td>', $board['link'], $board['new'] ? ' <a href="' . $board['href'] . '"><img src="' . $settings['lang_images_url'] . '/new.gif" alt="' . $txt['new'] . '" /></a>' : '', '</td>
  549. <td align="right">', comma_format($board['num_topics']), '</td>
  550. <td align="right">', comma_format($board['num_posts']), '</td>
  551. </tr>';
  552. echo '
  553. </table>';
  554. }
  555. // Shows the top topics.
  556. function ssi_topTopics($type = 'replies', $num_topics = 10, $output_method = 'echo')
  557. {
  558. global $db_prefix, $txt, $scripturl, $user_info, $modSettings, $smcFunc, $context;
  559. if ($modSettings['totalMessages'] > 100000)
  560. {
  561. // !!! Why don't we use {query(_wanna)_see_board}?
  562. $request = $smcFunc['db_query']('', '
  563. SELECT id_topic
  564. FROM {db_prefix}topics
  565. WHERE num_' . ($type != 'replies' ? 'views' : 'replies') . ' != 0' . ($modSettings['postmod_active'] ? '
  566. AND approved = {int:is_approved}' : '') . '
  567. ORDER BY num_' . ($type != 'replies' ? 'views' : 'replies') . ' DESC
  568. LIMIT {int:limit}',
  569. array(
  570. 'is_approved' => 1,
  571. 'limit' => $num_topics > 100 ? ($num_topics + ($num_topics / 2)) : 100,
  572. )
  573. );
  574. $topic_ids = array();
  575. while ($row = $smcFunc['db_fetch_assoc']($request))
  576. $topic_ids[] = $row['id_topic'];
  577. $smcFunc['db_free_result']($request);
  578. }
  579. else
  580. $topic_ids = array();
  581. $request = $smcFunc['db_query']('', '
  582. SELECT m.subject, m.id_topic, t.num_views, t.num_replies
  583. FROM {db_prefix}topics AS t
  584. INNER JOIN {db_prefix}messages AS m ON (m.id_msg = t.id_first_msg)
  585. INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board)
  586. WHERE {query_wanna_see_board}' . ($modSettings['postmod_active'] ? '
  587. AND t.approved = {int:is_approved}' : '') . (!empty($topic_ids) ? '
  588. AND t.id_topic IN ({array_int:topic_list})' : '') . (!empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] > 0 ? '
  589. AND b.id_board != {int:recycle_enable}' : '') . '
  590. ORDER BY t.num_' . ($type != 'replies' ? 'views' : 'replies') . ' DESC
  591. LIMIT {int:limit}',
  592. array(
  593. 'topic_list' => $topic_ids,
  594. 'is_approved' => 1,
  595. 'recycle_enable' => $modSettings['recycle_board'],
  596. 'limit' => $num_topics,
  597. )
  598. );
  599. $topics = array();
  600. while ($row = $smcFunc['db_fetch_assoc']($request))
  601. {
  602. censorText($row['subject']);
  603. $topics[] = array(
  604. 'id' => $row['id_topic'],
  605. 'subject' => $row['subject'],
  606. 'num_replies' => $row['num_replies'],
  607. 'num_views' => $row['num_views'],
  608. 'href' => $scripturl . '?topic=' . $row['id_topic'] . '.0',
  609. 'link' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.0">' . $row['subject'] . '</a>',
  610. );
  611. }
  612. $smcFunc['db_free_result']($request);
  613. if ($output_method != 'echo' || empty($topics))
  614. return $topics;
  615. echo '
  616. <table class="ssi_table">
  617. <tr>
  618. <th align="left"></th>
  619. <th align="left">', $txt['views'], '</th>
  620. <th align="left">', $txt['replies'], '</th>
  621. </tr>';
  622. foreach ($topics as $topic)
  623. echo '
  624. <tr>
  625. <td align="left">
  626. ', $topic['link'], '
  627. </td>
  628. <td align="right">', comma_format($topic['num_views']), '</td>
  629. <td align="right">', comma_format($topic['num_replies']), '</td>
  630. </tr>';
  631. echo '
  632. </table>';
  633. }
  634. // Shows the top topics, by replies.
  635. function ssi_topTopicsReplies($num_topics = 10, $output_method = 'echo')
  636. {
  637. return ssi_topTopics('replies', $num_topics, $output_method);
  638. }
  639. // Shows the top topics, by views.
  640. function ssi_topTopicsViews($num_topics = 10, $output_method = 'echo')
  641. {
  642. return ssi_topTopics('views', $num_topics, $output_method);
  643. }
  644. // Show a link to the latest member: Please welcome, Someone, out latest member.
  645. function ssi_latestMember($output_method = 'echo')
  646. {
  647. global $db_prefix, $txt, $scripturl, $context;
  648. if ($output_method == 'echo')
  649. echo '
  650. ', $txt['welcome_member'], ' ', $context['common_stats']['latest_member']['link'], '', $txt['newest_member'], '<br />';
  651. else
  652. return $context['common_stats']['latest_member'];
  653. }
  654. // Fetch a random member - if type set to 'day' will only change once a day!
  655. function ssi_randomMember($random_type = '', $output_method = 'echo')
  656. {
  657. global $modSettings;
  658. // If we're looking for something to stay the same each day then seed the generator.
  659. if ($random_type == 'day')
  660. {
  661. // Set the seed to change only once per day.
  662. mt_srand(floor(time() / 86400));
  663. }
  664. // Get the lowest ID we're interested in.
  665. $member_id = mt_rand(1, $modSettings['latestMember']);
  666. $where_query = '
  667. id_member >= {int:selected_member}
  668. AND is_activated = {int:is_activated}';
  669. $query_where_params = array(
  670. 'selected_member' => $member_id,
  671. 'is_activated' => 1,
  672. );
  673. $result = ssi_queryMembers($where_query, $query_where_params, 1, 'id_member ASC', $output_method);
  674. // If we got nothing do the reverse - in case of unactivated members.
  675. if (empty($result))
  676. {
  677. $where_query = '
  678. id_member <= {int:selected_member}
  679. AND is_activated = {int:is_activated}';
  680. $query_where_params = array(
  681. 'selected_member' => $member_id,
  682. 'is_activated' => 1,
  683. );
  684. $result = ssi_queryMembers($where_query, $query_where_params, 1, 'id_member DESC', $output_method);
  685. }
  686. // Just to be sure put the random generator back to something... random.
  687. if ($random_type != '')
  688. mt_srand(time());
  689. return $result;
  690. }
  691. // Fetch a specific member.
  692. function ssi_fetchMember($member_ids = array(), $output_method = 'echo')
  693. {
  694. if (empty($member_ids))
  695. return;
  696. // Can have more than one member if you really want...
  697. $member_ids = is_array($member_ids) ? $member_ids : array($member_ids);
  698. // Restrict it right!
  699. $query_where = '
  700. id_member IN ({array_int:member_list})';
  701. $query_where_params = array(
  702. 'member_list' => $member_ids,
  703. );
  704. // Then make the query and dump the data.
  705. return ssi_queryMembers($query_where, $query_where_params, '', 'id_member', $output_method);
  706. }
  707. // Get all members of a group.
  708. function ssi_fetchGroupMembers($group_id = null, $output_method = 'echo')
  709. {
  710. if ($group_id === null)
  711. return;
  712. $query_where = '
  713. id_group = {int:id_group}
  714. OR id_post_group = {int:id_group}
  715. OR FIND_IN_SET({int:id_group}, additional_groups) != 0';
  716. $query_where_params = array(
  717. 'id_group' => $group_id,
  718. );
  719. return ssi_queryMembers($query_where, $query_where_params, '', 'real_name', $output_method);
  720. }
  721. // Fetch some member data!
  722. function ssi_queryMembers($query_where = null, $query_where_params = array(), $query_limit = '', $query_order = 'id_member DESC', $output_method = 'echo')
  723. {
  724. global $context, $settings, $scripturl, $txt, $db_prefix, $user_info;
  725. global $modSettings, $smcFunc, $memberContext;
  726. if ($query_where === null)
  727. return;
  728. // Fetch the members in question.
  729. $request = $smcFunc['db_query']('', '
  730. SELECT id_member
  731. FROM {db_prefix}members
  732. WHERE ' . $query_where . '
  733. ORDER BY ' . $query_order . '
  734. ' . ($query_limit == '' ? '' : 'LIMIT ' . $query_limit),
  735. array_merge($query_where_params, array(
  736. ))
  737. );
  738. $members = array();
  739. while ($row = $smcFunc['db_fetch_assoc']($request))
  740. $members[] = $row['id_member'];
  741. $smcFunc['db_free_result']($request);
  742. if (empty($members))
  743. return array();
  744. // Load the members.
  745. loadMemberData($members);
  746. // Draw the table!
  747. if ($output_method == 'echo')
  748. echo '
  749. <table border="0" class="ssi_table">';
  750. $query_members = array();
  751. foreach ($members as $member)
  752. {
  753. // Load their context data.
  754. if (!loadMemberContext($member))
  755. continue;
  756. // Store this member's information.
  757. $query_members[$member] = $memberContext[$member];
  758. // Only do something if we're echo'ing.
  759. if ($output_method == 'echo')
  760. echo '
  761. <tr>
  762. <td align="right" valign="top" nowrap="nowrap">
  763. ', $query_members[$member]['link'], '
  764. <br />', $query_members[$member]['blurb'], '
  765. <br />', $query_members[$member]['avatar']['image'], '
  766. </td>
  767. </tr>';
  768. }
  769. // End the table if appropriate.
  770. if ($output_method == 'echo')
  771. echo '
  772. </table>';
  773. // Send back the data.
  774. return $query_members;
  775. }
  776. // Show some basic stats: Total This: XXXX, etc.
  777. function ssi_boardStats($output_method = 'echo')
  778. {
  779. global $db_prefix, $txt, $scripturl, $modSettings, $smcFunc;
  780. $totals = array(
  781. 'members' => $modSettings['totalMembers'],
  782. 'posts' => $modSettings['totalMessages'],
  783. 'topics' => $modSettings['totalTopics']
  784. );
  785. $result = $smcFunc['db_query']('', '
  786. SELECT COUNT(*)
  787. FROM {db_prefix}boards',
  788. array(
  789. )
  790. );
  791. list ($totals['boards']) = $smcFunc['db_fetch_row']($result);
  792. $smcFunc['db_free_result']($result);
  793. $result = $smcFunc['db_query']('', '
  794. SELECT COUNT(*)
  795. FROM {db_prefix}categories',
  796. array(
  797. )
  798. );
  799. list ($totals['categories']) = $smcFunc['db_fetch_row']($result);
  800. $smcFunc['db_free_result']($result);
  801. if ($output_method != 'echo')
  802. return $totals;
  803. echo '
  804. ', $txt['total_members'], ': <a href="', $scripturl . '?action=mlist">', comma_format($totals['members']), '</a><br />
  805. ', $txt['total_posts'], ': ', comma_format($totals['posts']), '<br />
  806. ', $txt['total_topics'], ': ', comma_format($totals['topics']), ' <br />
  807. ', $txt['total_cats'], ': ', comma_format($totals['categories']), '<br />
  808. ', $txt['total_boards'], ': ', comma_format($totals['boards']);
  809. }
  810. // Shows a list of online users: YY Guests, ZZ Users and then a list...
  811. function ssi_whosOnline($output_method = 'echo')
  812. {
  813. global $user_info, $txt, $sourcedir, $settings, $modSettings;
  814. require_once($sourcedir . '/Subs-MembersOnline.php');
  815. $membersOnlineOptions = array(
  816. 'show_hidden' => allowedTo('moderate_forum'),
  817. 'sort' => 'log_time',
  818. 'reverse_sort' => true,
  819. );
  820. $return = getMembersOnlineStats($membersOnlineOptions);
  821. // Add some redundancy for backwards compatibility reasons.
  822. if ($output_method != 'echo')
  823. return $return + array(
  824. 'users' => $return['users_online'],
  825. 'guests' => $return['num_guests'],
  826. 'hidden' => $return['num_users_hidden'],
  827. 'buddies' => $return['num_buddies'],
  828. 'num_users' => $return['num_users_online'],
  829. 'total_users' => $return['num_users_online'] + $return['num_guests'] + $return['num_spiders'],
  830. );
  831. echo '
  832. ', 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'];
  833. $bracketList = array();
  834. if (!empty($user_info['buddies']))
  835. $bracketList[] = comma_format($return['num_buddies']) . ' ' . ($return['num_buddies'] == 1 ? $txt['buddy'] : $txt['buddies']);
  836. if (!empty($return['num_spiders']))
  837. $bracketList[] = comma_format($return['num_spiders']) . ' ' . ($return['num_spiders'] == 1 ? $txt['spider'] : $txt['spiders']);
  838. if (!empty($return['num_users_hidden']))
  839. $bracketList[] = comma_format($return['num_users_hidden']) . ' ' . $txt['hidden'];
  840. if (!empty($bracketList))
  841. echo ' (' . implode(', ', $bracketList) . ')';
  842. echo '<br />
  843. ', implode(', ', $return['list_users_online']);
  844. // Showing membergroups?
  845. if (!empty($settings['show_group_key']) && !empty($return['membergroups']))
  846. echo '<br />
  847. [' . implode(']&nbsp;&nbsp;[', $return['membergroups']) . ']';
  848. }
  849. // Just like whosOnline except it also logs the online presence.
  850. function ssi_logOnline($output_method = 'echo')
  851. {
  852. writeLog();
  853. if ($output_method != 'echo')
  854. return ssi_whosOnline($output_method);
  855. else
  856. ssi_whosOnline($output_method);
  857. }
  858. // Shows a login box.
  859. function ssi_login($redirect_to = '', $output_method = 'echo')
  860. {
  861. global $scripturl, $txt, $user_info, $context, $modSettings;
  862. if ($redirect_to != '')
  863. $_SESSION['login_url'] = $redirect_to;
  864. if ($output_method != 'echo' || !$user_info['is_guest'])
  865. return $user_info['is_guest'];
  866. echo '
  867. <form action="', $scripturl, '?action=login2" method="post" accept-charset="', $context['character_set'], '">
  868. <table border="0" cellspacing="1" cellpadding="0" class="ssi_table">
  869. <tr>
  870. <td align="right"><label for="user">', $txt['username'], ':</label>&nbsp;</td>
  871. <td><input type="text" id="user" name="user" size="9" value="', $user_info['username'], '" class="input_text" /></td>
  872. </tr><tr>
  873. <td align="right"><label for="passwrd">', $txt['password'], ':</label>&nbsp;</td>
  874. <td><input type="password" name="passwrd" id="passwrd" size="9" class="input_password" /></td>
  875. </tr>';
  876. // Open ID?
  877. if (!empty($modSettings['enableOpenID']))
  878. echo '<tr>
  879. <td colspan="2" align="center"><strong>&mdash;', $txt['or'], '&mdash;</strong></td>
  880. </tr><tr>
  881. <td align="right"><label for="openid_url">', $txt['openid'], ':</label>&nbsp;</td>
  882. <td><input type="text" name="openid_identifier" id="openid_url" class="input_text openid_login" size="17" /></td>
  883. </tr>';
  884. echo '<tr>
  885. <td><input type="hidden" name="cookielength" value="-1" /></td>
  886. <td><input type="submit" value="', $txt['login'], '" class="button_submit" /></td>
  887. </tr>
  888. </table>
  889. </form>';
  890. }
  891. // Show the most-voted-in poll.
  892. function ssi_topPoll($output_method = 'echo')
  893. {
  894. // Just use recentPoll, no need to duplicate code...
  895. return ssi_recentPoll(true, $output_method);
  896. }
  897. // Show the most recently posted poll.
  898. function ssi_recentPoll($topPollInstead = false, $output_method = 'echo')
  899. {
  900. global $db_prefix, $txt, $settings, $boardurl, $user_info, $context, $smcFunc, $modSettings;
  901. $boardsAllowed = array_intersect(boardsAllowedTo('poll_view'), boardsAllowedTo('poll_vote'));
  902. if (empty($boardsAllowed))
  903. return array();
  904. $request = $smcFunc['db_query']('', '
  905. SELECT p.id_poll, p.question, t.id_topic, p.max_votes, p.guest_vote, p.hide_results, p.expire_time
  906. FROM {db_prefix}polls AS p
  907. INNER JOIN {db_prefix}topics AS t ON (t.id_poll = p.id_poll' . ($modSettings['postmod_active'] ? ' AND t.approved = {int:is_approved}' : '') . ')
  908. INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board)' . ($topPollInstead ? '
  909. INNER JOIN {db_prefix}poll_choices AS pc ON (pc.id_poll = p.id_poll)' : '') . '
  910. 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})
  911. WHERE p.voting_locked = {int:voting_opened}
  912. AND (p.expire_time = {int:no_expiration} OR {int:current_time} < p.expire_time)
  913. AND ' . ($user_info['is_guest'] ? 'p.guest_vote = {int:guest_vote_allowed}' : 'lp.id_choice IS NULL') . '
  914. AND {query_wanna_see_board}' . (!in_array(0, $boardsAllowed) ? '
  915. AND b.id_board IN ({array_int:boards_allowed_list})' : '') . (!empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] > 0 ? '
  916. AND b.id_board != {int:recycle_enable}' : '') . '
  917. ORDER BY ' . ($topPollInstead ? 'pc.votes' : 'p.id_poll') . ' DESC
  918. LIMIT 1',
  919. array(
  920. 'current_member' => $user_info['id'],
  921. 'boards_allowed_list' => $boardsAllowed,
  922. 'is_approved' => 1,
  923. 'guest_vote_allowed' => 1,
  924. 'no_member' => 0,
  925. 'voting_opened' => 0,
  926. 'no_expiration' => 0,
  927. 'current_time' => time(),
  928. 'recycle_enable' => $modSettings['recycle_board'],
  929. )
  930. );
  931. $row = $smcFunc['db_fetch_assoc']($request);
  932. $smcFunc['db_free_result']($request);
  933. // This user has voted on all the polls.
  934. if ($row === false)
  935. return array();
  936. // If this is a guest who's voted we'll through ourselves to show poll to show the results.
  937. if ($user_info['is_guest'] && (!$row['guest_vote'] || (isset($_COOKIE['guest_poll_vote']) && in_array($row['id_poll'], explode(',', $_COOKIE['guest_poll_vote'])))))
  938. return ssi_showPoll($row['id_topic'], $output_method);
  939. $request = $smcFunc['db_query']('', '
  940. SELECT COUNT(DISTINCT id_member)
  941. FROM {db_prefix}log_polls
  942. WHERE id_poll = {int:current_poll}',
  943. array(
  944. 'current_poll' => $row['id_poll'],
  945. )
  946. );
  947. list ($total) = $smcFunc['db_fetch_row']($request);
  948. $smcFunc['db_free_result']($request);
  949. $request = $smcFunc['db_query']('', '
  950. SELECT id_choice, label, votes
  951. FROM {db_prefix}poll_choices
  952. WHERE id_poll = {int:current_poll}',
  953. array(
  954. 'current_poll' => $row['id_poll'],
  955. )
  956. );
  957. $options = array();
  958. while ($rowChoice = $smcFunc['db_fetch_assoc']($request))
  959. {
  960. censorText($rowChoice['label']);
  961. $options[$rowChoice['id_choice']] = array($rowChoice['label'], $rowChoice['votes']);
  962. }
  963. $smcFunc['db_free_result']($request);
  964. // Can they view it?
  965. $is_expired = !empty($row['expire_time']) && $row['expire_time'] < time();
  966. $allow_view_results = allowedTo('moderate_board') || $row['hide_results'] == 0 || $is_expired;
  967. $return = array(
  968. 'id' => $row['id_poll'],
  969. 'image' => 'poll',
  970. 'question' => $row['question'],
  971. 'total_votes' => $total,
  972. 'is_locked' => false,
  973. 'topic' => $row['id_topic'],
  974. 'allow_view_results' => $allow_view_results,
  975. 'options' => array()
  976. );
  977. // Calculate the percentages and bar lengths...
  978. $divisor = $return['total_votes'] == 0 ? 1 : $return['total_votes'];
  979. foreach ($options as $i => $option)
  980. {
  981. $bar = floor(($option[1] * 100) / $divisor);
  982. $barWide = $bar == 0 ? 1 : floor(($bar * 5) / 3);
  983. $return['options'][$i] = array(
  984. 'id' => 'options-' . ($topPollInstead ? 'top-' : 'recent-') . $i,
  985. 'percent' => $bar,
  986. 'votes' => $option[1],
  987. '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>',
  988. 'option' => parse_bbc($option[0]),
  989. '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') . '" />'
  990. );
  991. }
  992. $return['allowed_warning'] = $row['max_votes'] > 1 ? sprintf($txt['poll_options6'], min(count($options), $row['max_votes'])) : '';
  993. if ($output_method != 'echo')
  994. return $return;
  995. if ($allow_view_results)
  996. {
  997. echo '
  998. <form class="ssi_poll" action="', $boardurl, '/SSI.php?ssi_function=pollVote" method="post" accept-charset="', $context['character_set'], '">
  999. <strong>', $return['question'], '</strong><br />
  1000. ', !empty($return['allowed_warning']) ? $return['allowed_warning'] . '<br />' : '';
  1001. foreach ($return['options'] as $option)
  1002. echo '
  1003. <label for="', $option['id'], '">', $option['vote_button'], ' ', $option['option'], '</label><br />';
  1004. echo '
  1005. <input type="submit" value="', $txt['poll_vote'], '" class="button_submit" />
  1006. <input type="hidden" name="poll" value="', $return['id'], '" />
  1007. <input type="hidden" name="', $context['session_var'], '" value="', $context['session_id'], '" />
  1008. </form>';
  1009. }
  1010. else
  1011. echo $txt['poll_cannot_see'];
  1012. }
  1013. function ssi_showPoll($topic = null, $output_method = 'echo')
  1014. {
  1015. global $db_prefix, $txt, $settings, $boardurl, $user_info, $context, $smcFunc, $modSettings;
  1016. $boardsAllowed = boardsAllowedTo('poll_view');
  1017. if (empty($boardsAllowed))
  1018. return array();
  1019. if ($topic === null && isset($_REQUEST['ssi_topic']))
  1020. $topic = (int) $_REQUEST['ssi_topic'];
  1021. else
  1022. $topic = (int) $topic;
  1023. $request = $smcFunc['db_query']('', '
  1024. SELECT
  1025. p.id_poll, p.question, p.voting_locked, p.hide_results, p.expire_time, p.max_votes, p.guest_vote, b.id_board
  1026. FROM {db_prefix}topics AS t
  1027. INNER JOIN {db_prefix}polls AS p ON (p.id_poll = t.id_poll)
  1028. INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board)
  1029. WHERE t.id_topic = {int:current_topic}
  1030. AND {query_see_board}' . (!in_array(0, $boardsAllowed) ? '
  1031. AND b.id_board IN ({array_int:boards_allowed_see})' : '') . ($modSettings['postmod_active'] ? '
  1032. AND t.approved = {int:is_approved}' : '') . '
  1033. LIMIT 1',
  1034. array(
  1035. 'current_topic' => $topic,
  1036. 'boards_allowed_see' => $boardsAllowed,
  1037. 'is_approved' => 1,
  1038. )
  1039. );
  1040. // Either this topic has no poll, or the user cannot view it.
  1041. if ($smcFunc['db_num_rows']($request) == 0)
  1042. return array();
  1043. $row = $smcFunc['db_fetch_assoc']($request);
  1044. $smcFunc['db_free_result']($request);
  1045. // Check if they can vote.
  1046. if (!empty($row['expire_time']) && $row['expire_time'] < time())
  1047. $allow_vote = false;
  1048. elseif ($user_info['is_guest'] && $row['guest_vote'] && (!isset($_COOKIE['guest_poll_vote']) || !in_array($row['id_poll'], explode(',', $_COOKIE['guest_poll_vote']))))
  1049. $allow_vote = true;
  1050. elseif ($user_info['is_guest'])
  1051. $allow_vote = false;
  1052. elseif (!empty($row['voting_locked']) || !allowedTo('poll_vote', $row['id_board']))
  1053. $allow_vote = false;
  1054. else
  1055. {
  1056. $request = $smcFunc['db_query']('', '
  1057. SELECT id_member
  1058. FROM {db_prefix}log_polls
  1059. WHERE id_poll = {int:current_poll}
  1060. AND id_member = {int:current_member}
  1061. LIMIT 1',
  1062. array(
  1063. 'current_member' => $user_info['id'],
  1064. 'current_poll' => $row['id_poll'],
  1065. )
  1066. );
  1067. $allow_vote = $smcFunc['db_num_rows']($request) == 0;
  1068. $smcFunc['db_free_result']($request);
  1069. }
  1070. // Can they view?
  1071. $is_expired = !empty($row['expire_time']) && $row['expire_time'] < time();
  1072. $allow_view_results = allowedTo('moderate_board') || $row['hide_results'] == 0 || ($row['hide_results'] == 1 && !$allow_vote) || $is_expired;
  1073. $request = $smcFunc['db_query']('', '
  1074. SELECT COUNT(DISTINCT id_member)
  1075. FROM {db_prefix}log_polls
  1076. WHERE id_poll = {int:current_poll}',
  1077. array(
  1078. 'current_poll' => $row['id_poll'],
  1079. )
  1080. );
  1081. list ($total) = $smcFunc['db_fetch_row']($request);
  1082. $smcFunc['db_free_result']($request);
  1083. $request = $smcFunc['db_query']('', '
  1084. SELECT id_choice, label, votes
  1085. FROM {db_prefix}poll_choices
  1086. WHERE id_poll = {int:current_poll}',
  1087. array(
  1088. 'current_poll' => $row['id_poll'],
  1089. )
  1090. );
  1091. $options = array();
  1092. $total_votes = 0;
  1093. while ($rowChoice = $smcFunc['db_fetch_assoc']($request))
  1094. {
  1095. censorText($rowChoice['label']);
  1096. $options[$rowChoice['id_choice']] = array($rowChoice['label'], $rowChoice['votes']);
  1097. $total_votes += $rowChoice['votes'];
  1098. }
  1099. $smcFunc['db_free_result']($request);
  1100. $return = array(
  1101. 'id' => $row['id_poll'],
  1102. 'image' => empty($pollinfo['voting_locked']) ? 'poll' : 'locked_poll',
  1103. 'question' => $row['question'],
  1104. 'total_votes' => $total,
  1105. 'is_locked' => !empty($pollinfo['voting_locked']),
  1106. 'allow_vote' => $allow_vote,
  1107. 'allow_view_results' => $allow_view_results,
  1108. 'topic' => $topic
  1109. );
  1110. // Calculate the percentages and bar lengths...
  1111. $divisor = $total_votes == 0 ? 1 : $total_votes;
  1112. foreach ($options as $i => $option)
  1113. {
  1114. $bar = floor(($option[1] * 100) / $divisor);
  1115. $barWide = $bar == 0 ? 1 : floor(($bar * 5) / 3);
  1116. $return['options'][$i] = array(
  1117. 'id' => 'options-' . $i,
  1118. 'percent' => $bar,
  1119. 'votes' => $option[1],
  1120. '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>',
  1121. 'option' => parse_bbc($option[0]),
  1122. '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') . '" />'
  1123. );
  1124. }
  1125. $return['allowed_warning'] = $row['max_votes'] > 1 ? sprintf($txt['poll_options6'], min(count($options), $row['max_votes'])) : '';
  1126. if ($output_method != 'echo')
  1127. return $return;
  1128. if ($return['allow_vote'])
  1129. {
  1130. echo '
  1131. <form class="ssi_poll" action="', $boardurl, '/SSI.php?ssi_function=pollVote" method="post" accept-charset="', $context['character_set'], '">
  1132. <strong>', $return['question'], '</strong><br />
  1133. ', !empty($return['allowed_warning']) ? $return['allowed_warning'] . '<br />' : '';
  1134. foreach ($return['options'] as $option)
  1135. echo '
  1136. <label for="', $option['id'], '">', $option['vote_button'], ' ', $option['option'], '</label><br />';
  1137. echo '
  1138. <input type="submit" value="', $txt['poll_vote'], '" class="button_submit" />
  1139. <input type="hidden" name="poll" value="', $return['id'], '" />
  1140. <input type="hidden" name="', $context['session_var'], '" value="', $context['session_id'], '" />
  1141. </form>';
  1142. }
  1143. elseif ($return['allow_view_results'])
  1144. {
  1145. echo '
  1146. <div class="ssi_poll">
  1147. <strong>', $return['question'], '</strong>
  1148. <dl>';
  1149. foreach ($return['options'] as $option)
  1150. echo '
  1151. <dt>', $option['option'], '</dt>
  1152. <dd>
  1153. <div class="ssi_poll_bar" style="border: 1px solid #666; height: 1em">
  1154. <div class="ssi_poll_bar_fill" style="background: #ccf; height: 1em; width: ', $option['percent'], '%;">
  1155. </div>
  1156. </div>
  1157. ', $option['votes'], ' (', $option['percent'], '%)
  1158. </dd>';
  1159. echo '
  1160. </dl>
  1161. <strong>', $txt['poll_total_voters'], ': ', $return['total_votes'], '</strong>
  1162. </div>';
  1163. }
  1164. // Cannot see it I'm afraid!
  1165. else
  1166. echo $txt['poll_cannot_see'];
  1167. }
  1168. // Takes care of voting - don't worry, this is done automatically.
  1169. function ssi_pollVote()
  1170. {
  1171. global $context, $db_prefix, $user_info, $sc

Large files files are truncated, but you can click here to view the full file