SSI.php 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048
  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. $recycle_board = !empty($modSettings['recycle_enable']) && !empty($modSettings['recycle_board']) ? (int) $modSettings['recycle_board'] : 0;
  410. // Find all the posts in distinct topics. Newer ones will have higher IDs.
  411. $request = $smcFunc['db_query']('substring', '
  412. SELECT
  413. mf.poster_time, mf.subject, ml.id_topic, mf.id_member, ml.id_msg, t.num_replies, t.num_views, mg.online_color,
  414. IFNULL(mem.real_name, mf.poster_name) AS poster_name, ' . ($user_info['is_guest'] ? '1 AS is_read, 0 AS new_from' : '
  415. IFNULL(lt.id_msg, IFNULL(lmr.id_msg, 0)) >= ml.id_msg_modified AS is_read,
  416. 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
  417. FROM {db_prefix}topics AS t
  418. INNER JOIN {db_prefix}messages AS ml ON (ml.id_msg = t.id_last_msg)
  419. INNER JOIN {db_prefix}messages AS mf ON (mf.id_msg = t.id_last_msg)
  420. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = mf.id_member)' . (!$user_info['is_guest'] ? '
  421. LEFT JOIN {db_prefix}log_topics AS lt ON (lt.id_topic = t.id_topic AND lt.id_member = {int:current_member})
  422. LEFT JOIN {db_prefix}log_mark_read AS lmr ON (lmr.id_board = t.id_board AND lmr.id_member = {int:current_member})' : '') . '
  423. LEFT JOIN {db_prefix}membergroups AS mg ON (mg.id_group = mem.id_group)
  424. WHERE t.id_topic IN ({array_int:topic_list})',
  425. array(
  426. 'current_member' => $user_info['id'],
  427. 'topic_list' => array_keys($topics),
  428. )
  429. );
  430. $posts = array();
  431. while ($row = $smcFunc['db_fetch_assoc']($request))
  432. {
  433. $row['body'] = strip_tags(strtr(parse_bbc($row['body'], $row['smileys_enabled'], $row['id_msg']), array('<br>' => '&#10;')));
  434. if ($smcFunc['strlen']($row['body']) > 128)
  435. $row['body'] = $smcFunc['substr']($row['body'], 0, 128) . '...';
  436. // Censor the subject.
  437. censorText($row['subject']);
  438. censorText($row['body']);
  439. // Recycled icon
  440. if (!empty($recycle_board) && $topics[$row['id_topic']]['id_board'])
  441. $row['icon'] = 'recycled';
  442. if (!empty($modSettings['messageIconChecks_enable']) && !isset($icon_sources[$row['icon']]))
  443. $icon_sources[$row['icon']] = file_exists($settings['theme_dir'] . '/images/post/' . $row['icon'] . '.png') ? 'images_url' : 'default_images_url';
  444. // Build the array.
  445. $posts[] = array(
  446. 'board' => array(
  447. 'id' => $topics[$row['id_topic']]['id_board'],
  448. 'name' => $topics[$row['id_topic']]['board_name'],
  449. 'href' => $scripturl . '?board=' . $topics[$row['id_topic']]['id_board'] . '.0',
  450. 'link' => '<a href="' . $scripturl . '?board=' . $topics[$row['id_topic']]['id_board'] . '.0">' . $topics[$row['id_topic']]['board_name'] . '</a>',
  451. ),
  452. 'topic' => $row['id_topic'],
  453. 'poster' => array(
  454. 'id' => $row['id_member'],
  455. 'name' => $row['poster_name'],
  456. 'href' => empty($row['id_member']) ? '' : $scripturl . '?action=profile;u=' . $row['id_member'],
  457. 'link' => empty($row['id_member']) ? $row['poster_name'] : '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['poster_name'] . '</a>'
  458. ),
  459. 'subject' => $row['subject'],
  460. 'replies' => $row['num_replies'],
  461. 'views' => $row['num_views'],
  462. 'short_subject' => shorten_subject($row['subject'], 25),
  463. 'preview' => $row['body'],
  464. 'time' => timeformat($row['poster_time']),
  465. 'timestamp' => forum_time(true, $row['poster_time']),
  466. 'href' => $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . ';topicseen#new',
  467. 'link' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . '#new" rel="nofollow">' . $row['subject'] . '</a>',
  468. // Retained for compatibility - is technically incorrect!
  469. 'new' => !empty($row['is_read']),
  470. 'is_new' => empty($row['is_read']),
  471. 'new_from' => $row['new_from'],
  472. 'icon' => '<img src="' . $settings[$icon_sources[$row['icon']]] . '/post/' . $row['icon'] . '.png" align="middle" alt="' . $row['icon'] . '">',
  473. );
  474. }
  475. $smcFunc['db_free_result']($request);
  476. // Just return it.
  477. if ($output_method != 'echo' || empty($posts))
  478. return $posts;
  479. echo '
  480. <table border="0" class="ssi_table">';
  481. foreach ($posts as $post)
  482. echo '
  483. <tr>
  484. <td align="right" valign="top" nowrap="nowrap">
  485. [', $post['board']['link'], ']
  486. </td>
  487. <td valign="top">
  488. <a href="', $post['href'], '">', $post['subject'], '</a>
  489. ', $txt['by'], ' ', $post['poster']['link'], '
  490. ', !$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>', '
  491. </td>
  492. <td align="right" nowrap="nowrap">
  493. ', $post['time'], '
  494. </td>
  495. </tr>';
  496. echo '
  497. </table>';
  498. }
  499. // Show the top poster's name and profile link.
  500. function ssi_topPoster($topNumber = 1, $output_method = 'echo')
  501. {
  502. global $scripturl, $smcFunc;
  503. // Find the latest poster.
  504. $request = $smcFunc['db_query']('', '
  505. SELECT id_member, real_name, posts
  506. FROM {db_prefix}members
  507. ORDER BY posts DESC
  508. LIMIT ' . $topNumber,
  509. array(
  510. )
  511. );
  512. $return = array();
  513. while ($row = $smcFunc['db_fetch_assoc']($request))
  514. $return[] = array(
  515. 'id' => $row['id_member'],
  516. 'name' => $row['real_name'],
  517. 'href' => $scripturl . '?action=profile;u=' . $row['id_member'],
  518. 'link' => '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['real_name'] . '</a>',
  519. 'posts' => $row['posts']
  520. );
  521. $smcFunc['db_free_result']($request);
  522. // Just return all the top posters.
  523. if ($output_method != 'echo')
  524. return $return;
  525. // Make a quick array to list the links in.
  526. $temp_array = array();
  527. foreach ($return as $member)
  528. $temp_array[] = $member['link'];
  529. echo implode(', ', $temp_array);
  530. }
  531. // Show boards by activity.
  532. function ssi_topBoards($num_top = 10, $output_method = 'echo')
  533. {
  534. global $context, $txt, $scripturl, $user_info, $modSettings, $smcFunc;
  535. // Find boards with lots of posts.
  536. $request = $smcFunc['db_query']('', '
  537. SELECT
  538. b.name, b.num_topics, b.num_posts, b.id_board,' . (!$user_info['is_guest'] ? ' 1 AS is_read' : '
  539. (IFNULL(lb.id_msg, 0) >= b.id_last_msg) AS is_read') . '
  540. FROM {db_prefix}boards AS b
  541. LEFT JOIN {db_prefix}log_boards AS lb ON (lb.id_board = b.id_board AND lb.id_member = {int:current_member})
  542. WHERE {query_wanna_see_board}' . (!empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] > 0 ? '
  543. AND b.id_board != {int:recycle_board}' : '') . '
  544. ORDER BY b.num_posts DESC
  545. LIMIT ' . $num_top,
  546. array(
  547. 'current_member' => $user_info['id'],
  548. 'recycle_board' => (int) $modSettings['recycle_board'],
  549. )
  550. );
  551. $boards = array();
  552. while ($row = $smcFunc['db_fetch_assoc']($request))
  553. $boards[] = array(
  554. 'id' => $row['id_board'],
  555. 'num_posts' => $row['num_posts'],
  556. 'num_topics' => $row['num_topics'],
  557. 'name' => $row['name'],
  558. 'new' => empty($row['is_read']),
  559. 'href' => $scripturl . '?board=' . $row['id_board'] . '.0',
  560. 'link' => '<a href="' . $scripturl . '?board=' . $row['id_board'] . '.0">' . $row['name'] . '</a>'
  561. );
  562. $smcFunc['db_free_result']($request);
  563. // If we shouldn't output or have nothing to output, just jump out.
  564. if ($output_method != 'echo' || empty($boards))
  565. return $boards;
  566. echo '
  567. <table class="ssi_table">
  568. <tr>
  569. <th align="left">', $txt['board'], '</th>
  570. <th align="left">', $txt['board_topics'], '</th>
  571. <th align="left">', $txt['posts'], '</th>
  572. </tr>';
  573. foreach ($boards as $board)
  574. echo '
  575. <tr>
  576. <td>', $board['link'], $board['new'] ? ' <a href="' . $board['href'] . '"><span class="new_posts">' . $txt['new'] . '</span></a>' : '', '</td>
  577. <td align="right">', comma_format($board['num_topics']), '</td>
  578. <td align="right">', comma_format($board['num_posts']), '</td>
  579. </tr>';
  580. echo '
  581. </table>';
  582. }
  583. // Shows the top topics.
  584. function ssi_topTopics($type = 'replies', $num_topics = 10, $output_method = 'echo')
  585. {
  586. global $txt, $scripturl, $user_info, $modSettings, $smcFunc, $context;
  587. if ($modSettings['totalMessages'] > 100000)
  588. {
  589. // @todo Why don't we use {query(_wanna)_see_board}?
  590. $request = $smcFunc['db_query']('', '
  591. SELECT id_topic
  592. FROM {db_prefix}topics
  593. WHERE num_' . ($type != 'replies' ? 'views' : 'replies') . ' != 0' . ($modSettings['postmod_active'] ? '
  594. AND approved = {int:is_approved}' : '') . '
  595. ORDER BY num_' . ($type != 'replies' ? 'views' : 'replies') . ' DESC
  596. LIMIT {int:limit}',
  597. array(
  598. 'is_approved' => 1,
  599. 'limit' => $num_topics > 100 ? ($num_topics + ($num_topics / 2)) : 100,
  600. )
  601. );
  602. $topic_ids = array();
  603. while ($row = $smcFunc['db_fetch_assoc']($request))
  604. $topic_ids[] = $row['id_topic'];
  605. $smcFunc['db_free_result']($request);
  606. }
  607. else
  608. $topic_ids = array();
  609. $request = $smcFunc['db_query']('', '
  610. SELECT m.subject, m.id_topic, t.num_views, t.num_replies
  611. FROM {db_prefix}topics AS t
  612. INNER JOIN {db_prefix}messages AS m ON (m.id_msg = t.id_first_msg)
  613. INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board)
  614. WHERE {query_wanna_see_board}' . ($modSettings['postmod_active'] ? '
  615. AND t.approved = {int:is_approved}' : '') . (!empty($topic_ids) ? '
  616. AND t.id_topic IN ({array_int:topic_list})' : '') . (!empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] > 0 ? '
  617. AND b.id_board != {int:recycle_enable}' : '') . '
  618. ORDER BY t.num_' . ($type != 'replies' ? 'views' : 'replies') . ' DESC
  619. LIMIT {int:limit}',
  620. array(
  621. 'topic_list' => $topic_ids,
  622. 'is_approved' => 1,
  623. 'recycle_enable' => $modSettings['recycle_board'],
  624. 'limit' => $num_topics,
  625. )
  626. );
  627. $topics = array();
  628. while ($row = $smcFunc['db_fetch_assoc']($request))
  629. {
  630. censorText($row['subject']);
  631. $topics[] = array(
  632. 'id' => $row['id_topic'],
  633. 'subject' => $row['subject'],
  634. 'num_replies' => $row['num_replies'],
  635. 'num_views' => $row['num_views'],
  636. 'href' => $scripturl . '?topic=' . $row['id_topic'] . '.0',
  637. 'link' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.0">' . $row['subject'] . '</a>',
  638. );
  639. }
  640. $smcFunc['db_free_result']($request);
  641. if ($output_method != 'echo' || empty($topics))
  642. return $topics;
  643. echo '
  644. <table class="ssi_table">
  645. <tr>
  646. <th align="left"></th>
  647. <th align="left">', $txt['views'], '</th>
  648. <th align="left">', $txt['replies'], '</th>
  649. </tr>';
  650. foreach ($topics as $topic)
  651. echo '
  652. <tr>
  653. <td align="left">
  654. ', $topic['link'], '
  655. </td>
  656. <td align="right">', comma_format($topic['num_views']), '</td>
  657. <td align="right">', comma_format($topic['num_replies']), '</td>
  658. </tr>';
  659. echo '
  660. </table>';
  661. }
  662. // Shows the top topics, by replies.
  663. function ssi_topTopicsReplies($num_topics = 10, $output_method = 'echo')
  664. {
  665. return ssi_topTopics('replies', $num_topics, $output_method);
  666. }
  667. // Shows the top topics, by views.
  668. function ssi_topTopicsViews($num_topics = 10, $output_method = 'echo')
  669. {
  670. return ssi_topTopics('views', $num_topics, $output_method);
  671. }
  672. // Show a link to the latest member: Please welcome, Someone, out latest member.
  673. function ssi_latestMember($output_method = 'echo')
  674. {
  675. global $txt, $scripturl, $context;
  676. if ($output_method == 'echo')
  677. echo '
  678. ', sprintf($txt['welcome_newest_member'], $context['common_stats']['latest_member']['link']), '<br>';
  679. else
  680. return $context['common_stats']['latest_member'];
  681. }
  682. // Fetch a random member - if type set to 'day' will only change once a day!
  683. function ssi_randomMember($random_type = '', $output_method = 'echo')
  684. {
  685. global $modSettings;
  686. // If we're looking for something to stay the same each day then seed the generator.
  687. if ($random_type == 'day')
  688. {
  689. // Set the seed to change only once per day.
  690. mt_srand(floor(time() / 86400));
  691. }
  692. // Get the lowest ID we're interested in.
  693. $member_id = mt_rand(1, $modSettings['latestMember']);
  694. $where_query = '
  695. id_member >= {int:selected_member}
  696. AND is_activated = {int:is_activated}';
  697. $query_where_params = array(
  698. 'selected_member' => $member_id,
  699. 'is_activated' => 1,
  700. );
  701. $result = ssi_queryMembers($where_query, $query_where_params, 1, 'id_member ASC', $output_method);
  702. // If we got nothing do the reverse - in case of unactivated members.
  703. if (empty($result))
  704. {
  705. $where_query = '
  706. id_member <= {int:selected_member}
  707. AND is_activated = {int:is_activated}';
  708. $query_where_params = array(
  709. 'selected_member' => $member_id,
  710. 'is_activated' => 1,
  711. );
  712. $result = ssi_queryMembers($where_query, $query_where_params, 1, 'id_member DESC', $output_method);
  713. }
  714. // Just to be sure put the random generator back to something... random.
  715. if ($random_type != '')
  716. mt_srand(time());
  717. return $result;
  718. }
  719. // Fetch a specific member.
  720. function ssi_fetchMember($member_ids = array(), $output_method = 'echo')
  721. {
  722. if (empty($member_ids))
  723. return;
  724. // Can have more than one member if you really want...
  725. $member_ids = is_array($member_ids) ? $member_ids : array($member_ids);
  726. // Restrict it right!
  727. $query_where = '
  728. id_member IN ({array_int:member_list})';
  729. $query_where_params = array(
  730. 'member_list' => $member_ids,
  731. );
  732. // Then make the query and dump the data.
  733. return ssi_queryMembers($query_where, $query_where_params, '', 'id_member', $output_method);
  734. }
  735. // Get all members of a group.
  736. function ssi_fetchGroupMembers($group_id = null, $output_method = 'echo')
  737. {
  738. if ($group_id === null)
  739. return;
  740. $query_where = '
  741. id_group = {int:id_group}
  742. OR id_post_group = {int:id_group}
  743. OR FIND_IN_SET({int:id_group}, additional_groups) != 0';
  744. $query_where_params = array(
  745. 'id_group' => $group_id,
  746. );
  747. return ssi_queryMembers($query_where, $query_where_params, '', 'real_name', $output_method);
  748. }
  749. // Fetch some member data!
  750. function ssi_queryMembers($query_where = null, $query_where_params = array(), $query_limit = '', $query_order = 'id_member DESC', $output_method = 'echo')
  751. {
  752. global $context, $scripturl, $txt, $user_info;
  753. global $modSettings, $smcFunc, $memberContext;
  754. if ($query_where === null)
  755. return;
  756. // Fetch the members in question.
  757. $request = $smcFunc['db_query']('', '
  758. SELECT id_member
  759. FROM {db_prefix}members
  760. WHERE ' . $query_where . '
  761. ORDER BY ' . $query_order . '
  762. ' . ($query_limit == '' ? '' : 'LIMIT ' . $query_limit),
  763. array_merge($query_where_params, array(
  764. ))
  765. );
  766. $members = array();
  767. while ($row = $smcFunc['db_fetch_assoc']($request))
  768. $members[] = $row['id_member'];
  769. $smcFunc['db_free_result']($request);
  770. if (empty($members))
  771. return array();
  772. // Load the members.
  773. loadMemberData($members);
  774. // Draw the table!
  775. if ($output_method == 'echo')
  776. echo '
  777. <table border="0" class="ssi_table">';
  778. $query_members = array();
  779. foreach ($members as $member)
  780. {
  781. // Load their context data.
  782. if (!loadMemberContext($member))
  783. continue;
  784. // Store this member's information.
  785. $query_members[$member] = $memberContext[$member];
  786. // Only do something if we're echo'ing.
  787. if ($output_method == 'echo')
  788. echo '
  789. <tr>
  790. <td align="right" valign="top" nowrap="nowrap">
  791. ', $query_members[$member]['link'], '
  792. <br>', $query_members[$member]['blurb'], '
  793. <br>', $query_members[$member]['avatar']['image'], '
  794. </td>
  795. </tr>';
  796. }
  797. // End the table if appropriate.
  798. if ($output_method == 'echo')
  799. echo '
  800. </table>';
  801. // Send back the data.
  802. return $query_members;
  803. }
  804. // Show some basic stats: Total This: XXXX, etc.
  805. function ssi_boardStats($output_method = 'echo')
  806. {
  807. global $txt, $scripturl, $modSettings, $smcFunc;
  808. if (!allowedTo('view_stats'))
  809. return;
  810. $totals = array(
  811. 'members' => $modSettings['totalMembers'],
  812. 'posts' => $modSettings['totalMessages'],
  813. 'topics' => $modSettings['totalTopics']
  814. );
  815. $result = $smcFunc['db_query']('', '
  816. SELECT COUNT(*)
  817. FROM {db_prefix}boards',
  818. array(
  819. )
  820. );
  821. list ($totals['boards']) = $smcFunc['db_fetch_row']($result);
  822. $smcFunc['db_free_result']($result);
  823. $result = $smcFunc['db_query']('', '
  824. SELECT COUNT(*)
  825. FROM {db_prefix}categories',
  826. array(
  827. )
  828. );
  829. list ($totals['categories']) = $smcFunc['db_fetch_row']($result);
  830. $smcFunc['db_free_result']($result);
  831. if ($output_method != 'echo')
  832. return $totals;
  833. echo '
  834. ', $txt['total_members'], ': <a href="', $scripturl . '?action=mlist">', comma_format($totals['members']), '</a><br>
  835. ', $txt['total_posts'], ': ', comma_format($totals['posts']), '<br>
  836. ', $txt['total_topics'], ': ', comma_format($totals['topics']), ' <br>
  837. ', $txt['total_cats'], ': ', comma_format($totals['categories']), '<br>
  838. ', $txt['total_boards'], ': ', comma_format($totals['boards']);
  839. }
  840. // Shows a list of online users: YY Guests, ZZ Users and then a list...
  841. function ssi_whosOnline($output_method = 'echo')
  842. {
  843. global $user_info, $txt, $sourcedir, $settings, $modSettings;
  844. require_once($sourcedir . '/Subs-MembersOnline.php');
  845. $membersOnlineOptions = array(
  846. 'show_hidden' => allowedTo('moderate_forum'),
  847. );
  848. $return = getMembersOnlineStats($membersOnlineOptions);
  849. // Add some redundancy for backwards compatibility reasons.
  850. if ($output_method != 'echo')
  851. return $return + array(
  852. 'users' => $return['users_online'],
  853. 'guests' => $return['num_guests'],
  854. 'hidden' => $return['num_users_hidden'],
  855. 'buddies' => $return['num_buddies'],
  856. 'num_users' => $return['num_users_online'],
  857. 'total_users' => $return['num_users_online'] + $return['num_guests'] + $return['num_spiders'],
  858. );
  859. echo '
  860. ', 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'];
  861. $bracketList = array();
  862. if (!empty($user_info['buddies']))
  863. $bracketList[] = comma_format($return['num_buddies']) . ' ' . ($return['num_buddies'] == 1 ? $txt['buddy'] : $txt['buddies']);
  864. if (!empty($return['num_spiders']))
  865. $bracketList[] = comma_format($return['num_spiders']) . ' ' . ($return['num_spiders'] == 1 ? $txt['spider'] : $txt['spiders']);
  866. if (!empty($return['num_users_hidden']))
  867. $bracketList[] = comma_format($return['num_users_hidden']) . ' ' . $txt['hidden'];
  868. if (!empty($bracketList))
  869. echo ' (' . implode(', ', $bracketList) . ')';
  870. echo '<br>
  871. ', implode(', ', $return['list_users_online']);
  872. // Showing membergroups?
  873. if (!empty($settings['show_group_key']) && !empty($return['membergroups']))
  874. echo '<br>
  875. [' . implode(']&nbsp;&nbsp;[', $return['membergroups']) . ']';
  876. }
  877. // Just like whosOnline except it also logs the online presence.
  878. function ssi_logOnline($output_method = 'echo')
  879. {
  880. writeLog();
  881. if ($output_method != 'echo')
  882. return ssi_whosOnline($output_method);
  883. else
  884. ssi_whosOnline($output_method);
  885. }
  886. // Shows a login box.
  887. function ssi_login($redirect_to = '', $output_method = 'echo')
  888. {
  889. global $scripturl, $txt, $user_info, $context, $modSettings;
  890. if ($redirect_to != '')
  891. $_SESSION['login_url'] = $redirect_to;
  892. if ($output_method != 'echo' || !$user_info['is_guest'])
  893. return $user_info['is_guest'];
  894. echo '
  895. <form action="', $scripturl, '?action=login2" method="post" accept-charset="', $context['character_set'], '">
  896. <table border="0" cellspacing="1" cellpadding="0" class="ssi_table">
  897. <tr>
  898. <td align="right"><label for="user">', $txt['username'], ':</label>&nbsp;</td>
  899. <td><input type="text" id="user" name="user" size="9" value="', $user_info['username'], '" class="input_text"></td>
  900. </tr><tr>
  901. <td align="right"><label for="passwrd">', $txt['password'], ':</label>&nbsp;</td>
  902. <td><input type="password" name="passwrd" id="passwrd" size="9" class="input_password"></td>
  903. </tr>';
  904. // Open ID?
  905. if (!empty($modSettings['enableOpenID']))
  906. echo '<tr>
  907. <td colspan="2" align="center"><strong>&mdash;', $txt['or'], '&mdash;</strong></td>
  908. </tr><tr>
  909. <td align="right"><label for="openid_url">', $txt['openid'], ':</label>&nbsp;</td>
  910. <td><input type="text" name="openid_identifier" id="openid_url" class="input_text openid_login" size="17"></td>
  911. </tr>';
  912. echo '<tr>
  913. <td><input type="hidden" name="cookielength" value="-1"></td>
  914. <td><input type="submit" value="', $txt['login'], '" class="button_submit"></td>
  915. </tr>
  916. </table>
  917. </form>';
  918. }
  919. // Show the most-voted-in poll.
  920. function ssi_topPoll($output_method = 'echo')
  921. {
  922. // Just use recentPoll, no need to duplicate code...
  923. return ssi_recentPoll(true, $output_method);
  924. }
  925. // Show the most recently posted poll.
  926. function ssi_recentPoll($topPollInstead = false, $output_method = 'echo')
  927. {
  928. global $txt, $settings, $boardurl, $user_info, $context, $smcFunc, $modSettings;
  929. $boardsAllowed = array_intersect(boardsAllowedTo('poll_view'), boardsAllowedTo('poll_vote'));
  930. if (empty($boardsAllowed))
  931. return array();
  932. $request = $smcFunc['db_query']('', '
  933. SELECT p.id_poll, p.question, t.id_topic, p.max_votes, p.guest_vote, p.hide_results, p.expire_time
  934. FROM {db_prefix}polls AS p
  935. INNER JOIN {db_prefix}topics AS t ON (t.id_poll = p.id_poll' . ($modSettings['postmod_active'] ? ' AND t.approved = {int:is_approved}' : '') . ')
  936. INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board)' . ($topPollInstead ? '
  937. INNER JOIN {db_prefix}poll_choices AS pc ON (pc.id_poll = p.id_poll)' : '') . '
  938. 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})
  939. WHERE p.voting_locked = {int:voting_opened}
  940. AND (p.expire_time = {int:no_expiration} OR {int:current_time} < p.expire_time)
  941. AND ' . ($user_info['is_guest'] ? 'p.guest_vote = {int:guest_vote_allowed}' : 'lp.id_choice IS NULL') . '
  942. AND {query_wanna_see_board}' . (!in_array(0, $boardsAllowed) ? '
  943. AND b.id_board IN ({array_int:boards_allowed_list})' : '') . (!empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] > 0 ? '
  944. AND b.id_board != {int:recycle_enable}' : '') . '
  945. ORDER BY ' . ($topPollInstead ? 'pc.votes' : 'p.id_poll') . ' DESC
  946. LIMIT 1',
  947. array(
  948. 'current_member' => $user_info['id'],
  949. 'boards_allowed_list' => $boardsAllowed,
  950. 'is_approved' => 1,
  951. 'guest_vote_allowed' => 1,
  952. 'no_member' => 0,
  953. 'voting_opened' => 0,
  954. 'no_expiration' => 0,
  955. 'current_time' => time(),
  956. 'recycle_enable' => $modSettings['recycle_board'],
  957. )
  958. );
  959. $row = $smcFunc['db_fetch_assoc']($request);
  960. $smcFunc['db_free_result']($request);
  961. // This user has voted on all the polls.
  962. if (empty($row) || !is_array($row))
  963. return array();
  964. // If this is a guest who's voted we'll through ourselves to show poll to show the results.
  965. if ($user_info['is_guest'] && (!$row['guest_vote'] || (isset($_COOKIE['guest_poll_vote']) && in_array($row['id_poll'], explode(',', $_COOKIE['guest_poll_vote'])))))
  966. return ssi_showPoll($row['id_topic'], $output_method);
  967. $request = $smcFunc['db_query']('', '
  968. SELECT COUNT(DISTINCT id_member)
  969. FROM {db_prefix}log_polls
  970. WHERE id_poll = {int:current_poll}',
  971. array(
  972. 'current_poll' => $row['id_poll'],
  973. )
  974. );
  975. list ($total) = $smcFunc['db_fetch_row']($request);
  976. $smcFunc['db_free_result']($request);
  977. $request = $smcFunc['db_query']('', '
  978. SELECT id_choice, label, votes
  979. FROM {db_prefix}poll_choices
  980. WHERE id_poll = {int:current_poll}',
  981. array(
  982. 'current_poll' => $row['id_poll'],
  983. )
  984. );
  985. $options = array();
  986. while ($rowChoice = $smcFunc['db_fetch_assoc']($request))
  987. {
  988. censorText($rowChoice['label']);
  989. $options[$rowChoice['id_choice']] = array($rowChoice['label'], $rowChoice['votes']);
  990. }
  991. $smcFunc['db_free_result']($request);
  992. // Can they view it?
  993. $is_expired = !empty($row['expire_time']) && $row['expire_time'] < time();
  994. $allow_view_results = allowedTo('moderate_board') || $row['hide_results'] == 0 || $is_expired;
  995. $return = array(
  996. 'id' => $row['id_poll'],
  997. 'image' => 'poll',
  998. 'question' => $row['question'],
  999. 'total_votes' => $total,
  1000. 'is_locked' => false,
  1001. 'topic' => $row['id_topic'],
  1002. 'allow_view_results' => $allow_view_results,
  1003. 'options' => array()
  1004. );
  1005. // Calculate the percentages and bar lengths...
  1006. $divisor = $return['total_votes'] == 0 ? 1 : $return['total_votes'];
  1007. foreach ($options as $i => $option)
  1008. {
  1009. $bar = floor(($option[1] * 100) / $divisor);
  1010. $barWide = $bar == 0 ? 1 : floor(($bar * 5) / 3);
  1011. $return['options'][$i] = array(
  1012. 'id' => 'options-' . ($topPollInstead ? 'top-' : 'recent-') . $i,
  1013. 'percent' => $bar,
  1014. 'votes' => $option[1],
  1015. 'option' => parse_bbc($option[0]),
  1016. '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') . '">'
  1017. );
  1018. }
  1019. $return['allowed_warning'] = $row['max_votes'] > 1 ? sprintf($txt['poll_options6'], min(count($options), $row['max_votes'])) : '';
  1020. if ($output_method != 'echo')
  1021. return $return;
  1022. if ($allow_view_results)
  1023. {
  1024. echo '
  1025. <form class="ssi_poll" action="', $boardurl, '/SSI.php?ssi_function=pollVote" method="post" accept-charset="', $context['character_set'], '">
  1026. <strong>', $return['question'], '</strong><br>
  1027. ', !empty($return['allowed_warning']) ? $return['allowed_warning'] . '<br>' : '';
  1028. foreach ($return['options'] as $option)
  1029. echo '
  1030. <label for="', $option['id'], '">', $option['vote_button'], ' ', $option['option'], '</label><br>';
  1031. echo '
  1032. <input type="submit" value="', $txt['poll_vote'], '" class="button_submit">
  1033. <input type="hidden" name="poll" value="', $return['id'], '">
  1034. <input type="hidden" name="', $context['session_var'], '" value="', $context['session_id'], '">
  1035. </form>';
  1036. }
  1037. else
  1038. echo $txt['poll_cannot_see'];
  1039. }
  1040. function ssi_showPoll($topic = null, $output_method = 'echo')
  1041. {
  1042. global $txt, $settings, $boardurl, $user_info, $context, $smcFunc, $modSettings;
  1043. $boardsAllowed = boardsAllowedTo('poll_view');
  1044. if (empty($boardsAllowed))
  1045. return array();
  1046. if ($topic === null && isset($_REQUEST['ssi_topic']))
  1047. $topic = (int) $_REQUEST['ssi_topic'];
  1048. else
  1049. $topic = (int) $topic;
  1050. $request = $smcFunc['db_query']('', '
  1051. SELECT
  1052. p.id_poll, p.question, p.voting_locked, p.hide_results, p.expire_time, p.max_votes, p.guest_vote, b.id_board
  1053. FROM {db_prefix}topics AS t
  1054. INNER JOIN {db_prefix}polls AS p ON (p.id_poll = t.id_poll)
  1055. INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board)
  1056. WHERE t.id_topic = {int:current_topic}
  1057. AND {query_see_board}' . (!in_array(0, $boardsAllowed) ? '
  1058. AND b.id_board IN ({array_int:boards_allowed_see})' : '') . ($modSettings['postmod_active'] ? '
  1059. AND t.approved = {int:is_approved}' : '') . '
  1060. LIMIT 1',
  1061. array(
  1062. 'current_topic' => $topic,
  1063. 'boards_allowed_see' => $boardsAllowed,
  1064. 'is_approved' => 1,
  1065. )
  1066. );
  1067. // Either this topic has no poll, or the user cannot view it.
  1068. if ($smcFunc['db_num_rows']($request) == 0)
  1069. return array();
  1070. $row = $smcFunc['db_fetch_assoc']($request);
  1071. $smcFunc['db_free_result']($request);
  1072. // Check if they can vote.
  1073. if (!empty($row['expire_time']) && $row['expire_time'] < time())
  1074. $allow_vote = false;
  1075. elseif ($user_info['is_guest'] && $row['guest_vote'] && (!isset($_COOKIE['guest_poll_vote']) || !in_array($row['id_poll'], explode(',', $_COOKIE['guest_poll_vote']))))
  1076. $allow_vote = true;
  1077. elseif ($user_info['is_guest'])
  1078. $allow_vote = false;
  1079. elseif (!empty($row['voting_locked']) || !allowedTo('poll_vote', $row['id_board']))
  1080. $allow_vote = false;
  1081. else
  1082. {
  1083. $request = $smcFunc['db_query']('', '
  1084. SELECT id_member
  1085. FROM {db_prefix}log_polls
  1086. WHERE id_poll = {int:current_poll}
  1087. AND id_member = {int:current_member}
  1088. LIMIT 1',
  1089. array(
  1090. 'current_member' => $user_info['id'],
  1091. 'current_poll' => $row['id_poll'],
  1092. )
  1093. );
  1094. $allow_vote = $smcFunc['db_num_rows']($request) == 0;
  1095. $smcFunc['db_free_result']($request);
  1096. }
  1097. // Can they view?
  1098. $is_expired = !empty($row['expire_time']) && $row['expire_time'] < time();
  1099. $allow_view_results = allowedTo('moderate_board') || $row['hide_results'] == 0 || ($row['hide_results'] == 1 && !$allow_vote) || $is_expired;
  1100. $request = $smcFunc['db_query']('', '
  1101. SELECT COUNT(DISTINCT id_member)
  1102. FROM {db_prefix}log_polls
  1103. WHERE id_poll = {int:current_poll}',
  1104. array(
  1105. 'current_poll' => $row['id_poll'],
  1106. )
  1107. );
  1108. list ($total) = $smcFunc['db_fetch_row']($request);
  1109. $smcFunc['db_free_result']($request);
  1110. $request = $smcFunc['db_query']('', '
  1111. SELECT id_choice, label, votes
  1112. FROM {db_prefix}poll_choices
  1113. WHERE id_poll = {int:current_poll}',
  1114. array(
  1115. 'current_poll' => $row['id_poll'],
  1116. )
  1117. );
  1118. $options = array();
  1119. $total_votes = 0;
  1120. while ($rowChoice = $smcFunc['db_fetch_assoc']($request))
  1121. {
  1122. censorText($rowChoice['label']);
  1123. $options[$rowChoice['id_choice']] = array($rowChoice['label'], $rowChoice['votes']);
  1124. $total_votes += $rowChoice['votes'];
  1125. }
  1126. $smcFunc['db_free_result']($request);
  1127. $return = array(
  1128. 'id' => $row['id_poll'],
  1129. 'image' => empty($row['voting_locked']) ? 'poll' : 'locked_poll',
  1130. 'question' => $row['question'],
  1131. 'total_votes' => $total,
  1132. 'is_locked' => !empty($row['voting_locked']),
  1133. 'allow_vote' => $allow_vote,
  1134. 'allow_view_results' => $allow_view_results,
  1135. 'topic' => $topic
  1136. );
  1137. // Calculate the percentages and bar lengths...
  1138. $divisor = $total_votes == 0 ? 1 : $total_votes;
  1139. foreach ($options as $i => $option)
  1140. {
  1141. $bar = floor(($option[1] * 100) / $divisor);
  1142. $barWide = $bar == 0 ? 1 : floor(($bar * 5) / 3);
  1143. $return['options'][$i] = array(
  1144. 'id' => 'options-' . $i,
  1145. 'percent' => $bar,
  1146. 'votes' => $option[1],
  1147. 'option' => parse_bbc($option[0]),
  1148. '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') . '">'
  1149. );
  1150. }
  1151. $return['allowed_warning'] = $row['max_votes'] > 1 ? sprintf($txt['poll_options6'], min(count($options), $row['max_votes'])) : '';
  1152. if ($output_method != 'echo')
  1153. return $return;
  1154. if ($return['allow_vote'])
  1155. {
  1156. echo '
  1157. <form class="ssi_poll" action="', $boardurl, '/SSI.php?ssi_function=pollVote" method="post" accept-charset="', $context['character_set'], '">
  1158. <strong>', $return['question'], '</strong><br>
  1159. ', !empty($return['allowed_warning']) ? $return['allowed_warning'] . '<br>' : '';
  1160. foreach ($return['options'] as $option)
  1161. echo '
  1162. <label for="', $option['id'], '">', $option['vote_button'], ' ', $option['option'], '</label><br>';
  1163. echo '
  1164. <input type="submit" value="', $txt['poll_vote'], '" class="button_submit">
  1165. <input type="hidden" name="poll" value="', $return['id'], '">
  1166. <input type="hidden" name="', $context['session_var'], '" value="', $context['session_id'], '">
  1167. </form>';
  1168. }
  1169. elseif ($return['allow_view_results'])
  1170. {
  1171. echo '
  1172. <div class="ssi_poll">
  1173. <strong>', $return['question'], '</strong>
  1174. <dl>';
  1175. foreach ($return['options'] as $option)
  1176. echo '
  1177. <dt>', $option['option'], '</dt>
  1178. <dd>
  1179. <div class="ssi_poll_bar" style="border: 1px solid #666; height: 1em">
  1180. <div class="ssi_poll_bar_fill" style="background: #ccf; height: 1em; width: ', $option['percent'], '%;">
  1181. </div>
  1182. </div>
  1183. ', $option['votes'], ' (', $option['percent'], '%)
  1184. </dd>';
  1185. echo '
  1186. </dl>
  1187. <strong>', $txt['poll_total_voters'], ': ', $return['total_votes'], '</strong>
  1188. </div>';
  1189. }
  1190. // Cannot see it I'm afraid!
  1191. else
  1192. echo $txt['poll_cannot_see'];
  1193. }
  1194. // Takes care of voting - don't worry, this is done automatically.
  1195. function ssi_pollVote()
  1196. {
  1197. global $context, $db_prefix, $user_info, $sc, $smcFunc, $sourcedir, $modSettings;
  1198. if (!isset($_POST[$context['session_var']]) || $_POST[$context['session_var']] != $sc || empty($_POST['options']) || !isset($_POST['poll']))
  1199. {
  1200. echo '<!DOCTYPE html>
  1201. <html>
  1202. <head>
  1203. <script><!-- // --><![CDATA[
  1204. history.go(-1);
  1205. // ]]></script>
  1206. </head>
  1207. <body>&laquo;</body>
  1208. </html>';
  1209. return;
  1210. }
  1211. // This can cause weird errors! (ie. copyright missing.)
  1212. checkSession();
  1213. $_POST['poll'] = (int) $_POST['poll'];
  1214. // Check if they have already voted, or voting is locked.
  1215. $request = $smcFunc['db_query']('', '
  1216. SELECT
  1217. p.id_poll, p.voting_locked, p.expire_time, p.max_votes, p.guest_vote,
  1218. t.id_topic,
  1219. IFNULL(lp.id_choice, -1) AS selected
  1220. FROM {db_prefix}polls AS p
  1221. INNER JOIN {db_prefix}topics AS t ON (t.id_poll = {int:current_poll})
  1222. INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board)
  1223. LEFT JOIN {db_prefix}log_polls AS lp ON (lp.id_poll = p.id_poll AND lp.id_member = {int:current_member})
  1224. WHERE p.id_poll = {int:current_poll}
  1225. AND {query_see_board}' . ($modSettings['postmod_active'] ? '
  1226. AND t.approved = {int:is_approved}' : '') . '
  1227. LIMIT 1',
  1228. array(
  1229. 'current_member' => $user_info['id'],
  1230. 'current_poll' => $_POST['poll'],
  1231. 'is_approved' => 1,
  1232. )
  1233. );
  1234. if ($smcFunc['db_num_rows']($request) == 0)
  1235. die;
  1236. $row = $smcFunc['db_fetch_assoc']($request);
  1237. $smcFunc['db_free_result']($request);
  1238. if (!empty($row['voting_locked']) || ($row['selected'] != -1 && !$user_info['is_guest']) || (!empty($row['expire_time']) && time() > $row['expire_time']))
  1239. redirectexit('topic=' . $row['id_topic'] . '.0');
  1240. // Too many options checked?
  1241. if (count($_REQUEST['options']) > $row['max_votes'])
  1242. redirectexit('topic=' . $row['id_topic'] . '.0');
  1243. // It's a guest who has already voted?
  1244. if ($user_info['is_guest'])
  1245. {
  1246. // Guest voting disabled?
  1247. if (!$row['guest_vote'])
  1248. redirectexit('topic=' . $row['id_topic'] . '.0');
  1249. // Already voted?
  1250. elseif (isset($_COOKIE['guest_poll_vote']) && in_array($row['id_poll'], explode(',', $_COOKIE['guest_poll_vote'])))
  1251. redirectexit('topic=' . $row['id_topic'] . '.0');
  1252. }
  1253. $options = array();
  1254. $inserts = array();
  1255. foreach ($_REQUEST['options'] as $id)
  1256. {
  1257. $id = (int) $id;
  1258. $options[] = $id;
  1259. $inserts[] = array($_POST['poll'], $user_info['id'], $id);
  1260. }
  1261. // Add their vote in to the tally.
  1262. $smcFunc['db_insert']('insert',
  1263. $db_prefix . 'log_polls',
  1264. array('id_poll' => 'int', 'id_member' => 'int', 'id_choice' => 'int'),
  1265. $inserts,
  1266. array('id_poll', 'id_member', 'id_choice')
  1267. );
  1268. $smcFunc['db_query']('', '
  1269. UPDATE {db_prefix}poll_choices
  1270. SET votes = votes + 1
  1271. WHERE id_poll = {int:current_poll}
  1272. AND id_choice IN ({array_int:option_list})',
  1273. array(
  1274. 'option_list' => $options,
  1275. 'current_poll' => $_POST['poll'],
  1276. )
  1277. );
  1278. // Track the vote if a guest.
  1279. if ($user_info['is_guest'])
  1280. {
  1281. $_COOKIE['guest_poll_vote'] = !empty($_COOKIE['guest_poll_vote']) ? ($_COOKIE['guest_poll_vote'] . ',' . $row['id_poll']) : $row['id_poll'];
  1282. require_once($sourcedir . '/Subs-Auth.php');
  1283. $cookie_url = url_parts(!empty($modSettings['localCookies']), !empty($modSettings['globalCookies']));
  1284. smf_setcookie('guest_poll_vote', $_COOKIE['guest_poll_vote'], time() + 2500000, $cookie_url[1], $cookie_url[0], false, false);
  1285. }
  1286. redirectexit('topic=' . $row['id_topic'] . '.0');
  1287. }
  1288. // Show a search box.
  1289. function ssi_quickSearch($output_method = 'echo')
  1290. {
  1291. global $scripturl, $txt, $context;
  1292. if (!allowedTo('search_posts'))
  1293. return;
  1294. if ($output_method != 'echo')
  1295. return $scripturl . '?action=search';
  1296. echo '
  1297. <form action="', $scripturl, '?action=search2" method="post" accept-charset="', $context['character_set'], '">
  1298. <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">
  1299. </form>';
  1300. }
  1301. // Show what would be the forum news.
  1302. function ssi_news($output_method = 'echo')
  1303. {
  1304. global $context;
  1305. if ($output_method != 'echo')
  1306. return $context['random_news_line'];
  1307. echo $context['random_news_line'];
  1308. }
  1309. // Show today's birthdays.
  1310. function ssi_todaysBirthdays($output_method = 'echo')
  1311. {
  1312. global $scripturl, $modSettings, $user_info;
  1313. if (empty($modSettings['cal_enabled']) || !allowedTo('calendar_view') || !allowedTo('profile_view'))
  1314. return;
  1315. $eventOptions = array(
  1316. 'include_birthdays' => true,
  1317. 'num_days_shown' => empty($modSettings['cal_days_for_index']) || $modSettings['cal_days_for_index'] < 1 ? 1 : $modSettings['cal_days_for_index'],
  1318. );
  1319. $return = cache_quick_get('calendar_index_offset_' . ($user_info['time_offset'] + $modSettings['time_offset']), 'Subs-Calendar.php', 'cache_getRecentEvents', array($eventOptions));
  1320. if ($output_method != 'echo')
  1321. return $return['calendar_birthdays'];
  1322. foreach ($return['calendar_birthdays'] as $member)
  1323. echo '
  1324. <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'] ? ', ' : '');
  1325. }
  1326. // Show today's holidays.
  1327. function ssi_todaysHolidays($output_method = 'echo')
  1328. {
  1329. global $modSettings, $user_info;
  1330. if (empty($modSettings['cal_enabled']) || !allowedTo('calendar_view'))
  1331. return;
  1332. $eventOptions = array(
  1333. 'include_holidays' => true,
  1334. 'num_days_shown' => empty($modSettings['cal_days_for_index']) || $modSettings['cal_days_for_index'] < 1 ? 1 : $modSettings['cal_days_for_index'],
  1335. );
  1336. $return = cache_quick_get('calendar_index_offset_' . ($user_info['time_offset'] + $modSettings['time_offset']), 'Subs-Calendar.php', 'cache_getRecentEvents', array($eventOptions));
  1337. if ($output_method != 'echo')
  1338. return $return['calendar_holidays'];
  1339. echo '
  1340. ', implode(', ', $return['calendar_holidays']);
  1341. }
  1342. // Show today's events.
  1343. function ssi_todaysEvents($output_method = 'echo')
  1344. {
  1345. global $modSettings, $user_info;
  1346. if (empty($modSettings['cal_enabled']) || !allowedTo('calendar_view'))
  1347. return;
  1348. $eventOptions = array(
  1349. 'include_events' => true,
  1350. 'num_days_shown' => empty($modSettings['cal_days_for_index']) || $modSettings['cal_days_for_index'] < 1 ? 1 : $modSettings['cal_days_for_index'],
  1351. );
  1352. $return = cache_quick_get('calendar_index_offset_' . ($user_info['time_offset'] + $modSettings['time_offset']), 'Subs-Calendar.php', 'cache_getRecentEvents', array($eventOptions));
  1353. if ($output_method != 'echo')
  1354. return $return['calendar_events'];
  1355. foreach ($return['calendar_events'] as $event)
  1356. {
  1357. if ($event['can_edit'])
  1358. echo '
  1359. <a href="' . $event['modify_href'] . '" style="color: #ff0000;">*</a> ';
  1360. echo '
  1361. ' . $event['link'] . (!$event['is_last'] ? ', ' : '');
  1362. }
  1363. }
  1364. // Show all calendar entires for today. (birthdays, holodays, and events.)
  1365. function ssi_todaysCalendar($output_method = 'echo')
  1366. {
  1367. global $modSettings, $txt, $scripturl, $user_info;
  1368. if (empty($modSettings['cal_enabled']) || !allowedTo('calendar_view'))
  1369. return;
  1370. $eventOptions = array(
  1371. 'include_birthdays' => allowedTo('profile_view'),
  1372. 'include_holidays' => true,
  1373. 'include_events' => true,
  1374. 'num_days_shown' => empty($modSettings['cal_days_for_index']) || $modSettings['cal_days_for_index'] < 1 ? 1 : $modSettings['cal_days_for_index'],
  1375. );
  1376. $return = cache_quick_get('calendar_index_offset_' . ($user_info['time_offset'] + $modSettings['time_offset']), 'Subs-Calendar.php', 'cache_getRecentEvents', array($eventOptions));
  1377. if ($output_method != 'echo')
  1378. return $return;
  1379. if (!empty($return['calendar_holidays']))
  1380. echo '
  1381. <span class="holiday">' . $txt['calendar_prompt'] . ' ' . implode(', ', $return['calendar_holidays']) . '<br></span>';
  1382. if (!empty($return['calendar_birthdays']))
  1383. {
  1384. echo '
  1385. <span class="birthday">' . $txt['birthdays_upcoming'] . '</span> ';
  1386. foreach ($return['calendar_birthdays'] as $member)
  1387. echo '
  1388. <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'] ? ', ' : '';
  1389. echo '
  1390. <br>';
  1391. }
  1392. if (!empty($return['calendar_events']))
  1393. {
  1394. echo '
  1395. <span class="event">' . $txt['events_upcoming'] . '</span> ';
  1396. foreach ($return['calendar_events'] as $event)
  1397. {
  1398. if ($event['can_edit'])
  1399. echo '
  1400. <a href="' . $event['modify_href'] . '" style="color: #ff0000;">*</a> ';
  1401. echo '
  1402. ' . $event['link'] . (!$event['is_last'] ? ', ' : '');
  1403. }
  1404. }
  1405. }
  1406. // Show the latest news, with a template... by board.
  1407. function ssi_boardNews($board = null, $limit = null, $start = null, $length = null, $output_method = 'echo')
  1408. {
  1409. global $scripturl, $txt, $settings, $modSettings, $context;
  1410. global $smcFunc;
  1411. loadLanguage('Stats');
  1412. // Must be integers....
  1413. if ($limit === null)
  1414. $limit = isset($_GET['limit']) ? (int) $_GET['limit'] : 5;
  1415. else
  1416. $limit = (int) $limit;
  1417. if ($start === null)
  1418. $start = isset($_GET['start']) ? (int) $_GET['start'] : 0;
  1419. else
  1420. $start = (int) $start;
  1421. if ($board !== null)
  1422. $board = (int) $board;
  1423. elseif (isset($_GET['board']))
  1424. $board = (int) $_GET['board'];
  1425. if ($length === null)
  1426. $length = isset($_GET['length']) ? (int) $_GET['length'] : 0;
  1427. else
  1428. $length = (int) $length;
  1429. $limit = max(0, $limit);
  1430. $start = max(0, $start);
  1431. // Make sure guests can see this board.
  1432. $request = $smcFunc['db_query']('', '
  1433. SELECT id_board
  1434. FROM {db_prefix}boards
  1435. WHERE ' . ($board === null ? '' : 'id_board = {int:current_board}
  1436. AND ') . 'FIND_IN_SET(-1, member_groups) != 0
  1437. LIMIT 1',
  1438. array(
  1439. 'current_board' => $board,
  1440. )
  1441. );
  1442. if ($smcFunc['db_num_rows']($request) == 0)
  1443. {
  1444. if ($output_method == 'echo')
  1445. die($txt['ssi_no_guests']);
  1446. else
  1447. return array();
  1448. }
  1449. list ($board) = $smcFunc['db_fetch_row']($request);
  1450. $smcFunc['db_free_result']($request);
  1451. // Load the message icons - the usual suspects.
  1452. $stable_icons = array('xx', 'thumbup', 'thumbdown', 'exclamation', 'question', 'lamp', 'smiley', 'angry', 'cheesy', 'grin', 'sad', 'wink', 'poll', 'moved', 'recycled', 'wireless');
  1453. $icon_sources = array();
  1454. foreach ($stable_icons as $icon)
  1455. $icon_sources[$icon] = 'images_url';
  1456. // Find the post ids.
  1457. $request = $smcFunc['db_query']('', '
  1458. SELECT t.id_first_msg
  1459. FROM {db_prefix}topics as t
  1460. LEFT JOIN {db_prefix}boards as b ON (b.id_board = t.id_board)
  1461. WHERE t.id_board = {int:current_board}' . ($modSettings['postmod_active'] ? '
  1462. AND t.approved = {int:is_approved}' : '') . '
  1463. AND {query_see_board}
  1464. ORDER BY t.id_first_msg DESC
  1465. LIMIT ' . $start . ', ' . $limit,
  1466. array(
  1467. 'current_board' => $board,
  1468. 'is_approved' => 1,
  1469. )
  1470. );
  1471. $posts = array();
  1472. while ($row = $smcFunc['db_fetch_assoc']($request))
  1473. $posts[] = $row['id_first_msg'];
  1474. $smcFunc['db_free_result']($request);
  1475. if (empty($posts))
  1476. return array();
  1477. // Find the posts.
  1478. $request = $smcFunc['db_query']('', '
  1479. SELECT
  1480. m.icon, m.subject, m.body, IFNULL(mem.real_name, m.poster_name) AS poster_name, m.poster_time,
  1481. t.num_replies, t.id_topic, m.id_member, m.smileys_enabled, m.id_msg, t.locked, t.id_last_msg, m.id_board
  1482. FROM {db_prefix}topics AS t
  1483. INNER JOIN {db_prefix}messages AS m ON (m.id_msg = t.id_first_msg)
  1484. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member)
  1485. WHERE t.id_first_msg IN ({array_int:post_list})
  1486. ORDER BY t.id_first_msg DESC
  1487. LIMIT ' . count($posts),
  1488. array(
  1489. 'post_list' => $posts,
  1490. )
  1491. );
  1492. $return = array();
  1493. $recycle_board = !empty($modSettings['recycle_enable']) && !empty($modSettings['recycle_board']) ? (int) $modSettings['recycle_board'] : 0;
  1494. while ($row = $smcFunc['db_fetch_assoc']($request))
  1495. {
  1496. // If we want to limit the length of the post.
  1497. if (!empty($length) && $smcFunc['strlen']($row['body']) > $length)
  1498. {
  1499. $row['body'] = $smcFunc['substr']($row['body'], 0, $length);
  1500. $cutoff = false;
  1501. $last_space = strrpos($row['body'], ' ');
  1502. $last_open = strrpos($row['body'], '<');
  1503. $last_close = strrpos($row['body'], '>');
  1504. 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)
  1505. $cutoff = $last_open;
  1506. elseif (empty($last_close) || $last_close < $last_open)
  1507. $cutoff = $last_space;
  1508. if ($cutoff !== false)
  1509. $row['body'] = $smcFunc['substr']($row['body'], 0, $cutoff);
  1510. $row['body'] .= '...';
  1511. }
  1512. $row['body'] = parse_bbc($row['body'], $row['smileys_enabled'], $row['id_msg']);
  1513. if (!empty($recycle_board) && $row['id_board'] == $recycle_board)
  1514. $row['icon'] = 'recycled';
  1515. // Check that this message icon is there...
  1516. if (!empty($modSettings['messageIconChecks_enable']) && !isset($icon_sources[$row['icon']]))
  1517. $icon_sources[$row['icon']] = file_exists($settings['theme_dir'] . '/images/post/' . $row['icon'] . '.png') ? 'images_url' : 'default_images_url';
  1518. censorText($row['subject']);
  1519. censorText($row['body']);
  1520. $return[] = array(
  1521. 'id' => $row['id_topic'],
  1522. 'message_id' => $row['id_msg'],
  1523. 'icon' => '<img src="' . $settings[$icon_sources[$row['icon']]] . '/post/' . $row['icon'] . '.png" alt="' . $row['icon'] . '">',
  1524. 'subject' => $row['subject'],
  1525. 'time' => timeformat($row['poster_time']),
  1526. 'timestamp' => forum_time(true, $row['poster_time']),
  1527. 'body' => $row['body'],
  1528. 'href' => $scripturl . '?topic=' . $row['id_topic'] . '.0',
  1529. 'link' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.0">' . $row['num_replies'] . ' ' . ($row['num_replies'] == 1 ? $txt['ssi_comment'] : $txt['ssi_comments']) . '</a>',
  1530. 'replies' => $row['num_replies'],
  1531. 'comment_href' => !empty($row['locked']) ? '' : $scripturl . '?action=post;topic=' . $row['id_topic'] . '.' . $row['num_replies'] . ';last_msg=' . $row['id_last_msg'],
  1532. '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>',
  1533. 'new_comment' => !empty($row['locked']) ? '' : '<a href="' . $scripturl . '?action=post;topic=' . $row['id_topic'] . '.' . $row['num_replies'] . '">' . $txt['ssi_write_comment'] . '</a>',
  1534. 'poster' => array(
  1535. 'id' => $row['id_member'],
  1536. 'name' => $row['poster_name'],
  1537. 'href' => !empty($row['id_member']) ? $scripturl . '?action=profile;u=' . $row['id_member'] : '',
  1538. 'link' => !empty($row['id_member']) ? '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['poster_name'] . '</a>' : $row['poster_name']
  1539. ),
  1540. 'locked' => !empty($row['locked']),
  1541. 'is_last' => false
  1542. );
  1543. }
  1544. $smcFunc['db_free_result']($request);
  1545. if (empty($return))
  1546. return $return;
  1547. $return[count($return) - 1]['is_last'] = true;
  1548. if ($output_method != 'echo')
  1549. return $return;
  1550. foreach ($return as $news)
  1551. {
  1552. echo '
  1553. <div class="news_item">
  1554. <h3 class="news_header">
  1555. ', $news['icon'], '
  1556. <a href="', $news['href'], '">', $news['subject'], '</a>
  1557. </h3>
  1558. <div class="news_timestamp">', $news['time'], ' ', $txt['by'], ' ', $news['poster']['link'], '</div>
  1559. <div class="news_body" style="padding: 2ex 0;">', $news['body'], '</div>
  1560. ', $news['link'], $news['locked'] ? '' : ' | ' . $news['comment_link'], '
  1561. </div>';
  1562. if (!$news['is_last'])
  1563. echo '
  1564. <hr>';
  1565. }
  1566. }
  1567. // Show the most recent events.
  1568. function ssi_recentEvents($max_events = 7, $output_method = 'echo')
  1569. {
  1570. global $user_info, $scripturl, $modSettings, $txt, $context, $smcFunc;
  1571. if (empty($modSettings['cal_enabled']) || !allowedTo('calendar_view'))
  1572. return;
  1573. // Find all events which are happening in the near future that the member can see.
  1574. $request = $smcFunc['db_query']('', '
  1575. SELECT
  1576. cal.id_event, cal.start_date, cal.end_date, cal.title, cal.id_member, cal.id_topic,
  1577. cal.id_board, t.id_first_msg, t.approved
  1578. FROM {db_prefix}calendar AS cal
  1579. LEFT JOIN {db_prefix}boards AS b ON (b.id_board = cal.id_board)
  1580. LEFT JOIN {db_prefix}topics AS t ON (t.id_topic = cal.id_topic)
  1581. WHERE cal.start_date <= {date:current_date}
  1582. AND cal.end_date >= {date:current_date}
  1583. AND (cal.id_board = {int:no_board} OR {query_wanna_see_board})
  1584. ORDER BY cal.start_date DESC
  1585. LIMIT ' . $max_events,
  1586. array(
  1587. 'current_date' => strftime('%Y-%m-%d', forum_time(false)),
  1588. 'no_board' => 0,
  1589. )
  1590. );
  1591. $return = array();
  1592. $duplicates = array();
  1593. while ($row = $smcFunc['db_fetch_assoc']($request))
  1594. {
  1595. // 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.
  1596. if (!empty($duplicates[$row['title'] . $row['id_topic']]))
  1597. continue;
  1598. // Censor the title.
  1599. censorText($row['title']);
  1600. if ($row['start_date'] < strftime('%Y-%m-%d', forum_time(false)))
  1601. $date = strftime('%Y-%m-%d', forum_time(false));
  1602. else
  1603. $date = $row['start_date'];
  1604. // If the topic it is attached to is not approved then don't link it.
  1605. if (!empty($row['id_first_msg']) && !$row['approved'])
  1606. $row['id_board'] = $row['id_topic'] = $row['id_first_msg'] = 0;
  1607. $return[$date][] = array(
  1608. 'id' => $row['id_event'],
  1609. 'title' => $row['title'],
  1610. 'can_edit' => allowedTo('calendar_edit_any') || ($row['id_member'] == $user_info['id'] && allowedTo('calendar_edit_own')),
  1611. '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'],
  1612. 'href' => $row['id_board'] == 0 ? '' : $scripturl . '?topic=' . $row['id_topic'] . '.0',
  1613. 'link' => $row['id_board'] == 0 ? $row['title'] : '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.0">' . $row['title'] . '</a>',
  1614. 'start_date' => $row['start_date'],
  1615. 'end_date' => $row['end_date'],
  1616. 'is_last' => false
  1617. );
  1618. // Let's not show this one again, huh?
  1619. $duplicates[$row['title'] . $row['id_topic']] = true;
  1620. }
  1621. $smcFunc['db_free_result']($request);
  1622. foreach ($return as $mday => $array)
  1623. $return[$mday][count($array) - 1]['is_last'] = true;
  1624. if ($output_method != 'echo' || empty($return))
  1625. return $return;
  1626. // Well the output method is echo.
  1627. echo '
  1628. <span class="event">' . $txt['events'] . '</span> ';
  1629. foreach ($return as $mday => $array)
  1630. foreach ($array as $event)
  1631. {
  1632. if ($event['can_edit'])
  1633. echo '
  1634. <a href="' . $event['modify_href'] . '" style="color: #ff0000;">*</a> ';
  1635. echo '
  1636. ' . $event['link'] . (!$event['is_last'] ? ', ' : '');
  1637. }
  1638. }
  1639. // Check the passed id_member/password. If $is_username is true, treats $id as a username.
  1640. function ssi_checkPassword($id = null, $password = null, $is_username = false)
  1641. {
  1642. global $sourcedir, $smcFunc;
  1643. // If $id is null, this was most likely called from a query string and should do nothing.
  1644. if ($id === null)
  1645. return;
  1646. $request = $smcFunc['db_query']('', '
  1647. SELECT passwd, member_name, is_activated
  1648. FROM {db_prefix}members
  1649. WHERE ' . ($is_username ? 'member_name' : 'id_member') . ' = {string:id}
  1650. LIMIT 1',
  1651. array(
  1652. 'id' => $id,
  1653. )
  1654. );
  1655. list ($pass, $user, $active) = $smcFunc['db_fetch_row']($request);
  1656. $smcFunc['db_free_result']($request);
  1657. return sha1(strtolower($user) . $password) == $pass && $active == 1;
  1658. }
  1659. // We want to show the recent attachments outside of the forum.
  1660. function ssi_recentAttachments($num_attachments = 10, $attachment_ext = array(), $output_method = 'echo')
  1661. {
  1662. global $smcFunc, $context, $modSettings, $scripturl, $txt, $settings;
  1663. // We want to make sure that we only get attachments for boards that we can see *if* any.
  1664. $attachments_boards = boardsAllowedTo('view_attachments');
  1665. // No boards? Adios amigo.
  1666. if (empty($attachments_boards))
  1667. return array();
  1668. // Is it an array?
  1669. if (!is_array($attachment_ext))
  1670. $attachment_ext = array($attachment_ext);
  1671. // Lets build the query.
  1672. $request = $smcFunc['db_query']('', '
  1673. SELECT
  1674. att.id_attach, att.id_msg, att.filename, IFNULL(att.size, 0) AS filesize, att.downloads, mem.id_member,
  1675. IFNULL(mem.real_name, m.poster_name) AS poster_name, m.id_topic, m.subject, t.id_board, m.poster_time,
  1676. 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') . '
  1677. FROM {db_prefix}attachments AS att
  1678. INNER JOIN {db_prefix}messages AS m ON (m.id_msg = att.id_msg)
  1679. INNER JOIN {db_prefix}topics AS t ON (t.id_topic = m.id_topic)
  1680. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member)' . (empty($modSettings['attachmentShowImages']) || empty($modSettings['attachmentThumbnails']) ? '' : '
  1681. LEFT JOIN {db_prefix}attachments AS thumb ON (thumb.id_attach = att.id_thumb)') . '
  1682. WHERE att.attachment_type = 0' . ($attachments_boards === array(0) ? '' : '
  1683. AND m.id_board IN ({array_int:boards_can_see})') . (!empty($attachment_ext) ? '
  1684. AND att.fileext IN ({array_string:attachment_ext})' : '') .
  1685. (!$modSettings['postmod_active'] || allowedTo('approve_posts') ? '' : '
  1686. AND t.approved = {int:is_approved}
  1687. AND m.approved = {int:is_approved}
  1688. AND att.approved = {int:is_approved}') . '
  1689. ORDER BY att.id_attach DESC
  1690. LIMIT {int:num_attachments}',
  1691. array(
  1692. 'boards_can_see' => $attachments_boards,
  1693. 'attachment_ext' => $attachment_ext,
  1694. 'num_attachments' => $num_attachments,
  1695. 'is_approved' => 1,
  1696. )
  1697. );
  1698. // We have something.
  1699. $attachments = array();
  1700. while ($row = $smcFunc['db_fetch_assoc']($request))
  1701. {
  1702. $filename = preg_replace('~&amp;#(\\d{1,7}|x[0-9a-fA-F]{1,6});~', '&#\\1;', htmlspecialchars($row['filename']));
  1703. // Is it an image?
  1704. $attachments[$row['id_attach']] = array(
  1705. 'member' => array(
  1706. 'id' => $row['id_member'],
  1707. 'name' => $row['poster_name'],
  1708. 'link' => empty($row['id_member']) ? $row['poster_name'] : '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['poster_name'] . '</a>',
  1709. ),
  1710. 'file' => array(
  1711. 'filename' => $filename,
  1712. 'filesize' => round($row['filesize'] /1024, 2) . $txt['kilobyte'],
  1713. 'downloads' => $row['downloads'],
  1714. 'href' => $scripturl . '?action=dlattach;topic=' . $row['id_topic'] . '.0;attach=' . $row['id_attach'],
  1715. '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>',
  1716. 'is_image' => !empty($row['width']) && !empty($row['height']) && !empty($modSettings['attachmentShowImages']),
  1717. ),
  1718. 'topic' => array(
  1719. 'id' => $row['id_topic'],
  1720. 'subject' => $row['subject'],
  1721. 'href' => $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . '#msg' . $row['id_msg'],
  1722. 'link' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . '#msg' . $row['id_msg'] . '">' . $row['subject'] . '</a>',
  1723. 'time' => timeformat($row['poster_time']),
  1724. ),
  1725. );
  1726. // Images.
  1727. if ($attachments[$row['id_attach']]['file']['is_image'])
  1728. {
  1729. $id_thumb = empty($row['id_thumb']) ? $row['id_attach'] : $row['id_thumb'];
  1730. $attachments[$row['id_attach']]['file']['image'] = array(
  1731. 'id' => $id_thumb,
  1732. 'width' => $row['width'],
  1733. 'height' => $row['height'],
  1734. 'img' => '<img src="' . $scripturl . '?action=dlattach;topic=' . $row['id_topic'] . '.0;attach=' . $row['id_attach'] . ';image" alt="' . $filename . '">',
  1735. 'thumb' => '<img src="' . $scripturl . '?action=dlattach;topic=' . $row['id_topic'] . '.0;attach=' . $id_thumb . ';image" alt="' . $filename . '">',
  1736. 'href' => $scripturl . '?action=dlattach;topic=' . $row['id_topic'] . '.0;attach=' . $id_thumb . ';image',
  1737. '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>',
  1738. );
  1739. }
  1740. }
  1741. $smcFunc['db_free_result']($request);
  1742. // So you just want an array? Here you can have it.
  1743. if ($output_method == 'array' || empty($attachments))
  1744. return $attachments;
  1745. // Give them the default.
  1746. echo '
  1747. <table class="ssi_downloads" cellpadding="2">
  1748. <tr>
  1749. <th align="left">', $txt['file'], '</th>
  1750. <th align="left">', $txt['posted_by'], '</th>
  1751. <th align="left">', $txt['downloads'], '</th>
  1752. <th align="left">', $txt['filesize'], '</th>
  1753. </tr>';
  1754. foreach ($attachments as $attach)
  1755. echo '
  1756. <tr>
  1757. <td>', $attach['file']['link'], '</td>
  1758. <td>', $attach['member']['link'], '</td>
  1759. <td align="center">', $attach['file']['downloads'], '</td>
  1760. <td>', $attach['file']['filesize'], '</td>
  1761. </tr>';
  1762. echo '
  1763. </table>';
  1764. }
  1765. ?>