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/
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');
29 $user->session_begin();
31 if (!empty($config['feed_http_auth']) && request_var('auth', '') == 'http')
33 phpbb_http_login(array(
34 'auth_message' => 'Feed',
35 'viewonline' => request_var('viewonline', true),
39 $auth->acl($user->data);
43 $forum_id = request_var('f', 0);
44 $topic_id = request_var('t', 0);
45 $mode = request_var('mode', '');
47 // We do not use a template, therefore we simply define the global template variables here
48 $global_vars = $item_vars = array();
49 $feed_updated_time = 0;
51 // Generate params array for use in append_sid() to correctly link back to this page
53 if ($forum_id || $topic_id || $mode)
56 'f' => ($forum_id) ? $forum_id : NULL,
57 't' => ($topic_id) ? $topic_id : NULL,
58 'mode' => ($mode) ? $mode : NULL,
63 $board_url = generate_board_url();
65 // Get correct feed object
66 $feed = phpbb_feed_factory::init($mode, $forum_id, $topic_id);
71 trigger_error('NO_FEED');
77 // Iterate through items
78 while ($row = $feed->get_item())
80 // BBCode options to correctly disable urls, smilies, bbcode...
81 if ($feed->get('options') === NULL)
83 // Allow all combinations
86 if ($feed->get('enable_bbcode') !== NULL && $feed->get('enable_smilies') !== NULL && $feed->get('enable_magic_url') !== NULL)
88 $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);
93 $options = $row[$feed->get('options')];
96 $title = (isset($row[$feed->get('title')]) && $row[$feed->get('title')] !== '') ? $row[$feed->get('title')] : ((isset($row[$feed->get('title2')])) ? $row[$feed->get('title2')] : '');
98 $published = ($feed->get('published') !== NULL) ? (int) $row[$feed->get('published')] : 0;
99 $updated = ($feed->get('updated') !== NULL) ? (int) $row[$feed->get('updated')] : 0;
102 'author' => ($feed->get('creator') !== NULL) ? $row[$feed->get('creator')] : '',
103 'published' => ($published > 0) ? feed_format_date($published) : '',
104 'updated' => ($updated > 0) ? feed_format_date($updated) : '',
106 'title' => censor_text($title),
107 'category' => ($config['feed_item_statistics'] && !empty($row['forum_id'])) ? $board_url . '/viewforum.' . $phpEx . '?f=' . $row['forum_id'] : '',
108 'category_name' => ($config['feed_item_statistics'] && isset($row['forum_name'])) ? $row['forum_name'] : '',
109 'description' => censor_text(feed_generate_content($row[$feed->get('text')], $row[$feed->get('bbcode_uid')], $row[$feed->get('bitfield')], $options)),
113 // Adjust items, fill link, etc.
114 $feed->adjust_item($item_row, $row);
116 $item_vars[] = $item_row;
118 $feed_updated_time = max($feed_updated_time, $published, $updated);
121 // If we do not have any items at all, sending the current time is better than sending no time.
122 if (!$feed_updated_time)
124 $feed_updated_time = time();
127 // Some default assignments
128 // FEED_IMAGE is not used (atom)
129 $global_vars = array_merge($global_vars, array(
130 'FEED_IMAGE' => ($user->img('site_logo', '', false, '', 'src')) ? $board_url . '/' . substr($user->img('site_logo', '', false, '', 'src'), strlen($phpbb_root_path)) : '',
131 'SELF_LINK' => feed_append_sid('/feed.' . $phpEx, $params),
132 'FEED_LINK' => $board_url . '/index.' . $phpEx,
133 'FEED_TITLE' => $config['sitename'],
134 'FEED_SUBTITLE' => $config['site_desc'],
135 'FEED_UPDATED' => feed_format_date($feed_updated_time),
136 'FEED_LANG' => $user->lang['USER_LANG'],
137 'FEED_AUTHOR' => $config['sitename'],
145 if ($config['gzip_compress'])
147 if (@extension_loaded('zlib') && !headers_sent())
149 ob_start('ob_gzhandler');
153 // IF debug extra is enabled and admin want to "explain" the page we need to set other headers...
154 if (defined('DEBUG_EXTRA') && request_var('explain', 0) && $auth->acl_get('a_'))
156 header('Content-type: text/html; charset=UTF-8');
157 header('Cache-Control: private, no-cache="set-cookie"');
158 header('Expires: 0');
159 header('Pragma: no-cache');
161 $mtime = explode(' ', microtime());
162 $totaltime = $mtime[0] + $mtime[1] - $starttime;
164 if (method_exists($db, 'sql_report'))
166 $db->sql_report('display');
169 garbage_collection();
173 header("Content-Type: application/atom+xml; charset=UTF-8");
174 header("Last-Modified: " . gmdate('D, d M Y H:i:s', $feed_updated_time) . ' GMT');
176 if (!empty($user->data['is_bot']))
178 // Let reverse proxies know we detected a bot.
179 header('X-PHPBB-IS-BOT: yes');
182 echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
183 echo '<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="' . $global_vars['FEED_LANG'] . '">' . "\n";
184 echo '<link rel="self" type="application/atom+xml" href="' . $global_vars['SELF_LINK'] . '" />' . "\n\n";
186 echo (!empty($global_vars['FEED_TITLE'])) ? '<title>' . $global_vars['FEED_TITLE'] . '</title>' . "\n" : '';
187 echo (!empty($global_vars['FEED_SUBTITLE'])) ? '<subtitle>' . $global_vars['FEED_SUBTITLE'] . '</subtitle>' . "\n" : '';
188 echo (!empty($global_vars['FEED_LINK'])) ? '<link href="' . $global_vars['FEED_LINK'] .'" />' . "\n" : '';
189 echo '<updated>' . $global_vars['FEED_UPDATED'] . '</updated>' . "\n\n";
191 echo '<author><name><![CDATA[' . $global_vars['FEED_AUTHOR'] . ']]></name></author>' . "\n";
192 echo '<id>' . $global_vars['SELF_LINK'] . '</id>' . "\n";
194 foreach ($item_vars as $row)
196 echo '<entry>' . "\n";
198 if (!empty($row['author']))
200 echo '<author><name><![CDATA[' . $row['author'] . ']]></name></author>' . "\n";
203 echo '<updated>' . ((!empty($row['updated'])) ? $row['updated'] : $row['published']) . '</updated>' . "\n";
205 if (!empty($row['published']))
207 echo '<published>' . $row['published'] . '</published>' . "\n";
210 echo '<id>' . $row['link'] . '</id>' . "\n";
211 echo '<link href="' . $row['link'] . '"/>' . "\n";
212 echo '<title type="html"><![CDATA[' . $row['title'] . ']]></title>' . "\n\n";
214 if (!empty($row['category']) && isset($row['category_name']) && $row['category_name'] !== '')
216 echo '<category term="' . $row['category_name'] . '" scheme="' . $row['category'] . '" label="' . $row['category_name'] . '"/>' . "\n";
219 echo '<content type="html" xml:base="' . $row['link'] . '"><![CDATA[' . "\n";
220 echo $row['description'];
222 if (!empty($row['statistics']))
224 echo '<p>' . $user->lang['STATISTICS'] . ': ' . $row['statistics'] . '</p>';
227 echo '<hr />' . "\n" . ']]></content>' . "\n";
228 echo '</entry>' . "\n";
233 garbage_collection();
237 * Run links through append_sid(), prepend generate_board_url() and remove session id
239 function feed_append_sid($url, $params)
243 return append_sid($board_url . $url, $params, true, '');
247 * Generate ISO 8601 date string (RFC 3339)
249 function feed_format_date($time)
252 static $offset_string;
254 if (empty($offset_string))
258 $zone_offset = (int) $user->timezone + (int) $user->dst;
260 $sign = ($zone_offset < 0) ? '-' : '+';
261 $time_offset = abs($zone_offset);
263 $offset_seconds = $time_offset % 3600;
264 $offset_minutes = $offset_seconds / 60;
265 $offset_hours = ($time_offset - $offset_seconds) / 3600;
267 $offset_string = sprintf("%s%02d:%02d", $sign, $offset_hours, $offset_minutes);
270 return gmdate("Y-m-d\TH:i:s", $time + $zone_offset) . $offset_string;
274 * Generate text content
276 function feed_generate_content($content, $uid, $bitfield, $options)
278 global $user, $config, $phpbb_root_path, $phpEx, $board_url;
285 // Prepare some bbcodes for better parsing
286 $content = preg_replace("#\[quote(=".*?")?:$uid\]\s*(.*?)\s*\[/quote:$uid\]#si", "[quote$1:$uid]<br />$2<br />[/quote:$uid]", $content);
288 $content = generate_text_for_display($content, $uid, $bitfield, $options);
291 $content = str_replace('<br />', '<br />' . "\n", $content);
293 // Convert smiley Relative paths to Absolute path, Windows style
294 $content = str_replace($phpbb_root_path . $config['smilies_path'], $board_url . '/' . $config['smilies_path'], $content);
296 // Remove "Select all" link and mouse events
297 $content = str_replace('<a href="#" onclick="selectCode(this); return false;">' . $user->lang['SELECT_ALL_CODE'] . '</a>', '', $content);
298 $content = preg_replace('#(onkeypress|onclick)="(.*?)"#si', '', $content);
300 // Firefox does not support CSS for feeds, though
303 // $content = preg_replace('#<span style="font-size: [0-9]+%; line-height: [0-9]+%;">([^>]+)</span>#iU', '\1', $content);
305 // Make text strong :P
306 // $content = preg_replace('#<span style="font-weight: bold?">(.*?)</span>#iU', '<strong>\1</strong>', $content);
309 // $content = preg_replace('#<span style="font-style: italic?">([^<]+)</span>#iU', '<em>\1</em>', $content);
312 // $content = preg_replace('#<span style="text-decoration: underline?">([^<]+)</span>#iU', '<u>\1</u>', $content);
314 // Remove embed Windows Media Streams
315 $content = preg_replace( '#<\!--\[if \!IE\]>-->([^[]+)<\!--<!\[endif\]-->#si', '', $content);
317 // Do not use < and >, because we want to retain code contained in [code][/code]
319 // Remove embed and objects
320 $content = preg_replace( '#<(object|embed)(.*?) (value|src)=(.*?) ([^[]+)(object|embed)>#si',' <a href=$4 target="_blank"><strong>$1</strong></a> ',$content);
322 // Remove some specials html tag, because somewhere there are a mod to allow html tags ;)
323 $content = preg_replace( '#<(script|iframe)([^[]+)\1>#siU', ' <strong>$1</strong> ', $content);
325 // Remove Comments from inline attachments [ia]
326 $content = preg_replace('#<div class="(inline-attachment|attachtitle)">(.*?)<!-- ia(.*?) -->(.*?)<!-- ia(.*?) -->(.*?)</div>#si','$4',$content);
328 // Replace some entities with their unicode counterpart
330 ' ' => "\xC2\xA0",
331 '•' => "\xE2\x80\xA2",
332 '·' => "\xC2\xB7",
333 '©' => "\xC2\xA9",
336 $content = str_replace(array_keys($entities), array_values($entities), $content);
338 // Remove CDATA blocks. ;)
339 $content = preg_replace('#\<\!\[CDATA\[(.*?)\]\]\>#s', '', $content);
341 // Other control characters
342 $content = preg_replace('#(?:[\x00-\x1F\x7F]+|(?:\xC2[\x80-\x9F])+)#', '', $content);
348 * Factory class to return correct object
351 class phpbb_feed_factory
354 * Return correct object for specified mode
356 * @param string $mode The feeds mode.
357 * @param int $forum_id Forum id specified by the script if forum feed provided.
358 * @param int $topic_id Topic id specified by the script if topic feed provided.
360 * @return object Returns correct feeds object for specified mode.
362 function init($mode, $forum_id, $topic_id)
369 if (!$config['feed_overall_forums'])
374 return new phpbb_feed_forums();
379 if (!$config['feed_topics_new'])
384 return new phpbb_feed_topics();
387 case 'topics_active':
388 if (!$config['feed_topics_active'])
393 return new phpbb_feed_topics_active();
399 // Get at least one news forum
400 $sql = 'SELECT forum_id
401 FROM ' . FORUMS_TABLE . '
402 WHERE ' . $db->sql_bit_and('forum_options', FORUM_OPTION_FEED_NEWS, '<> 0');
403 $result = $db->sql_query_limit($sql, 1, 0, 600);
404 $s_feed_news = (int) $db->sql_fetchfield('forum_id');
405 $db->sql_freeresult($result);
412 return new phpbb_feed_news();
416 if ($topic_id && $config['feed_topic'])
418 return new phpbb_feed_topic($topic_id);
420 else if ($forum_id && $config['feed_forum'])
422 return new phpbb_feed_forum($forum_id);
424 else if ($config['feed_overall'])
426 return new phpbb_feed_overall();
436 * Base class with some generic functions and settings.
440 class phpbb_feed_base
443 * SQL Query to be executed to get feed items
448 * Keys specified for retrieval of title, content, etc.
453 * Number of items to fetch. Usually overwritten by $config['feed_something']
458 * Separator for title elements to separate items (for example forum / topic)
460 var $separator = "\xE2\x80\xA2"; // •
463 * Separator for the statistics row (Posted by, post date, replies, etc.)
465 var $separator_stats = "\xE2\x80\x94"; // —
470 function phpbb_feed_base()
476 // Allow num_items to be string
477 if (is_string($this->num_items))
479 $this->num_items = (int) $config[$this->num_items];
482 if (!$this->num_items)
484 $this->num_items = 10;
510 if (!empty($this->result))
512 $db->sql_freeresult($this->result);
519 function set($key, $value)
521 $this->keys[$key] = $value;
529 return (isset($this->keys[$key])) ? $this->keys[$key] : NULL;
532 function get_readable_forums()
537 if (!isset($forum_ids))
539 $forum_ids = array_keys($auth->acl_getf('f_read', true));
545 function get_moderator_approve_forums()
550 if (!isset($forum_ids))
552 $forum_ids = array_keys($auth->acl_getf('m_approve', true));
558 function is_moderator_approve_forum($forum_id)
562 if (!isset($forum_ids))
564 $forum_ids = array_flip($this->get_moderator_approve_forums());
569 // Global announcement, your a moderator in any forum than it's okay.
570 return (!empty($forum_ids)) ? true : false;
573 return (isset($forum_ids[$forum_id])) ? true : false;
576 function get_excluded_forums()
581 // Matches acp/acp_board.php
582 $cache_name = 'feed_excluded_forum_ids';
584 if (!isset($forum_ids) && ($forum_ids = $cache->get('_' . $cache_name)) === false)
586 $sql = 'SELECT forum_id
587 FROM ' . FORUMS_TABLE . '
588 WHERE ' . $db->sql_bit_and('forum_options', FORUM_OPTION_FEED_EXCLUDE, '<> 0');
589 $result = $db->sql_query($sql);
591 $forum_ids = array();
592 while ($forum_id = (int) $db->sql_fetchfield('forum_id'))
594 $forum_ids[$forum_id] = $forum_id;
596 $db->sql_freeresult($result);
598 $cache->put('_' . $cache_name, $forum_ids);
604 function is_excluded_forum($forum_id)
606 $forum_ids = $this->get_excluded_forums();
608 return isset($forum_ids[$forum_id]) ? true : false;
611 function get_passworded_forums()
615 return $user->get_passworded_forums();
625 if (!$this->get_sql())
631 $sql = $db->sql_build_query('SELECT', $this->sql);
632 $result = $db->sql_query_limit($sql, $this->num_items);
635 return $db->sql_fetchrow($result);
638 function user_viewprofile($row)
640 global $phpEx, $user;
642 $author_id = (int) $row[$this->get('author_id')];
644 if ($author_id == ANONYMOUS)
646 // Since we cannot link to a profile, we just return GUEST
647 // instead of $row['username']
648 return $user->lang['GUEST'];
651 return '<a href="' . feed_append_sid('/memberlist.' . $phpEx, 'mode=viewprofile&u=' . $author_id) . '">' . $row[$this->get('creator')] . '</a>';
656 * Abstract class for post based feeds
660 class phpbb_feed_post_base extends phpbb_feed_base
662 var $num_items = 'feed_limit_post';
666 $this->set('title', 'post_subject');
667 $this->set('title2', 'topic_title');
669 $this->set('author_id', 'user_id');
670 $this->set('creator', 'username');
671 $this->set('published', 'post_time');
672 $this->set('updated', 'post_edit_time');
673 $this->set('text', 'post_text');
675 $this->set('bitfield', 'bbcode_bitfield');
676 $this->set('bbcode_uid','bbcode_uid');
678 $this->set('enable_bbcode', 'enable_bbcode');
679 $this->set('enable_smilies', 'enable_smilies');
680 $this->set('enable_magic_url', 'enable_magic_url');
683 function adjust_item(&$item_row, &$row)
685 global $phpEx, $config, $user;
687 $item_row['link'] = feed_append_sid('/viewtopic.' . $phpEx, "t={$row['topic_id']}&p={$row['post_id']}#p{$row['post_id']}");
689 if ($config['feed_item_statistics'])
691 $item_row['statistics'] = $user->lang['POSTED'] . ' ' . $user->lang['POST_BY_AUTHOR'] . ' ' . $this->user_viewprofile($row)
692 . ' ' . $this->separator_stats . ' ' . $user->format_date($row[$this->get('published')])
693 . (($this->is_moderator_approve_forum($row['forum_id']) && !$row['post_approved']) ? ' ' . $this->separator_stats . ' ' . $user->lang['POST_UNAPPROVED'] : '');
699 * Abstract class for topic based feeds
703 class phpbb_feed_topic_base extends phpbb_feed_base
705 var $num_items = 'feed_limit_topic';
709 $this->set('title', 'topic_title');
710 $this->set('title2', 'forum_name');
712 $this->set('author_id', 'topic_poster');
713 $this->set('creator', 'topic_first_poster_name');
714 $this->set('published', 'post_time');
715 $this->set('updated', 'post_edit_time');
716 $this->set('text', 'post_text');
718 $this->set('bitfield', 'bbcode_bitfield');
719 $this->set('bbcode_uid','bbcode_uid');
721 $this->set('enable_bbcode', 'enable_bbcode');
722 $this->set('enable_smilies', 'enable_smilies');
723 $this->set('enable_magic_url', 'enable_magic_url');
726 function adjust_item(&$item_row, &$row)
728 global $phpEx, $config, $user;
730 $item_row['link'] = feed_append_sid('/viewtopic.' . $phpEx, 't=' . $row['topic_id'] . '&p=' . $row['post_id'] . '#p' . $row['post_id']);
732 if ($config['feed_item_statistics'])
734 $item_row['statistics'] = $user->lang['POSTED'] . ' ' . $user->lang['POST_BY_AUTHOR'] . ' ' . $this->user_viewprofile($row)
735 . ' ' . $this->separator_stats . ' ' . $user->format_date($row[$this->get('published')])
736 . ' ' . $this->separator_stats . ' ' . $user->lang['REPLIES'] . ' ' . (($this->is_moderator_approve_forum($row['forum_id'])) ? $row['topic_replies_real'] : $row['topic_replies'])
737 . ' ' . $this->separator_stats . ' ' . $user->lang['VIEWS'] . ' ' . $row['topic_views']
738 . (($this->is_moderator_approve_forum($row['forum_id']) && ($row['topic_replies_real'] != $row['topic_replies'])) ? ' ' . $this->separator_stats . ' ' . $user->lang['POSTS_UNAPPROVED'] : '');
744 * Board wide feed (aka overall feed)
746 * This will give you the newest {$this->num_items} posts
747 * from the whole board.
751 class phpbb_feed_overall extends phpbb_feed_post_base
757 $forum_ids = array_diff($this->get_readable_forums(), $this->get_excluded_forums(), $this->get_passworded_forums());
758 if (empty($forum_ids))
763 // Add global forum id
767 $fid_m_approve = $this->get_moderator_approve_forums();
768 $sql_m_approve = (!empty($fid_m_approve)) ? 'OR ' . $db->sql_in_set('forum_id', $fid_m_approve) : '';
770 // Determine topics with recent activity
771 $sql = 'SELECT topic_id, topic_last_post_time
772 FROM ' . TOPICS_TABLE . '
773 WHERE ' . $db->sql_in_set('forum_id', $forum_ids) . '
774 AND topic_moved_id = 0
775 AND (topic_approved = 1
776 ' . $sql_m_approve . ')
777 ORDER BY topic_last_post_time DESC';
778 $result = $db->sql_query_limit($sql, $this->num_items);
780 $topic_ids = array();
782 while ($row = $db->sql_fetchrow())
784 $topic_ids[] = (int) $row['topic_id'];
786 $min_post_time = (int) $row['topic_last_post_time'];
788 $db->sql_freeresult($result);
790 if (empty($topic_ids))
795 // Get the actual data
797 'SELECT' => 'f.forum_id, f.forum_name, ' .
798 'p.post_id, p.topic_id, p.post_time, p.post_edit_time, p.post_approved, p.post_subject, p.post_text, p.bbcode_bitfield, p.bbcode_uid, p.enable_bbcode, p.enable_smilies, p.enable_magic_url, ' .
799 'u.username, u.user_id',
804 'LEFT_JOIN' => array(
806 'FROM' => array(FORUMS_TABLE => 'f'),
807 'ON' => 'f.forum_id = p.forum_id',
810 'WHERE' => $db->sql_in_set('p.topic_id', $topic_ids) . '
811 AND (p.post_approved = 1
812 ' . str_replace('forum_id', 'p.forum_id', $sql_m_approve) . ')
813 AND p.post_time >= ' . $min_post_time . '
814 AND u.user_id = p.poster_id',
815 'ORDER_BY' => 'p.post_time DESC',
821 function adjust_item(&$item_row, &$row)
823 parent::adjust_item($item_row, $row);
825 $item_row['title'] = (isset($row['forum_name']) && $row['forum_name'] !== '') ? $row['forum_name'] . ' ' . $this->separator . ' ' . $item_row['title'] : $item_row['title'];
832 * This will give you the last {$this->num_items} posts made
833 * within a specific forum.
837 class phpbb_feed_forum extends phpbb_feed_post_base
840 var $forum_data = array();
842 function phpbb_feed_forum($forum_id)
844 parent::phpbb_feed_base();
846 $this->forum_id = (int) $forum_id;
853 // Check if forum exists
854 $sql = 'SELECT forum_id, forum_name, forum_password, forum_type, forum_options
855 FROM ' . FORUMS_TABLE . '
856 WHERE forum_id = ' . $this->forum_id;
857 $result = $db->sql_query($sql);
858 $this->forum_data = $db->sql_fetchrow($result);
859 $db->sql_freeresult($result);
861 if (empty($this->forum_data))
863 trigger_error('NO_FORUM');
866 // Forum needs to be postable
867 if ($this->forum_data['forum_type'] != FORUM_POST)
869 trigger_error('NO_FEED');
872 // Make sure forum is not excluded from feed
873 if (phpbb_optionget(FORUM_OPTION_FEED_EXCLUDE, $this->forum_data['forum_options']))
875 trigger_error('NO_FEED');
878 // Make sure we can read this forum
879 if (!$auth->acl_get('f_read', $this->forum_id))
881 trigger_error('SORRY_AUTH_READ');
884 // Make sure forum is not passworded or user is authed
885 if ($this->forum_data['forum_password'])
887 $forum_ids_passworded = $this->get_passworded_forums();
889 if (isset($forum_ids_passworded[$this->forum_id]))
891 trigger_error('SORRY_AUTH_READ');
894 unset($forum_ids_passworded);
902 $m_approve = ($auth->acl_get('m_approve', $this->forum_id)) ? true : false;
903 $forum_ids = array(0, $this->forum_id);
905 // Determine topics with recent activity
906 $sql = 'SELECT topic_id, topic_last_post_time
907 FROM ' . TOPICS_TABLE . '
908 WHERE ' . $db->sql_in_set('forum_id', $forum_ids) . '
909 AND topic_moved_id = 0
910 ' . ((!$m_approve) ? 'AND topic_approved = 1' : '') . '
911 ORDER BY topic_last_post_time DESC';
912 $result = $db->sql_query_limit($sql, $this->num_items);
914 $topic_ids = array();
916 while ($row = $db->sql_fetchrow())
918 $topic_ids[] = (int) $row['topic_id'];
920 $min_post_time = (int) $row['topic_last_post_time'];
922 $db->sql_freeresult($result);
924 if (empty($topic_ids))
930 'SELECT' => 'p.post_id, p.topic_id, p.post_time, p.post_edit_time, p.post_approved, p.post_subject, p.post_text, p.bbcode_bitfield, p.bbcode_uid, p.enable_bbcode, p.enable_smilies, p.enable_magic_url, ' .
931 'u.username, u.user_id',
936 'WHERE' => $db->sql_in_set('p.topic_id', $topic_ids) . '
937 ' . ((!$m_approve) ? 'AND p.post_approved = 1' : '') . '
938 AND p.post_time >= ' . $min_post_time . '
939 AND p.poster_id = u.user_id',
940 'ORDER_BY' => 'p.post_time DESC',
946 function adjust_item(&$item_row, &$row)
948 parent::adjust_item($item_row, $row);
950 $item_row['title'] = (isset($row['forum_name']) && $row['forum_name'] !== '') ? $row['forum_name'] . ' ' . $this->separator . ' ' . $item_row['title'] : $item_row['title'];
955 return ($row = parent::get_item()) ? array_merge($this->forum_data, $row) : $row;
960 * Topic feed for a specific topic
962 * This will give you the last {$this->num_items} posts made within this topic.
966 class phpbb_feed_topic extends phpbb_feed_post_base
970 var $topic_data = array();
972 function phpbb_feed_topic($topic_id)
974 parent::phpbb_feed_base();
976 $this->topic_id = (int) $topic_id;
981 global $auth, $db, $user;
983 $sql = 'SELECT f.forum_options, f.forum_password, t.topic_id, t.forum_id, t.topic_approved, t.topic_title, t.topic_time, t.topic_views, t.topic_replies, t.topic_type
984 FROM ' . TOPICS_TABLE . ' t
985 LEFT JOIN ' . FORUMS_TABLE . ' f
986 ON (f.forum_id = t.forum_id)
987 WHERE t.topic_id = ' . $this->topic_id;
988 $result = $db->sql_query($sql);
989 $this->topic_data = $db->sql_fetchrow($result);
990 $db->sql_freeresult($result);
992 if (empty($this->topic_data))
994 trigger_error('NO_TOPIC');
997 if ($this->topic_data['topic_type'] == POST_GLOBAL)
999 // We need to find at least one postable forum where feeds are enabled,
1000 // that the user can read and maybe also has approve permissions.
1001 $in_fid_ary = $this->get_readable_forums();
1003 if (empty($in_fid_ary))
1005 // User cannot read any forums
1006 trigger_error('SORRY_AUTH_READ');
1009 if (!$this->topic_data['topic_approved'])
1011 // Also require m_approve
1012 $in_fid_ary = array_intersect($in_fid_ary, $this->get_moderator_approve_forums());
1014 if (empty($in_fid_ary))
1016 trigger_error('SORRY_AUTH_READ');
1020 // Diff excluded forums
1021 $in_fid_ary = array_diff($in_fid_ary, $this->get_excluded_forums());
1023 if (empty($in_fid_ary))
1025 trigger_error('SORRY_AUTH_READ');
1028 // Also exclude passworded forums
1029 $in_fid_ary = array_diff($in_fid_ary, $this->get_passworded_forums());
1031 if (empty($in_fid_ary))
1033 trigger_error('SORRY_AUTH_READ');
1036 $sql = 'SELECT forum_id, left_id
1037 FROM ' . FORUMS_TABLE . '
1038 WHERE forum_type = ' . FORUM_POST . '
1039 AND ' . $db->sql_in_set('forum_id', $in_fid_ary) . '
1040 ORDER BY left_id ASC';
1041 $result = $db->sql_query_limit($sql, 1);
1042 $this->forum_data = $db->sql_fetchrow($result);
1043 $db->sql_freeresult($result);
1045 if (empty($this->forum_data))
1048 trigger_error('SORRY_AUTH_READ');
1055 $this->forum_id = (int) $this->topic_data['forum_id'];
1057 // Make sure topic is either approved or user authed
1058 if (!$this->topic_data['topic_approved'] && !$auth->acl_get('m_approve', $this->forum_id))
1060 trigger_error('SORRY_AUTH_READ');
1063 // Make sure forum is not excluded from feed
1064 if (phpbb_optionget(FORUM_OPTION_FEED_EXCLUDE, $this->topic_data['forum_options']))
1066 trigger_error('NO_FEED');
1069 // Make sure we can read this forum
1070 if (!$auth->acl_get('f_read', $this->forum_id))
1072 trigger_error('SORRY_AUTH_READ');
1075 // Make sure forum is not passworded or user is authed
1076 if ($this->topic_data['forum_password'])
1078 $forum_ids_passworded = $this->get_passworded_forums();
1080 if (isset($forum_ids_passworded[$this->forum_id]))
1082 trigger_error('SORRY_AUTH_READ');
1085 unset($forum_ids_passworded);
1095 'SELECT' => 'p.post_id, p.post_time, p.post_edit_time, p.post_approved, p.post_subject, p.post_text, p.bbcode_bitfield, p.bbcode_uid, p.enable_bbcode, p.enable_smilies, p.enable_magic_url, ' .
1096 'u.username, u.user_id',
1101 'WHERE' => 'p.topic_id = ' . $this->topic_id . '
1102 ' . ($this->forum_id && !$auth->acl_get('m_approve', $this->forum_id) ? 'AND p.post_approved = 1' : '') . '
1103 AND p.poster_id = u.user_id',
1104 'ORDER_BY' => 'p.post_time DESC',
1112 return ($row = parent::get_item()) ? array_merge($this->topic_data, $row) : $row;
1119 * This will give you a list of all postable forums where feeds are enabled
1120 * including forum description, topic stats and post stats
1124 class phpbb_feed_forums extends phpbb_feed_base
1130 $this->set('title', 'forum_name');
1131 $this->set('text', 'forum_desc');
1132 $this->set('bitfield', 'forum_desc_bitfield');
1133 $this->set('bbcode_uid','forum_desc_uid');
1134 $this->set('updated', 'forum_last_post_time');
1135 $this->set('options', 'forum_desc_options');
1142 $in_fid_ary = array_diff($this->get_readable_forums(), $this->get_excluded_forums());
1143 if (empty($in_fid_ary))
1150 'SELECT' => 'f.forum_id, f.left_id, f.forum_name, f.forum_last_post_time,
1151 f.forum_desc, f.forum_desc_bitfield, f.forum_desc_uid, f.forum_desc_options,
1152 f.forum_topics, f.forum_posts',
1153 'FROM' => array(FORUMS_TABLE => 'f'),
1154 'WHERE' => 'f.forum_type = ' . FORUM_POST . '
1155 AND ' . $db->sql_in_set('f.forum_id', $in_fid_ary),
1156 'ORDER_BY' => 'f.left_id ASC',
1162 function adjust_item(&$item_row, &$row)
1164 global $phpEx, $config;
1166 $item_row['link'] = feed_append_sid('/viewforum.' . $phpEx, 'f=' . $row['forum_id']);
1168 if ($config['feed_item_statistics'])
1172 $item_row['statistics'] = sprintf($user->lang['TOTAL_TOPICS_OTHER'], $row['forum_topics'])
1173 . ' ' . $this->separator_stats . ' ' . sprintf($user->lang['TOTAL_POSTS_OTHER'], $row['forum_posts']);
1181 * This will give you {$this->num_items} first posts
1182 * of all topics in the selected news forums.
1186 class phpbb_feed_news extends phpbb_feed_topic_base
1188 function get_news_forums()
1193 // Matches acp/acp_board.php
1194 $cache_name = 'feed_news_forum_ids';
1196 if (!isset($forum_ids) && ($forum_ids = $cache->get('_' . $cache_name)) === false)
1198 $sql = 'SELECT forum_id
1199 FROM ' . FORUMS_TABLE . '
1200 WHERE ' . $db->sql_bit_and('forum_options', FORUM_OPTION_FEED_NEWS, '<> 0');
1201 $result = $db->sql_query($sql);
1203 $forum_ids = array();
1204 while ($forum_id = (int) $db->sql_fetchfield('forum_id'))
1206 $forum_ids[$forum_id] = $forum_id;
1208 $db->sql_freeresult($result);
1210 $cache->put('_' . $cache_name, $forum_ids);
1218 global $auth, $config, $db;
1220 // Determine forum ids
1221 $in_fid_ary = array_intersect($this->get_news_forums(), $this->get_readable_forums());
1222 if (empty($in_fid_ary))
1227 $in_fid_ary = array_diff($in_fid_ary, $this->get_passworded_forums());
1228 if (empty($in_fid_ary))
1236 // We really have to get the post ids first!
1237 $sql = 'SELECT topic_first_post_id, topic_time
1238 FROM ' . TOPICS_TABLE . '
1239 WHERE ' . $db->sql_in_set('forum_id', $in_fid_ary) . '
1240 AND topic_moved_id = 0
1241 AND topic_approved = 1
1242 ORDER BY topic_time DESC';
1243 $result = $db->sql_query_limit($sql, $this->num_items);
1245 $post_ids = array();
1246 while ($row = $db->sql_fetchrow($result))
1248 $post_ids[] = (int) $row['topic_first_post_id'];
1250 $db->sql_freeresult($result);
1252 if (empty($post_ids))
1258 'SELECT' => 'f.forum_id, f.forum_name,
1259 t.topic_id, t.topic_title, t.topic_poster, t.topic_first_poster_name, t.topic_replies, t.topic_replies_real, t.topic_views, t.topic_time, t.topic_last_post_time,
1260 p.post_id, p.post_time, p.post_edit_time, p.post_text, p.bbcode_bitfield, p.bbcode_uid, p.enable_bbcode, p.enable_smilies, p.enable_magic_url',
1262 TOPICS_TABLE => 't',
1265 'LEFT_JOIN' => array(
1267 'FROM' => array(FORUMS_TABLE => 'f'),
1268 'ON' => 'p.forum_id = f.forum_id',
1271 'WHERE' => 'p.topic_id = t.topic_id
1272 AND ' . $db->sql_in_set('p.post_id', $post_ids),
1273 'ORDER_BY' => 'p.post_time DESC',
1283 * This will give you the last {$this->num_items} created topics
1284 * including the first post.
1288 class phpbb_feed_topics extends phpbb_feed_topic_base
1292 global $db, $config;
1294 $forum_ids_read = $this->get_readable_forums();
1295 if (empty($forum_ids_read))
1300 $in_fid_ary = array_diff($forum_ids_read, $this->get_excluded_forums(), $this->get_passworded_forums());
1301 if (empty($in_fid_ary))
1309 // We really have to get the post ids first!
1310 $sql = 'SELECT topic_first_post_id, topic_time
1311 FROM ' . TOPICS_TABLE . '
1312 WHERE ' . $db->sql_in_set('forum_id', $in_fid_ary) . '
1313 AND topic_moved_id = 0
1314 AND topic_approved = 1
1315 ORDER BY topic_time DESC';
1316 $result = $db->sql_query_limit($sql, $this->num_items);
1318 $post_ids = array();
1319 while ($row = $db->sql_fetchrow($result))
1321 $post_ids[] = (int) $row['topic_first_post_id'];
1323 $db->sql_freeresult($result);
1325 if (empty($post_ids))
1331 'SELECT' => 'f.forum_id, f.forum_name,
1332 t.topic_id, t.topic_title, t.topic_poster, t.topic_first_poster_name, t.topic_replies, t.topic_replies_real, t.topic_views, t.topic_time, t.topic_last_post_time,
1333 p.post_id, p.post_time, p.post_edit_time, p.post_text, p.bbcode_bitfield, p.bbcode_uid, p.enable_bbcode, p.enable_smilies, p.enable_magic_url',
1335 TOPICS_TABLE => 't',
1338 'LEFT_JOIN' => array(
1340 'FROM' => array(FORUMS_TABLE => 'f'),
1341 'ON' => 'p.forum_id = f.forum_id',
1344 'WHERE' => 'p.topic_id = t.topic_id
1345 AND ' . $db->sql_in_set('p.post_id', $post_ids),
1346 'ORDER_BY' => 'p.post_time DESC',
1352 function adjust_item(&$item_row, &$row)
1354 parent::adjust_item($item_row, $row);
1356 $item_row['title'] = (isset($row['forum_name']) && $row['forum_name'] !== '') ? $row['forum_name'] . ' ' . $this->separator . ' ' . $item_row['title'] : $item_row['title'];
1361 * Active Topics feed
1363 * This will give you the last {$this->num_items} topics
1364 * with replies made withing the last {$this->sort_days} days
1365 * including the last post.
1369 class phpbb_feed_topics_active extends phpbb_feed_topic_base
1377 $this->set('author_id', 'topic_last_poster_id');
1378 $this->set('creator', 'topic_last_poster_name');
1383 global $db, $config;
1385 $forum_ids_read = $this->get_readable_forums();
1386 if (empty($forum_ids_read))
1391 $in_fid_ary = array_intersect($forum_ids_read, $this->get_forum_ids());
1392 $in_fid_ary = array_diff($in_fid_ary, $this->get_passworded_forums());
1393 if (empty($in_fid_ary))
1401 // Search for topics in last X days
1402 $last_post_time_sql = ($this->sort_days) ? ' AND topic_last_post_time > ' . (time() - ($this->sort_days * 24 * 3600)) : '';
1404 // We really have to get the post ids first!
1405 $sql = 'SELECT topic_last_post_id, topic_last_post_time
1406 FROM ' . TOPICS_TABLE . '
1407 WHERE ' . $db->sql_in_set('forum_id', $in_fid_ary) . '
1408 AND topic_moved_id = 0
1409 AND topic_approved = 1
1410 ' . $last_post_time_sql . '
1411 ORDER BY topic_last_post_time DESC';
1412 $result = $db->sql_query_limit($sql, $this->num_items);
1414 $post_ids = array();
1415 while ($row = $db->sql_fetchrow($result))
1417 $post_ids[] = (int) $row['topic_last_post_id'];
1419 $db->sql_freeresult($result);
1421 if (empty($post_ids))
1427 'SELECT' => 'f.forum_id, f.forum_name,
1428 t.topic_id, t.topic_title, t.topic_replies, t.topic_replies_real, t.topic_views,
1429 t.topic_last_poster_id, t.topic_last_poster_name, t.topic_last_post_time,
1430 p.post_id, p.post_time, p.post_edit_time, p.post_text, p.bbcode_bitfield, p.bbcode_uid, p.enable_bbcode, p.enable_smilies, p.enable_magic_url',
1432 TOPICS_TABLE => 't',
1435 'LEFT_JOIN' => array(
1437 'FROM' => array(FORUMS_TABLE => 'f'),
1438 'ON' => 'p.forum_id = f.forum_id',
1441 'WHERE' => 'p.topic_id = t.topic_id
1442 AND ' . $db->sql_in_set('p.post_id', $post_ids),
1443 'ORDER_BY' => 'p.post_time DESC',
1449 function get_forum_ids()
1454 $cache_name = 'feed_topic_active_forum_ids';
1456 if (!isset($forum_ids) && ($forum_ids = $cache->get('_' . $cache_name)) === false)
1458 $sql = 'SELECT forum_id
1459 FROM ' . FORUMS_TABLE . '
1460 WHERE forum_type = ' . FORUM_POST . '
1461 AND ' . $db->sql_bit_and('forum_options', FORUM_OPTION_FEED_EXCLUDE, '= 0') . '
1462 AND ' . $db->sql_bit_and('forum_flags', log(FORUM_FLAG_ACTIVE_TOPICS, 2), '<> 0');
1463 $result = $db->sql_query($sql);
1465 $forum_ids = array();
1466 while ($forum_id = (int) $db->sql_fetchfield('forum_id'))
1468 $forum_ids[$forum_id] = $forum_id;
1470 $db->sql_freeresult($result);
1472 $cache->put('_' . $cache_name, $forum_ids, 180);
1478 function adjust_item(&$item_row, &$row)
1480 parent::adjust_item($item_row, $row);
1482 $item_row['title'] = (isset($row['forum_name']) && $row['forum_name'] !== '') ? $row['forum_name'] . ' ' . $this->separator . ' ' . $item_row['title'] : $item_row['title'];