News.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962
  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 2014 Simple Machines and individual contributors
  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('No direct access...');
  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, $boardurl, $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')) ? $_GET['type'] : 'smf';
  150. // @todo Birthdays?
  151. // List all the different types of data they can pull.
  152. $subActions = array(
  153. 'recent' => array('getXmlRecent', 'recent-post'),
  154. 'news' => array('getXmlNews', 'article'),
  155. 'members' => array('getXmlMembers', 'member'),
  156. 'profile' => array('getXmlProfile', null),
  157. );
  158. // Easy adding of sub actions
  159. call_integration_hook('integrate_xmlfeeds', array(&$subActions));
  160. if (empty($_GET['sa']) || !isset($subActions[$_GET['sa']]))
  161. $_GET['sa'] = 'recent';
  162. // We only want some information, not all of it.
  163. $cachekey = array($xml_format, $_GET['action'], $_GET['limit'], $_GET['sa']);
  164. foreach (array('board', 'boards', 'c') as $var)
  165. if (isset($_REQUEST[$var]))
  166. $cachekey[] = $_REQUEST[$var];
  167. $cachekey = md5(serialize($cachekey) . (!empty($query_this_board) ? $query_this_board : ''));
  168. $cache_t = microtime();
  169. // Get the associative array representing the xml.
  170. if (!empty($modSettings['cache_enable']) && (!$user_info['is_guest'] || $modSettings['cache_enable'] >= 3))
  171. $xml = cache_get_data('xmlfeed-' . $xml_format . ':' . ($user_info['is_guest'] ? '' : $user_info['id'] . '-') . $cachekey, 240);
  172. if (empty($xml))
  173. {
  174. $xml = $subActions[$_GET['sa']][0]($xml_format);
  175. if (!empty($modSettings['cache_enable']) && (($user_info['is_guest'] && $modSettings['cache_enable'] >= 3)
  176. || (!$user_info['is_guest'] && (array_sum(explode(' ', microtime())) - array_sum(explode(' ', $cache_t)) > 0.2))))
  177. cache_put_data('xmlfeed-' . $xml_format . ':' . ($user_info['is_guest'] ? '' : $user_info['id'] . '-') . $cachekey, $xml, 240);
  178. }
  179. $feed_title = $smcFunc['htmlspecialchars'](strip_tags($context['forum_name'])) . (isset($feed_title) ? $feed_title : '');
  180. // This is an xml file....
  181. ob_end_clean();
  182. if (!empty($modSettings['enableCompressedOutput']))
  183. @ob_start('ob_gzhandler');
  184. else
  185. ob_start();
  186. if ($xml_format == 'smf' || isset($_REQUEST['debug']))
  187. header('Content-Type: text/xml; charset=' . (empty($context['character_set']) ? 'ISO-8859-1' : $context['character_set']));
  188. elseif ($xml_format == 'rss' || $xml_format == 'rss2')
  189. header('Content-Type: application/rss+xml; charset=' . (empty($context['character_set']) ? 'ISO-8859-1' : $context['character_set']));
  190. elseif ($xml_format == 'atom')
  191. header('Content-Type: application/atom+xml; charset=' . (empty($context['character_set']) ? 'ISO-8859-1' : $context['character_set']));
  192. elseif ($xml_format == 'rdf')
  193. header('Content-Type: ' . (isBrowser('ie') ? 'text/xml' : 'application/rdf+xml') . '; charset=' . (empty($context['character_set']) ? 'ISO-8859-1' : $context['character_set']));
  194. // First, output the xml header.
  195. echo '<?xml version="1.0" encoding="', $context['character_set'], '"?' . '>';
  196. // Are we outputting an rss feed or one with more information?
  197. if ($xml_format == 'rss' || $xml_format == 'rss2')
  198. {
  199. // Start with an RSS 2.0 header.
  200. echo '
  201. <rss version=', $xml_format == 'rss2' ? '"2.0"' : '"0.92"', ' xml:lang="', strtr($txt['lang_locale'], '_', '-'), '">
  202. <channel>
  203. <title>', $feed_title, '</title>
  204. <link>', $scripturl, '</link>
  205. <description><![CDATA[', strip_tags($txt['xml_rss_desc']), ']]></description>';
  206. // Output all of the associative array, start indenting with 2 tabs, and name everything "item".
  207. dumpTags($xml, 2, 'item', $xml_format);
  208. // Output the footer of the xml.
  209. echo '
  210. </channel>
  211. </rss>';
  212. }
  213. elseif ($xml_format == 'atom')
  214. {
  215. foreach (array('board', 'boards', 'c') as $var)
  216. if (isset($_REQUEST[$var]))
  217. $url_parts[] = $var . '=' . (is_array($_REQUEST[$var]) ? implode(',', $_REQUEST[$var]) : $_REQUEST[$var]);
  218. echo '
  219. <feed xmlns="http://www.w3.org/2005/Atom">
  220. <title>', $feed_title, '</title>
  221. <link rel="alternate" type="text/html" href="', $scripturl, '" />
  222. <link rel="self" type="application/rss+xml" href="', $scripturl, '?type=atom;action=.xml', !empty($url_parts) ? ';' . implode(';', $url_parts) : '', '" />
  223. <id>', $scripturl, '</id>
  224. <icon>', $boardurl, '/favicon.ico</icon>
  225. <updated>', gmstrftime('%Y-%m-%dT%H:%M:%SZ'), '</updated>
  226. <subtitle><![CDATA[', strip_tags($txt['xml_rss_desc']), ']]></subtitle>
  227. <generator uri="http://www.simplemachines.org" version="', strtr($forum_version, array('SMF' => '')), '">SMF</generator>
  228. <author>
  229. <name>', strip_tags($context['forum_name']), '</name>
  230. </author>';
  231. dumpTags($xml, 2, 'entry', $xml_format);
  232. echo '
  233. </feed>';
  234. }
  235. elseif ($xml_format == 'rdf')
  236. {
  237. echo '
  238. <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/">
  239. <channel rdf:about="', $scripturl, '">
  240. <title>', $feed_title, '</title>
  241. <link>', $scripturl, '</link>
  242. <description><![CDATA[', strip_tags($txt['xml_rss_desc']), ']]></description>
  243. <items>
  244. <rdf:Seq>';
  245. foreach ($xml as $item)
  246. echo '
  247. <rdf:li rdf:resource="', $item['link'], '" />';
  248. echo '
  249. </rdf:Seq>
  250. </items>
  251. </channel>
  252. ';
  253. dumpTags($xml, 1, 'item', $xml_format);
  254. echo '
  255. </rdf:RDF>';
  256. }
  257. // Otherwise, we're using our proprietary formats - they give more data, though.
  258. else
  259. {
  260. echo '
  261. <smf:xml-feed xmlns:smf="http://www.simplemachines.org/" xmlns="http://www.simplemachines.org/xml/', $_GET['sa'], '" xml:lang="', strtr($txt['lang_locale'], '_', '-'), '">';
  262. // Dump out that associative array. Indent properly.... and use the right names for the base elements.
  263. dumpTags($xml, 1, $subActions[$_GET['sa']][1], $xml_format);
  264. echo '
  265. </smf:xml-feed>';
  266. }
  267. obExit(false);
  268. }
  269. /**
  270. * Called from dumpTags to convert data to xml
  271. * Finds urls for local sitte and santizes them
  272. *
  273. * @param type $val
  274. * @return type
  275. */
  276. function fix_possible_url($val)
  277. {
  278. global $modSettings, $context, $scripturl;
  279. if (substr($val, 0, strlen($scripturl)) != $scripturl)
  280. return $val;
  281. call_integration_hook('integrate_fix_url', array(&$val));
  282. 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']))
  283. return $val;
  284. $val = preg_replace_callback('~^' . preg_quote($scripturl, '/') . '\?((?:board|topic)=[^#"]+)(#[^"]*)?$~', create_function('$m', 'global $scripturl; return $scripturl . \'/\' . strtr("$m[1]", \'&;=\', \'//,\') . \'.html\' . (isset($m[2]) ? $m[2] : "");'), $val);
  285. return $val;
  286. }
  287. /**
  288. * Ensures supplied data is properly encpsulated in cdata xml tags
  289. * Called from getXmlProfile in News.php
  290. *
  291. * @param type $data
  292. * @param type $ns
  293. * @return type
  294. */
  295. function cdata_parse($data, $ns = '')
  296. {
  297. global $smcFunc, $cdata_override;
  298. // Are we not doing it?
  299. if (!empty($cdata_override))
  300. return $data;
  301. $cdata = '<![CDATA[';
  302. for ($pos = 0, $n = $smcFunc['strlen']($data); $pos < $n; null)
  303. {
  304. $positions = array(
  305. $smcFunc['strpos']($data, '&', $pos),
  306. $smcFunc['strpos']($data, ']', $pos),
  307. );
  308. if ($ns != '')
  309. $positions[] = $smcFunc['strpos']($data, '<', $pos);
  310. foreach ($positions as $k => $dummy)
  311. {
  312. if ($dummy === false)
  313. unset($positions[$k]);
  314. }
  315. $old = $pos;
  316. $pos = empty($positions) ? $n : min($positions);
  317. if ($pos - $old > 0)
  318. $cdata .= $smcFunc['substr']($data, $old, $pos - $old);
  319. if ($pos >= $n)
  320. break;
  321. if ($smcFunc['substr']($data, $pos, 1) == '<')
  322. {
  323. $pos2 = $smcFunc['strpos']($data, '>', $pos);
  324. if ($pos2 === false)
  325. $pos2 = $n;
  326. if ($smcFunc['substr']($data, $pos + 1, 1) == '/')
  327. $cdata .= ']]></' . $ns . ':' . $smcFunc['substr']($data, $pos + 2, $pos2 - $pos - 1) . '<![CDATA[';
  328. else
  329. $cdata .= ']]><' . $ns . ':' . $smcFunc['substr']($data, $pos + 1, $pos2 - $pos) . '<![CDATA[';
  330. $pos = $pos2 + 1;
  331. }
  332. elseif ($smcFunc['substr']($data, $pos, 1) == ']')
  333. {
  334. $cdata .= ']]>&#093;<![CDATA[';
  335. $pos++;
  336. }
  337. elseif ($smcFunc['substr']($data, $pos, 1) == '&')
  338. {
  339. $pos2 = $smcFunc['strpos']($data, ';', $pos);
  340. if ($pos2 === false)
  341. $pos2 = $n;
  342. $ent = $smcFunc['substr']($data, $pos + 1, $pos2 - $pos - 1);
  343. if ($smcFunc['substr']($data, $pos + 1, 1) == '#')
  344. $cdata .= ']]>' . $smcFunc['substr']($data, $pos, $pos2 - $pos + 1) . '<![CDATA[';
  345. elseif (in_array($ent, array('amp', 'lt', 'gt', 'quot')))
  346. $cdata .= ']]>' . $smcFunc['substr']($data, $pos, $pos2 - $pos + 1) . '<![CDATA[';
  347. $pos = $pos2 + 1;
  348. }
  349. }
  350. $cdata .= ']]>';
  351. return strtr($cdata, array('<![CDATA[]]>' => ''));
  352. }
  353. /**
  354. * Formats data retrieved in other functions into xml format.
  355. * Additionally formats data based on the specific format passed.
  356. * This function is recursively called to handle sub arrays of data.
  357. * @param array $data the array to output as xml data
  358. * @param int $i the amount of indentation to use.
  359. * @param string $tag if specified, it will be used instead of the keys of data.
  360. * @param string $xml_format
  361. */
  362. function dumpTags($data, $i, $tag = null, $xml_format = '')
  363. {
  364. // For every array in the data...
  365. foreach ($data as $key => $val)
  366. {
  367. // Skip it, it's been set to null.
  368. if ($val === null)
  369. continue;
  370. // If a tag was passed, use it instead of the key.
  371. $key = isset($tag) ? $tag : $key;
  372. // First let's indent!
  373. echo "\n", str_repeat("\t", $i);
  374. // Grr, I hate kludges... almost worth doing it properly, here, but not quite.
  375. if ($xml_format == 'atom' && $key == 'link')
  376. {
  377. echo '<link rel="alternate" type="text/html" href="', fix_possible_url($val), '" />';
  378. continue;
  379. }
  380. // If it's empty/0/nothing simply output an empty tag.
  381. if ($val == '')
  382. echo '<', $key, '>';
  383. elseif ($xml_format == 'atom' && $key == 'category')
  384. echo '<', $key, ' term="', $val, '" />';
  385. else
  386. {
  387. // Beginning tag.
  388. if ($xml_format == 'rdf' && $key == 'item' && isset($val['link']))
  389. {
  390. echo '<', $key, ' rdf:about="', fix_possible_url($val['link']), '">';
  391. echo "\n", str_repeat("\t", $i + 1);
  392. echo '<dc:format>text/html</dc:format>';
  393. }
  394. elseif ($xml_format == 'atom' && $key == 'summary')
  395. echo '<', $key, ' type="html">';
  396. else
  397. echo '<', $key, '>';
  398. if (is_array($val))
  399. {
  400. // An array. Dump it, and then indent the tag.
  401. dumpTags($val, $i + 1, null, $xml_format);
  402. echo "\n", str_repeat("\t", $i), '</', $key, '>';
  403. }
  404. // A string with returns in it.... show this as a multiline element.
  405. elseif (strpos($val, "\n") !== false || strpos($val, '<br>') !== false)
  406. echo "\n", fix_possible_url($val), "\n", str_repeat("\t", $i), '</', $key, '>';
  407. // A simple string.
  408. else
  409. echo fix_possible_url($val), '</', $key, '>';
  410. }
  411. }
  412. }
  413. /**
  414. * Retrieve the list of members from database.
  415. * The array will be generated to match the format.
  416. * @todo get the list of members from Subs-Members.
  417. *
  418. * @param string $xml_format
  419. * @return array
  420. */
  421. function getXmlMembers($xml_format)
  422. {
  423. global $scripturl, $smcFunc;
  424. if (!allowedTo('view_mlist'))
  425. return array();
  426. // Find the most recent members.
  427. $request = $smcFunc['db_query']('', '
  428. SELECT id_member, member_name, real_name, date_registered, last_login
  429. FROM {db_prefix}members
  430. ORDER BY id_member DESC
  431. LIMIT {int:limit}',
  432. array(
  433. 'limit' => $_GET['limit'],
  434. )
  435. );
  436. $data = array();
  437. while ($row = $smcFunc['db_fetch_assoc']($request))
  438. {
  439. // Make the data look rss-ish.
  440. if ($xml_format == 'rss' || $xml_format == 'rss2')
  441. $data[] = array(
  442. 'title' => cdata_parse($row['real_name']),
  443. 'link' => $scripturl . '?action=profile;u=' . $row['id_member'],
  444. 'comments' => $scripturl . '?action=pm;sa=send;u=' . $row['id_member'],
  445. 'pubDate' => gmdate('D, d M Y H:i:s \G\M\T', $row['date_registered']),
  446. 'guid' => $scripturl . '?action=profile;u=' . $row['id_member'],
  447. );
  448. elseif ($xml_format == 'rdf')
  449. $data[] = array(
  450. 'title' => cdata_parse($row['real_name']),
  451. 'link' => $scripturl . '?action=profile;u=' . $row['id_member'],
  452. );
  453. elseif ($xml_format == 'atom')
  454. $data[] = array(
  455. 'title' => cdata_parse($row['real_name']),
  456. 'link' => $scripturl . '?action=profile;u=' . $row['id_member'],
  457. 'published' => gmstrftime('%Y-%m-%dT%H:%M:%SZ', $row['date_registered']),
  458. 'updated' => gmstrftime('%Y-%m-%dT%H:%M:%SZ', $row['last_login']),
  459. 'id' => $scripturl . '?action=profile;u=' . $row['id_member'],
  460. );
  461. // More logical format for the data, but harder to apply.
  462. else
  463. $data[] = array(
  464. 'name' => cdata_parse($row['real_name']),
  465. 'time' => $smcFunc['htmlspecialchars'](strip_tags(timeformat($row['date_registered']))),
  466. 'id' => $row['id_member'],
  467. 'link' => $scripturl . '?action=profile;u=' . $row['id_member']
  468. );
  469. }
  470. $smcFunc['db_free_result']($request);
  471. return $data;
  472. }
  473. /**
  474. * Get the latest topics information from a specific board,
  475. * to display later.
  476. * The returned array will be generated to match the xmf_format.
  477. * @todo does not belong here
  478. *
  479. * @param $xml_format
  480. * @return array, array of topics
  481. */
  482. function getXmlNews($xml_format)
  483. {
  484. global $scripturl, $modSettings, $board;
  485. global $query_this_board, $smcFunc, $context;
  486. /* Find the latest posts that:
  487. - are the first post in their topic.
  488. - are on an any board OR in a specified board.
  489. - can be seen by this user.
  490. - are actually the latest posts. */
  491. $done = false;
  492. $loops = 0;
  493. while (!$done)
  494. {
  495. $optimize_msg = implode(' AND ', $context['optimize_msg']);
  496. $request = $smcFunc['db_query']('', '
  497. SELECT
  498. m.smileys_enabled, m.poster_time, m.id_msg, m.subject, m.body, m.modified_time,
  499. m.icon, t.id_topic, t.id_board, t.num_replies,
  500. b.name AS bname,
  501. IFNULL(mem.id_member, 0) AS id_member,
  502. IFNULL(mem.email_address, m.poster_email) AS poster_email,
  503. IFNULL(mem.real_name, m.poster_name) AS poster_name
  504. FROM {db_prefix}topics AS t
  505. INNER JOIN {db_prefix}messages AS m ON (m.id_msg = t.id_first_msg)
  506. INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board)
  507. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member)
  508. WHERE ' . $query_this_board . (empty($optimize_msg) ? '' : '
  509. AND {raw:optimize_msg}') . (empty($board) ? '' : '
  510. AND t.id_board = {int:current_board}') . ($modSettings['postmod_active'] ? '
  511. AND t.approved = {int:is_approved}' : '') . '
  512. ORDER BY t.id_first_msg DESC
  513. LIMIT {int:limit}',
  514. array(
  515. 'current_board' => $board,
  516. 'is_approved' => 1,
  517. 'limit' => $_GET['limit'],
  518. 'optimize_msg' => $optimize_msg,
  519. )
  520. );
  521. // If we don't have $_GET['limit'] results, try again with an unoptimized version covering all rows.
  522. if ($loops < 2 && $smcFunc['db_num_rows']($request) < $_GET['limit'])
  523. {
  524. $smcFunc['db_free_result']($request);
  525. if (empty($_REQUEST['boards']) && empty($board))
  526. unset($context['optimize_msg']['lowest']);
  527. else
  528. $context['optimize_msg']['lowest'] = 'm.id_msg >= t.id_first_msg';
  529. $context['optimize_msg']['highest'] = 'm.id_msg <= t.id_last_msg';
  530. $loops++;
  531. }
  532. else
  533. $done = true;
  534. }
  535. $data = array();
  536. while ($row = $smcFunc['db_fetch_assoc']($request))
  537. {
  538. // Limit the length of the message, if the option is set.
  539. if (!empty($modSettings['xmlnews_maxlen']) && $smcFunc['strlen'](str_replace('<br>', "\n", $row['body'])) > $modSettings['xmlnews_maxlen'])
  540. $row['body'] = strtr($smcFunc['substr'](str_replace('<br>', "\n", $row['body']), 0, $modSettings['xmlnews_maxlen'] - 3), array("\n" => '<br>')) . '...';
  541. $row['body'] = parse_bbc($row['body'], $row['smileys_enabled'], $row['id_msg']);
  542. censorText($row['body']);
  543. censorText($row['subject']);
  544. // Being news, this actually makes sense in rss format.
  545. if ($xml_format == 'rss' || $xml_format == 'rss2')
  546. $data[] = array(
  547. 'title' => cdata_parse($row['subject']),
  548. 'link' => $scripturl . '?topic=' . $row['id_topic'] . '.0',
  549. 'description' => cdata_parse($row['body']),
  550. 'author' => (allowedTo('moderate_forum') || $row['id_member'] == $user_info['id']) ? $row['poster_email'] . ' ('.$row['poster_name'].')' : null,
  551. 'comments' => $scripturl . '?action=post;topic=' . $row['id_topic'] . '.0',
  552. 'category' => '<![CDATA[' . $row['bname'] . ']]>',
  553. 'pubDate' => gmdate('D, d M Y H:i:s \G\M\T', $row['poster_time']),
  554. 'guid' => $scripturl . '?topic=' . $row['id_topic'] . '.0',
  555. );
  556. elseif ($xml_format == 'rdf')
  557. $data[] = array(
  558. 'title' => cdata_parse($row['subject']),
  559. 'link' => $scripturl . '?topic=' . $row['id_topic'] . '.0',
  560. 'description' => cdata_parse($row['body']),
  561. );
  562. elseif ($xml_format == 'atom')
  563. $data[] = array(
  564. 'title' => cdata_parse($row['subject']),
  565. 'link' => $scripturl . '?topic=' . $row['id_topic'] . '.0',
  566. 'summary' => cdata_parse($row['body']),
  567. 'category' => $row['bname'],
  568. 'author' => array(
  569. 'name' => $row['poster_name'],
  570. 'email' => (allowedTo('moderate_forum') || $row['id_member'] == $user_info['id']) ? $row['poster_email'] : null,
  571. 'uri' => !empty($row['id_member']) ? $scripturl . '?action=profile;u=' . $row['id_member'] : '',
  572. ),
  573. 'published' => gmstrftime('%Y-%m-%dT%H:%M:%SZ', $row['poster_time']),
  574. 'modified' => gmstrftime('%Y-%m-%dT%H:%M:%SZ', empty($row['modified_time']) ? $row['poster_time'] : $row['modified_time']),
  575. 'id' => $scripturl . '?topic=' . $row['id_topic'] . '.0',
  576. );
  577. // The biggest difference here is more information.
  578. else
  579. $data[] = array(
  580. 'time' => $smcFunc['htmlspecialchars'](strip_tags(timeformat($row['poster_time']))),
  581. 'id' => $row['id_topic'],
  582. 'subject' => cdata_parse($row['subject']),
  583. 'body' => cdata_parse($row['body']),
  584. 'poster' => array(
  585. 'name' => cdata_parse($row['poster_name']),
  586. 'id' => $row['id_member'],
  587. 'link' => !empty($row['id_member']) ? $scripturl . '?action=profile;u=' . $row['id_member'] : '',
  588. ),
  589. 'topic' => $row['id_topic'],
  590. 'board' => array(
  591. 'name' => cdata_parse($row['bname']),
  592. 'id' => $row['id_board'],
  593. 'link' => $scripturl . '?board=' . $row['id_board'] . '.0',
  594. ),
  595. 'link' => $scripturl . '?topic=' . $row['id_topic'] . '.0',
  596. );
  597. }
  598. $smcFunc['db_free_result']($request);
  599. return $data;
  600. }
  601. /**
  602. * Get the recent topics to display.
  603. * The returned array will be generated to match the xml_format.
  604. * @todo does not belong here.
  605. *
  606. * @param $xml_format
  607. * @return array, of recent posts
  608. */
  609. function getXmlRecent($xml_format)
  610. {
  611. global $scripturl, $modSettings, $board;
  612. global $query_this_board, $smcFunc, $context;
  613. $done = false;
  614. $loops = 0;
  615. while (!$done)
  616. {
  617. $optimize_msg = implode(' AND ', $context['optimize_msg']);
  618. $request = $smcFunc['db_query']('', '
  619. SELECT m.id_msg
  620. FROM {db_prefix}messages AS m
  621. INNER JOIN {db_prefix}boards AS b ON (b.id_board = m.id_board)
  622. INNER JOIN {db_prefix}topics AS t ON (t.id_topic = m.id_topic)
  623. WHERE ' . $query_this_board . (empty($optimize_msg) ? '' : '
  624. AND {raw:optimize_msg}') . (empty($board) ? '' : '
  625. AND m.id_board = {int:current_board}') . ($modSettings['postmod_active'] ? '
  626. AND m.approved = {int:is_approved}' : '') . '
  627. ORDER BY m.id_msg DESC
  628. LIMIT {int:limit}',
  629. array(
  630. 'limit' => $_GET['limit'],
  631. 'current_board' => $board,
  632. 'is_approved' => 1,
  633. 'optimize_msg' => $optimize_msg,
  634. )
  635. );
  636. // If we don't have $_GET['limit'] results, try again with an unoptimized version covering all rows.
  637. if ($loops < 2 && $smcFunc['db_num_rows']($request) < $_GET['limit'])
  638. {
  639. $smcFunc['db_free_result']($request);
  640. if (empty($_REQUEST['boards']) && empty($board))
  641. unset($context['optimize_msg']['lowest']);
  642. else
  643. $context['optimize_msg']['lowest'] = $loops ? 'm.id_msg >= t.id_first_msg' : 'm.id_msg >= (t.id_last_msg - t.id_first_msg) / 2';
  644. $loops++;
  645. }
  646. else
  647. $done = true;
  648. }
  649. $messages = array();
  650. while ($row = $smcFunc['db_fetch_assoc']($request))
  651. $messages[] = $row['id_msg'];
  652. $smcFunc['db_free_result']($request);
  653. if (empty($messages))
  654. return array();
  655. // Find the most recent posts this user can see.
  656. $request = $smcFunc['db_query']('', '
  657. SELECT
  658. m.smileys_enabled, m.poster_time, m.id_msg, m.subject, m.body, m.id_topic, t.id_board,
  659. b.name AS bname, t.num_replies, m.id_member, m.icon, mf.id_member AS id_first_member,
  660. IFNULL(mem.real_name, m.poster_name) AS poster_name, mf.subject AS first_subject,
  661. IFNULL(memf.real_name, mf.poster_name) AS first_poster_name,
  662. IFNULL(mem.email_address, m.poster_email) AS poster_email, m.modified_time
  663. FROM {db_prefix}messages AS m
  664. INNER JOIN {db_prefix}topics AS t ON (t.id_topic = m.id_topic)
  665. INNER JOIN {db_prefix}messages AS mf ON (mf.id_msg = t.id_first_msg)
  666. INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board)
  667. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member)
  668. LEFT JOIN {db_prefix}members AS memf ON (memf.id_member = mf.id_member)
  669. WHERE m.id_msg IN ({array_int:message_list})
  670. ' . (empty($board) ? '' : 'AND t.id_board = {int:current_board}') . '
  671. ORDER BY m.id_msg DESC
  672. LIMIT {int:limit}',
  673. array(
  674. 'limit' => $_GET['limit'],
  675. 'current_board' => $board,
  676. 'message_list' => $messages,
  677. )
  678. );
  679. $data = array();
  680. while ($row = $smcFunc['db_fetch_assoc']($request))
  681. {
  682. // Limit the length of the message, if the option is set.
  683. if (!empty($modSettings['xmlnews_maxlen']) && $smcFunc['strlen'](str_replace('<br>', "\n", $row['body'])) > $modSettings['xmlnews_maxlen'])
  684. $row['body'] = strtr($smcFunc['substr'](str_replace('<br>', "\n", $row['body']), 0, $modSettings['xmlnews_maxlen'] - 3), array("\n" => '<br>')) . '...';
  685. $row['body'] = parse_bbc($row['body'], $row['smileys_enabled'], $row['id_msg']);
  686. censorText($row['body']);
  687. censorText($row['subject']);
  688. // Doesn't work as well as news, but it kinda does..
  689. if ($xml_format == 'rss' || $xml_format == 'rss2')
  690. $data[] = array(
  691. 'title' => $row['subject'],
  692. 'link' => $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . '#msg' . $row['id_msg'],
  693. 'description' => cdata_parse($row['body']),
  694. 'author' => (allowedTo('moderate_forum') || (!empty($row['id_member']) && $row['id_member'] == $user_info['id'])) ? $row['poster_email'] : null,
  695. 'category' => cdata_parse($row['bname']),
  696. 'comments' => $scripturl . '?action=post;topic=' . $row['id_topic'] . '.0',
  697. 'pubDate' => gmdate('D, d M Y H:i:s \G\M\T', $row['poster_time']),
  698. 'guid' => $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . '#msg' . $row['id_msg']
  699. );
  700. elseif ($xml_format == 'rdf')
  701. $data[] = array(
  702. 'title' => $row['subject'],
  703. 'link' => $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . '#msg' . $row['id_msg'],
  704. 'description' => cdata_parse($row['body']),
  705. );
  706. elseif ($xml_format == 'atom')
  707. $data[] = array(
  708. 'title' => $row['subject'],
  709. 'link' => $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . '#msg' . $row['id_msg'],
  710. 'summary' => cdata_parse($row['body']),
  711. 'category' => $row['bname'],
  712. 'author' => array(
  713. 'name' => $row['poster_name'],
  714. 'email' => (allowedTo('moderate_forum') || (!empty($row['id_member']) && $row['id_member'] == $user_info['id'])) ? $row['poster_email'] : null,
  715. 'uri' => !empty($row['id_member']) ? $scripturl . '?action=profile;u=' . $row['id_member'] : ''
  716. ),
  717. 'published' => gmstrftime('%Y-%m-%dT%H:%M:%SZ', $row['poster_time']),
  718. 'updated' => gmstrftime('%Y-%m-%dT%H:%M:%SZ', empty($row['modified_time']) ? $row['poster_time'] : $row['modified_time']),
  719. 'id' => $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . '#msg' . $row['id_msg'],
  720. );
  721. // A lot of information here. Should be enough to please the rss-ers.
  722. else
  723. $data[] = array(
  724. 'time' => $smcFunc['htmlspecialchars'](strip_tags(timeformat($row['poster_time']))),
  725. 'id' => $row['id_msg'],
  726. 'subject' => cdata_parse($row['subject']),
  727. 'body' => cdata_parse($row['body']),
  728. 'starter' => array(
  729. 'name' => cdata_parse($row['first_poster_name']),
  730. 'id' => $row['id_first_member'],
  731. 'link' => !empty($row['id_first_member']) ? $scripturl . '?action=profile;u=' . $row['id_first_member'] : ''
  732. ),
  733. 'poster' => array(
  734. 'name' => cdata_parse($row['poster_name']),
  735. 'id' => $row['id_member'],
  736. 'link' => !empty($row['id_member']) ? $scripturl . '?action=profile;u=' . $row['id_member'] : ''
  737. ),
  738. 'topic' => array(
  739. 'subject' => cdata_parse($row['first_subject']),
  740. 'id' => $row['id_topic'],
  741. 'link' => $scripturl . '?topic=' . $row['id_topic'] . '.new#new'
  742. ),
  743. 'board' => array(
  744. 'name' => cdata_parse($row['bname']),
  745. 'id' => $row['id_board'],
  746. 'link' => $scripturl . '?board=' . $row['id_board'] . '.0'
  747. ),
  748. 'link' => $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . '#msg' . $row['id_msg']
  749. );
  750. }
  751. $smcFunc['db_free_result']($request);
  752. return $data;
  753. }
  754. /**
  755. * Get the profile information for member into an array,
  756. * which will be generated to match the xml_format.
  757. * @todo refactor.
  758. *
  759. * @param $xml_format
  760. * @return array, of profile data.
  761. */
  762. function getXmlProfile($xml_format)
  763. {
  764. global $scripturl, $memberContext, $user_profile, $modSettings, $user_info;
  765. // You must input a valid user....
  766. if (empty($_GET['u']) || loadMemberData((int) $_GET['u']) === false)
  767. return array();
  768. // Make sure the id is a number and not "I like trying to hack the database".
  769. $_GET['u'] = (int) $_GET['u'];
  770. // Load the member's contextual information!
  771. if (!loadMemberContext($_GET['u']) || !allowedTo('profile_view'))
  772. return array();
  773. // Okay, I admit it, I'm lazy. Stupid $_GET['u'] is long and hard to type.
  774. $profile = &$memberContext[$_GET['u']];
  775. if ($xml_format == 'rss' || $xml_format == 'rss2')
  776. $data = array(array(
  777. 'title' => cdata_parse($profile['name']),
  778. 'link' => $scripturl . '?action=profile;u=' . $profile['id'],
  779. 'description' => cdata_parse(isset($profile['group']) ? $profile['group'] : $profile['post_group']),
  780. 'comments' => $scripturl . '?action=pm;sa=send;u=' . $profile['id'],
  781. 'pubDate' => gmdate('D, d M Y H:i:s \G\M\T', $user_profile[$profile['id']]['date_registered']),
  782. 'guid' => $scripturl . '?action=profile;u=' . $profile['id'],
  783. ));
  784. elseif ($xml_format == 'rdf')
  785. $data = array(array(
  786. 'title' => cdata_parse($profile['name']),
  787. 'link' => $scripturl . '?action=profile;u=' . $profile['id'],
  788. 'description' => cdata_parse(isset($profile['group']) ? $profile['group'] : $profile['post_group']),
  789. ));
  790. elseif ($xml_format == 'atom')
  791. $data[] = array(
  792. 'title' => cdata_parse($profile['name']),
  793. 'link' => $scripturl . '?action=profile;u=' . $profile['id'],
  794. 'summary' => cdata_parse(isset($profile['group']) ? $profile['group'] : $profile['post_group']),
  795. 'author' => array(
  796. 'name' => $profile['real_name'],
  797. 'email' => $profile['show_email'] ? $profile['email'] : null,
  798. 'uri' => !empty($profile['website']) ? $profile['website']['url'] : ''
  799. ),
  800. 'published' => gmstrftime('%Y-%m-%dT%H:%M:%SZ', $user_profile[$profile['id']]['date_registered']),
  801. 'updated' => gmstrftime('%Y-%m-%dT%H:%M:%SZ', $user_profile[$profile['id']]['last_login']),
  802. 'id' => $scripturl . '?action=profile;u=' . $profile['id'],
  803. 'logo' => !empty($profile['avatar']) ? $profile['avatar']['url'] : '',
  804. );
  805. else
  806. {
  807. $data = array(
  808. 'username' => $user_info['is_admin'] || $user_info['id'] == $profile['id'] ? cdata_parse($profile['username']) : '',
  809. 'name' => cdata_parse($profile['name']),
  810. 'link' => $scripturl . '?action=profile;u=' . $profile['id'],
  811. 'posts' => $profile['posts'],
  812. 'post-group' => cdata_parse($profile['post_group']),
  813. 'language' => cdata_parse($profile['language']),
  814. 'last-login' => gmdate('D, d M Y H:i:s \G\M\T', $user_profile[$profile['id']]['last_login']),
  815. 'registered' => gmdate('D, d M Y H:i:s \G\M\T', $user_profile[$profile['id']]['date_registered'])
  816. );
  817. // Everything below here might not be set, and thus maybe shouldn't be displayed.
  818. if ($profile['gender']['name'] != '')
  819. $data['gender'] = cdata_parse($profile['gender']['name']);
  820. if ($profile['avatar']['name'] != '')
  821. $data['avatar'] = $profile['avatar']['url'];
  822. // If they are online, show an empty tag... no reason to put anything inside it.
  823. if ($profile['online']['is_online'])
  824. $data['online'] = '';
  825. if ($profile['signature'] != '')
  826. $data['signature'] = cdata_parse($profile['signature']);
  827. if ($profile['blurb'] != '')
  828. $data['blurb'] = cdata_parse($profile['blurb']);
  829. if ($profile['title'] != '')
  830. $data['title'] = cdata_parse($profile['title']);
  831. if ($profile['website']['title'] != '')
  832. $data['website'] = array(
  833. 'title' => cdata_parse($profile['website']['title']),
  834. 'link' => $profile['website']['url']
  835. );
  836. if ($profile['group'] != '')
  837. $data['position'] = cdata_parse($profile['group']);
  838. if (!empty($modSettings['karmaMode']))
  839. $data['karma'] = array(
  840. 'good' => $profile['karma']['good'],
  841. 'bad' => $profile['karma']['bad']
  842. );
  843. if ($profile['show_email'])
  844. $data['email'] = $profile['email'];
  845. if (!empty($profile['birth_date']) && substr($profile['birth_date'], 0, 4) != '0000')
  846. {
  847. list ($birth_year, $birth_month, $birth_day) = sscanf($profile['birth_date'], '%d-%d-%d');
  848. $datearray = getdate(forum_time());
  849. $data['age'] = $datearray['year'] - $birth_year - (($datearray['mon'] > $birth_month || ($datearray['mon'] == $birth_month && $datearray['mday'] >= $birth_day)) ? 0 : 1);
  850. }
  851. }
  852. // Save some memory.
  853. unset($profile, $memberContext[$_GET['u']]);
  854. return $data;
  855. }
  856. ?>