PageRenderTime 37ms CodeModel.GetById 23ms 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
  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, $smcFunc, $sourcedir, $modSettings;
  1172. if (!isset($_POST[$context['session_var']]) || $_POST[$context['session_var']] != $sc || empty($_POST['options']) || !isset($_POST['poll']))
  1173. {
  1174. echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  1175. <html>
  1176. <head>
  1177. <script type="text/javascript"><!-- // --><![CDATA[
  1178. history.go(-1);
  1179. // ]]></script>
  1180. </head>
  1181. <body>&laquo;</body>
  1182. </html>';
  1183. return;
  1184. }
  1185. // This can cause weird errors! (ie. copyright missing.)
  1186. checkSession();
  1187. $_POST['poll'] = (int) $_POST['poll'];
  1188. // Check if they have already voted, or voting is locked.
  1189. $request = $smcFunc['db_query']('', '
  1190. SELECT
  1191. p.id_poll, p.voting_locked, p.expire_time, p.max_votes, p.guest_vote,
  1192. t.id_topic,
  1193. IFNULL(lp.id_choice, -1) AS selected
  1194. FROM {db_prefix}polls AS p
  1195. INNER JOIN {db_prefix}topics AS t ON (t.id_poll = {int:current_poll})
  1196. INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board)
  1197. LEFT JOIN {db_prefix}log_polls AS lp ON (lp.id_poll = p.id_poll AND lp.id_member = {int:current_member})
  1198. WHERE p.id_poll = {int:current_poll}
  1199. AND {query_see_board}' . ($modSettings['postmod_active'] ? '
  1200. AND t.approved = {int:is_approved}' : '') . '
  1201. LIMIT 1',
  1202. array(
  1203. 'current_member' => $user_info['id'],
  1204. 'current_poll' => $_POST['poll'],
  1205. 'is_approved' => 1,
  1206. )
  1207. );
  1208. if ($smcFunc['db_num_rows']($request) == 0)
  1209. die;
  1210. $row = $smcFunc['db_fetch_assoc']($request);
  1211. $smcFunc['db_free_result']($request);
  1212. if (!empty($row['voting_locked']) || ($row['selected'] != -1 && !$user_info['is_guest']) || (!empty($row['expire_time']) && time() > $row['expire_time']))
  1213. redirectexit('topic=' . $row['id_topic'] . '.0');
  1214. // Too many options checked?
  1215. if (count($_REQUEST['options']) > $row['max_votes'])
  1216. redirectexit('topic=' . $row['id_topic'] . '.0');
  1217. // It's a guest who has already voted?
  1218. if ($user_info['is_guest'])
  1219. {
  1220. // Guest voting disabled?
  1221. if (!$row['guest_vote'])
  1222. redirectexit('topic=' . $row['id_topic'] . '.0');
  1223. // Already voted?
  1224. elseif (isset($_COOKIE['guest_poll_vote']) && in_array($row['id_poll'], explode(',', $_COOKIE['guest_poll_vote'])))
  1225. redirectexit('topic=' . $row['id_topic'] . '.0');
  1226. }
  1227. $options = array();
  1228. $inserts = array();
  1229. foreach ($_REQUEST['options'] as $id)
  1230. {
  1231. $id = (int) $id;
  1232. $options[] = $id;
  1233. $inserts[] = array($_POST['poll'], $user_info['id'], $id);
  1234. }
  1235. // Add their vote in to the tally.
  1236. $smcFunc['db_insert']('insert',
  1237. $db_prefix . 'log_polls',
  1238. array('id_poll' => 'int', 'id_member' => 'int', 'id_choice' => 'int'),
  1239. $inserts,
  1240. array('id_poll', 'id_member', 'id_choice')
  1241. );
  1242. $smcFunc['db_query']('', '
  1243. UPDATE {db_prefix}poll_choices
  1244. SET votes = votes + 1
  1245. WHERE id_poll = {int:current_poll}
  1246. AND id_choice IN ({array_int:option_list})',
  1247. array(
  1248. 'option_list' => $options,
  1249. 'current_poll' => $_POST['poll'],
  1250. )
  1251. );
  1252. // Track the vote if a guest.
  1253. if ($user_info['is_guest'])
  1254. {
  1255. $_COOKIE['guest_poll_vote'] = !empty($_COOKIE['guest_poll_vote']) ? ($_COOKIE['guest_poll_vote'] . ',' . $row['id_poll']) : $row['id_poll'];
  1256. require_once($sourcedir . '/Subs-Auth.php');
  1257. $cookie_url = url_parts(!empty($modSettings['localCookies']), !empty($modSettings['globalCookies']));
  1258. setcookie('guest_poll_vote', $_COOKIE['guest_poll_vote'], time() + 2500000, $cookie_url[1], $cookie_url[0], 0);
  1259. }
  1260. redirectexit('topic=' . $row['id_topic'] . '.0');
  1261. }
  1262. // Show a search box.
  1263. function ssi_quickSearch($output_method = 'echo')
  1264. {
  1265. global $scripturl, $txt, $context;
  1266. if ($output_method != 'echo')
  1267. return $scripturl . '?action=search';
  1268. echo '
  1269. <form action="', $scripturl, '?action=search2" method="post" accept-charset="', $context['character_set'], '">
  1270. <input type="hidden" name="advanced" value="0" /><input type="text" name="search" size="30" class="input_text" /> <input type="submit" name="submit" value="', $txt['search'], '" class="button_submit" />
  1271. </form>';
  1272. }
  1273. // Show what would be the forum news.
  1274. function ssi_news($output_method = 'echo')
  1275. {
  1276. global $context;
  1277. if ($output_method != 'echo')
  1278. return $context['random_news_line'];
  1279. echo $context['random_news_line'];
  1280. }
  1281. // Show today's birthdays.
  1282. function ssi_todaysBirthdays($output_method = 'echo')
  1283. {
  1284. global $scripturl, $modSettings, $user_info;
  1285. if (empty($modSettings['cal_enabled']) || !allowedTo('calendar_view') || !allowedTo('profile_view_any'))
  1286. return;
  1287. $eventOptions = array(
  1288. 'include_birthdays' => true,
  1289. 'num_days_shown' => empty($modSettings['cal_days_for_index']) || $modSettings['cal_days_for_index'] < 1 ? 1 : $modSettings['cal_days_for_index'],
  1290. );
  1291. $return = cache_quick_get('calendar_index_offset_' . ($user_info['time_offset'] + $modSettings['time_offset']), 'Subs-Calendar.php', 'cache_getRecentEvents', array($eventOptions));
  1292. if ($output_method != 'echo')
  1293. return $return['calendar_birthdays'];
  1294. foreach ($return['calendar_birthdays'] as $member)
  1295. echo '
  1296. <a href="', $scripturl, '?action=profile;u=', $member['id'], '">' . $member['name'] . (isset($member['age']) ? ' (' . $member['age'] . ')' : '') . '</a>' . (!$member['is_last'] ? ', ' : '');
  1297. }
  1298. // Show today's holidays.
  1299. function ssi_todaysHolidays($output_method = 'echo')
  1300. {
  1301. global $modSettings, $user_info;
  1302. if (empty($modSettings['cal_enabled']) || !allowedTo('calendar_view'))
  1303. return;
  1304. $eventOptions = array(
  1305. 'include_holidays' => true,
  1306. 'num_days_shown' => empty($modSettings['cal_days_for_index']) || $modSettings['cal_days_for_index'] < 1 ? 1 : $modSettings['cal_days_for_index'],
  1307. );
  1308. $return = cache_quick_get('calendar_index_offset_' . ($user_info['time_offset'] + $modSettings['time_offset']), 'Subs-Calendar.php', 'cache_getRecentEvents', array($eventOptions));
  1309. if ($output_method != 'echo')
  1310. return $return['calendar_holidays'];
  1311. echo '
  1312. ', implode(', ', $return['calendar_holidays']);
  1313. }
  1314. // Show today's events.
  1315. function ssi_todaysEvents($output_method = 'echo')
  1316. {
  1317. global $modSettings, $user_info;
  1318. if (empty($modSettings['cal_enabled']) || !allowedTo('calendar_view'))
  1319. return;
  1320. $eventOptions = array(
  1321. 'include_events' => true,
  1322. 'num_days_shown' => empty($modSettings['cal_days_for_index']) || $modSettings['cal_days_for_index'] < 1 ? 1 : $modSettings['cal_days_for_index'],
  1323. );
  1324. $return = cache_quick_get('calendar_index_offset_' . ($user_info['time_offset'] + $modSettings['time_offset']), 'Subs-Calendar.php', 'cache_getRecentEvents', array($eventOptions));
  1325. if ($output_method != 'echo')
  1326. return $return['calendar_events'];
  1327. foreach ($return['calendar_events'] as $event)
  1328. {
  1329. if ($event['can_edit'])
  1330. echo '
  1331. <a href="' . $event['modify_href'] . '" style="color: #ff0000;">*</a> ';
  1332. echo '
  1333. ' . $event['link'] . (!$event['is_last'] ? ', ' : '');
  1334. }
  1335. }
  1336. // Show all calendar entires for today. (birthdays, holodays, and events.)
  1337. function ssi_todaysCalendar($output_method = 'echo')
  1338. {
  1339. global $modSettings, $txt, $scripturl, $user_info;
  1340. $eventOptions = array(
  1341. 'include_birthdays' => allowedTo('profile_view_any'),
  1342. 'include_holidays' => true,
  1343. 'include_events' => true,
  1344. 'num_days_shown' => empty($modSettings['cal_days_for_index']) || $modSettings['cal_days_for_index'] < 1 ? 1 : $modSettings['cal_days_for_index'],
  1345. );
  1346. $return = cache_quick_get('calendar_index_offset_' . ($user_info['time_offset'] + $modSettings['time_offset']), 'Subs-Calendar.php', 'cache_getRecentEvents', array($eventOptions));
  1347. if ($output_method != 'echo')
  1348. return $return;
  1349. if (!empty($return['calendar_holidays']))
  1350. echo '
  1351. <span class="holiday">' . $txt['calendar_prompt'] . ' ' . implode(', ', $return['calendar_holidays']) . '<br /></span>';
  1352. if (!empty($return['calendar_birthdays']))
  1353. {
  1354. echo '
  1355. <span class="birthday">' . $txt['birthdays_upcoming'] . '</span> ';
  1356. foreach ($return['calendar_birthdays'] as $member)
  1357. echo '
  1358. <a href="', $scripturl, '?action=profile;u=', $member['id'], '">', $member['name'], isset($member['age']) ? ' (' . $member['age'] . ')' : '', '</a>', !$member['is_last'] ? ', ' : '';
  1359. echo '
  1360. <br />';
  1361. }
  1362. if (!empty($return['calendar_events']))
  1363. {
  1364. echo '
  1365. <span class="event">' . $txt['events_upcoming'] . '</span> ';
  1366. foreach ($return['calendar_events'] as $event)
  1367. {
  1368. if ($event['can_edit'])
  1369. echo '
  1370. <a href="' . $event['modify_href'] . '" style="color: #ff0000;">*</a> ';
  1371. echo '
  1372. ' . $event['link'] . (!$event['is_last'] ? ', ' : '');
  1373. }
  1374. }
  1375. }
  1376. // Show the latest news, with a template... by board.
  1377. function ssi_boardNews($board = null, $limit = null, $start = null, $length = null, $output_method = 'echo')
  1378. {
  1379. global $scripturl, $db_prefix, $txt, $settings, $modSettings, $context;
  1380. global $smcFunc;
  1381. loadLanguage('Stats');
  1382. // Must be integers....
  1383. if ($limit === null)
  1384. $limit = isset($_GET['limit']) ? (int) $_GET['limit'] : 5;
  1385. else
  1386. $limit = (int) $limit;
  1387. if ($start === null)
  1388. $start = isset($_GET['start']) ? (int) $_GET['start'] : 0;
  1389. else
  1390. $start = (int) $start;
  1391. if ($board !== null)
  1392. $board = (int) $board;
  1393. elseif (isset($_GET['board']))
  1394. $board = (int) $_GET['board'];
  1395. if ($length === null)
  1396. $length = isset($_GET['length']) ? (int) $_GET['length'] : 0;
  1397. else
  1398. $length = (int) $length;
  1399. $limit = max(0, $limit);
  1400. $start = max(0, $start);
  1401. // Make sure guests can see this board.
  1402. $request = $smcFunc['db_query']('', '
  1403. SELECT id_board
  1404. FROM {db_prefix}boards
  1405. WHERE ' . ($board === null ? '' : 'id_board = {int:current_board}
  1406. AND ') . 'FIND_IN_SET(-1, member_groups) != 0
  1407. LIMIT 1',
  1408. array(
  1409. 'current_board' => $board,
  1410. )
  1411. );
  1412. if ($smcFunc['db_num_rows']($request) == 0)
  1413. {
  1414. if ($output_method == 'echo')
  1415. die($txt['ssi_no_guests']);
  1416. else
  1417. return array();
  1418. }
  1419. list ($board) = $smcFunc['db_fetch_row']($request);
  1420. $smcFunc['db_free_result']($request);
  1421. // Load the message icons - the usual suspects.
  1422. $stable_icons = array('xx', 'thumbup', 'thumbdown', 'exclamation', 'question', 'lamp', 'smiley', 'angry', 'cheesy', 'grin', 'sad', 'wink', 'moved', 'recycled', 'wireless');
  1423. $icon_sources = array();
  1424. foreach ($stable_icons as $icon)
  1425. $icon_sources[$icon] = 'images_url';
  1426. // Find the post ids.
  1427. $request = $smcFunc['db_query']('', '
  1428. SELECT t.id_first_msg
  1429. FROM {db_prefix}topics as t
  1430. LEFT JOIN {db_prefix}boards as b ON (b.id_board = t.id_board)
  1431. WHERE t.id_board = {int:current_board}' . ($modSettings['postmod_active'] ? '
  1432. AND t.approved = {int:is_approved}' : '') . '
  1433. AND {query_see_board}
  1434. ORDER BY t.id_first_msg DESC
  1435. LIMIT ' . $start . ', ' . $limit,
  1436. array(
  1437. 'current_board' => $board,
  1438. 'is_approved' => 1,
  1439. )
  1440. );
  1441. $posts = array();
  1442. while ($row = $smcFunc['db_fetch_assoc']($request))
  1443. $posts[] = $row['id_first_msg'];
  1444. $smcFunc['db_free_result']($request);
  1445. if (empty($posts))
  1446. return array();
  1447. // Find the posts.
  1448. $request = $smcFunc['db_query']('', '
  1449. SELECT
  1450. m.icon, m.subject, m.body, IFNULL(mem.real_name, m.poster_name) AS poster_name, m.poster_time,
  1451. t.num_replies, t.id_topic, m.id_member, m.smileys_enabled, m.id_msg, t.locked, t.id_last_msg
  1452. FROM {db_prefix}topics AS t
  1453. INNER JOIN {db_prefix}messages AS m ON (m.id_msg = t.id_first_msg)
  1454. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member)
  1455. WHERE t.id_first_msg IN ({array_int:post_list})
  1456. ORDER BY t.id_first_msg DESC
  1457. LIMIT ' . count($posts),
  1458. array(
  1459. 'post_list' => $posts,
  1460. )
  1461. );
  1462. $return = array();
  1463. while ($row = $smcFunc['db_fetch_assoc']($request))
  1464. {
  1465. // If we want to limit the length of the post.
  1466. if (!empty($length) && $smcFunc['strlen']($row['body']) > $length)
  1467. {
  1468. $row['body'] = $smcFunc['substr']($row['body'], 0, $length);
  1469. // The first space or line break. (<br />, etc.)
  1470. $cutoff = max(strrpos($row['body'], ' '), strrpos($row['body'], '<'));
  1471. if ($cutoff !== false)
  1472. $row['body'] = $smcFunc['substr']($row['body'], 0, $cutoff);
  1473. $row['body'] .= '...';
  1474. }
  1475. $row['body'] = parse_bbc($row['body'], $row['smileys_enabled'], $row['id_msg']);
  1476. // Check that this message icon is there...
  1477. if (empty($modSettings['messageIconChecks_disable']) && !isset($icon_sources[$row['icon']]))
  1478. $icon_sources[$row['icon']] = file_exists($settings['theme_dir'] . '/images/post/' . $row['icon'] . '.gif') ? 'images_url' : 'default_images_url';
  1479. censorText($row['subject']);
  1480. censorText($row['body']);
  1481. $return[] = array(
  1482. 'id' => $row['id_topic'],
  1483. 'message_id' => $row['id_msg'],
  1484. 'icon' => '<img src="' . $settings[$icon_sources[$row['icon']]] . '/post/' . $row['icon'] . '.gif" alt="' . $row['icon'] . '" />',
  1485. 'subject' => $row['subject'],
  1486. 'time' => timeformat($row['poster_time']),
  1487. 'timestamp' => forum_time(true, $row['poster_time']),
  1488. 'body' => $row['body'],
  1489. 'href' => $scripturl . '?topic=' . $row['id_topic'] . '.0',
  1490. 'link' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.0">' . $row['num_replies'] . ' ' . ($row['num_replies'] == 1 ? $txt['ssi_comment'] : $txt['ssi_comments']) . '</a>',
  1491. 'replies' => $row['num_replies'],
  1492. 'comment_href' => !empty($row['locked']) ? '' : $scripturl . '?action=post;topic=' . $row['id_topic'] . '.' . $row['num_replies'] . ';last_msg=' . $row['id_last_msg'],
  1493. 'comment_link' => !empty($row['locked']) ? '' : '<a href="' . $scripturl . '?action=post;topic=' . $row['id_topic'] . '.' . $row['num_replies'] . ';last_msg=' . $row['id_last_msg'] . '">' . $txt['ssi_write_comment'] . '</a>',
  1494. 'new_comment' => !empty($row['locked']) ? '' : '<a href="' . $scripturl . '?action=post;topic=' . $row['id_topic'] . '.' . $row['num_replies'] . '">' . $txt['ssi_write_comment'] . '</a>',
  1495. 'poster' => array(
  1496. 'id' => $row['id_member'],
  1497. 'name' => $row['poster_name'],
  1498. 'href' => !empty($row['id_member']) ? $scripturl . '?action=profile;u=' . $row['id_member'] : '',
  1499. 'link' => !empty($row['id_member']) ? '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['poster_name'] . '</a>' : $row['poster_name']
  1500. ),
  1501. 'locked' => !empty($row['locked']),
  1502. 'is_last' => false
  1503. );
  1504. }
  1505. $smcFunc['db_free_result']($request);
  1506. if (empty($return))
  1507. return $return;
  1508. $return[count($return) - 1]['is_last'] = true;
  1509. if ($output_method != 'echo')
  1510. return $return;
  1511. foreach ($return as $news)
  1512. {
  1513. echo '
  1514. <div class="news_item">
  1515. <h3 class="news_header">
  1516. ', $news['icon'], '
  1517. <a href="', $news['href'], '">', $news['subject'], '</a>
  1518. </h3>
  1519. <div class="news_timestamp">', $news['time'], ' ', $txt['by'], ' ', $news['poster']['link'], '</div>
  1520. <div class="news_body" style="padding: 2ex 0;">', $news['body'], '</div>
  1521. ', $news['link'], $news['locked'] ? '' : ' | ' . $news['comment_link'], '
  1522. </div>';
  1523. if (!$news['is_last'])
  1524. echo '
  1525. <hr />';
  1526. }
  1527. }
  1528. // Show the most recent events.
  1529. function ssi_recentEvents($max_events = 7, $output_method = 'echo')
  1530. {
  1531. global $db_prefix, $user_info, $scripturl, $modSettings, $txt, $context, $smcFunc;
  1532. if (empty($modSettings['cal_enabled']) || !allowedTo('calendar_view'))
  1533. return;
  1534. // Find all events which are happening in the near future that the member can see.
  1535. $request = $smcFunc['db_query']('', '
  1536. SELECT
  1537. cal.id_event, cal.start_date, cal.end_date, cal.title, cal.id_member, cal.id_topic,
  1538. cal.id_board, t.id_first_msg, t.approved
  1539. FROM {db_prefix}calendar AS cal
  1540. LEFT JOIN {db_prefix}boards AS b ON (b.id_board = cal.id_board)
  1541. LEFT JOIN {db_prefix}topics AS t ON (t.id_topic = cal.id_topic)
  1542. WHERE cal.start_date <= {date:current_date}
  1543. AND cal.end_date >= {date:current_date}
  1544. AND (cal.id_board = {int:no_board} OR {query_wanna_see_board})
  1545. ORDER BY cal.start_date DESC
  1546. LIMIT ' . $max_events,
  1547. array(
  1548. 'current_date' => strftime('%Y-%m-%d', forum_time(false)),
  1549. 'no_board' => 0,
  1550. )
  1551. );
  1552. $return = array();
  1553. $duplicates = array();
  1554. while ($row = $smcFunc['db_fetch_assoc']($request))
  1555. {
  1556. // Check if we've already come by an event linked to this same topic with the same title... and don't display it if we have.
  1557. if (!empty($duplicates[$row['title'] . $row['id_topic']]))
  1558. continue;
  1559. // Censor the title.
  1560. censorText($row['title']);
  1561. if ($row['start_date'] < strftime('%Y-%m-%d', forum_time(false)))
  1562. $date = strftime('%Y-%m-%d', forum_time(false));
  1563. else
  1564. $date = $row['start_date'];
  1565. // If the topic it is attached to is not approved then don't link it.
  1566. if (!empty($row['id_first_msg']) && !$row['approved'])
  1567. $row['id_board'] = $row['id_topic'] = $row['id_first_msg'] = 0;
  1568. $return[$date][] = array(
  1569. 'id' => $row['id_event'],
  1570. 'title' => $row['title'],
  1571. 'can_edit' => allowedTo('calendar_edit_any') || ($row['id_member'] == $user_info['id'] && allowedTo('calendar_edit_own')),
  1572. 'modify_href' => $scripturl . '?action=' . ($row['id_board'] == 0 ? 'calendar;sa=post;' : 'post;msg=' . $row['id_first_msg'] . ';topic=' . $row['id_topic'] . '.0;calendar;') . 'eventid=' . $row['id_event'] . ';' . $context['session_var'] . '=' . $context['session_id'],
  1573. 'href' => $row['id_board'] == 0 ? '' : $scripturl . '?topic=' . $row['id_topic'] . '.0',
  1574. 'link' => $row['id_board'] == 0 ? $row['title'] : '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.0">' . $row['title'] . '</a>',
  1575. 'start_date' => $row['start_date'],
  1576. 'end_date' => $row['end_date'],
  1577. 'is_last' => false
  1578. );
  1579. // Let's not show this one again, huh?
  1580. $duplicates[$row['title'] . $row['id_topic']] = true;
  1581. }
  1582. $smcFunc['db_free_result']($request);
  1583. foreach ($return as $mday => $array)
  1584. $return[$mday][count($array) - 1]['is_last'] = true;
  1585. if ($output_method != 'echo' || empty($return))
  1586. return $return;
  1587. // Well the output method is echo.
  1588. echo '
  1589. <span class="event">' . $txt['events'] . '</span> ';
  1590. foreach ($return as $mday => $array)
  1591. foreach ($array as $event)
  1592. {
  1593. if ($event['can_edit'])
  1594. echo '
  1595. <a href="' . $event['modify_href'] . '" style="color: #ff0000;">*</a> ';
  1596. echo '
  1597. ' . $event['link'] . (!$event['is_last'] ? ', ' : '');
  1598. }
  1599. }
  1600. // Check the passed id_member/password. If $is_username is true, treats $id as a username.
  1601. function ssi_checkPassword($id = null, $password = null, $is_username = false)
  1602. {
  1603. global $db_prefix, $sourcedir, $smcFunc;
  1604. // If $id is null, this was most likely called from a query string and should do nothing.
  1605. if ($id === null)
  1606. return;
  1607. $request = $smcFunc['db_query']('', '
  1608. SELECT passwd, member_name, is_activated
  1609. FROM {db_prefix}members
  1610. WHERE ' . ($is_username ? 'member_name' : 'id_member') . ' = {string:id}
  1611. LIMIT 1',
  1612. array(
  1613. 'id' => $id,
  1614. )
  1615. );
  1616. list ($pass, $user, $active) = $smcFunc['db_fetch_row']($request);
  1617. $smcFunc['db_free_result']($request);
  1618. return sha1(strtolower($user) . $password) == $pass && $active == 1;
  1619. }
  1620. // We want to show the recent attachments outside of the forum.
  1621. function ssi_recentAttachments($num_attachments = 10, $attachment_ext = array(), $output_method = 'echo')
  1622. {
  1623. global $smcFunc, $context, $modSettings, $scripturl, $txt, $settings;
  1624. // We want to make sure that we only get attachments for boards that we can see *if* any.
  1625. $attachments_boards = boardsAllowedTo('view_attachments');
  1626. // No boards? Adios amigo.
  1627. if (empty($attachments_boards))
  1628. return array();
  1629. // Is it an array?
  1630. if (!is_array($attachment_ext))
  1631. $attachment_ext = array($attachment_ext);
  1632. // Lets build the query.
  1633. $request = $smcFunc['db_query']('', '
  1634. SELECT
  1635. att.id_attach, att.id_msg, att.filename, IFNULL(att.size, 0) AS filesize, att.downloads, mem.id_member,
  1636. IFNULL(mem.real_name, m.poster_name) AS poster_name, m.id_topic, m.subject, t.id_board, m.poster_time,
  1637. att.width, att.height' . (empty($modSettings['attachmentShowImages']) || empty($modSettings['attachmentThumbnails']) ? '' : ', IFNULL(thumb.id_attach, 0) AS id_thumb, thumb.width AS thumb_width, thumb.height AS thumb_height') . '
  1638. FROM {db_prefix}attachments AS att
  1639. INNER JOIN {db_prefix}messages AS m ON (m.id_msg = att.id_msg)
  1640. INNER JOIN {db_prefix}topics AS t ON (t.id_topic = m.id_topic)
  1641. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member)' . (empty($modSettings['attachmentShowImages']) || empty($modSettings['attachmentThumbnails']) ? '' : '
  1642. LEFT JOIN {db_prefix}attachments AS thumb ON (thumb.id_attach = att.id_thumb)') . '
  1643. WHERE att.attachment_type = 0' . ($attachments_boards === array(0) ? '' : '
  1644. AND m.id_board IN ({array_int:boards_can_see})') . (!empty($attachment_ext) ? '
  1645. AND att.fileext IN ({array_string:attachment_ext})' : '') .
  1646. (!$modSettings['postmod_active'] || allowedTo('approve_posts') ? '' : '
  1647. AND t.approved = {int:is_approved}
  1648. AND m.approved = {int:is_approved}
  1649. AND att.approved = {int:is_approved}') . '
  1650. ORDER BY att.id_attach DESC
  1651. LIMIT {int:num_attachments}',
  1652. array(
  1653. 'boards_can_see' => $attachments_boards,
  1654. 'attachment_ext' => $attachment_ext,
  1655. 'num_attachments' => $num_attachments,
  1656. 'is_approved' => 1,
  1657. )
  1658. );
  1659. // We have something.
  1660. $attachments = array();
  1661. while ($row = $smcFunc['db_fetch_assoc']($request))
  1662. {
  1663. $filename = preg_replace('~&amp;#(\\d{1,7}|x[0-9a-fA-F]{1,6});~', '&#\\1;', htmlspecialchars($row['filename']));
  1664. // Is it an image?
  1665. $attachments[$row['id_attach']] = array(
  1666. 'member' => array(
  1667. 'id' => $row['id_member'],
  1668. 'name' => $row['poster_name'],
  1669. 'link' => empty($row['id_member']) ? $row['poster_name'] : '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['poster_name'] . '</a>',
  1670. ),
  1671. 'file' => array(
  1672. 'filename' => $filename,
  1673. 'filesize' => round($row['filesize'] /1024, 2) . $txt['kilobyte'],
  1674. 'downloads' => $row['downloads'],
  1675. 'href' => $scripturl . '?action=dlattach;topic=' . $row['id_topic'] . '.0;attach=' . $row['id_attach'],
  1676. 'link' => '<img src="' . $settings['images_url'] . '/icons/clip.gif" alt="" /> <a href="' . $scripturl . '?action=dlattach;topic=' . $row['id_topic'] . '.0;attach=' . $row['id_attach'] . '">' . $filename . '</a>',
  1677. 'is_image' => !empty($row['width']) && !empty($row['height']) && !empty($modSettings['attachmentShowImages']),
  1678. ),
  1679. 'topic' => array(
  1680. 'id' => $row['id_topic'],
  1681. 'subject' => $row['subject'],
  1682. 'href' => $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . '#msg' . $row['id_msg'],
  1683. 'link' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . '#msg' . $row['id_msg'] . '">' . $row['subject'] . '</a>',
  1684. 'time' => timeformat($row['poster_time']),
  1685. ),
  1686. );
  1687. // Images.
  1688. if ($attachments[$row['id_attach']]['file']['is_image'])
  1689. {
  1690. $id_thumb = empty($row['id_thumb']) ? $row['id_attach'] : $row['id_thumb'];
  1691. $attachments[$row['id_attach']]['file']['image'] = array(
  1692. 'id' => $id_thumb,
  1693. 'width' => $row['width'],
  1694. 'height' => $row['height'],
  1695. 'img' => '<img src="' . $scripturl . '?action=dlattach;topic=' . $row['id_topic'] . '.0;attach=' . $row['id_attach'] . ';image" alt="' . $filename . '" />',
  1696. 'thumb' => '<img src="' . $scripturl . '?action=dlattach;topic=' . $row['id_topic'] . '.0;attach=' . $id_thumb . ';image" alt="' . $filename . '" />',
  1697. 'href' => $scripturl . '?action=dlattach;topic=' . $row['id_topic'] . '.0;attach=' . $id_thumb . ';image',
  1698. 'link' => '<a href="' . $scripturl . '?action=dlattach;topic=' . $row['id_topic'] . '.0;attach=' . $row['id_attach'] . ';image"><img src="' . $scripturl . '?action=dlattach;topic=' . $row['id_topic'] . '.0;attach=' . $id_thumb . ';image" alt="' . $filename . '" /></a>',
  1699. );
  1700. }
  1701. }
  1702. $smcFunc['db_free_result']($request);
  1703. // So you just want an array? Here you can have it.
  1704. if ($output_method == 'array' || empty($attachments))
  1705. return $attachments;
  1706. // Give them the default.
  1707. echo '
  1708. <table class="ssi_downloads" cellpadding="2">
  1709. <tr>
  1710. <th align="left">', $txt['file'], '</th>
  1711. <th align="left">', $txt['posted_by'], '</th>
  1712. <th align="left">', $txt['downloads'], '</th>
  1713. <th align="left">', $txt['filesize'], '</th>
  1714. </tr>';
  1715. foreach ($attachments as $attach)
  1716. echo '
  1717. <tr>
  1718. <td>', $attach['file']['link'], '</td>
  1719. <td>', $attach['member']['link'], '</td>
  1720. <td align="center">', $attach['file']['downloads'], '</td>
  1721. <td>', $attach['file']['filesize'], '</td>
  1722. </tr>';
  1723. echo '
  1724. </table>';
  1725. }
  1726. ?>