News.php 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990
  1. <?php
  2. /**
  3. * This file contains the files necessary to display news as an XML feed.
  4. *
  5. * Simple Machines Forum (SMF)
  6. *
  7. * @package SMF
  8. * @author Simple Machines http://www.simplemachines.org
  9. * @copyright 2011 Simple Machines
  10. * @license http://www.simplemachines.org/about/smf/license.php BSD
  11. *
  12. * @version 2.1 Alpha 1
  13. */
  14. if (!defined('SMF'))
  15. die('Hacking attempt...');
  16. /**
  17. * Outputs xml data representing recent information or a profile.
  18. * Can be passed 4 subactions which decide what is output:
  19. * 'recent' for recent posts,
  20. * 'news' for news topics,
  21. * 'members' for recently registered members,
  22. * 'profile' for a member's profile.
  23. * To display a member's profile, a user id has to be given. (;u=1)
  24. * Outputs an rss feed instead of a proprietary one if the 'type' $_GET
  25. * parameter is 'rss' or 'rss2'.
  26. * Accessed via ?action=.xml.
  27. * Does not use any templates, sub templates, or template layers.
  28. *
  29. * @uses Stats language file.
  30. */
  31. function ShowXmlFeed()
  32. {
  33. global $board, $board_info, $context, $scripturl, $txt, $modSettings, $user_info;
  34. global $query_this_board, $smcFunc, $forum_version, $cdata_override;
  35. // If it's not enabled, die.
  36. if (empty($modSettings['xmlnews_enable']))
  37. obExit(false);
  38. loadLanguage('Stats');
  39. // Default to latest 5. No more than 255, please.
  40. $_GET['limit'] = empty($_GET['limit']) || (int) $_GET['limit'] < 1 ? 5 : min((int) $_GET['limit'], 255);
  41. // Handle the cases where a board, boards, or category is asked for.
  42. $query_this_board = 1;
  43. $context['optimize_msg'] = array(
  44. 'highest' => 'm.id_msg <= b.id_last_msg',
  45. );
  46. if (!empty($_REQUEST['c']) && empty($board))
  47. {
  48. $_REQUEST['c'] = explode(',', $_REQUEST['c']);
  49. foreach ($_REQUEST['c'] as $i => $c)
  50. $_REQUEST['c'][$i] = (int) $c;
  51. if (count($_REQUEST['c']) == 1)
  52. {
  53. $request = $smcFunc['db_query']('', '
  54. SELECT name
  55. FROM {db_prefix}categories
  56. WHERE id_cat = {int:current_category}',
  57. array(
  58. 'current_category' => (int) $_REQUEST['c'][0],
  59. )
  60. );
  61. list ($feed_title) = $smcFunc['db_fetch_row']($request);
  62. $smcFunc['db_free_result']($request);
  63. $feed_title = ' - ' . strip_tags($feed_title);
  64. }
  65. $request = $smcFunc['db_query']('', '
  66. SELECT b.id_board, b.num_posts
  67. FROM {db_prefix}boards AS b
  68. WHERE b.id_cat IN ({array_int:current_category_list})
  69. AND {query_see_board}',
  70. array(
  71. 'current_category_list' => $_REQUEST['c'],
  72. )
  73. );
  74. $total_cat_posts = 0;
  75. $boards = array();
  76. while ($row = $smcFunc['db_fetch_assoc']($request))
  77. {
  78. $boards[] = $row['id_board'];
  79. $total_cat_posts += $row['num_posts'];
  80. }
  81. $smcFunc['db_free_result']($request);
  82. if (!empty($boards))
  83. $query_this_board = 'b.id_board IN (' . implode(', ', $boards) . ')';
  84. // Try to limit the number of messages we look through.
  85. if ($total_cat_posts > 100 && $total_cat_posts > $modSettings['totalMessages'] / 15)
  86. $context['optimize_msg']['lowest'] = 'm.id_msg >= ' . max(0, $modSettings['maxMsgID'] - 400 - $_GET['limit'] * 5);
  87. }
  88. elseif (!empty($_REQUEST['boards']))
  89. {
  90. $_REQUEST['boards'] = explode(',', $_REQUEST['boards']);
  91. foreach ($_REQUEST['boards'] as $i => $b)
  92. $_REQUEST['boards'][$i] = (int) $b;
  93. $request = $smcFunc['db_query']('', '
  94. SELECT b.id_board, b.num_posts, b.name
  95. FROM {db_prefix}boards AS b
  96. WHERE b.id_board IN ({array_int:board_list})
  97. AND {query_see_board}
  98. LIMIT ' . count($_REQUEST['boards']),
  99. array(
  100. 'board_list' => $_REQUEST['boards'],
  101. )
  102. );
  103. // Either the board specified doesn't exist or you have no access.
  104. $num_boards = $smcFunc['db_num_rows']($request);
  105. if ($num_boards == 0)
  106. fatal_lang_error('no_board');
  107. $total_posts = 0;
  108. $boards = array();
  109. while ($row = $smcFunc['db_fetch_assoc']($request))
  110. {
  111. if ($num_boards == 1)
  112. $feed_title = ' - ' . strip_tags($row['name']);
  113. $boards[] = $row['id_board'];
  114. $total_posts += $row['num_posts'];
  115. }
  116. $smcFunc['db_free_result']($request);
  117. if (!empty($boards))
  118. $query_this_board = 'b.id_board IN (' . implode(', ', $boards) . ')';
  119. // The more boards, the more we're going to look through...
  120. if ($total_posts > 100 && $total_posts > $modSettings['totalMessages'] / 12)
  121. $context['optimize_msg']['lowest'] = 'm.id_msg >= ' . max(0, $modSettings['maxMsgID'] - 500 - $_GET['limit'] * 5);
  122. }
  123. elseif (!empty($board))
  124. {
  125. $request = $smcFunc['db_query']('', '
  126. SELECT num_posts
  127. FROM {db_prefix}boards
  128. WHERE id_board = {int:current_board}
  129. LIMIT 1',
  130. array(
  131. 'current_board' => $board,
  132. )
  133. );
  134. list ($total_posts) = $smcFunc['db_fetch_row']($request);
  135. $smcFunc['db_free_result']($request);
  136. $feed_title = ' - ' . strip_tags($board_info['name']);
  137. $query_this_board = 'b.id_board = ' . $board;
  138. // Try to look through just a few messages, if at all possible.
  139. if ($total_posts > 80 && $total_posts > $modSettings['totalMessages'] / 10)
  140. $context['optimize_msg']['lowest'] = 'm.id_msg >= ' . max(0, $modSettings['maxMsgID'] - 600 - $_GET['limit'] * 5);
  141. }
  142. else
  143. {
  144. $query_this_board = '{query_see_board}' . (!empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] > 0 ? '
  145. AND b.id_board != ' . $modSettings['recycle_board'] : '');
  146. $context['optimize_msg']['lowest'] = 'm.id_msg >= ' . max(0, $modSettings['maxMsgID'] - 100 - $_GET['limit'] * 5);
  147. }
  148. // Show in rss or proprietary format?
  149. $xml_format = isset($_GET['type']) && in_array($_GET['type'], array('smf', 'rss', 'rss2', 'atom', 'rdf', 'webslice')) ? $_GET['type'] : 'smf';
  150. /**
  151. * @todo Birthdays?
  152. */
  153. // List all the different types of data they can pull.
  154. $subActions = array(
  155. 'recent' => array('getXmlRecent', 'recent-post'),
  156. 'news' => array('getXmlNews', 'article'),
  157. 'members' => array('getXmlMembers', 'member'),
  158. 'profile' => array('getXmlProfile', null),
  159. );
  160. if (empty($_GET['sa']) || !isset($subActions[$_GET['sa']]))
  161. $_GET['sa'] = 'recent';
  162. /**
  163. * @todo Temp - webslices doesn't do everything yet.
  164. */
  165. if ($xml_format == 'webslice' && $_GET['sa'] != 'recent')
  166. $xml_format = 'rss2';
  167. // If this is webslices we kinda cheat - we allow a template that we call direct for the HTML, and we override the CDATA.
  168. elseif ($xml_format == 'webslice')
  169. {
  170. $context['user'] += $user_info;
  171. $cdata_override = true;
  172. loadTemplate('Xml');
  173. }
  174. // We only want some information, not all of it.
  175. $cachekey = array($xml_format, $_GET['action'], $_GET['limit'], $_GET['sa']);
  176. foreach (array('board', 'boards', 'c') as $var)
  177. if (isset($_REQUEST[$var]))
  178. $cachekey[] = $_REQUEST[$var];
  179. $cachekey = md5(serialize($cachekey) . (!empty($query_this_board) ? $query_this_board : ''));
  180. $cache_t = microtime();
  181. // Get the associative array representing the xml.
  182. if (!empty($modSettings['cache_enable']) && (!$user_info['is_guest'] || $modSettings['cache_enable'] >= 3))
  183. $xml = cache_get_data('xmlfeed-' . $xml_format . ':' . ($user_info['is_guest'] ? '' : $user_info['id'] . '-') . $cachekey, 240);
  184. if (empty($xml))
  185. {
  186. $xml = $subActions[$_GET['sa']][0]($xml_format);
  187. if (!empty($modSettings['cache_enable']) && (($user_info['is_guest'] && $modSettings['cache_enable'] >= 3)
  188. || (!$user_info['is_guest'] && (array_sum(explode(' ', microtime())) - array_sum(explode(' ', $cache_t)) > 0.2))))
  189. cache_put_data('xmlfeed-' . $xml_format . ':' . ($user_info['is_guest'] ? '' : $user_info['id'] . '-') . $cachekey, $xml, 240);
  190. }
  191. $feed_title = htmlspecialchars(strip_tags($context['forum_name'])) . (isset($feed_title) ? $feed_title : '');
  192. // This is an xml file....
  193. ob_end_clean();
  194. if (!empty($modSettings['enableCompressedOutput']))
  195. @ob_start('ob_gzhandler');
  196. else
  197. ob_start();
  198. if ($xml_format == 'smf' || isset($_REQUEST['debug']))
  199. header('Content-Type: text/xml; charset=' . (empty($context['character_set']) ? 'ISO-8859-1' : $context['character_set']));
  200. elseif ($xml_format == 'rss' || $xml_format == 'rss2' || $xml_format == 'webslice')
  201. header('Content-Type: application/rss+xml; charset=' . (empty($context['character_set']) ? 'ISO-8859-1' : $context['character_set']));
  202. elseif ($xml_format == 'atom')
  203. header('Content-Type: application/atom+xml; charset=' . (empty($context['character_set']) ? 'ISO-8859-1' : $context['character_set']));
  204. elseif ($xml_format == 'rdf')
  205. header('Content-Type: ' . ($context['browser']['is_ie'] ? 'text/xml' : 'application/rdf+xml') . '; charset=' . (empty($context['character_set']) ? 'ISO-8859-1' : $context['character_set']));
  206. // First, output the xml header.
  207. echo '<?xml version="1.0" encoding="', $context['character_set'], '"?' . '>';
  208. // Are we outputting an rss feed or one with more information?
  209. if ($xml_format == 'rss' || $xml_format == 'rss2')
  210. {
  211. // Start with an RSS 2.0 header.
  212. echo '
  213. <rss version=', $xml_format == 'rss2' ? '"2.0"' : '"0.92"', ' xml:lang="', strtr($txt['lang_locale'], '_', '-'), '">
  214. <channel>
  215. <title>', $feed_title, '</title>
  216. <link>', $scripturl, '</link>
  217. <description><![CDATA[', strip_tags($txt['xml_rss_desc']), ']]></description>';
  218. // Output all of the associative array, start indenting with 2 tabs, and name everything "item".
  219. dumpTags($xml, 2, 'item', $xml_format);
  220. // Output the footer of the xml.
  221. echo '
  222. </channel>
  223. </rss>';
  224. }
  225. elseif ($xml_format == 'webslice')
  226. {
  227. $context['recent_posts_data'] = $xml;
  228. // This always has RSS 2
  229. echo '
  230. <rss version="2.0" xmlns:mon="http://www.microsoft.com/schemas/rss/monitoring/2007" xml:lang="', strtr($txt['lang_locale'], '_', '-'), '">
  231. <channel>
  232. <title>', $feed_title, ' - ', $txt['recent_posts'], '</title>
  233. <link>', $scripturl, '?action=recent</link>
  234. <description><![CDATA[', strip_tags($txt['xml_rss_desc']), ']]></description>
  235. <item>
  236. <title>', $feed_title, ' - ', $txt['recent_posts'], '</title>
  237. <link>', $scripturl, '?action=recent</link>
  238. <description><![CDATA[
  239. ', template_webslice_header_above(), '
  240. ', template_webslice_recent_posts(), '
  241. ', template_webslice_header_below(), '
  242. ]]></description>
  243. </item>
  244. </channel>
  245. </rss>';
  246. }
  247. elseif ($xml_format == 'atom')
  248. {
  249. echo '
  250. <feed xmlns="http://www.w3.org/2005/Atom">
  251. <title>', $feed_title, '</title>
  252. <link rel="alternate" type="text/html" href="', $scripturl, '" />
  253. <modified>', gmstrftime('%Y-%m-%dT%H:%M:%SZ'), '</modified>
  254. <tagline><![CDATA[', strip_tags($txt['xml_rss_desc']), ']]></tagline>
  255. <generator uri="http://www.simplemachines.org" version="', strtr($forum_version, array('SMF' => '')), '">SMF</generator>
  256. <author>
  257. <name>', strip_tags($context['forum_name']), '</name>
  258. </author>';
  259. dumpTags($xml, 2, 'entry', $xml_format);
  260. echo '
  261. </feed>';
  262. }
  263. elseif ($xml_format == 'rdf')
  264. {
  265. echo '
  266. <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns="http://purl.org/rss/1.0/">
  267. <channel rdf:about="', $scripturl, '">
  268. <title>', $feed_title, '</title>
  269. <link>', $scripturl, '</link>
  270. <description><![CDATA[', strip_tags($txt['xml_rss_desc']), ']]></description>
  271. <items>
  272. <rdf:Seq>';
  273. foreach ($xml as $item)
  274. echo '
  275. <rdf:li rdf:resource="', $item['link'], '" />';
  276. echo '
  277. </rdf:Seq>
  278. </items>
  279. </channel>
  280. ';
  281. dumpTags($xml, 1, 'item', $xml_format);
  282. echo '
  283. </rdf:RDF>';
  284. }
  285. // Otherwise, we're using our proprietary formats - they give more data, though.
  286. else
  287. {
  288. echo '
  289. <smf:xml-feed xmlns:smf="http://www.simplemachines.org/" xmlns="http://www.simplemachines.org/xml/', $_GET['sa'], '" xml:lang="', strtr($txt['lang_locale'], '_', '-'), '">';
  290. // Dump out that associative array. Indent properly.... and use the right names for the base elements.
  291. dumpTags($xml, 1, $subActions[$_GET['sa']][1], $xml_format);
  292. echo '
  293. </smf:xml-feed>';
  294. }
  295. obExit(false);
  296. }
  297. function fix_possible_url($val)
  298. {
  299. global $modSettings, $context, $scripturl;
  300. if (substr($val, 0, strlen($scripturl)) != $scripturl)
  301. return $val;
  302. call_integration_hook('integrate_fix_url', array(&$val));
  303. if (empty($modSettings['queryless_urls']) || ($context['server']['is_cgi'] && @ini_get('cgi.fix_pathinfo') == 0 && @get_cfg_var('cgi.fix_pathinfo') == 0) || (!$context['server']['is_apache'] && !$context['server']['is_lighttpd']))
  304. return $val;
  305. $val = preg_replace('/^' . preg_quote($scripturl, '/') . '\?((?:board|topic)=[^#"]+)(#[^"]*)?$/e', '\'\' . $scripturl . \'/\' . strtr(\'$1\', \'&;=\', \'//,\') . \'.html$2\'', $val);
  306. return $val;
  307. }
  308. function cdata_parse($data, $ns = '')
  309. {
  310. global $smcFunc, $cdata_override;
  311. // Are we not doing it?
  312. if (!empty($cdata_override))
  313. return $data;
  314. $cdata = '<![CDATA[';
  315. for ($pos = 0, $n = $smcFunc['strlen']($data); $pos < $n; null)
  316. {
  317. $positions = array(
  318. $smcFunc['strpos']($data, '&', $pos),
  319. $smcFunc['strpos']($data, ']', $pos),
  320. );
  321. if ($ns != '')
  322. $positions[] = $smcFunc['strpos']($data, '<', $pos);
  323. foreach ($positions as $k => $dummy)
  324. {
  325. if ($dummy === false)
  326. unset($positions[$k]);
  327. }
  328. $old = $pos;
  329. $pos = empty($positions) ? $n : min($positions);
  330. if ($pos - $old > 0)
  331. $cdata .= $smcFunc['substr']($data, $old, $pos - $old);
  332. if ($pos >= $n)
  333. break;
  334. if ($smcFunc['substr']($data, $pos, 1) == '<')
  335. {
  336. $pos2 = $smcFunc['strpos']($data, '>', $pos);
  337. if ($pos2 === false)
  338. $pos2 = $n;
  339. if ($smcFunc['substr']($data, $pos + 1, 1) == '/')
  340. $cdata .= ']]></' . $ns . ':' . $smcFunc['substr']($data, $pos + 2, $pos2 - $pos - 1) . '<![CDATA[';
  341. else
  342. $cdata .= ']]><' . $ns . ':' . $smcFunc['substr']($data, $pos + 1, $pos2 - $pos) . '<![CDATA[';
  343. $pos = $pos2 + 1;
  344. }
  345. elseif ($smcFunc['substr']($data, $pos, 1) == ']')
  346. {
  347. $cdata .= ']]>&#093;<![CDATA[';
  348. $pos++;
  349. }
  350. elseif ($smcFunc['substr']($data, $pos, 1) == '&')
  351. {
  352. $pos2 = $smcFunc['strpos']($data, ';', $pos);
  353. if ($pos2 === false)
  354. $pos2 = $n;
  355. $ent = $smcFunc['substr']($data, $pos + 1, $pos2 - $pos - 1);
  356. if ($smcFunc['substr']($data, $pos + 1, 1) == '#')
  357. $cdata .= ']]>' . $smcFunc['substr']($data, $pos, $pos2 - $pos + 1) . '<![CDATA[';
  358. elseif (in_array($ent, array('amp', 'lt', 'gt', 'quot')))
  359. $cdata .= ']]>' . $smcFunc['substr']($data, $pos, $pos2 - $pos + 1) . '<![CDATA[';
  360. $pos = $pos2 + 1;
  361. }
  362. }
  363. $cdata .= ']]>';
  364. return strtr($cdata, array('<![CDATA[]]>' => ''));
  365. }
  366. /**
  367. * Formats data retrieved in other functions into xml format.
  368. * Additionally formats data based on the specific format passed.
  369. * This function is recursively called to handle sub arrays of data.
  370. * @param array $data, the array to output as xml data
  371. * @param int $i, the amount of indentation to use.
  372. * @param string $tag, if specified, it will be used instead of the keys of data.
  373. * @param string $xml_format
  374. */
  375. function dumpTags($data, $i, $tag = null, $xml_format = '')
  376. {
  377. global $modSettings, $context, $scripturl;
  378. // For every array in the data...
  379. foreach ($data as $key => $val)
  380. {
  381. // Skip it, it's been set to null.
  382. if ($val === null)
  383. continue;
  384. // If a tag was passed, use it instead of the key.
  385. $key = isset($tag) ? $tag : $key;
  386. // First let's indent!
  387. echo "\n", str_repeat("\t", $i);
  388. // Grr, I hate kludges... almost worth doing it properly, here, but not quite.
  389. if ($xml_format == 'atom' && $key == 'link')
  390. {
  391. echo '<link rel="alternate" type="text/html" href="', fix_possible_url($val), '" />';
  392. continue;
  393. }
  394. // If it's empty/0/nothing simply output an empty tag.
  395. if ($val == '')
  396. echo '<', $key, ' />';
  397. else
  398. {
  399. // Beginning tag.
  400. if ($xml_format == 'rdf' && $key == 'item' && isset($val['link']))
  401. {
  402. echo '<', $key, ' rdf:about="', fix_possible_url($val['link']), '">';
  403. echo "\n", str_repeat("\t", $i + 1);
  404. echo '<dc:format>text/html</dc:format>';
  405. }
  406. elseif ($xml_format == 'atom' && $key == 'summary')
  407. echo '<', $key, ' type="html">';
  408. else
  409. echo '<', $key, '>';
  410. if (is_array($val))
  411. {
  412. // An array. Dump it, and then indent the tag.
  413. dumpTags($val, $i + 1, null, $xml_format);
  414. echo "\n", str_repeat("\t", $i), '</', $key, '>';
  415. }
  416. // A string with returns in it.... show this as a multiline element.
  417. elseif (strpos($val, "\n") !== false || strpos($val, '<br />') !== false)
  418. echo "\n", fix_possible_url($val), "\n", str_repeat("\t", $i), '</', $key, '>';
  419. // A simple string.
  420. else
  421. echo fix_possible_url($val), '</', $key, '>';
  422. }
  423. }
  424. }
  425. /**
  426. * Retrieve the list of members from database.
  427. * The array will be generated to match the format.
  428. * @todo get the list of members from Subs-Members.
  429. *
  430. * @param string $xml_format
  431. * @return array
  432. */
  433. function getXmlMembers($xml_format)
  434. {
  435. global $scripturl, $smcFunc;
  436. if (!allowedTo('view_mlist'))
  437. return array();
  438. // Find the most recent members.
  439. $request = $smcFunc['db_query']('', '
  440. SELECT id_member, member_name, real_name, date_registered, last_login
  441. FROM {db_prefix}members
  442. ORDER BY id_member DESC
  443. LIMIT {int:limit}',
  444. array(
  445. 'limit' => $_GET['limit'],
  446. )
  447. );
  448. $data = array();
  449. while ($row = $smcFunc['db_fetch_assoc']($request))
  450. {
  451. // Make the data look rss-ish.
  452. if ($xml_format == 'rss' || $xml_format == 'rss2')
  453. $data[] = array(
  454. 'title' => cdata_parse($row['real_name']),
  455. 'link' => $scripturl . '?action=profile;u=' . $row['id_member'],
  456. 'comments' => $scripturl . '?action=pm;sa=send;u=' . $row['id_member'],
  457. 'pubDate' => gmdate('D, d M Y H:i:s \G\M\T', $row['date_registered']),
  458. 'guid' => $scripturl . '?action=profile;u=' . $row['id_member'],
  459. );
  460. elseif ($xml_format == 'rdf')
  461. $data[] = array(
  462. 'title' => cdata_parse($row['real_name']),
  463. 'link' => $scripturl . '?action=profile;u=' . $row['id_member'],
  464. );
  465. elseif ($xml_format == 'atom')
  466. $data[] = array(
  467. 'title' => cdata_parse($row['real_name']),
  468. 'link' => $scripturl . '?action=profile;u=' . $row['id_member'],
  469. 'published' => gmstrftime('%Y-%m-%dT%H:%M:%SZ', $row['date_registered']),
  470. 'updated' => gmstrftime('%Y-%m-%dT%H:%M:%SZ', $row['last_login']),
  471. 'id' => $scripturl . '?action=profile;u=' . $row['id_member'],
  472. );
  473. // More logical format for the data, but harder to apply.
  474. else
  475. $data[] = array(
  476. 'name' => cdata_parse($row['real_name']),
  477. 'time' => htmlspecialchars(strip_tags(timeformat($row['date_registered']))),
  478. 'id' => $row['id_member'],
  479. 'link' => $scripturl . '?action=profile;u=' . $row['id_member']
  480. );
  481. }
  482. $smcFunc['db_free_result']($request);
  483. return $data;
  484. }
  485. /**
  486. * Get the latest topics information from a specific board,
  487. * to display later.
  488. * The returned array will be generated to match the xmf_format.
  489. * @todo does not belong here
  490. *
  491. * @param $xml_format
  492. * @return array, array of topics
  493. */
  494. function getXmlNews($xml_format)
  495. {
  496. global $user_info, $scripturl, $modSettings, $board;
  497. global $query_this_board, $smcFunc, $settings, $context;
  498. /* Find the latest posts that:
  499. - are the first post in their topic.
  500. - are on an any board OR in a specified board.
  501. - can be seen by this user.
  502. - are actually the latest posts. */
  503. $done = false;
  504. $loops = 0;
  505. while (!$done)
  506. {
  507. $optimize_msg = implode(' AND ', $context['optimize_msg']);
  508. $request = $smcFunc['db_query']('', '
  509. SELECT
  510. m.smileys_enabled, m.poster_time, m.id_msg, m.subject, m.body, m.modified_time,
  511. m.icon, t.id_topic, t.id_board, t.num_replies,
  512. b.name AS bname,
  513. mem.hide_email, IFNULL(mem.id_member, 0) AS id_member,
  514. IFNULL(mem.email_address, m.poster_email) AS poster_email,
  515. IFNULL(mem.real_name, m.poster_name) AS poster_name
  516. FROM {db_prefix}topics AS t
  517. INNER JOIN {db_prefix}messages AS m ON (m.id_msg = t.id_first_msg)
  518. INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board)
  519. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member)
  520. WHERE ' . $query_this_board . (empty($optimize_msg) ? '' : '
  521. AND {raw:optimize_msg}') . (empty($board) ? '' : '
  522. AND t.id_board = {int:current_board}') . ($modSettings['postmod_active'] ? '
  523. AND t.approved = {int:is_approved}' : '') . '
  524. ORDER BY t.id_first_msg DESC
  525. LIMIT {int:limit}',
  526. array(
  527. 'current_board' => $board,
  528. 'is_approved' => 1,
  529. 'limit' => $_GET['limit'],
  530. 'optimize_msg' => $optimize_msg,
  531. )
  532. );
  533. // If we don't have $_GET['limit'] results, try again with an unoptimized version covering all rows.
  534. if ($loops < 2 && $smcFunc['db_num_rows']($request) < $_GET['limit'])
  535. {
  536. $smcFunc['db_free_result']($request);
  537. if (empty($_REQUEST['boards']) && empty($board))
  538. unset($context['optimize_msg']['lowest']);
  539. else
  540. $context['optimize_msg']['lowest'] = 'm.id_msg >= t.id_first_msg';
  541. $context['optimize_msg']['highest'] = 'm.id_msg <= t.id_last_msg';
  542. $loops++;
  543. }
  544. else
  545. $done = true;
  546. }
  547. $data = array();
  548. while ($row = $smcFunc['db_fetch_assoc']($request))
  549. {
  550. // Limit the length of the message, if the option is set.
  551. if (!empty($modSettings['xmlnews_maxlen']) && $smcFunc['strlen'](str_replace('<br />', "\n", $row['body'])) > $modSettings['xmlnews_maxlen'])
  552. $row['body'] = strtr($smcFunc['substr'](str_replace('<br />', "\n", $row['body']), 0, $modSettings['xmlnews_maxlen'] - 3), array("\n" => '<br />')) . '...';
  553. $row['body'] = parse_bbc($row['body'], $row['smileys_enabled'], $row['id_msg']);
  554. censorText($row['body']);
  555. censorText($row['subject']);
  556. // Being news, this actually makes sense in rss format.
  557. if ($xml_format == 'rss' || $xml_format == 'rss2')
  558. $data[] = array(
  559. 'title' => cdata_parse($row['subject']),
  560. 'link' => $scripturl . '?topic=' . $row['id_topic'] . '.0',
  561. 'description' => cdata_parse($row['body']),
  562. 'author' => in_array(showEmailAddress(!empty($row['hide_email']), $row['id_member']), array('yes', 'yes_permission_override')) ? $row['poster_email'] : null,
  563. 'comments' => $scripturl . '?action=post;topic=' . $row['id_topic'] . '.0',
  564. 'category' => '<![CDATA[' . $row['bname'] . ']]>',
  565. 'pubDate' => gmdate('D, d M Y H:i:s \G\M\T', $row['poster_time']),
  566. 'guid' => $scripturl . '?topic=' . $row['id_topic'] . '.0',
  567. );
  568. elseif ($xml_format == 'rdf')
  569. $data[] = array(
  570. 'title' => cdata_parse($row['subject']),
  571. 'link' => $scripturl . '?topic=' . $row['id_topic'] . '.0',
  572. 'description' => cdata_parse($row['body']),
  573. );
  574. elseif ($xml_format == 'atom')
  575. $data[] = array(
  576. 'title' => cdata_parse($row['subject']),
  577. 'link' => $scripturl . '?topic=' . $row['id_topic'] . '.0',
  578. 'summary' => cdata_parse($row['body']),
  579. 'category' => array('term' => $row['id_board'], 'label' => cdata_parse($row['bname'])),
  580. 'author' => array(
  581. 'name' => $row['poster_name'],
  582. 'email' => in_array(showEmailAddress(!empty($row['hide_email']), $row['id_member']), array('yes', 'yes_permission_override')) ? $row['poster_email'] : null,
  583. 'uri' => !empty($row['id_member']) ? $scripturl . '?action=profile;u=' . $row['id_member'] : '',
  584. ),
  585. 'published' => gmstrftime('%Y-%m-%dT%H:%M:%SZ', $row['poster_time']),
  586. 'modified' => gmstrftime('%Y-%m-%dT%H:%M:%SZ', empty($row['modified_time']) ? $row['poster_time'] : $row['modified_time']),
  587. 'id' => $scripturl . '?topic=' . $row['id_topic'] . '.0',
  588. 'icon' => $settings['images_url'] . '/icons/' . $row['icon'] . '.gif',
  589. );
  590. // The biggest difference here is more information.
  591. else
  592. $data[] = array(
  593. 'time' => htmlspecialchars(strip_tags(timeformat($row['poster_time']))),
  594. 'id' => $row['id_topic'],
  595. 'subject' => cdata_parse($row['subject']),
  596. 'body' => cdata_parse($row['body']),
  597. 'poster' => array(
  598. 'name' => cdata_parse($row['poster_name']),
  599. 'id' => $row['id_member'],
  600. 'link' => !empty($row['id_member']) ? $scripturl . '?action=profile;u=' . $row['id_member'] : '',
  601. ),
  602. 'topic' => $row['id_topic'],
  603. 'board' => array(
  604. 'name' => cdata_parse($row['bname']),
  605. 'id' => $row['id_board'],
  606. 'link' => $scripturl . '?board=' . $row['id_board'] . '.0',
  607. ),
  608. 'link' => $scripturl . '?topic=' . $row['id_topic'] . '.0',
  609. );
  610. }
  611. $smcFunc['db_free_result']($request);
  612. return $data;
  613. }
  614. /**
  615. * Get the recent topics to display.
  616. * The returned array will be generated to match the xml_format.
  617. * @todo does not belong here.
  618. *
  619. * @param $xml_format
  620. * @return array, of recent posts
  621. */
  622. function getXmlRecent($xml_format)
  623. {
  624. global $user_info, $scripturl, $modSettings, $board;
  625. global $query_this_board, $smcFunc, $settings, $context;
  626. $done = false;
  627. $loops = 0;
  628. while (!$done)
  629. {
  630. $optimize_msg = implode(' AND ', $context['optimize_msg']);
  631. $request = $smcFunc['db_query']('', '
  632. SELECT m.id_msg
  633. FROM {db_prefix}messages AS m
  634. INNER JOIN {db_prefix}boards AS b ON (b.id_board = m.id_board)
  635. INNER JOIN {db_prefix}topics AS t ON (t.id_topic = m.id_topic)
  636. WHERE ' . $query_this_board . (empty($optimize_msg) ? '' : '
  637. AND {raw:optimize_msg}') . (empty($board) ? '' : '
  638. AND m.id_board = {int:current_board}') . ($modSettings['postmod_active'] ? '
  639. AND m.approved = {int:is_approved}' : '') . '
  640. ORDER BY m.id_msg DESC
  641. LIMIT {int:limit}',
  642. array(
  643. 'limit' => $_GET['limit'],
  644. 'current_board' => $board,
  645. 'is_approved' => 1,
  646. 'optimize_msg' => $optimize_msg,
  647. )
  648. );
  649. // If we don't have $_GET['limit'] results, try again with an unoptimized version covering all rows.
  650. if ($loops < 2 && $smcFunc['db_num_rows']($request) < $_GET['limit'])
  651. {
  652. $smcFunc['db_free_result']($request);
  653. if (empty($_REQUEST['boards']) && empty($board))
  654. unset($context['optimize_msg']['lowest']);
  655. else
  656. $context['optimize_msg']['lowest'] = $loops ? 'm.id_msg >= t.id_first_msg' : 'm.id_msg >= (t.id_last_msg - t.id_first_msg) / 2';
  657. $loops++;
  658. }
  659. else
  660. $done = true;
  661. }
  662. $messages = array();
  663. while ($row = $smcFunc['db_fetch_assoc']($request))
  664. $messages[] = $row['id_msg'];
  665. $smcFunc['db_free_result']($request);
  666. if (empty($messages))
  667. return array();
  668. // Find the most recent posts this user can see.
  669. $request = $smcFunc['db_query']('', '
  670. SELECT
  671. m.smileys_enabled, m.poster_time, m.id_msg, m.subject, m.body, m.id_topic, t.id_board,
  672. b.name AS bname, t.num_replies, m.id_member, m.icon, mf.id_member AS id_first_member,
  673. IFNULL(mem.real_name, m.poster_name) AS poster_name, mf.subject AS first_subject,
  674. IFNULL(memf.real_name, mf.poster_name) AS first_poster_name, mem.hide_email,
  675. IFNULL(mem.email_address, m.poster_email) AS poster_email, m.modified_time
  676. FROM {db_prefix}messages AS m
  677. INNER JOIN {db_prefix}topics AS t ON (t.id_topic = m.id_topic)
  678. INNER JOIN {db_prefix}messages AS mf ON (mf.id_msg = t.id_first_msg)
  679. INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board)
  680. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member)
  681. LEFT JOIN {db_prefix}members AS memf ON (memf.id_member = mf.id_member)
  682. WHERE m.id_msg IN ({array_int:message_list})
  683. ' . (empty($board) ? '' : 'AND t.id_board = {int:current_board}') . '
  684. ORDER BY m.id_msg DESC
  685. LIMIT {int:limit}',
  686. array(
  687. 'limit' => $_GET['limit'],
  688. 'current_board' => $board,
  689. 'message_list' => $messages,
  690. )
  691. );
  692. $data = array();
  693. while ($row = $smcFunc['db_fetch_assoc']($request))
  694. {
  695. // Limit the length of the message, if the option is set.
  696. if (!empty($modSettings['xmlnews_maxlen']) && $smcFunc['strlen'](str_replace('<br />', "\n", $row['body'])) > $modSettings['xmlnews_maxlen'])
  697. $row['body'] = strtr($smcFunc['substr'](str_replace('<br />', "\n", $row['body']), 0, $modSettings['xmlnews_maxlen'] - 3), array("\n" => '<br />')) . '...';
  698. $row['body'] = parse_bbc($row['body'], $row['smileys_enabled'], $row['id_msg']);
  699. censorText($row['body']);
  700. censorText($row['subject']);
  701. // Doesn't work as well as news, but it kinda does..
  702. if ($xml_format == 'rss' || $xml_format == 'rss2')
  703. $data[] = array(
  704. 'title' => $row['subject'],
  705. 'link' => $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . '#msg' . $row['id_msg'],
  706. 'description' => cdata_parse($row['body']),
  707. 'author' => in_array(showEmailAddress(!empty($row['hide_email']), $row['id_member']), array('yes', 'yes_permission_override')) ? $row['poster_email'] : null,
  708. 'category' => cdata_parse($row['bname']),
  709. 'comments' => $scripturl . '?action=post;topic=' . $row['id_topic'] . '.0',
  710. 'pubDate' => gmdate('D, d M Y H:i:s \G\M\T', $row['poster_time']),
  711. 'guid' => $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . '#msg' . $row['id_msg']
  712. );
  713. elseif ($xml_format == 'rdf')
  714. $data[] = array(
  715. 'title' => $row['subject'],
  716. 'link' => $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . '#msg' . $row['id_msg'],
  717. 'description' => cdata_parse($row['body']),
  718. );
  719. elseif ($xml_format == 'atom')
  720. $data[] = array(
  721. 'title' => $row['subject'],
  722. 'link' => $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . '#msg' . $row['id_msg'],
  723. 'summary' => cdata_parse($row['body']),
  724. 'category' => array(
  725. 'term' => $row['id_board'],
  726. 'label' => cdata_parse($row['bname'])
  727. ),
  728. 'author' => array(
  729. 'name' => $row['poster_name'],
  730. 'email' => in_array(showEmailAddress(!empty($row['hide_email']), $row['id_member']), array('yes', 'yes_permission_override')) ? $row['poster_email'] : null,
  731. 'uri' => !empty($row['id_member']) ? $scripturl . '?action=profile;u=' . $row['id_member'] : ''
  732. ),
  733. 'published' => gmstrftime('%Y-%m-%dT%H:%M:%SZ', $row['poster_time']),
  734. 'updated' => gmstrftime('%Y-%m-%dT%H:%M:%SZ', empty($row['modified_time']) ? $row['poster_time'] : $row['modified_time']),
  735. 'id' => $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . '#msg' . $row['id_msg'],
  736. 'icon' => $settings['images_url'] . '/icons/' . $row['icon'] . '.gif',
  737. );
  738. // A lot of information here. Should be enough to please the rss-ers.
  739. else
  740. $data[] = array(
  741. 'time' => htmlspecialchars(strip_tags(timeformat($row['poster_time']))),
  742. 'id' => $row['id_msg'],
  743. 'subject' => cdata_parse($row['subject']),
  744. 'body' => cdata_parse($row['body']),
  745. 'starter' => array(
  746. 'name' => cdata_parse($row['first_poster_name']),
  747. 'id' => $row['id_first_member'],
  748. 'link' => !empty($row['id_first_member']) ? $scripturl . '?action=profile;u=' . $row['id_first_member'] : ''
  749. ),
  750. 'poster' => array(
  751. 'name' => cdata_parse($row['poster_name']),
  752. 'id' => $row['id_member'],
  753. 'link' => !empty($row['id_member']) ? $scripturl . '?action=profile;u=' . $row['id_member'] : ''
  754. ),
  755. 'topic' => array(
  756. 'subject' => cdata_parse($row['first_subject']),
  757. 'id' => $row['id_topic'],
  758. 'link' => $scripturl . '?topic=' . $row['id_topic'] . '.new#new'
  759. ),
  760. 'board' => array(
  761. 'name' => cdata_parse($row['bname']),
  762. 'id' => $row['id_board'],
  763. 'link' => $scripturl . '?board=' . $row['id_board'] . '.0'
  764. ),
  765. 'link' => $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . '#msg' . $row['id_msg']
  766. );
  767. }
  768. $smcFunc['db_free_result']($request);
  769. return $data;
  770. }
  771. /**
  772. * Get the profile information for member into an array,
  773. * which will be generated to match the xml_format.
  774. * @todo refactor.
  775. *
  776. * @param $xml_format
  777. * @return array, of profile data.
  778. */
  779. function getXmlProfile($xml_format)
  780. {
  781. global $scripturl, $memberContext, $user_profile, $modSettings, $user_info;
  782. // You must input a valid user....
  783. if (empty($_GET['u']) || loadMemberData((int) $_GET['u']) === false)
  784. return array();
  785. // Make sure the id is a number and not "I like trying to hack the database".
  786. $_GET['u'] = (int) $_GET['u'];
  787. // Load the member's contextual information!
  788. if (!loadMemberContext($_GET['u']) || !allowedTo('profile_view_any'))
  789. return array();
  790. // Okay, I admit it, I'm lazy. Stupid $_GET['u'] is long and hard to type.
  791. $profile = &$memberContext[$_GET['u']];
  792. if ($xml_format == 'rss' || $xml_format == 'rss2')
  793. $data = array(array(
  794. 'title' => cdata_parse($profile['name']),
  795. 'link' => $scripturl . '?action=profile;u=' . $profile['id'],
  796. 'description' => cdata_parse(isset($profile['group']) ? $profile['group'] : $profile['post_group']),
  797. 'comments' => $scripturl . '?action=pm;sa=send;u=' . $profile['id'],
  798. 'pubDate' => gmdate('D, d M Y H:i:s \G\M\T', $user_profile[$profile['id']]['date_registered']),
  799. 'guid' => $scripturl . '?action=profile;u=' . $profile['id'],
  800. ));
  801. elseif ($xml_format == 'rdf')
  802. $data = array(array(
  803. 'title' => cdata_parse($profile['name']),
  804. 'link' => $scripturl . '?action=profile;u=' . $profile['id'],
  805. 'description' => cdata_parse(isset($profile['group']) ? $profile['group'] : $profile['post_group']),
  806. ));
  807. elseif ($xml_format == 'atom')
  808. $data[] = array(
  809. 'title' => cdata_parse($profile['name']),
  810. 'link' => $scripturl . '?action=profile;u=' . $profile['id'],
  811. 'summary' => cdata_parse(isset($profile['group']) ? $profile['group'] : $profile['post_group']),
  812. 'author' => array(
  813. 'name' => $profile['real_name'],
  814. 'email' => in_array(showEmailAddress(!empty($profile['hide_email']), $profile['id']), array('yes', 'yes_permission_override')) ? $profile['email'] : null,
  815. 'uri' => !empty($profile['website']) ? $profile['website']['url'] : ''
  816. ),
  817. 'published' => gmstrftime('%Y-%m-%dT%H:%M:%SZ', $user_profile[$profile['id']]['date_registered']),
  818. 'updated' => gmstrftime('%Y-%m-%dT%H:%M:%SZ', $user_profile[$profile['id']]['last_login']),
  819. 'id' => $scripturl . '?action=profile;u=' . $profile['id'],
  820. 'logo' => !empty($profile['avatar']) ? $profile['avatar']['url'] : '',
  821. );
  822. else
  823. {
  824. $data = array(
  825. 'username' => $user_info['is_admin'] || $user_info['id'] == $profile['id'] ? cdata_parse($profile['username']) : '',
  826. 'name' => cdata_parse($profile['name']),
  827. 'link' => $scripturl . '?action=profile;u=' . $profile['id'],
  828. 'posts' => $profile['posts'],
  829. 'post-group' => cdata_parse($profile['post_group']),
  830. 'language' => cdata_parse($profile['language']),
  831. 'last-login' => gmdate('D, d M Y H:i:s \G\M\T', $user_profile[$profile['id']]['last_login']),
  832. 'registered' => gmdate('D, d M Y H:i:s \G\M\T', $user_profile[$profile['id']]['date_registered'])
  833. );
  834. // Everything below here might not be set, and thus maybe shouldn't be displayed.
  835. if ($profile['gender']['name'] != '')
  836. $data['gender'] = cdata_parse($profile['gender']['name']);
  837. if ($profile['avatar']['name'] != '')
  838. $data['avatar'] = $profile['avatar']['url'];
  839. // If they are online, show an empty tag... no reason to put anything inside it.
  840. if ($profile['online']['is_online'])
  841. $data['online'] = '';
  842. if ($profile['signature'] != '')
  843. $data['signature'] = cdata_parse($profile['signature']);
  844. if ($profile['blurb'] != '')
  845. $data['blurb'] = cdata_parse($profile['blurb']);
  846. if ($profile['location'] != '')
  847. $data['location'] = cdata_parse($profile['location']);
  848. if ($profile['title'] != '')
  849. $data['title'] = cdata_parse($profile['title']);
  850. if (!empty($profile['icq']['name']) && !(!empty($modSettings['guest_hideContacts']) && $user_info['is_guest']))
  851. $data['icq'] = $profile['icq']['name'];
  852. if ($profile['aim']['name'] != '' && !(!empty($modSettings['guest_hideContacts']) && $user_info['is_guest']))
  853. $data['aim'] = $profile['aim']['name'];
  854. if ($profile['msn']['name'] != '' && !(!empty($modSettings['guest_hideContacts']) && $user_info['is_guest']))
  855. $data['msn'] = $profile['msn']['name'];
  856. if ($profile['yim']['name'] != '' && !(!empty($modSettings['guest_hideContacts']) && $user_info['is_guest']))
  857. $data['yim'] = $profile['yim']['name'];
  858. if ($profile['website']['title'] != '')
  859. $data['website'] = array(
  860. 'title' => cdata_parse($profile['website']['title']),
  861. 'link' => $profile['website']['url']
  862. );
  863. if ($profile['group'] != '')
  864. $data['position'] = cdata_parse($profile['group']);
  865. if (!empty($modSettings['karmaMode']))
  866. $data['karma'] = array(
  867. 'good' => $profile['karma']['good'],
  868. 'bad' => $profile['karma']['bad']
  869. );
  870. if (in_array($profile['show_email'], array('yes', 'yes_permission_override')))
  871. $data['email'] = $profile['email'];
  872. if (!empty($profile['birth_date']) && substr($profile['birth_date'], 0, 4) != '0000')
  873. {
  874. list ($birth_year, $birth_month, $birth_day) = sscanf($profile['birth_date'], '%d-%d-%d');
  875. $datearray = getdate(forum_time());
  876. $data['age'] = $datearray['year'] - $birth_year - (($datearray['mon'] > $birth_month || ($datearray['mon'] == $birth_month && $datearray['mday'] >= $birth_day)) ? 0 : 1);
  877. }
  878. }
  879. // Save some memory.
  880. unset($profile, $memberContext[$_GET['u']]);
  881. return $data;
  882. }
  883. ?>