SSI.php 74 KB

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