SSI.php 73 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038
  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. 'option' => parse_bbc($option[0]),
  1012. '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') . '" />'
  1013. );
  1014. }
  1015. $return['allowed_warning'] = $row['max_votes'] > 1 ? sprintf($txt['poll_options6'], min(count($options), $row['max_votes'])) : '';
  1016. if ($output_method != 'echo')
  1017. return $return;
  1018. if ($allow_view_results)
  1019. {
  1020. echo '
  1021. <form class="ssi_poll" action="', $boardurl, '/SSI.php?ssi_function=pollVote" method="post" accept-charset="', $context['character_set'], '">
  1022. <strong>', $return['question'], '</strong><br />
  1023. ', !empty($return['allowed_warning']) ? $return['allowed_warning'] . '<br />' : '';
  1024. foreach ($return['options'] as $option)
  1025. echo '
  1026. <label for="', $option['id'], '">', $option['vote_button'], ' ', $option['option'], '</label><br />';
  1027. echo '
  1028. <input type="submit" value="', $txt['poll_vote'], '" class="button_submit" />
  1029. <input type="hidden" name="poll" value="', $return['id'], '" />
  1030. <input type="hidden" name="', $context['session_var'], '" value="', $context['session_id'], '" />
  1031. </form>';
  1032. }
  1033. else
  1034. echo $txt['poll_cannot_see'];
  1035. }
  1036. function ssi_showPoll($topic = null, $output_method = 'echo')
  1037. {
  1038. global $txt, $settings, $boardurl, $user_info, $context, $smcFunc, $modSettings;
  1039. $boardsAllowed = boardsAllowedTo('poll_view');
  1040. if (empty($boardsAllowed))
  1041. return array();
  1042. if ($topic === null && isset($_REQUEST['ssi_topic']))
  1043. $topic = (int) $_REQUEST['ssi_topic'];
  1044. else
  1045. $topic = (int) $topic;
  1046. $request = $smcFunc['db_query']('', '
  1047. SELECT
  1048. p.id_poll, p.question, p.voting_locked, p.hide_results, p.expire_time, p.max_votes, p.guest_vote, b.id_board
  1049. FROM {db_prefix}topics AS t
  1050. INNER JOIN {db_prefix}polls AS p ON (p.id_poll = t.id_poll)
  1051. INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board)
  1052. WHERE t.id_topic = {int:current_topic}
  1053. AND {query_see_board}' . (!in_array(0, $boardsAllowed) ? '
  1054. AND b.id_board IN ({array_int:boards_allowed_see})' : '') . ($modSettings['postmod_active'] ? '
  1055. AND t.approved = {int:is_approved}' : '') . '
  1056. LIMIT 1',
  1057. array(
  1058. 'current_topic' => $topic,
  1059. 'boards_allowed_see' => $boardsAllowed,
  1060. 'is_approved' => 1,
  1061. )
  1062. );
  1063. // Either this topic has no poll, or the user cannot view it.
  1064. if ($smcFunc['db_num_rows']($request) == 0)
  1065. return array();
  1066. $row = $smcFunc['db_fetch_assoc']($request);
  1067. $smcFunc['db_free_result']($request);
  1068. // Check if they can vote.
  1069. if (!empty($row['expire_time']) && $row['expire_time'] < time())
  1070. $allow_vote = false;
  1071. elseif ($user_info['is_guest'] && $row['guest_vote'] && (!isset($_COOKIE['guest_poll_vote']) || !in_array($row['id_poll'], explode(',', $_COOKIE['guest_poll_vote']))))
  1072. $allow_vote = true;
  1073. elseif ($user_info['is_guest'])
  1074. $allow_vote = false;
  1075. elseif (!empty($row['voting_locked']) || !allowedTo('poll_vote', $row['id_board']))
  1076. $allow_vote = false;
  1077. else
  1078. {
  1079. $request = $smcFunc['db_query']('', '
  1080. SELECT id_member
  1081. FROM {db_prefix}log_polls
  1082. WHERE id_poll = {int:current_poll}
  1083. AND id_member = {int:current_member}
  1084. LIMIT 1',
  1085. array(
  1086. 'current_member' => $user_info['id'],
  1087. 'current_poll' => $row['id_poll'],
  1088. )
  1089. );
  1090. $allow_vote = $smcFunc['db_num_rows']($request) == 0;
  1091. $smcFunc['db_free_result']($request);
  1092. }
  1093. // Can they view?
  1094. $is_expired = !empty($row['expire_time']) && $row['expire_time'] < time();
  1095. $allow_view_results = allowedTo('moderate_board') || $row['hide_results'] == 0 || ($row['hide_results'] == 1 && !$allow_vote) || $is_expired;
  1096. $request = $smcFunc['db_query']('', '
  1097. SELECT COUNT(DISTINCT id_member)
  1098. FROM {db_prefix}log_polls
  1099. WHERE id_poll = {int:current_poll}',
  1100. array(
  1101. 'current_poll' => $row['id_poll'],
  1102. )
  1103. );
  1104. list ($total) = $smcFunc['db_fetch_row']($request);
  1105. $smcFunc['db_free_result']($request);
  1106. $request = $smcFunc['db_query']('', '
  1107. SELECT id_choice, label, votes
  1108. FROM {db_prefix}poll_choices
  1109. WHERE id_poll = {int:current_poll}',
  1110. array(
  1111. 'current_poll' => $row['id_poll'],
  1112. )
  1113. );
  1114. $options = array();
  1115. $total_votes = 0;
  1116. while ($rowChoice = $smcFunc['db_fetch_assoc']($request))
  1117. {
  1118. censorText($rowChoice['label']);
  1119. $options[$rowChoice['id_choice']] = array($rowChoice['label'], $rowChoice['votes']);
  1120. $total_votes += $rowChoice['votes'];
  1121. }
  1122. $smcFunc['db_free_result']($request);
  1123. $return = array(
  1124. 'id' => $row['id_poll'],
  1125. 'image' => empty($row['voting_locked']) ? 'poll' : 'locked_poll',
  1126. 'question' => $row['question'],
  1127. 'total_votes' => $total,
  1128. 'is_locked' => !empty($row['voting_locked']),
  1129. 'allow_vote' => $allow_vote,
  1130. 'allow_view_results' => $allow_view_results,
  1131. 'topic' => $topic
  1132. );
  1133. // Calculate the percentages and bar lengths...
  1134. $divisor = $total_votes == 0 ? 1 : $total_votes;
  1135. foreach ($options as $i => $option)
  1136. {
  1137. $bar = floor(($option[1] * 100) / $divisor);
  1138. $barWide = $bar == 0 ? 1 : floor(($bar * 5) / 3);
  1139. $return['options'][$i] = array(
  1140. 'id' => 'options-' . $i,
  1141. 'percent' => $bar,
  1142. 'votes' => $option[1],
  1143. 'option' => parse_bbc($option[0]),
  1144. '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') . '" />'
  1145. );
  1146. }
  1147. $return['allowed_warning'] = $row['max_votes'] > 1 ? sprintf($txt['poll_options6'], min(count($options), $row['max_votes'])) : '';
  1148. if ($output_method != 'echo')
  1149. return $return;
  1150. if ($return['allow_vote'])
  1151. {
  1152. echo '
  1153. <form class="ssi_poll" action="', $boardurl, '/SSI.php?ssi_function=pollVote" method="post" accept-charset="', $context['character_set'], '">
  1154. <strong>', $return['question'], '</strong><br />
  1155. ', !empty($return['allowed_warning']) ? $return['allowed_warning'] . '<br />' : '';
  1156. foreach ($return['options'] as $option)
  1157. echo '
  1158. <label for="', $option['id'], '">', $option['vote_button'], ' ', $option['option'], '</label><br />';
  1159. echo '
  1160. <input type="submit" value="', $txt['poll_vote'], '" class="button_submit" />
  1161. <input type="hidden" name="poll" value="', $return['id'], '" />
  1162. <input type="hidden" name="', $context['session_var'], '" value="', $context['session_id'], '" />
  1163. </form>';
  1164. }
  1165. elseif ($return['allow_view_results'])
  1166. {
  1167. echo '
  1168. <div class="ssi_poll">
  1169. <strong>', $return['question'], '</strong>
  1170. <dl>';
  1171. foreach ($return['options'] as $option)
  1172. echo '
  1173. <dt>', $option['option'], '</dt>
  1174. <dd>
  1175. <div class="ssi_poll_bar" style="border: 1px solid #666; height: 1em">
  1176. <div class="ssi_poll_bar_fill" style="background: #ccf; height: 1em; width: ', $option['percent'], '%;">
  1177. </div>
  1178. </div>
  1179. ', $option['votes'], ' (', $option['percent'], '%)
  1180. </dd>';
  1181. echo '
  1182. </dl>
  1183. <strong>', $txt['poll_total_voters'], ': ', $return['total_votes'], '</strong>
  1184. </div>';
  1185. }
  1186. // Cannot see it I'm afraid!
  1187. else
  1188. echo $txt['poll_cannot_see'];
  1189. }
  1190. // Takes care of voting - don't worry, this is done automatically.
  1191. function ssi_pollVote()
  1192. {
  1193. global $context, $db_prefix, $user_info, $sc, $smcFunc, $sourcedir, $modSettings;
  1194. if (!isset($_POST[$context['session_var']]) || $_POST[$context['session_var']] != $sc || empty($_POST['options']) || !isset($_POST['poll']))
  1195. {
  1196. echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  1197. <html>
  1198. <head>
  1199. <script type="text/javascript"><!-- // --><![CDATA[
  1200. history.go(-1);
  1201. // ]]></script>
  1202. </head>
  1203. <body>&laquo;</body>
  1204. </html>';
  1205. return;
  1206. }
  1207. // This can cause weird errors! (ie. copyright missing.)
  1208. checkSession();
  1209. $_POST['poll'] = (int) $_POST['poll'];
  1210. // Check if they have already voted, or voting is locked.
  1211. $request = $smcFunc['db_query']('', '
  1212. SELECT
  1213. p.id_poll, p.voting_locked, p.expire_time, p.max_votes, p.guest_vote,
  1214. t.id_topic,
  1215. IFNULL(lp.id_choice, -1) AS selected
  1216. FROM {db_prefix}polls AS p
  1217. INNER JOIN {db_prefix}topics AS t ON (t.id_poll = {int:current_poll})
  1218. INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board)
  1219. LEFT JOIN {db_prefix}log_polls AS lp ON (lp.id_poll = p.id_poll AND lp.id_member = {int:current_member})
  1220. WHERE p.id_poll = {int:current_poll}
  1221. AND {query_see_board}' . ($modSettings['postmod_active'] ? '
  1222. AND t.approved = {int:is_approved}' : '') . '
  1223. LIMIT 1',
  1224. array(
  1225. 'current_member' => $user_info['id'],
  1226. 'current_poll' => $_POST['poll'],
  1227. 'is_approved' => 1,
  1228. )
  1229. );
  1230. if ($smcFunc['db_num_rows']($request) == 0)
  1231. die;
  1232. $row = $smcFunc['db_fetch_assoc']($request);
  1233. $smcFunc['db_free_result']($request);
  1234. if (!empty($row['voting_locked']) || ($row['selected'] != -1 && !$user_info['is_guest']) || (!empty($row['expire_time']) && time() > $row['expire_time']))
  1235. redirectexit('topic=' . $row['id_topic'] . '.0');
  1236. // Too many options checked?
  1237. if (count($_REQUEST['options']) > $row['max_votes'])
  1238. redirectexit('topic=' . $row['id_topic'] . '.0');
  1239. // It's a guest who has already voted?
  1240. if ($user_info['is_guest'])
  1241. {
  1242. // Guest voting disabled?
  1243. if (!$row['guest_vote'])
  1244. redirectexit('topic=' . $row['id_topic'] . '.0');
  1245. // Already voted?
  1246. elseif (isset($_COOKIE['guest_poll_vote']) && in_array($row['id_poll'], explode(',', $_COOKIE['guest_poll_vote'])))
  1247. redirectexit('topic=' . $row['id_topic'] . '.0');
  1248. }
  1249. $options = array();
  1250. $inserts = array();
  1251. foreach ($_REQUEST['options'] as $id)
  1252. {
  1253. $id = (int) $id;
  1254. $options[] = $id;
  1255. $inserts[] = array($_POST['poll'], $user_info['id'], $id);
  1256. }
  1257. // Add their vote in to the tally.
  1258. $smcFunc['db_insert']('insert',
  1259. $db_prefix . 'log_polls',
  1260. array('id_poll' => 'int', 'id_member' => 'int', 'id_choice' => 'int'),
  1261. $inserts,
  1262. array('id_poll', 'id_member', 'id_choice')
  1263. );
  1264. $smcFunc['db_query']('', '
  1265. UPDATE {db_prefix}poll_choices
  1266. SET votes = votes + 1
  1267. WHERE id_poll = {int:current_poll}
  1268. AND id_choice IN ({array_int:option_list})',
  1269. array(
  1270. 'option_list' => $options,
  1271. 'current_poll' => $_POST['poll'],
  1272. )
  1273. );
  1274. // Track the vote if a guest.
  1275. if ($user_info['is_guest'])
  1276. {
  1277. $_COOKIE['guest_poll_vote'] = !empty($_COOKIE['guest_poll_vote']) ? ($_COOKIE['guest_poll_vote'] . ',' . $row['id_poll']) : $row['id_poll'];
  1278. require_once($sourcedir . '/Subs-Auth.php');
  1279. $cookie_url = url_parts(!empty($modSettings['localCookies']), !empty($modSettings['globalCookies']));
  1280. smf_setcookie('guest_poll_vote', $_COOKIE['guest_poll_vote'], time() + 2500000, $cookie_url[1], $cookie_url[0], false, false);
  1281. }
  1282. redirectexit('topic=' . $row['id_topic'] . '.0');
  1283. }
  1284. // Show a search box.
  1285. function ssi_quickSearch($output_method = 'echo')
  1286. {
  1287. global $scripturl, $txt, $context;
  1288. if (!allowedTo('search_posts'))
  1289. return;
  1290. if ($output_method != 'echo')
  1291. return $scripturl . '?action=search';
  1292. echo '
  1293. <form action="', $scripturl, '?action=search2" method="post" accept-charset="', $context['character_set'], '">
  1294. <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" />
  1295. </form>';
  1296. }
  1297. // Show what would be the forum news.
  1298. function ssi_news($output_method = 'echo')
  1299. {
  1300. global $context;
  1301. if ($output_method != 'echo')
  1302. return $context['random_news_line'];
  1303. echo $context['random_news_line'];
  1304. }
  1305. // Show today's birthdays.
  1306. function ssi_todaysBirthdays($output_method = 'echo')
  1307. {
  1308. global $scripturl, $modSettings, $user_info;
  1309. if (empty($modSettings['cal_enabled']) || !allowedTo('calendar_view') || !allowedTo('profile_view'))
  1310. return;
  1311. $eventOptions = array(
  1312. 'include_birthdays' => true,
  1313. 'num_days_shown' => empty($modSettings['cal_days_for_index']) || $modSettings['cal_days_for_index'] < 1 ? 1 : $modSettings['cal_days_for_index'],
  1314. );
  1315. $return = cache_quick_get('calendar_index_offset_' . ($user_info['time_offset'] + $modSettings['time_offset']), 'Subs-Calendar.php', 'cache_getRecentEvents', array($eventOptions));
  1316. if ($output_method != 'echo')
  1317. return $return['calendar_birthdays'];
  1318. foreach ($return['calendar_birthdays'] as $member)
  1319. echo '
  1320. <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'] ? ', ' : '');
  1321. }
  1322. // Show today's holidays.
  1323. function ssi_todaysHolidays($output_method = 'echo')
  1324. {
  1325. global $modSettings, $user_info;
  1326. if (empty($modSettings['cal_enabled']) || !allowedTo('calendar_view'))
  1327. return;
  1328. $eventOptions = array(
  1329. 'include_holidays' => true,
  1330. 'num_days_shown' => empty($modSettings['cal_days_for_index']) || $modSettings['cal_days_for_index'] < 1 ? 1 : $modSettings['cal_days_for_index'],
  1331. );
  1332. $return = cache_quick_get('calendar_index_offset_' . ($user_info['time_offset'] + $modSettings['time_offset']), 'Subs-Calendar.php', 'cache_getRecentEvents', array($eventOptions));
  1333. if ($output_method != 'echo')
  1334. return $return['calendar_holidays'];
  1335. echo '
  1336. ', implode(', ', $return['calendar_holidays']);
  1337. }
  1338. // Show today's events.
  1339. function ssi_todaysEvents($output_method = 'echo')
  1340. {
  1341. global $modSettings, $user_info;
  1342. if (empty($modSettings['cal_enabled']) || !allowedTo('calendar_view'))
  1343. return;
  1344. $eventOptions = array(
  1345. 'include_events' => true,
  1346. 'num_days_shown' => empty($modSettings['cal_days_for_index']) || $modSettings['cal_days_for_index'] < 1 ? 1 : $modSettings['cal_days_for_index'],
  1347. );
  1348. $return = cache_quick_get('calendar_index_offset_' . ($user_info['time_offset'] + $modSettings['time_offset']), 'Subs-Calendar.php', 'cache_getRecentEvents', array($eventOptions));
  1349. if ($output_method != 'echo')
  1350. return $return['calendar_events'];
  1351. foreach ($return['calendar_events'] as $event)
  1352. {
  1353. if ($event['can_edit'])
  1354. echo '
  1355. <a href="' . $event['modify_href'] . '" style="color: #ff0000;">*</a> ';
  1356. echo '
  1357. ' . $event['link'] . (!$event['is_last'] ? ', ' : '');
  1358. }
  1359. }
  1360. // Show all calendar entires for today. (birthdays, holodays, and events.)
  1361. function ssi_todaysCalendar($output_method = 'echo')
  1362. {
  1363. global $modSettings, $txt, $scripturl, $user_info;
  1364. if (empty($modSettings['cal_enabled']) || !allowedTo('calendar_view'))
  1365. return;
  1366. $eventOptions = array(
  1367. 'include_birthdays' => allowedTo('profile_view'),
  1368. 'include_holidays' => true,
  1369. 'include_events' => true,
  1370. 'num_days_shown' => empty($modSettings['cal_days_for_index']) || $modSettings['cal_days_for_index'] < 1 ? 1 : $modSettings['cal_days_for_index'],
  1371. );
  1372. $return = cache_quick_get('calendar_index_offset_' . ($user_info['time_offset'] + $modSettings['time_offset']), 'Subs-Calendar.php', 'cache_getRecentEvents', array($eventOptions));
  1373. if ($output_method != 'echo')
  1374. return $return;
  1375. if (!empty($return['calendar_holidays']))
  1376. echo '
  1377. <span class="holiday">' . $txt['calendar_prompt'] . ' ' . implode(', ', $return['calendar_holidays']) . '<br /></span>';
  1378. if (!empty($return['calendar_birthdays']))
  1379. {
  1380. echo '
  1381. <span class="birthday">' . $txt['birthdays_upcoming'] . '</span> ';
  1382. foreach ($return['calendar_birthdays'] as $member)
  1383. echo '
  1384. <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'] ? ', ' : '';
  1385. echo '
  1386. <br />';
  1387. }
  1388. if (!empty($return['calendar_events']))
  1389. {
  1390. echo '
  1391. <span class="event">' . $txt['events_upcoming'] . '</span> ';
  1392. foreach ($return['calendar_events'] as $event)
  1393. {
  1394. if ($event['can_edit'])
  1395. echo '
  1396. <a href="' . $event['modify_href'] . '" style="color: #ff0000;">*</a> ';
  1397. echo '
  1398. ' . $event['link'] . (!$event['is_last'] ? ', ' : '');
  1399. }
  1400. }
  1401. }
  1402. // Show the latest news, with a template... by board.
  1403. function ssi_boardNews($board = null, $limit = null, $start = null, $length = null, $output_method = 'echo')
  1404. {
  1405. global $scripturl, $txt, $settings, $modSettings, $context;
  1406. global $smcFunc;
  1407. loadLanguage('Stats');
  1408. // Must be integers....
  1409. if ($limit === null)
  1410. $limit = isset($_GET['limit']) ? (int) $_GET['limit'] : 5;
  1411. else
  1412. $limit = (int) $limit;
  1413. if ($start === null)
  1414. $start = isset($_GET['start']) ? (int) $_GET['start'] : 0;
  1415. else
  1416. $start = (int) $start;
  1417. if ($board !== null)
  1418. $board = (int) $board;
  1419. elseif (isset($_GET['board']))
  1420. $board = (int) $_GET['board'];
  1421. if ($length === null)
  1422. $length = isset($_GET['length']) ? (int) $_GET['length'] : 0;
  1423. else
  1424. $length = (int) $length;
  1425. $limit = max(0, $limit);
  1426. $start = max(0, $start);
  1427. // Make sure guests can see this board.
  1428. $request = $smcFunc['db_query']('', '
  1429. SELECT id_board
  1430. FROM {db_prefix}boards
  1431. WHERE ' . ($board === null ? '' : 'id_board = {int:current_board}
  1432. AND ') . 'FIND_IN_SET(-1, member_groups) != 0
  1433. LIMIT 1',
  1434. array(
  1435. 'current_board' => $board,
  1436. )
  1437. );
  1438. if ($smcFunc['db_num_rows']($request) == 0)
  1439. {
  1440. if ($output_method == 'echo')
  1441. die($txt['ssi_no_guests']);
  1442. else
  1443. return array();
  1444. }
  1445. list ($board) = $smcFunc['db_fetch_row']($request);
  1446. $smcFunc['db_free_result']($request);
  1447. // Load the message icons - the usual suspects.
  1448. $stable_icons = array('xx', 'thumbup', 'thumbdown', 'exclamation', 'question', 'lamp', 'smiley', 'angry', 'cheesy', 'grin', 'sad', 'wink', 'poll', 'moved', 'recycled', 'wireless');
  1449. $icon_sources = array();
  1450. foreach ($stable_icons as $icon)
  1451. $icon_sources[$icon] = 'images_url';
  1452. // Find the post ids.
  1453. $request = $smcFunc['db_query']('', '
  1454. SELECT t.id_first_msg
  1455. FROM {db_prefix}topics as t
  1456. LEFT JOIN {db_prefix}boards as b ON (b.id_board = t.id_board)
  1457. WHERE t.id_board = {int:current_board}' . ($modSettings['postmod_active'] ? '
  1458. AND t.approved = {int:is_approved}' : '') . '
  1459. AND {query_see_board}
  1460. ORDER BY t.id_first_msg DESC
  1461. LIMIT ' . $start . ', ' . $limit,
  1462. array(
  1463. 'current_board' => $board,
  1464. 'is_approved' => 1,
  1465. )
  1466. );
  1467. $posts = array();
  1468. while ($row = $smcFunc['db_fetch_assoc']($request))
  1469. $posts[] = $row['id_first_msg'];
  1470. $smcFunc['db_free_result']($request);
  1471. if (empty($posts))
  1472. return array();
  1473. // Find the posts.
  1474. $request = $smcFunc['db_query']('', '
  1475. SELECT
  1476. m.icon, m.subject, m.body, IFNULL(mem.real_name, m.poster_name) AS poster_name, m.poster_time,
  1477. t.num_replies, t.id_topic, m.id_member, m.smileys_enabled, m.id_msg, t.locked, t.id_last_msg
  1478. FROM {db_prefix}topics AS t
  1479. INNER JOIN {db_prefix}messages AS m ON (m.id_msg = t.id_first_msg)
  1480. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member)
  1481. WHERE t.id_first_msg IN ({array_int:post_list})
  1482. ORDER BY t.id_first_msg DESC
  1483. LIMIT ' . count($posts),
  1484. array(
  1485. 'post_list' => $posts,
  1486. )
  1487. );
  1488. $return = array();
  1489. while ($row = $smcFunc['db_fetch_assoc']($request))
  1490. {
  1491. // If we want to limit the length of the post.
  1492. if (!empty($length) && $smcFunc['strlen']($row['body']) > $length)
  1493. {
  1494. $row['body'] = $smcFunc['substr']($row['body'], 0, $length);
  1495. $cutoff = false;
  1496. $last_space = strrpos($row['body'], ' ');
  1497. $last_open = strrpos($row['body'], '<');
  1498. $last_close = strrpos($row['body'], '>');
  1499. 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)
  1500. $cutoff = $last_open;
  1501. elseif (empty($last_close) || $last_close < $last_open)
  1502. $cutoff = $last_space;
  1503. if ($cutoff !== false)
  1504. $row['body'] = $smcFunc['substr']($row['body'], 0, $cutoff);
  1505. $row['body'] .= '...';
  1506. }
  1507. $row['body'] = parse_bbc($row['body'], $row['smileys_enabled'], $row['id_msg']);
  1508. // Check that this message icon is there...
  1509. if (!empty($modSettings['messageIconChecks_enable']) && !isset($icon_sources[$row['icon']]))
  1510. $icon_sources[$row['icon']] = file_exists($settings['theme_dir'] . '/images/post/' . $row['icon'] . '.png') ? 'images_url' : 'default_images_url';
  1511. censorText($row['subject']);
  1512. censorText($row['body']);
  1513. $return[] = array(
  1514. 'id' => $row['id_topic'],
  1515. 'message_id' => $row['id_msg'],
  1516. 'icon' => '<img src="' . $settings[$icon_sources[$row['icon']]] . '/post/' . $row['icon'] . '.png" alt="' . $row['icon'] . '" />',
  1517. 'subject' => $row['subject'],
  1518. 'time' => timeformat($row['poster_time']),
  1519. 'timestamp' => forum_time(true, $row['poster_time']),
  1520. 'body' => $row['body'],
  1521. 'href' => $scripturl . '?topic=' . $row['id_topic'] . '.0',
  1522. 'link' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.0">' . $row['num_replies'] . ' ' . ($row['num_replies'] == 1 ? $txt['ssi_comment'] : $txt['ssi_comments']) . '</a>',
  1523. 'replies' => $row['num_replies'],
  1524. 'comment_href' => !empty($row['locked']) ? '' : $scripturl . '?action=post;topic=' . $row['id_topic'] . '.' . $row['num_replies'] . ';last_msg=' . $row['id_last_msg'],
  1525. '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>',
  1526. 'new_comment' => !empty($row['locked']) ? '' : '<a href="' . $scripturl . '?action=post;topic=' . $row['id_topic'] . '.' . $row['num_replies'] . '">' . $txt['ssi_write_comment'] . '</a>',
  1527. 'poster' => array(
  1528. 'id' => $row['id_member'],
  1529. 'name' => $row['poster_name'],
  1530. 'href' => !empty($row['id_member']) ? $scripturl . '?action=profile;u=' . $row['id_member'] : '',
  1531. 'link' => !empty($row['id_member']) ? '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['poster_name'] . '</a>' : $row['poster_name']
  1532. ),
  1533. 'locked' => !empty($row['locked']),
  1534. 'is_last' => false
  1535. );
  1536. }
  1537. $smcFunc['db_free_result']($request);
  1538. if (empty($return))
  1539. return $return;
  1540. $return[count($return) - 1]['is_last'] = true;
  1541. if ($output_method != 'echo')
  1542. return $return;
  1543. foreach ($return as $news)
  1544. {
  1545. echo '
  1546. <div class="news_item">
  1547. <h3 class="news_header">
  1548. ', $news['icon'], '
  1549. <a href="', $news['href'], '">', $news['subject'], '</a>
  1550. </h3>
  1551. <div class="news_timestamp">', $news['time'], ' ', $txt['by'], ' ', $news['poster']['link'], '</div>
  1552. <div class="news_body" style="padding: 2ex 0;">', $news['body'], '</div>
  1553. ', $news['link'], $news['locked'] ? '' : ' | ' . $news['comment_link'], '
  1554. </div>';
  1555. if (!$news['is_last'])
  1556. echo '
  1557. <hr />';
  1558. }
  1559. }
  1560. // Show the most recent events.
  1561. function ssi_recentEvents($max_events = 7, $output_method = 'echo')
  1562. {
  1563. global $user_info, $scripturl, $modSettings, $txt, $context, $smcFunc;
  1564. if (empty($modSettings['cal_enabled']) || !allowedTo('calendar_view'))
  1565. return;
  1566. // Find all events which are happening in the near future that the member can see.
  1567. $request = $smcFunc['db_query']('', '
  1568. SELECT
  1569. cal.id_event, cal.start_date, cal.end_date, cal.title, cal.id_member, cal.id_topic,
  1570. cal.id_board, t.id_first_msg, t.approved
  1571. FROM {db_prefix}calendar AS cal
  1572. LEFT JOIN {db_prefix}boards AS b ON (b.id_board = cal.id_board)
  1573. LEFT JOIN {db_prefix}topics AS t ON (t.id_topic = cal.id_topic)
  1574. WHERE cal.start_date <= {date:current_date}
  1575. AND cal.end_date >= {date:current_date}
  1576. AND (cal.id_board = {int:no_board} OR {query_wanna_see_board})
  1577. ORDER BY cal.start_date DESC
  1578. LIMIT ' . $max_events,
  1579. array(
  1580. 'current_date' => strftime('%Y-%m-%d', forum_time(false)),
  1581. 'no_board' => 0,
  1582. )
  1583. );
  1584. $return = array();
  1585. $duplicates = array();
  1586. while ($row = $smcFunc['db_fetch_assoc']($request))
  1587. {
  1588. // 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.
  1589. if (!empty($duplicates[$row['title'] . $row['id_topic']]))
  1590. continue;
  1591. // Censor the title.
  1592. censorText($row['title']);
  1593. if ($row['start_date'] < strftime('%Y-%m-%d', forum_time(false)))
  1594. $date = strftime('%Y-%m-%d', forum_time(false));
  1595. else
  1596. $date = $row['start_date'];
  1597. // If the topic it is attached to is not approved then don't link it.
  1598. if (!empty($row['id_first_msg']) && !$row['approved'])
  1599. $row['id_board'] = $row['id_topic'] = $row['id_first_msg'] = 0;
  1600. $return[$date][] = array(
  1601. 'id' => $row['id_event'],
  1602. 'title' => $row['title'],
  1603. 'can_edit' => allowedTo('calendar_edit_any') || ($row['id_member'] == $user_info['id'] && allowedTo('calendar_edit_own')),
  1604. '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'],
  1605. 'href' => $row['id_board'] == 0 ? '' : $scripturl . '?topic=' . $row['id_topic'] . '.0',
  1606. 'link' => $row['id_board'] == 0 ? $row['title'] : '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.0">' . $row['title'] . '</a>',
  1607. 'start_date' => $row['start_date'],
  1608. 'end_date' => $row['end_date'],
  1609. 'is_last' => false
  1610. );
  1611. // Let's not show this one again, huh?
  1612. $duplicates[$row['title'] . $row['id_topic']] = true;
  1613. }
  1614. $smcFunc['db_free_result']($request);
  1615. foreach ($return as $mday => $array)
  1616. $return[$mday][count($array) - 1]['is_last'] = true;
  1617. if ($output_method != 'echo' || empty($return))
  1618. return $return;
  1619. // Well the output method is echo.
  1620. echo '
  1621. <span class="event">' . $txt['events'] . '</span> ';
  1622. foreach ($return as $mday => $array)
  1623. foreach ($array as $event)
  1624. {
  1625. if ($event['can_edit'])
  1626. echo '
  1627. <a href="' . $event['modify_href'] . '" style="color: #ff0000;">*</a> ';
  1628. echo '
  1629. ' . $event['link'] . (!$event['is_last'] ? ', ' : '');
  1630. }
  1631. }
  1632. // Check the passed id_member/password. If $is_username is true, treats $id as a username.
  1633. function ssi_checkPassword($id = null, $password = null, $is_username = false)
  1634. {
  1635. global $sourcedir, $smcFunc;
  1636. // If $id is null, this was most likely called from a query string and should do nothing.
  1637. if ($id === null)
  1638. return;
  1639. $request = $smcFunc['db_query']('', '
  1640. SELECT passwd, member_name, is_activated
  1641. FROM {db_prefix}members
  1642. WHERE ' . ($is_username ? 'member_name' : 'id_member') . ' = {string:id}
  1643. LIMIT 1',
  1644. array(
  1645. 'id' => $id,
  1646. )
  1647. );
  1648. list ($pass, $user, $active) = $smcFunc['db_fetch_row']($request);
  1649. $smcFunc['db_free_result']($request);
  1650. return sha1(strtolower($user) . $password) == $pass && $active == 1;
  1651. }
  1652. // We want to show the recent attachments outside of the forum.
  1653. function ssi_recentAttachments($num_attachments = 10, $attachment_ext = array(), $output_method = 'echo')
  1654. {
  1655. global $smcFunc, $context, $modSettings, $scripturl, $txt, $settings;
  1656. // We want to make sure that we only get attachments for boards that we can see *if* any.
  1657. $attachments_boards = boardsAllowedTo('view_attachments');
  1658. // No boards? Adios amigo.
  1659. if (empty($attachments_boards))
  1660. return array();
  1661. // Is it an array?
  1662. if (!is_array($attachment_ext))
  1663. $attachment_ext = array($attachment_ext);
  1664. // Lets build the query.
  1665. $request = $smcFunc['db_query']('', '
  1666. SELECT
  1667. att.id_attach, att.id_msg, att.filename, IFNULL(att.size, 0) AS filesize, att.downloads, mem.id_member,
  1668. IFNULL(mem.real_name, m.poster_name) AS poster_name, m.id_topic, m.subject, t.id_board, m.poster_time,
  1669. 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') . '
  1670. FROM {db_prefix}attachments AS att
  1671. INNER JOIN {db_prefix}messages AS m ON (m.id_msg = att.id_msg)
  1672. INNER JOIN {db_prefix}topics AS t ON (t.id_topic = m.id_topic)
  1673. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member)' . (empty($modSettings['attachmentShowImages']) || empty($modSettings['attachmentThumbnails']) ? '' : '
  1674. LEFT JOIN {db_prefix}attachments AS thumb ON (thumb.id_attach = att.id_thumb)') . '
  1675. WHERE att.attachment_type = 0' . ($attachments_boards === array(0) ? '' : '
  1676. AND m.id_board IN ({array_int:boards_can_see})') . (!empty($attachment_ext) ? '
  1677. AND att.fileext IN ({array_string:attachment_ext})' : '') .
  1678. (!$modSettings['postmod_active'] || allowedTo('approve_posts') ? '' : '
  1679. AND t.approved = {int:is_approved}
  1680. AND m.approved = {int:is_approved}
  1681. AND att.approved = {int:is_approved}') . '
  1682. ORDER BY att.id_attach DESC
  1683. LIMIT {int:num_attachments}',
  1684. array(
  1685. 'boards_can_see' => $attachments_boards,
  1686. 'attachment_ext' => $attachment_ext,
  1687. 'num_attachments' => $num_attachments,
  1688. 'is_approved' => 1,
  1689. )
  1690. );
  1691. // We have something.
  1692. $attachments = array();
  1693. while ($row = $smcFunc['db_fetch_assoc']($request))
  1694. {
  1695. $filename = preg_replace('~&amp;#(\\d{1,7}|x[0-9a-fA-F]{1,6});~', '&#\\1;', htmlspecialchars($row['filename']));
  1696. // Is it an image?
  1697. $attachments[$row['id_attach']] = array(
  1698. 'member' => array(
  1699. 'id' => $row['id_member'],
  1700. 'name' => $row['poster_name'],
  1701. 'link' => empty($row['id_member']) ? $row['poster_name'] : '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['poster_name'] . '</a>',
  1702. ),
  1703. 'file' => array(
  1704. 'filename' => $filename,
  1705. 'filesize' => round($row['filesize'] /1024, 2) . $txt['kilobyte'],
  1706. 'downloads' => $row['downloads'],
  1707. 'href' => $scripturl . '?action=dlattach;topic=' . $row['id_topic'] . '.0;attach=' . $row['id_attach'],
  1708. '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>',
  1709. 'is_image' => !empty($row['width']) && !empty($row['height']) && !empty($modSettings['attachmentShowImages']),
  1710. ),
  1711. 'topic' => array(
  1712. 'id' => $row['id_topic'],
  1713. 'subject' => $row['subject'],
  1714. 'href' => $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . '#msg' . $row['id_msg'],
  1715. 'link' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . '#msg' . $row['id_msg'] . '">' . $row['subject'] . '</a>',
  1716. 'time' => timeformat($row['poster_time']),
  1717. ),
  1718. );
  1719. // Images.
  1720. if ($attachments[$row['id_attach']]['file']['is_image'])
  1721. {
  1722. $id_thumb = empty($row['id_thumb']) ? $row['id_attach'] : $row['id_thumb'];
  1723. $attachments[$row['id_attach']]['file']['image'] = array(
  1724. 'id' => $id_thumb,
  1725. 'width' => $row['width'],
  1726. 'height' => $row['height'],
  1727. 'img' => '<img src="' . $scripturl . '?action=dlattach;topic=' . $row['id_topic'] . '.0;attach=' . $row['id_attach'] . ';image" alt="' . $filename . '" />',
  1728. 'thumb' => '<img src="' . $scripturl . '?action=dlattach;topic=' . $row['id_topic'] . '.0;attach=' . $id_thumb . ';image" alt="' . $filename . '" />',
  1729. 'href' => $scripturl . '?action=dlattach;topic=' . $row['id_topic'] . '.0;attach=' . $id_thumb . ';image',
  1730. '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>',
  1731. );
  1732. }
  1733. }
  1734. $smcFunc['db_free_result']($request);
  1735. // So you just want an array? Here you can have it.
  1736. if ($output_method == 'array' || empty($attachments))
  1737. return $attachments;
  1738. // Give them the default.
  1739. echo '
  1740. <table class="ssi_downloads" cellpadding="2">
  1741. <tr>
  1742. <th align="left">', $txt['file'], '</th>
  1743. <th align="left">', $txt['posted_by'], '</th>
  1744. <th align="left">', $txt['downloads'], '</th>
  1745. <th align="left">', $txt['filesize'], '</th>
  1746. </tr>';
  1747. foreach ($attachments as $attach)
  1748. echo '
  1749. <tr>
  1750. <td>', $attach['file']['link'], '</td>
  1751. <td>', $attach['member']['link'], '</td>
  1752. <td align="center">', $attach['file']['downloads'], '</td>
  1753. <td>', $attach['file']['filesize'], '</td>
  1754. </tr>';
  1755. echo '
  1756. </table>';
  1757. }
  1758. ?>