comparison forum/Sources/News.php @ 76:e3e11437ecea website

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