(a little test for later merges)
[phpbb.git] / phpBB / feed.php
bloba6206d9203c22871720d502e75b4d584896c7bc1
1 <?php
2 /**
3 * @package phpBB3
4 * @version $Id$
5 * @copyright (c) 2009 phpBB Group
6 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
8 * Idea and original RSS Feed 2.0 MOD (Version 1.0.8/9) by leviatan21
9 * Original MOD: http://www.phpbb.com/community/viewtopic.php?f=69&t=1214645
10 * MOD Author Profile: http://www.phpbb.com/community/memberlist.php?mode=viewprofile&u=345763
11 * MOD Author Homepage: http://www.mssti.com/phpbb3/
13 **/
15 /**
16 * @ignore
17 **/
18 define('IN_PHPBB', true);
19 $phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
20 $phpEx = substr(strrchr(__FILE__, '.'), 1);
21 include($phpbb_root_path . 'common.' . $phpEx);
23 if (!$config['feed_enable'])
25 trigger_error('NO_FEED_ENABLED');
28 // Start session
29 $user->session_begin();
30 $auth->acl($user->data);
31 $user->setup();
33 // Initial var setup
34 $forum_id = request_var('f', 0);
35 $topic_id = request_var('t', 0);
36 $mode = request_var('mode', '');
38 // Feed date format for PHP > 5 and PHP4
39 $feed_date_format = (PHP_VERSION >= 5) ? 'c' : "Y-m-d\TH:i:sO";
40 $params = false;
42 // We do not use a template, therefore we simply define the global template variables here
43 $global_vars = $item_vars = array();
45 // Generate params array for use in append_sid() to correctly link back to this page
46 if ($forum_id || $topic_id || $mode)
48 $params = array(
49 'f' => ($forum_id) ? $forum_id : NULL,
50 't' => ($topic_id) ? $topic_id : NULL,
51 'mode' => ($mode) ? $mode : NULL,
55 // This boards URL
56 $board_url = generate_board_url();
58 // Get correct feed object
59 $feed = phpbb_feed_factory::init($mode, $forum_id, $topic_id);
61 // No feed found
62 if ($feed === false)
64 trigger_error('NO_FEED');
67 // Open Feed
68 $feed->open();
70 // Some default assignments
71 // FEED_IMAGE is not used (atom)
72 $global_vars = array(
73 'FEED_IMAGE' => ($user->img('site_logo', '', false, '', 'src')) ? $board_url . '/' . substr($user->img('site_logo', '', false, '', 'src'), strlen($phpbb_root_path)) : '',
74 'SELF_LINK' => feed_append_sid('/feed.' . $phpEx, $params),
75 'FEED_LINK' => $board_url . '/index.' . $phpEx,
76 'FEED_TITLE' => $config['sitename'],
77 'FEED_SUBTITLE' => $config['site_desc'],
78 'FEED_UPDATED' => $user->format_date(time(), $feed_date_format, true),
79 'FEED_LANG' => $user->lang['USER_LANG'],
80 'FEED_AUTHOR' => $config['sitename'],
83 // Iterate through items
84 while ($row = $feed->get_item())
86 // BBCode options to correctly disable urls, smilies, bbcode...
87 if ($feed->get('options') === NULL)
89 // Allow all combinations
90 $options = 7;
92 if ($feed->get('enable_bbcode') !== NULL && $feed->get('enable_smilies') !== NULL && $feed->get('enable_magic_url') !== NULL)
94 $options = (($row[$feed->get('enable_bbcode')]) ? OPTION_FLAG_BBCODE : 0) + (($row[$feed->get('enable_smilies')]) ? OPTION_FLAG_SMILIES : 0) + (($row[$feed->get('enable_magic_url')]) ? OPTION_FLAG_LINKS : 0);
97 else
99 $options = $row[$feed->get('options')];
102 $title = ($row[$feed->get('title')]) ? $row[$feed->get('title')] : ((isset($row[$feed->get('title2')])) ? $row[$feed->get('title2')] : '');
103 $title = censor_text($title);
105 $item_row = array(
106 'author' => ($feed->get('creator') !== NULL) ? $row[$feed->get('creator')] : '',
107 'pubdate' => $user->format_date($row[$feed->get('date')], $feed_date_format, true),
108 'link' => '',
109 'title' => censor_text($title),
110 'category' => ($config['feed_item_statistics']) ? $board_url . '/viewforum.' . $phpEx . '?f=' . $row['forum_id'] : '',
111 'category_name' => ($config['feed_item_statistics']) ? utf8_htmlspecialchars($row['forum_name']) : '',
112 'description' => censor_text(feed_generate_content($row[$feed->get('text')], $row[$feed->get('bbcode_uid')], $row[$feed->get('bitfield')], $options)),
113 'statistics' => '',
116 // Adjust items, fill link, etc.
117 $feed->adjust_item($item_row, $row);
119 $item_vars[] = $item_row;
122 $feed->close();
124 // Output page
126 // gzip_compression
127 if ($config['gzip_compress'])
129 if (@extension_loaded('zlib') && !headers_sent())
131 ob_start('ob_gzhandler');
135 // IF debug extra is enabled and admin want to "explain" the page we need to set other headers...
136 if (!defined('DEBUG_EXTRA') || !request_var('explain', 0) || !$auth->acl_get('a_'))
138 header("Content-Type: application/atom+xml; charset=UTF-8");
139 header("Last-Modified: " . gmdate('D, d M Y H:i:s', time()) . ' GMT');
141 else
143 header('Content-type: text/html; charset=UTF-8');
144 header('Cache-Control: private, no-cache="set-cookie"');
145 header('Expires: 0');
146 header('Pragma: no-cache');
148 $mtime = explode(' ', microtime());
149 $totaltime = $mtime[0] + $mtime[1] - $starttime;
151 if (method_exists($db, 'sql_report'))
153 $db->sql_report('display');
156 garbage_collection();
157 exit_handler();
160 echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
161 echo '<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="' . $global_vars['FEED_LANG'] . '">' . "\n";
162 echo '<link rel="self" type="application/atom+xml" href="' . $global_vars['SELF_LINK'] . '" />' . "\n\n";
164 echo (!empty($global_vars['FEED_TITLE'])) ? '<title>' . $global_vars['FEED_TITLE'] . '</title>' . "\n" : '';
165 echo (!empty($global_vars['FEED_SUBTITLE'])) ? '<subtitle>' . $global_vars['FEED_SUBTITLE'] . '</subtitle>' . "\n" : '';
166 echo (!empty($global_vars['FEED_LINK'])) ? '<link href="' . $global_vars['FEED_LINK'] .'" />' . "\n" : '';
167 echo '<updated>' . $global_vars['FEED_UPDATED'] . '</updated>' . "\n\n";
169 echo '<author><name><![CDATA[' . $global_vars['FEED_AUTHOR'] . ']]></name></author>' . "\n";
170 echo '<id>' . $global_vars['SELF_LINK'] . '</id>' . "\n";
172 foreach ($item_vars as $row)
174 echo '<entry>' . "\n";
176 if (!empty($row['author']))
178 echo '<author><name><![CDATA[' . $row['author'] . ']]></name></author>' . "\n";
181 echo '<updated>' . $row['pubdate'] . '</updated>' . "\n";
182 echo '<id>' . $row['link'] . '</id>' . "\n";
183 echo '<link href="' . $row['link'] . '"/>' . "\n";
184 echo '<title type="html"><![CDATA[' . $row['title'] . ']]></title>' . "\n\n";
186 if (!empty($row['category']))
188 echo '<category term="' . $row['category_name'] . '" scheme="' . $row['category'] . '" label="' . $row['category_name'] . '"/>' . "\n";
191 echo '<content type="html" xml:base="' . $row['link'] . '"><![CDATA[' . "\n";
192 echo $row['description'];
194 if (!empty($row['statistics']))
196 echo '<p>' . $user->lang['STATISTICS'] . ': ' . $row['statistics'] . '</p>';
199 echo '<hr /></div>' . "\n" . ']]></content>' . "\n";
200 echo '</entry>' . "\n";
203 echo '</feed>';
205 garbage_collection();
206 exit_handler();
209 * Run links through append_sid(), prepend generate_board_url() and remove session id
211 function feed_append_sid($url, $params)
213 global $board_url;
215 $link = append_sid($board_url . $url, $params);
217 // Remove added sid - not as easy as it sounds. ;)
218 $link = (strpos($link, 'sid=') !== false) ? trim(preg_replace('/(&amp;|&|\?)sid=[a-z0-9]+(&amp;|&)?/', '\1', $link), '?& ') : $link;
220 // Now the only thing remaining could be an empty &amp;
221 $link = (substr($link, -5) === '&amp;') ? substr($link, 0, -5) : $link;
222 // And &amp;#xxx
223 $link = str_replace('&amp;#', '#', $link);
225 return $link;
229 * Generate text content
231 function feed_generate_content($content, $uid, $bitfield, $options)
233 global $user, $config, $phpbb_root_path, $phpEx, $board_url;
235 if (empty($content))
237 return '';
240 // Prepare some bbcodes for better parsing
241 $content = preg_replace("#\[quote(=&quot;.*?&quot;)?:$uid\]\s*(.*?)\s*\[/quote:$uid\]#si", "[quote$1:$uid]<br />$2<br />[/quote:$uid]", $content);
243 $content = generate_text_for_display($content, $uid, $bitfield, $options);
245 // Add newlines
246 $content = str_replace('<br />', '<br />' . "\n", $content);
248 // Relative Path to Absolute path, Windows style
249 $content = str_replace('./', $board_url . '/', $content);
251 // Remove "Select all" link and mouse events
252 $content = str_replace('<a href="#" onclick="selectCode(this); return false;">' . $user->lang['SELECT_ALL_CODE'] . '</a>', '', $content);
253 $content = preg_replace('#(onkeypress|onclick)="(.*?)"#si', '', $content);
255 // Firefox does not support CSS for feeds, though
257 // Remove font sizes
258 // $content = preg_replace('#<span style="font-size: [0-9]+%; line-height: [0-9]+%;">([^>]+)</span>#iU', '\1', $content);
260 // Make text strong :P
261 // $content = preg_replace('#<span style="font-weight: bold?">(.*?)</span>#iU', '<strong>\1</strong>', $content);
263 // Italic
264 // $content = preg_replace('#<span style="font-style: italic?">([^<]+)</span>#iU', '<em>\1</em>', $content);
266 // Underline
267 // $content = preg_replace('#<span style="text-decoration: underline?">([^<]+)</span>#iU', '<u>\1</u>', $content);
269 // Remove embed Windows Media Streams
270 $content = preg_replace( '#<\!--\[if \!IE\]>-->([^[]+)<\!--<!\[endif\]-->#si', '', $content);
272 // Do not use &lt; and &gt;, because we want to retain code contained in [code][/code]
274 // Remove embed and objects
275 $content = preg_replace( '#<(object|embed)(.*?) (value|src)=(.*?) ([^[]+)(object|embed)>#si',' <a href=$4 target="_blank"><strong>$1</strong></a> ',$content);
277 // Remove some specials html tag, because somewhere there are a mod to allow html tags ;)
278 $content = preg_replace( '#<(script|iframe)([^[]+)\1>#siU', ' <strong>$1</strong> ', $content);
280 // Remove Comments from inline attachments [ia]
281 $content = preg_replace('#<div class="(inline-attachment|attachtitle)">(.*?)<!-- ia(.*?) -->(.*?)<!-- ia(.*?) -->(.*?)</div>#si','$4',$content);
283 // Replace some entities with their unicode counterpart
284 $entities = array(
285 '&nbsp;' => "\xC2\xA0",
286 '&bull;' => "\xE2\x80\xA2",
287 '&middot;' => "\xC2\xB7",
288 '&copy;' => "\xC2\xA9",
291 $content = str_replace(array_keys($entities), array_values($entities), $content);
293 // Remove CDATA blocks. ;)
294 $content = preg_replace('#\<\!\[CDATA\[(.*?)\]\]\>#s', '', $content);
296 // Other control characters
297 // $content = preg_replace('#(?:[\x00-\x1F\x7F]+|(?:\xC2[\x80-\x9F])+)#', '', $content);
299 return $content;
303 * Factory class to return correct object
304 * @package phpBB3
306 class phpbb_feed_factory
309 * Return correct object for specified mode
311 * @param string $mode The feeds mode.
312 * @param int $forum_id Forum id specified by the script if forum feed provided.
313 * @param int $topic_id Topic id specified by the script if topic feed provided.
315 * @return object Returns correct feeds object for specified mode.
317 function init($mode, $forum_id, $topic_id)
319 global $config;
321 switch ($mode)
323 case 'forums':
324 if (!$config['feed_overall_forums'])
326 return false;
329 return new phpbb_feed_forums();
330 break;
332 case 'topics':
333 if (!$config['feed_overall_topics'])
335 return false;
338 return new phpbb_feed_topics();
339 break;
341 case 'news':
342 global $db;
344 // Get at least one news forum
345 $sql = 'SELECT forum_id
346 FROM ' . FORUMS_TABLE . '
347 WHERE ' . $db->sql_bit_and('forum_options', FORUM_OPTION_FEED_NEWS, '<> 0');
348 $result = $db->sql_query_limit($sql, 1, 0, 600);
349 $s_feed_news = (int) $db->sql_fetchfield('forum_id');
350 $db->sql_freeresult($result);
352 if (!$s_feed_news)
354 return false;
357 return new phpbb_feed_news();
358 break;
360 default:
361 // Forum and/or topic specified?
362 if ($topic_id && !$config['feed_topic'])
364 return false;
367 if ($forum_id && !$topic_id && !$config['feed_forum'])
369 return false;
372 return new phpbb_feed($forum_id, $topic_id);
373 break;
379 * Base/default Feed class if no mode is specified.
380 * This can be the overall site feed or a forum/topic feed.
381 * @package phpBB3
383 class phpbb_feed
386 * Forum id specified for forum feed.
388 var $forum_id = 0;
391 * Topic id specified for topic feed.
393 var $topic_id = 0;
396 * SQL Query to be executed to get feed items
398 var $sql;
401 * Keys specified for retrieval of title, content, etc.
403 var $keys = array();
406 * An array of excluded forum ids.
408 var $excluded_forums_ary = NULL;
411 * Number of items to fetch
413 var $num_items;
416 * boolean to determine if items array is filled or not
418 var $items_filled = false;
421 * array holding items
423 var $items = array();
426 * Default setting for last x days
428 var $sort_days = 30;
431 * Default cache time of entries in seconds
433 var $cache_time = 300;
436 * Separator for title elements to separate items (for example forum / topic)
438 var $separator = "\xE2\x80\xA2"; // &bull;
441 * Constructor. Set standard keys.
443 function phpbb_feed($forum_id = 0, $topic_id = 0)
445 global $config;
447 $this->forum_id = $forum_id;
448 $this->topic_id = $topic_id;
450 $this->sql = array();
452 // Set some values for pagination
453 $this->num_items = (int) $config['feed_limit'];
454 $this->set_keys();
457 function set_keys()
459 // Set keys for items...
460 $this->set('title', 'post_subject');
461 $this->set('title2', 'topic_title');
462 $this->set('author_id', 'user_id');
463 $this->set('creator', 'username');
464 $this->set('text', 'post_text');
465 $this->set('bitfield', 'bbcode_bitfield');
466 $this->set('bbcode_uid','bbcode_uid');
467 $this->set('date', 'post_time');
469 $this->set('enable_bbcode', 'enable_bbcode');
470 $this->set('enable_smilies', 'enable_smilies');
471 $this->set('enable_magic_url', 'enable_magic_url');
474 function open()
476 if (!$this->forum_id && !$this->topic_id)
478 return;
480 else if ($this->forum_id && !$this->topic_id)
482 global $db, $user, $global_vars;
484 $sql = 'SELECT forum_name
485 FROM ' . FORUMS_TABLE . '
486 WHERE forum_id = ' . $this->forum_id;
487 $result = $db->sql_query($sql);
489 $global_vars['FEED_MODE'] = $user->lang['FORUM'] . ': ' . $db->sql_fetchfield('forum_name');
491 $db->sql_freeresult($result);
493 else if ($this->topic_id)
495 global $db, $user, $global_vars;
497 $sql = 'SELECT topic_title
498 FROM ' . TOPICS_TABLE . '
499 WHERE topic_id = ' . $this->topic_id;
500 $result = $db->sql_query($sql);
502 $global_vars['FEED_MODE'] = $user->lang['TOPIC'] . ': ' . $db->sql_fetchfield('topic_title');
504 $db->sql_freeresult($result);
508 function close()
510 if (!empty($this->result))
512 global $db;
514 $db->sql_freeresult($this->result);
519 * Set key
521 function set($key, $value)
523 $this->keys[$key] = $value;
527 * Get key
529 function get($key)
531 return (isset($this->keys[$key])) ? $this->keys[$key] : NULL;
535 * Return array of excluded forums
537 function excluded_forums()
539 if ($this->excluded_forums_ary !== NULL)
541 return $this->excluded_forums_ary;
544 global $auth, $db, $config, $phpbb_root_path, $phpEx, $user;
546 // Which forums should not be searched ?
547 $exclude_forums = array();
549 $sql = 'SELECT forum_id
550 FROM ' . FORUMS_TABLE . '
551 WHERE ' . $db->sql_bit_and('forum_options', FORUM_OPTION_FEED_EXCLUDE, '<> 0');
552 $result = $db->sql_query($sql);
554 while ($row = $db->sql_fetchrow($result))
556 $exclude_forums[] = (int) $row['forum_id'];
558 $db->sql_freeresult($result);
560 // Exclude forums the user is not able to read
561 $this->excluded_forums_ary = array_keys($auth->acl_getf('!f_read', true));
562 $this->excluded_forums_ary = (sizeof($exclude_forums)) ? array_merge($exclude_forums, $this->excluded_forums_ary) : $this->excluded_forums_ary;
564 $not_in_fid = (sizeof($this->excluded_forums_ary)) ? 'WHERE (' . $db->sql_in_set('f.forum_id', $this->excluded_forums_ary, true) . ' AND ' . $db->sql_in_set('f.parent_id', $this->excluded_forums_ary, true) . ") OR (f.forum_password <> '' AND fa.user_id <> " . (int) $user->data['user_id'] . ')' : '';
566 $sql = 'SELECT f.forum_id, f.forum_name, f.parent_id, f.forum_type, f.right_id, f.forum_password, fa.user_id
567 FROM ' . FORUMS_TABLE . ' f
568 LEFT JOIN ' . FORUMS_ACCESS_TABLE . " fa ON (fa.forum_id = f.forum_id
569 AND fa.session_id = '" . $db->sql_escape($user->session_id) . "')
570 $not_in_fid
571 ORDER BY f.left_id";
572 $result = $db->sql_query($sql);
574 $right_id = 0;
575 while ($row = $db->sql_fetchrow($result))
577 // Exclude passworded forum completely
578 if ($row['forum_password'] && $row['user_id'] != $user->data['user_id'])
580 $this->excluded_forums_ary[] = (int) $row['forum_id'];
581 continue;
584 if ($row['right_id'] > $right_id)
586 $right_id = (int) $row['right_id'];
588 else if ($row['right_id'] < $right_id)
590 continue;
593 $db->sql_freeresult($result);
595 return $this->excluded_forums_ary;
599 * Get SQL query for fetching items
601 function get_sql()
603 global $db;
605 $post_ids = array();
607 // Search for topics in last X days
608 $last_post_time_sql = ($this->sort_days) ? ' AND t.topic_last_post_time > ' . (time() - ($this->sort_days * 24 * 3600)) : '';
610 // Fetch latest post, grouped by topic...
611 if (!$this->forum_id && !$this->topic_id)
613 // First of all, the post ids...
614 $not_in_fid = (sizeof($this->excluded_forums())) ? ' AND ' . $db->sql_in_set('t.forum_id', $this->excluded_forums(), true) : '';
616 $sql = 'SELECT t.topic_last_post_id
617 FROM ' . TOPICS_TABLE . ' t
618 WHERE t.topic_approved = 1
619 AND t.topic_moved_id = 0' .
620 $not_in_fid .
621 $last_post_time_sql . '
622 ORDER BY t.topic_last_post_time DESC';
623 $result = $db->sql_query_limit($sql, $this->num_items);
625 while ($row = $db->sql_fetchrow($result))
627 $post_ids[] = (int) $row['topic_last_post_id'];
629 $db->sql_freeresult($result);
631 // Fetch latest posts from forum
632 else if (!$this->topic_id && $this->forum_id)
634 // Make sure the forum is not listed within the forbidden ones. ;)
635 if (in_array($this->forum_id, $this->excluded_forums()))
637 return false;
640 // Determine which forums to fetch
641 $not_in_fid = (sizeof($this->excluded_forums())) ? ' AND ' . $db->sql_in_set('f1.forum_id', $this->excluded_forums(), true) : '';
643 // Determine forum childs...
644 $sql = 'SELECT f2.forum_id
645 FROM ' . FORUMS_TABLE . ' f1, ' . FORUMS_TABLE . ' f2
646 WHERE f1.forum_id = ' . $this->forum_id . '
647 AND (f2.left_id BETWEEN f1.left_id AND f1.right_id' . $not_in_fid . ')';
648 $result = $db->sql_query($sql);
650 $forum_ids = array();
651 while ($row = $db->sql_fetchrow($result))
653 $forum_ids[] = (int) $row['forum_id'];
655 $db->sql_freeresult($result);
657 // Now select from forums...
658 $sql = 'SELECT t.topic_last_post_id
659 FROM ' . TOPICS_TABLE . ' t
660 WHERE ' . $db->sql_in_set('t.forum_id', $forum_ids) . '
661 AND t.topic_approved = 1
662 AND t.topic_moved_id = 0' .
663 $last_post_time_sql . '
664 ORDER BY t.topic_last_post_time DESC';
665 $result = $db->sql_query_limit($sql, $this->num_items);
667 while ($row = $db->sql_fetchrow($result))
669 $post_ids[] = (int) $row['topic_last_post_id'];
671 $db->sql_freeresult($result);
673 // Fetch last posts from specified topic...
674 else if ($this->topic_id)
676 // First of all, determine the forum...
677 $sql = 'SELECT forum_id
678 FROM ' . TOPICS_TABLE . '
679 WHERE topic_id = ' . $this->topic_id;
680 $result = $db->sql_query_limit($sql, 1);
681 $this->forum_id = (int) $db->sql_fetchfield('forum_id');
682 $db->sql_freeresult($result);
684 // non-global announcement
685 if ($this->forum_id && in_array($this->forum_id, $this->excluded_forums()))
687 return false;
690 $sql = 'SELECT post_id
691 FROM ' . POSTS_TABLE . '
692 WHERE topic_id = ' . $this->topic_id . '
693 AND post_approved = 1
694 ORDER BY post_time DESC';
695 $result = $db->sql_query_limit($sql, $this->num_items);
697 while ($row = $db->sql_fetchrow($result))
699 $post_ids[] = (int) $row['post_id'];
701 $db->sql_freeresult($result);
704 if (!sizeof($post_ids))
706 return false;
709 // Now build sql query for obtaining items
710 $this->sql = array(
711 'SELECT' => 'f.forum_id, f.forum_name, f.forum_desc_options, ' .
712 't.topic_last_post_time, t.topic_id, t.topic_title, t.topic_time, t.topic_replies, t.topic_views, ' .
713 'p.post_id, p.post_time, p.post_subject, p.post_text, p.bbcode_bitfield, p.bbcode_uid, p.enable_bbcode, p.enable_smilies, p.enable_magic_url, ' .
714 'u.username, u.user_id, u.user_email, u.user_colour',
715 'FROM' => array(
716 POSTS_TABLE => 'p',
717 TOPICS_TABLE => 't',
718 FORUMS_TABLE => 'f',
719 USERS_TABLE => 'u',
721 'WHERE' => $db->sql_in_set('p.post_id', $post_ids) . '
722 AND f.forum_id = p.forum_id
723 AND t.topic_id = p.topic_id
724 AND u.user_id = p.poster_id',
725 'ORDER_BY' => 'p.post_time DESC',
728 return true;
731 function get_item()
733 global $db, $cache;
735 // Disable cache if it is not a guest or a bot but a registered user
736 if ($this->cache_time)
738 global $user;
740 // We check this here because we call get_item() quite often
741 if (!empty($user) && $user->data['is_registered'])
743 $this->cache_time = 0;
747 if (!$this->cache_time)
749 if (empty($this->result))
751 if (!$this->get_sql())
753 return false;
756 // Query database
757 $sql = $db->sql_build_query('SELECT', $this->sql);
758 $this->result = $db->sql_query_limit($sql, $this->num_items);
761 return $db->sql_fetchrow($this->result);
763 else
765 if (empty($this->items_filled))
767 // Try to load result set...
768 $cache_filename = substr(get_class($this), strlen('phpbb_'));
770 if (($this->items = $cache->get('_' . $cache_filename)) === false)
772 $this->items = array();
774 if ($this->get_sql())
776 // Query database
777 $sql = $db->sql_build_query('SELECT', $this->sql);
778 $result = $db->sql_query_limit($sql, $this->num_items);
780 while ($row = $db->sql_fetchrow($result))
782 $this->items[] = $row;
784 $db->sql_freeresult($result);
787 $cache->put('_' . $cache_filename, $this->items, $this->cache_time);
790 $this->items_filled = true;
793 $row = array_shift($this->items);
794 return (!$row) ? false : $row;
798 function adjust_item(&$item_row, &$row)
800 global $phpEx, $config;
802 $item_row['title'] = (!$this->topic_id) ? $row['forum_name'] . ' ' . $this->separator . ' ' . $item_row['title'] : $item_row['title'];
803 $item_row['link'] = feed_append_sid('/viewtopic.' . $phpEx, "t={$row['topic_id']}&amp;p={$row['post_id']}#p{$row['post_id']}");
805 if ($config['feed_item_statistics'])
807 global $user;
809 $user_link = '<a href="' . feed_append_sid('/memberlist.' . $phpEx, 'mode=viewprofile&amp;u=' . $row['user_id']) . '">' . $row['username'] . '</a>';
811 $time = ($this->topic_id) ? $row['post_time'] : $row['topic_time'];
813 $item_row['statistics'] = $user->lang['POSTED'] . ' ' . $user->lang['POST_BY_AUTHOR'] . ' ' . $user_link . ' - ' . $user->format_date($time). ' - ' . $user->lang['REPLIES'] . ' ' . $row['topic_replies'] . ' - ' . $user->lang['VIEWS'] . ' ' . $row['topic_views'];
818 class phpbb_feed_forums extends phpbb_feed
820 function set_keys()
822 global $config;
824 $this->set('title', 'forum_name');
825 $this->set('text', 'forum_desc');
826 $this->set('bitfield', 'forum_desc_bitfield');
827 $this->set('bbcode_uid','forum_desc_uid');
828 $this->set('date', 'forum_last_post_time');
829 $this->set('options', 'forum_desc_options');
831 $this->num_items = (int) $config['feed_overall_forums_limit'];
834 function open()
836 global $user, $global_vars;
838 $global_vars['FEED_MODE'] = $user->lang['FORUMS'];
841 function get_sql()
843 global $db;
845 $not_in_fid = (sizeof($this->excluded_forums())) ? ' AND ' . $db->sql_in_set('f.forum_id', $this->excluded_forums(), true) : '';
847 // Build SQL Query
848 $this->sql = array(
849 'SELECT' => 'f.*',
850 'FROM' => array(FORUMS_TABLE => 'f'),
851 'WHERE' => 'f.forum_type = ' . FORUM_POST . '
852 AND (f.forum_last_post_id > 0' . $not_in_fid . ')',
853 'ORDER_BY' => 'f.left_id',
856 return true;
859 function adjust_item(&$item_row, &$row)
861 global $phpEx, $config;
863 $item_row['link'] = feed_append_sid('/viewforum.' . $phpEx, 'f=' . $row['forum_id']);
865 if ($config['feed_item_statistics'])
867 global $user;
869 $item_row['statistics'] = sprintf($user->lang['TOTAL_TOPICS_OTHER'], $row['forum_topics']) . ' - ' . sprintf($user->lang['TOTAL_POSTS_OTHER'], $row['forum_posts']);
874 class phpbb_feed_news extends phpbb_feed
876 function set_keys()
878 global $config;
880 $this->set('title', 'topic_title');
881 $this->set('title2', 'forum_name');
882 $this->set('author_id', 'topic_poster');
883 $this->set('creator', 'topic_first_poster_name');
884 $this->set('text', 'post_text');
885 $this->set('bitfield', 'bbcode_bitfield');
886 $this->set('bbcode_uid','bbcode_uid');
887 $this->set('date', 'topic_time');
889 $this->set('enable_bbcode', 'enable_bbcode');
890 $this->set('enable_smilies', 'enable_smilies');
891 $this->set('enable_magic_url', 'enable_magic_url');
893 $this->num_items = (int) $config['feed_overall_forums_limit'];
896 function open()
898 global $user, $global_vars;
900 $global_vars['FEED_MODE'] = $user->lang['FEED_NEWS'];
903 function get_sql()
905 global $db, $config;
907 // Get news forums...
908 $sql = 'SELECT forum_id
909 FROM ' . FORUMS_TABLE . '
910 WHERE ' . $db->sql_bit_and('forum_options', FORUM_OPTION_FEED_NEWS, '<> 0');
911 $result = $db->sql_query($sql);
913 $in_fid_ary = array();
914 while ($row = $db->sql_fetchrow($result))
916 $in_fid_ary[] = (int) $row['forum_id'];
918 $db->sql_freeresult($result);
920 if (!sizeof($in_fid_ary))
922 return false;
925 // Build SQL Query
926 $this->sql = array(
927 'SELECT' => 'f.forum_id, f.forum_password, f.forum_name, f.forum_topics, f.forum_posts, f.parent_id, f.left_id, f.right_id,
928 t.topic_id, t.topic_title, t.topic_poster, t.topic_first_poster_name, t.topic_replies, t.topic_views, t.topic_time,
929 p.post_id, p.post_text, p.bbcode_bitfield, p.bbcode_uid, p.enable_bbcode, p.enable_smilies, p.enable_magic_url,
930 u.username, u.user_id, u.user_email, u.user_colour',
931 'FROM' => array(
932 TOPICS_TABLE => 't',
933 FORUMS_TABLE => 'f',
934 POSTS_TABLE => 'p',
935 USERS_TABLE => 'u',
937 'WHERE' => $db->sql_in_set('t.forum_id', $in_fid_ary) . '
938 AND f.forum_id = t.forum_id
939 AND p.post_id = t.topic_first_post_id
940 AND t.topic_poster = u.user_id
941 AND t.topic_moved_id = 0',
942 'ORDER_BY' => 't.topic_time DESC',
945 return true;
948 function adjust_item(&$item_row, &$row)
950 global $phpEx, $config;
952 $item_row['link'] = feed_append_sid('/viewtopic.' . $phpEx, 't=' . $row['topic_id'] . '&amp;p=' . $row['post_id'] . '#p' . $row['post_id']);
954 if ($config['feed_item_statistics'])
956 global $user;
958 $user_link = '<a href="' . feed_append_sid('/memberlist.' . $phpEx, 'mode=viewprofile&amp;u=' . $row[$this->get('author_id')]) . '">' . $row[$this->get('creator')] . '</a>';
960 $item_row['statistics'] = $user->lang['POSTED'] . ' ' . $user->lang['POST_BY_AUTHOR'] . ' ' . $user_link . ' - ' . $user->format_date($row['topic_time']). ' - ' . $user->lang['REPLIES'] . ' ' . $row['topic_replies'] . ' - ' . $user->lang['VIEWS'] . ' ' . $row['topic_views'];
965 class phpbb_feed_topics extends phpbb_feed
967 function set_keys()
969 global $config;
971 $this->set('title', 'topic_title');
972 $this->set('title2', 'forum_name');
973 $this->set('author_id', 'topic_poster');
974 $this->set('creator', 'topic_first_poster_name');
975 $this->set('text', 'post_text');
976 $this->set('bitfield', 'bbcode_bitfield');
977 $this->set('bbcode_uid','bbcode_uid');
978 $this->set('date', 'topic_time');
980 $this->set('enable_bbcode', 'enable_bbcode');
981 $this->set('enable_smilies', 'enable_smilies');
982 $this->set('enable_magic_url', 'enable_magic_url');
984 $this->num_items = (int) $config['feed_overall_topics_limit'];
987 function open()
989 global $user, $global_vars;
991 $global_vars['FEED_MODE'] = $user->lang['TOPICS'];
994 function get_sql()
996 global $db, $config;
998 $post_ids = array();
999 $not_in_fid = (sizeof($this->excluded_forums())) ? ' AND ' . $db->sql_in_set('t.forum_id', $this->excluded_forums(), true) : '';
1001 // Search for topics in last x days
1002 $last_post_time_sql = ($this->sort_days) ? ' AND t.topic_last_post_time > ' . (time() - ($this->sort_days * 24 * 3600)) : '';
1004 // Last x topics from all forums, with first post from topic...
1005 $sql = 'SELECT t.topic_first_post_id
1006 FROM ' . TOPICS_TABLE . ' t
1007 WHERE t.topic_approved = 1
1008 AND t.topic_moved_id = 0' .
1009 $not_in_fid .
1010 $last_post_time_sql . '
1011 ORDER BY t.topic_last_post_time DESC';
1012 $result = $db->sql_query_limit($sql, $this->num_items);
1014 while ($row = $db->sql_fetchrow($result))
1016 $post_ids[] = (int) $row['topic_first_post_id'];
1018 $db->sql_freeresult($result);
1020 if (!sizeof($post_ids))
1022 return false;
1025 $this->sql = array(
1026 'SELECT' => 'f.forum_id, f.forum_password, f.forum_name, f.forum_topics, f.forum_posts, f.parent_id, f.left_id, f.right_id,
1027 t.topic_id, t.topic_title, t.topic_poster, t.topic_first_poster_name, t.topic_replies, t.topic_views, t.topic_time,
1028 p.post_id, p.post_text, p.bbcode_bitfield, p.bbcode_uid, p.enable_bbcode, p.enable_smilies, p.enable_magic_url,
1029 u.username, u.user_id, u.user_email, u.user_colour',
1030 'FROM' => array(
1031 TOPICS_TABLE => 't',
1032 FORUMS_TABLE => 'f',
1033 POSTS_TABLE => 'p',
1034 USERS_TABLE => 'u',
1036 'WHERE' => $db->sql_in_set('p.post_id', $post_ids) . '
1037 AND f.forum_id = p.forum_id
1038 AND t.topic_id = p.topic_id
1039 AND u.user_id = p.poster_id',
1040 'ORDER_BY' => 't.topic_last_post_time DESC',
1043 return true;
1046 function adjust_item(&$item_row, &$row)
1048 global $phpEx, $config;
1050 $item_row['link'] = feed_append_sid('/viewtopic.' . $phpEx, 't=' . $row['topic_id'] . '&amp;p=' . $row['post_id'] . '#p' . $row['post_id']);
1052 if ($config['feed_item_statistics'])
1054 global $user;
1056 $user_link = '<a href="' . feed_append_sid('/memberlist.' . $phpEx, 'mode=viewprofile&amp;u=' . $row[$this->get('author_id')]) . '">' . $row[$this->get('creator')] . '</a>';
1058 $item_row['statistics'] = $user->lang['POSTED'] . ' ' . $user->lang['POST_BY_AUTHOR'] . ' ' . $user_link . ' - ' . $user->format_date($row['topic_time']). ' - ' . $user->lang['REPLIES'] . ' ' . $row['topic_replies'] . ' - ' . $user->lang['VIEWS'] . ' ' . $row['topic_views'];