feed.php
changeset 0 ceef6d64c21d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/feed.php	Wed Mar 07 16:41:10 2012 +0400
     1.3 @@ -0,0 +1,1487 @@
     1.4 +<?php
     1.5 +/**
     1.6 +* @package phpBB3
     1.7 +* @version $Id$
     1.8 +* @copyright (c) 2009 phpBB Group
     1.9 +* @license http://opensource.org/licenses/gpl-license.php GNU Public License
    1.10 +*
    1.11 +* Idea and original RSS Feed 2.0 MOD (Version 1.0.8/9) by leviatan21
    1.12 +* Original MOD: http://www.phpbb.com/community/viewtopic.php?f=69&t=1214645
    1.13 +* MOD Author Profile: http://www.phpbb.com/community/memberlist.php?mode=viewprofile&u=345763
    1.14 +* MOD Author Homepage: http://www.mssti.com/phpbb3/
    1.15 +*
    1.16 +**/
    1.17 +
    1.18 +/**
    1.19 +* @ignore
    1.20 +**/
    1.21 +define('IN_PHPBB', true);
    1.22 +$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
    1.23 +$phpEx = substr(strrchr(__FILE__, '.'), 1);
    1.24 +include($phpbb_root_path . 'common.' . $phpEx);
    1.25 +
    1.26 +if (!$config['feed_enable'])
    1.27 +{
    1.28 +	trigger_error('NO_FEED_ENABLED');
    1.29 +}
    1.30 +
    1.31 +// Start session
    1.32 +$user->session_begin();
    1.33 +
    1.34 +if (!empty($config['feed_http_auth']) && request_var('auth', '') == 'http')
    1.35 +{
    1.36 +	phpbb_http_login(array(
    1.37 +		'auth_message'	=> 'Feed',
    1.38 +		'viewonline'	=> request_var('viewonline', true),
    1.39 +	));
    1.40 +}
    1.41 +
    1.42 +$auth->acl($user->data);
    1.43 +$user->setup();
    1.44 +
    1.45 +// Initial var setup
    1.46 +$forum_id	= request_var('f', 0);
    1.47 +$topic_id	= request_var('t', 0);
    1.48 +$mode		= request_var('mode', '');
    1.49 +
    1.50 +// We do not use a template, therefore we simply define the global template variables here
    1.51 +$global_vars = $item_vars = array();
    1.52 +$feed_updated_time = 0;
    1.53 +
    1.54 +// Generate params array for use in append_sid() to correctly link back to this page
    1.55 +$params = false;
    1.56 +if ($forum_id || $topic_id || $mode)
    1.57 +{
    1.58 +	$params = array(
    1.59 +		'f'		=> ($forum_id) ? $forum_id : NULL,
    1.60 +		't'		=> ($topic_id) ? $topic_id : NULL,
    1.61 +		'mode'	=> ($mode) ? $mode : NULL,
    1.62 +	);
    1.63 +}
    1.64 +
    1.65 +// This boards URL
    1.66 +$board_url = generate_board_url();
    1.67 +
    1.68 +// Get correct feed object
    1.69 +$feed = phpbb_feed_factory::init($mode, $forum_id, $topic_id);
    1.70 +
    1.71 +// No feed found
    1.72 +if ($feed === false)
    1.73 +{
    1.74 +	trigger_error('NO_FEED');
    1.75 +}
    1.76 +
    1.77 +// Open Feed
    1.78 +$feed->open();
    1.79 +
    1.80 +// Iterate through items
    1.81 +while ($row = $feed->get_item())
    1.82 +{
    1.83 +	// BBCode options to correctly disable urls, smilies, bbcode...
    1.84 +	if ($feed->get('options') === NULL)
    1.85 +	{
    1.86 +		// Allow all combinations
    1.87 +		$options = 7;
    1.88 +
    1.89 +		if ($feed->get('enable_bbcode') !== NULL && $feed->get('enable_smilies') !== NULL && $feed->get('enable_magic_url') !== NULL)
    1.90 +		{
    1.91 +			$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);
    1.92 +		}
    1.93 +	}
    1.94 +	else
    1.95 +	{
    1.96 +		$options = $row[$feed->get('options')];
    1.97 +	}
    1.98 +
    1.99 +	$title = (isset($row[$feed->get('title')]) && $row[$feed->get('title')] !== '') ? $row[$feed->get('title')] : ((isset($row[$feed->get('title2')])) ? $row[$feed->get('title2')] : '');
   1.100 +
   1.101 +	$published = ($feed->get('published') !== NULL) ? (int) $row[$feed->get('published')] : 0;
   1.102 +	$updated = ($feed->get('updated') !== NULL) ? (int) $row[$feed->get('updated')] : 0;
   1.103 +
   1.104 +	$item_row = array(
   1.105 +		'author'		=> ($feed->get('creator') !== NULL) ? $row[$feed->get('creator')] : '',
   1.106 +		'published'		=> ($published > 0) ? feed_format_date($published) : '',
   1.107 +		'updated'		=> ($updated > 0) ? feed_format_date($updated) : '',
   1.108 +		'link'			=> '',
   1.109 +		'title'			=> censor_text($title),
   1.110 +		'category'		=> ($config['feed_item_statistics'] && !empty($row['forum_id'])) ? $board_url . '/viewforum.' . $phpEx . '?f=' . $row['forum_id'] : '',
   1.111 +		'category_name'	=> ($config['feed_item_statistics'] && isset($row['forum_name'])) ? $row['forum_name'] : '',
   1.112 +		'description'	=> censor_text(feed_generate_content($row[$feed->get('text')], $row[$feed->get('bbcode_uid')], $row[$feed->get('bitfield')], $options)),
   1.113 +		'statistics'	=> '',
   1.114 +	);
   1.115 +
   1.116 +	// Adjust items, fill link, etc.
   1.117 +	$feed->adjust_item($item_row, $row);
   1.118 +
   1.119 +	$item_vars[] = $item_row;
   1.120 +
   1.121 +	$feed_updated_time = max($feed_updated_time, $published, $updated);
   1.122 +}
   1.123 +
   1.124 +// If we do not have any items at all, sending the current time is better than sending no time.
   1.125 +if (!$feed_updated_time)
   1.126 +{
   1.127 +	$feed_updated_time = time();
   1.128 +}
   1.129 +
   1.130 +// Some default assignments
   1.131 +// FEED_IMAGE is not used (atom)
   1.132 +$global_vars = array_merge($global_vars, array(
   1.133 +	'FEED_IMAGE'			=> ($user->img('site_logo', '', false, '', 'src')) ? $board_url . '/' . substr($user->img('site_logo', '', false, '', 'src'), strlen($phpbb_root_path)) : '',
   1.134 +	'SELF_LINK'				=> feed_append_sid('/feed.' . $phpEx, $params),
   1.135 +	'FEED_LINK'				=> $board_url . '/index.' . $phpEx,
   1.136 +	'FEED_TITLE'			=> $config['sitename'],
   1.137 +	'FEED_SUBTITLE'			=> $config['site_desc'],
   1.138 +	'FEED_UPDATED'			=> feed_format_date($feed_updated_time),
   1.139 +	'FEED_LANG'				=> $user->lang['USER_LANG'],
   1.140 +	'FEED_AUTHOR'			=> $config['sitename'],
   1.141 +));
   1.142 +
   1.143 +$feed->close();
   1.144 +
   1.145 +// Output page
   1.146 +
   1.147 +// gzip_compression
   1.148 +if ($config['gzip_compress'])
   1.149 +{
   1.150 +	if (@extension_loaded('zlib') && !headers_sent())
   1.151 +	{
   1.152 +		ob_start('ob_gzhandler');
   1.153 +	}
   1.154 +}
   1.155 +
   1.156 +// IF debug extra is enabled and admin want to "explain" the page we need to set other headers...
   1.157 +if (defined('DEBUG_EXTRA') && request_var('explain', 0) && $auth->acl_get('a_'))
   1.158 +{
   1.159 +	header('Content-type: text/html; charset=UTF-8');
   1.160 +	header('Cache-Control: private, no-cache="set-cookie"');
   1.161 +	header('Expires: 0');
   1.162 +	header('Pragma: no-cache');
   1.163 +
   1.164 +	$mtime = explode(' ', microtime());
   1.165 +	$totaltime = $mtime[0] + $mtime[1] - $starttime;
   1.166 +
   1.167 +	if (method_exists($db, 'sql_report'))
   1.168 +	{
   1.169 +		$db->sql_report('display');
   1.170 +	}
   1.171 +
   1.172 +	garbage_collection();
   1.173 +	exit_handler();
   1.174 +}
   1.175 +
   1.176 +header("Content-Type: application/atom+xml; charset=UTF-8");
   1.177 +header("Last-Modified: " . gmdate('D, d M Y H:i:s', $feed_updated_time) . ' GMT');
   1.178 +
   1.179 +if (!empty($user->data['is_bot']))
   1.180 +{
   1.181 +	// Let reverse proxies know we detected a bot.
   1.182 +	header('X-PHPBB-IS-BOT: yes');
   1.183 +}
   1.184 +
   1.185 +echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
   1.186 +echo '<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="' . $global_vars['FEED_LANG'] . '">' . "\n";
   1.187 +echo '<link rel="self" type="application/atom+xml" href="' . $global_vars['SELF_LINK'] . '" />' . "\n\n";
   1.188 +
   1.189 +echo (!empty($global_vars['FEED_TITLE'])) ? '<title>' . $global_vars['FEED_TITLE'] . '</title>' . "\n" : '';
   1.190 +echo (!empty($global_vars['FEED_SUBTITLE'])) ? '<subtitle>' . $global_vars['FEED_SUBTITLE'] . '</subtitle>' . "\n" : '';
   1.191 +echo (!empty($global_vars['FEED_LINK'])) ? '<link href="' . $global_vars['FEED_LINK'] .'" />' . "\n" : '';
   1.192 +echo '<updated>' . $global_vars['FEED_UPDATED'] . '</updated>' . "\n\n";
   1.193 +
   1.194 +echo '<author><name><![CDATA[' . $global_vars['FEED_AUTHOR'] . ']]></name></author>' . "\n";
   1.195 +echo '<id>' . $global_vars['SELF_LINK'] . '</id>' . "\n";
   1.196 +
   1.197 +foreach ($item_vars as $row)
   1.198 +{
   1.199 +	echo '<entry>' . "\n";
   1.200 +
   1.201 +	if (!empty($row['author']))
   1.202 +	{
   1.203 +		echo '<author><name><![CDATA[' . $row['author'] . ']]></name></author>' . "\n";
   1.204 +	}
   1.205 +
   1.206 +	echo '<updated>' . ((!empty($row['updated'])) ? $row['updated'] : $row['published']) . '</updated>' . "\n";
   1.207 +
   1.208 +	if (!empty($row['published']))
   1.209 +	{
   1.210 +		echo '<published>' . $row['published'] . '</published>' . "\n";
   1.211 +	}
   1.212 +
   1.213 +	echo '<id>' . $row['link'] . '</id>' . "\n";
   1.214 +	echo '<link href="' . $row['link'] . '"/>' . "\n";
   1.215 +	echo '<title type="html"><![CDATA[' . $row['title'] . ']]></title>' . "\n\n";
   1.216 +
   1.217 +	if (!empty($row['category']) && isset($row['category_name']) && $row['category_name'] !== '')
   1.218 +	{
   1.219 +		echo '<category term="' . $row['category_name'] . '" scheme="' . $row['category'] . '" label="' . $row['category_name'] . '"/>' . "\n";
   1.220 +	}
   1.221 +
   1.222 +	echo '<content type="html" xml:base="' . $row['link'] . '"><![CDATA[' . "\n";
   1.223 +	echo $row['description'];
   1.224 +
   1.225 +	if (!empty($row['statistics']))
   1.226 +	{
   1.227 +		echo '<p>' . $user->lang['STATISTICS'] . ': ' . $row['statistics'] . '</p>';
   1.228 +	}
   1.229 +
   1.230 +	echo '<hr />' . "\n" . ']]></content>' . "\n";
   1.231 +	echo '</entry>' . "\n";
   1.232 +}
   1.233 +
   1.234 +echo '</feed>';
   1.235 +
   1.236 +garbage_collection();
   1.237 +exit_handler();
   1.238 +
   1.239 +/**
   1.240 +* Run links through append_sid(), prepend generate_board_url() and remove session id
   1.241 +**/
   1.242 +function feed_append_sid($url, $params)
   1.243 +{
   1.244 +	global $board_url;
   1.245 +
   1.246 +	return append_sid($board_url . $url, $params, true, '');
   1.247 +}
   1.248 +
   1.249 +/**
   1.250 +* Generate ISO 8601 date string (RFC 3339)
   1.251 +**/
   1.252 +function feed_format_date($time)
   1.253 +{
   1.254 +	static $zone_offset;
   1.255 +	static $offset_string;
   1.256 +
   1.257 +	if (empty($offset_string))
   1.258 +	{
   1.259 +		global $user;
   1.260 +
   1.261 +		$zone_offset = (int) $user->timezone + (int) $user->dst;
   1.262 +
   1.263 +		$sign = ($zone_offset < 0) ? '-' : '+';
   1.264 +		$time_offset = abs($zone_offset);
   1.265 +
   1.266 +		$offset_seconds	= $time_offset % 3600;
   1.267 +		$offset_minutes	= $offset_seconds / 60;
   1.268 +		$offset_hours	= ($time_offset - $offset_seconds) / 3600;
   1.269 +
   1.270 +		$offset_string	= sprintf("%s%02d:%02d", $sign, $offset_hours, $offset_minutes);
   1.271 +	}
   1.272 +
   1.273 +	return gmdate("Y-m-d\TH:i:s", $time + $zone_offset) . $offset_string;
   1.274 +}
   1.275 +
   1.276 +/**
   1.277 +* Generate text content
   1.278 +**/
   1.279 +function feed_generate_content($content, $uid, $bitfield, $options)
   1.280 +{
   1.281 +	global $user, $config, $phpbb_root_path, $phpEx, $board_url;
   1.282 +
   1.283 +	if (empty($content))
   1.284 +	{
   1.285 +		return '';
   1.286 +	}
   1.287 +
   1.288 +	// Prepare some bbcodes for better parsing
   1.289 +	$content	= preg_replace("#\[quote(=&quot;.*?&quot;)?:$uid\]\s*(.*?)\s*\[/quote:$uid\]#si", "[quote$1:$uid]<br />$2<br />[/quote:$uid]", $content);
   1.290 +
   1.291 +	$content = generate_text_for_display($content, $uid, $bitfield, $options);
   1.292 +
   1.293 +	// Add newlines
   1.294 +	$content = str_replace('<br />', '<br />' . "\n", $content);
   1.295 +
   1.296 +	// Convert smiley Relative paths to Absolute path, Windows style
   1.297 +	$content = str_replace($phpbb_root_path . $config['smilies_path'], $board_url . '/' . $config['smilies_path'], $content);
   1.298 +
   1.299 +	// Remove "Select all" link and mouse events
   1.300 +	$content = str_replace('<a href="#" onclick="selectCode(this); return false;">' . $user->lang['SELECT_ALL_CODE'] . '</a>', '', $content);
   1.301 +	$content = preg_replace('#(onkeypress|onclick)="(.*?)"#si', '', $content);
   1.302 +
   1.303 +	// Firefox does not support CSS for feeds, though
   1.304 +
   1.305 +	// Remove font sizes
   1.306 +//	$content = preg_replace('#<span style="font-size: [0-9]+%; line-height: [0-9]+%;">([^>]+)</span>#iU', '\1', $content);
   1.307 +
   1.308 +	// Make text strong :P
   1.309 +//	$content = preg_replace('#<span style="font-weight: bold?">(.*?)</span>#iU', '<strong>\1</strong>', $content);
   1.310 +
   1.311 +	// Italic
   1.312 +//	$content = preg_replace('#<span style="font-style: italic?">([^<]+)</span>#iU', '<em>\1</em>', $content);
   1.313 +
   1.314 +	// Underline
   1.315 +//	$content = preg_replace('#<span style="text-decoration: underline?">([^<]+)</span>#iU', '<u>\1</u>', $content);
   1.316 +
   1.317 +	// Remove embed Windows Media Streams
   1.318 +	$content	= preg_replace( '#<\!--\[if \!IE\]>-->([^[]+)<\!--<!\[endif\]-->#si', '', $content);
   1.319 +
   1.320 +	// Do not use &lt; and &gt;, because we want to retain code contained in [code][/code]
   1.321 +
   1.322 +	// Remove embed and objects
   1.323 +	$content	= preg_replace( '#<(object|embed)(.*?) (value|src)=(.*?) ([^[]+)(object|embed)>#si',' <a href=$4 target="_blank"><strong>$1</strong></a> ',$content);
   1.324 +
   1.325 +	// Remove some specials html tag, because somewhere there are a mod to allow html tags ;)
   1.326 +	$content	= preg_replace( '#<(script|iframe)([^[]+)\1>#siU', ' <strong>$1</strong> ', $content);
   1.327 +
   1.328 +	// Remove Comments from inline attachments [ia]
   1.329 +	$content	= preg_replace('#<div class="(inline-attachment|attachtitle)">(.*?)<!-- ia(.*?) -->(.*?)<!-- ia(.*?) -->(.*?)</div>#si','$4',$content);
   1.330 +
   1.331 +	// Replace some entities with their unicode counterpart
   1.332 +	$entities = array(
   1.333 +		'&nbsp;'	=> "\xC2\xA0",
   1.334 +		'&bull;'	=> "\xE2\x80\xA2",
   1.335 +		'&middot;'	=> "\xC2\xB7",
   1.336 +		'&copy;'	=> "\xC2\xA9",
   1.337 +	);
   1.338 +
   1.339 +	$content = str_replace(array_keys($entities), array_values($entities), $content);
   1.340 +
   1.341 +	// Remove CDATA blocks. ;)
   1.342 +	$content = preg_replace('#\<\!\[CDATA\[(.*?)\]\]\>#s', '', $content);
   1.343 +
   1.344 +	// Other control characters
   1.345 +	$content = preg_replace('#(?:[\x00-\x1F\x7F]+|(?:\xC2[\x80-\x9F])+)#', '', $content);
   1.346 +
   1.347 +	return $content;
   1.348 +}
   1.349 +
   1.350 +/**
   1.351 +* Factory class to return correct object
   1.352 +* @package phpBB3
   1.353 +*/
   1.354 +class phpbb_feed_factory
   1.355 +{
   1.356 +	/**
   1.357 +	* Return correct object for specified mode
   1.358 +	*
   1.359 +	* @param string	$mode		The feeds mode.
   1.360 +	* @param int	$forum_id	Forum id specified by the script if forum feed provided.
   1.361 +	* @param int	$topic_id	Topic id specified by the script if topic feed provided.
   1.362 +	*
   1.363 +	* @return object	Returns correct feeds object for specified mode.
   1.364 +	*/
   1.365 +	function init($mode, $forum_id, $topic_id)
   1.366 +	{
   1.367 +		global $config;
   1.368 +
   1.369 +		switch ($mode)
   1.370 +		{
   1.371 +			case 'forums':
   1.372 +				if (!$config['feed_overall_forums'])
   1.373 +				{
   1.374 +					return false;
   1.375 +				}
   1.376 +
   1.377 +				return new phpbb_feed_forums();
   1.378 +			break;
   1.379 +
   1.380 +			case 'topics':
   1.381 +			case 'topics_new':
   1.382 +				if (!$config['feed_topics_new'])
   1.383 +				{
   1.384 +					return false;
   1.385 +				}
   1.386 +
   1.387 +				return new phpbb_feed_topics();
   1.388 +			break;
   1.389 +
   1.390 +			case 'topics_active':
   1.391 +				if (!$config['feed_topics_active'])
   1.392 +				{
   1.393 +					return false;
   1.394 +				}
   1.395 +
   1.396 +				return new phpbb_feed_topics_active();
   1.397 +			break;
   1.398 +
   1.399 +			case 'news':
   1.400 +				global $db;
   1.401 +
   1.402 +				// Get at least one news forum
   1.403 +				$sql = 'SELECT forum_id
   1.404 +					FROM ' . FORUMS_TABLE . '
   1.405 +					WHERE ' . $db->sql_bit_and('forum_options', FORUM_OPTION_FEED_NEWS, '<> 0');
   1.406 +				$result = $db->sql_query_limit($sql, 1, 0, 600);
   1.407 +				$s_feed_news = (int) $db->sql_fetchfield('forum_id');
   1.408 +				$db->sql_freeresult($result);
   1.409 +
   1.410 +				if (!$s_feed_news)
   1.411 +				{
   1.412 +					return false;
   1.413 +				}
   1.414 +
   1.415 +				return new phpbb_feed_news();
   1.416 +			break;
   1.417 +
   1.418 +			default:
   1.419 +				if ($topic_id && $config['feed_topic'])
   1.420 +				{
   1.421 +					return new phpbb_feed_topic($topic_id);
   1.422 +				}
   1.423 +				else if ($forum_id && $config['feed_forum'])
   1.424 +				{
   1.425 +					return new phpbb_feed_forum($forum_id);
   1.426 +				}
   1.427 +				else if ($config['feed_overall'])
   1.428 +				{
   1.429 +					return new phpbb_feed_overall();
   1.430 +				}
   1.431 +
   1.432 +				return false;
   1.433 +			break;
   1.434 +		}
   1.435 +	}
   1.436 +}
   1.437 +
   1.438 +/**
   1.439 +* Base class with some generic functions and settings.
   1.440 +*
   1.441 +* @package phpBB3
   1.442 +*/
   1.443 +class phpbb_feed_base
   1.444 +{
   1.445 +	/**
   1.446 +	* SQL Query to be executed to get feed items
   1.447 +	*/
   1.448 +	var $sql = array();
   1.449 +
   1.450 +	/**
   1.451 +	* Keys specified for retrieval of title, content, etc.
   1.452 +	*/
   1.453 +	var $keys = array();
   1.454 +
   1.455 +	/**
   1.456 +	* Number of items to fetch. Usually overwritten by $config['feed_something']
   1.457 +	*/
   1.458 +	var $num_items = 15;
   1.459 +
   1.460 +	/**
   1.461 +	* Separator for title elements to separate items (for example forum / topic)
   1.462 +	*/
   1.463 +	var $separator = "\xE2\x80\xA2"; // &bull;
   1.464 +
   1.465 +	/**
   1.466 +	* Separator for the statistics row (Posted by, post date, replies, etc.)
   1.467 +	*/
   1.468 +	var $separator_stats = "\xE2\x80\x94"; // &mdash;
   1.469 +
   1.470 +	/**
   1.471 +	* Constructor
   1.472 +	*/
   1.473 +	function phpbb_feed_base()
   1.474 +	{
   1.475 +		global $config;
   1.476 +
   1.477 +		$this->set_keys();
   1.478 +
   1.479 +		// Allow num_items to be string
   1.480 +		if (is_string($this->num_items))
   1.481 +		{
   1.482 +			$this->num_items = (int) $config[$this->num_items];
   1.483 +
   1.484 +			// A precaution
   1.485 +			if (!$this->num_items)
   1.486 +			{
   1.487 +				$this->num_items = 10;
   1.488 +			}
   1.489 +		}
   1.490 +	}
   1.491 +
   1.492 +	/**
   1.493 +	* Set keys.
   1.494 +	*/
   1.495 +	function set_keys()
   1.496 +	{
   1.497 +	}
   1.498 +
   1.499 +	/**
   1.500 +	* Open feed
   1.501 +	*/
   1.502 +	function open()
   1.503 +	{
   1.504 +	}
   1.505 +
   1.506 +	/**
   1.507 +	* Close feed
   1.508 +	*/
   1.509 +	function close()
   1.510 +	{
   1.511 +		global $db;
   1.512 +
   1.513 +		if (!empty($this->result))
   1.514 +		{
   1.515 +			$db->sql_freeresult($this->result);
   1.516 +		}
   1.517 +	}
   1.518 +
   1.519 +	/**
   1.520 +	* Set key
   1.521 +	*/
   1.522 +	function set($key, $value)
   1.523 +	{
   1.524 +		$this->keys[$key] = $value;
   1.525 +	}
   1.526 +
   1.527 +	/**
   1.528 +	* Get key
   1.529 +	*/
   1.530 +	function get($key)
   1.531 +	{
   1.532 +		return (isset($this->keys[$key])) ? $this->keys[$key] : NULL;
   1.533 +	}
   1.534 +
   1.535 +	function get_readable_forums()
   1.536 +	{
   1.537 +		global $auth;
   1.538 +		static $forum_ids;
   1.539 +
   1.540 +		if (!isset($forum_ids))
   1.541 +		{
   1.542 +			$forum_ids = array_keys($auth->acl_getf('f_read', true));
   1.543 +		}
   1.544 +
   1.545 +		return $forum_ids;
   1.546 +	}
   1.547 +
   1.548 +	function get_moderator_approve_forums()
   1.549 +	{
   1.550 +		global $auth;
   1.551 +		static $forum_ids;
   1.552 +
   1.553 +		if (!isset($forum_ids))
   1.554 +		{
   1.555 +			$forum_ids = array_keys($auth->acl_getf('m_approve', true));
   1.556 +		}
   1.557 +
   1.558 +		return $forum_ids;
   1.559 +	}
   1.560 +
   1.561 +	function is_moderator_approve_forum($forum_id)
   1.562 +	{
   1.563 +		static $forum_ids;
   1.564 +
   1.565 +		if (!isset($forum_ids))
   1.566 +		{
   1.567 +			$forum_ids = array_flip($this->get_moderator_approve_forums());
   1.568 +		}
   1.569 +
   1.570 +		if (!$forum_id)
   1.571 +		{
   1.572 +			// Global announcement, your a moderator in any forum than it's okay.
   1.573 +			return (!empty($forum_ids)) ? true : false;
   1.574 +		}
   1.575 +
   1.576 +		return (isset($forum_ids[$forum_id])) ? true : false;
   1.577 +	}
   1.578 +
   1.579 +	function get_excluded_forums()
   1.580 +	{
   1.581 +		global $db, $cache;
   1.582 +		static $forum_ids;
   1.583 +
   1.584 +		// Matches acp/acp_board.php
   1.585 +		$cache_name	= 'feed_excluded_forum_ids';
   1.586 +
   1.587 +		if (!isset($forum_ids) && ($forum_ids = $cache->get('_' . $cache_name)) === false)
   1.588 +		{
   1.589 +			$sql = 'SELECT forum_id
   1.590 +				FROM ' . FORUMS_TABLE . '
   1.591 +				WHERE ' . $db->sql_bit_and('forum_options', FORUM_OPTION_FEED_EXCLUDE, '<> 0');
   1.592 +			$result = $db->sql_query($sql);
   1.593 +
   1.594 +			$forum_ids = array();
   1.595 +			while ($forum_id = (int) $db->sql_fetchfield('forum_id'))
   1.596 +			{
   1.597 +				$forum_ids[$forum_id] = $forum_id;
   1.598 +			}
   1.599 +			$db->sql_freeresult($result);
   1.600 +
   1.601 +			$cache->put('_' . $cache_name, $forum_ids);
   1.602 +		}
   1.603 +
   1.604 +		return $forum_ids;
   1.605 +	}
   1.606 +
   1.607 +	function is_excluded_forum($forum_id)
   1.608 +	{
   1.609 +		$forum_ids = $this->get_excluded_forums();
   1.610 +
   1.611 +		return isset($forum_ids[$forum_id]) ? true : false;
   1.612 +	}
   1.613 +
   1.614 +	function get_passworded_forums()
   1.615 +	{
   1.616 +		global $user;
   1.617 +
   1.618 +		return $user->get_passworded_forums();
   1.619 +	}
   1.620 +
   1.621 +	function get_item()
   1.622 +	{
   1.623 +		global $db, $cache;
   1.624 +		static $result;
   1.625 +
   1.626 +		if (!isset($result))
   1.627 +		{
   1.628 +			if (!$this->get_sql())
   1.629 +			{
   1.630 +				return false;
   1.631 +			}
   1.632 +
   1.633 +			// Query database
   1.634 +			$sql = $db->sql_build_query('SELECT', $this->sql);
   1.635 +			$result = $db->sql_query_limit($sql, $this->num_items);
   1.636 +		}
   1.637 +
   1.638 +		return $db->sql_fetchrow($result);
   1.639 +	}
   1.640 +
   1.641 +	function user_viewprofile($row)
   1.642 +	{
   1.643 +		global $phpEx, $user;
   1.644 +
   1.645 +		$author_id = (int) $row[$this->get('author_id')];
   1.646 +
   1.647 +		if ($author_id == ANONYMOUS)
   1.648 +		{
   1.649 +			// Since we cannot link to a profile, we just return GUEST
   1.650 +			// instead of $row['username']
   1.651 +			return $user->lang['GUEST'];
   1.652 +		}
   1.653 +
   1.654 +		return '<a href="' . feed_append_sid('/memberlist.' . $phpEx, 'mode=viewprofile&amp;u=' . $author_id) . '">' . $row[$this->get('creator')] . '</a>';
   1.655 +	}
   1.656 +}
   1.657 +
   1.658 +/**
   1.659 +* Abstract class for post based feeds
   1.660 +*
   1.661 +* @package phpBB3
   1.662 +*/
   1.663 +class phpbb_feed_post_base extends phpbb_feed_base
   1.664 +{
   1.665 +	var $num_items = 'feed_limit_post';
   1.666 +
   1.667 +	function set_keys()
   1.668 +	{
   1.669 +		$this->set('title',		'post_subject');
   1.670 +		$this->set('title2',	'topic_title');
   1.671 +
   1.672 +		$this->set('author_id',	'user_id');
   1.673 +		$this->set('creator',	'username');
   1.674 +		$this->set('published',	'post_time');
   1.675 +		$this->set('updated',	'post_edit_time');
   1.676 +		$this->set('text',		'post_text');
   1.677 +
   1.678 +		$this->set('bitfield',	'bbcode_bitfield');
   1.679 +		$this->set('bbcode_uid','bbcode_uid');
   1.680 +
   1.681 +		$this->set('enable_bbcode',		'enable_bbcode');
   1.682 +		$this->set('enable_smilies',	'enable_smilies');
   1.683 +		$this->set('enable_magic_url',	'enable_magic_url');
   1.684 +	}
   1.685 +
   1.686 +	function adjust_item(&$item_row, &$row)
   1.687 +	{
   1.688 +		global $phpEx, $config, $user;
   1.689 +
   1.690 +		$item_row['link'] = feed_append_sid('/viewtopic.' . $phpEx, "t={$row['topic_id']}&amp;p={$row['post_id']}#p{$row['post_id']}");
   1.691 +
   1.692 +		if ($config['feed_item_statistics'])
   1.693 +		{
   1.694 +			$item_row['statistics'] = $user->lang['POSTED'] . ' ' . $user->lang['POST_BY_AUTHOR'] . ' ' . $this->user_viewprofile($row)
   1.695 +				. ' ' . $this->separator_stats . ' ' . $user->format_date($row[$this->get('published')])
   1.696 +				. (($this->is_moderator_approve_forum($row['forum_id']) && !$row['post_approved']) ? ' ' . $this->separator_stats . ' ' . $user->lang['POST_UNAPPROVED'] : '');
   1.697 +		}
   1.698 +	}
   1.699 +}
   1.700 +
   1.701 +/**
   1.702 +* Abstract class for topic based feeds
   1.703 +*
   1.704 +* @package phpBB3
   1.705 +*/
   1.706 +class phpbb_feed_topic_base extends phpbb_feed_base
   1.707 +{
   1.708 +	var $num_items = 'feed_limit_topic';
   1.709 +
   1.710 +	function set_keys()
   1.711 +	{
   1.712 +		$this->set('title',		'topic_title');
   1.713 +		$this->set('title2',	'forum_name');
   1.714 +
   1.715 +		$this->set('author_id',	'topic_poster');
   1.716 +		$this->set('creator',	'topic_first_poster_name');
   1.717 +		$this->set('published',	'post_time');
   1.718 +		$this->set('updated',	'post_edit_time');
   1.719 +		$this->set('text',		'post_text');
   1.720 +
   1.721 +		$this->set('bitfield',	'bbcode_bitfield');
   1.722 +		$this->set('bbcode_uid','bbcode_uid');
   1.723 +
   1.724 +		$this->set('enable_bbcode',		'enable_bbcode');
   1.725 +		$this->set('enable_smilies',	'enable_smilies');
   1.726 +		$this->set('enable_magic_url',	'enable_magic_url');
   1.727 +	}
   1.728 +
   1.729 +	function adjust_item(&$item_row, &$row)
   1.730 +	{
   1.731 +		global $phpEx, $config, $user;
   1.732 +
   1.733 +		$item_row['link'] = feed_append_sid('/viewtopic.' . $phpEx, 't=' . $row['topic_id'] . '&amp;p=' . $row['post_id'] . '#p' . $row['post_id']);
   1.734 +
   1.735 +		if ($config['feed_item_statistics'])
   1.736 +		{
   1.737 +			$item_row['statistics'] = $user->lang['POSTED'] . ' ' . $user->lang['POST_BY_AUTHOR'] . ' ' . $this->user_viewprofile($row)
   1.738 +				. ' ' . $this->separator_stats . ' ' . $user->format_date($row[$this->get('published')])
   1.739 +				. ' ' . $this->separator_stats . ' ' . $user->lang['REPLIES'] . ' ' . (($this->is_moderator_approve_forum($row['forum_id'])) ? $row['topic_replies_real'] : $row['topic_replies'])
   1.740 +				. ' ' . $this->separator_stats . ' ' . $user->lang['VIEWS'] . ' ' . $row['topic_views']
   1.741 +				. (($this->is_moderator_approve_forum($row['forum_id']) && ($row['topic_replies_real'] != $row['topic_replies'])) ? ' ' . $this->separator_stats . ' ' . $user->lang['POSTS_UNAPPROVED'] : '');
   1.742 +		}
   1.743 +	}
   1.744 +}
   1.745 +
   1.746 +/**
   1.747 +* Board wide feed (aka overall feed)
   1.748 +*
   1.749 +* This will give you the newest {$this->num_items} posts
   1.750 +* from the whole board.
   1.751 +*
   1.752 +* @package phpBB3
   1.753 +*/
   1.754 +class phpbb_feed_overall extends phpbb_feed_post_base
   1.755 +{
   1.756 +	function get_sql()
   1.757 +	{
   1.758 +		global $auth, $db;
   1.759 +
   1.760 +		$forum_ids = array_diff($this->get_readable_forums(), $this->get_excluded_forums(), $this->get_passworded_forums());
   1.761 +		if (empty($forum_ids))
   1.762 +		{
   1.763 +			return false;
   1.764 +		}
   1.765 +
   1.766 +		// Add global forum id
   1.767 +		$forum_ids[] = 0;
   1.768 +
   1.769 +		// m_approve forums
   1.770 +		$fid_m_approve = $this->get_moderator_approve_forums();
   1.771 +		$sql_m_approve = (!empty($fid_m_approve)) ? 'OR ' . $db->sql_in_set('forum_id', $fid_m_approve) : '';
   1.772 +
   1.773 +		// Determine topics with recent activity
   1.774 +		$sql = 'SELECT topic_id, topic_last_post_time
   1.775 +			FROM ' . TOPICS_TABLE . '
   1.776 +			WHERE ' . $db->sql_in_set('forum_id', $forum_ids) . '
   1.777 +				AND topic_moved_id = 0
   1.778 +				AND (topic_approved = 1
   1.779 +					' . $sql_m_approve . ')
   1.780 +			ORDER BY topic_last_post_time DESC';
   1.781 +		$result = $db->sql_query_limit($sql, $this->num_items);
   1.782 +
   1.783 +		$topic_ids = array();
   1.784 +		$min_post_time = 0;
   1.785 +		while ($row = $db->sql_fetchrow())
   1.786 +		{
   1.787 +			$topic_ids[] = (int) $row['topic_id'];
   1.788 +
   1.789 +			$min_post_time = (int) $row['topic_last_post_time'];
   1.790 +		}
   1.791 +		$db->sql_freeresult($result);
   1.792 +
   1.793 +		if (empty($topic_ids))
   1.794 +		{
   1.795 +			return false;
   1.796 +		}
   1.797 +
   1.798 +		// Get the actual data
   1.799 +		$this->sql = array(
   1.800 +			'SELECT'	=>	'f.forum_id, f.forum_name, ' .
   1.801 +							'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, ' .
   1.802 +							'u.username, u.user_id',
   1.803 +			'FROM'		=> array(
   1.804 +				USERS_TABLE		=> 'u',
   1.805 +				POSTS_TABLE		=> 'p',
   1.806 +			),
   1.807 +			'LEFT_JOIN'	=> array(
   1.808 +				array(
   1.809 +					'FROM'	=> array(FORUMS_TABLE	=> 'f'),
   1.810 +					'ON'	=> 'f.forum_id = p.forum_id',
   1.811 +				),
   1.812 +			),
   1.813 +			'WHERE'		=> $db->sql_in_set('p.topic_id', $topic_ids) . '
   1.814 +							AND (p.post_approved = 1
   1.815 +								' . str_replace('forum_id', 'p.forum_id', $sql_m_approve) . ')
   1.816 +							AND p.post_time >= ' . $min_post_time . '
   1.817 +							AND u.user_id = p.poster_id',
   1.818 +			'ORDER_BY'	=> 'p.post_time DESC',
   1.819 +		);
   1.820 +
   1.821 +		return true;
   1.822 +	}
   1.823 +
   1.824 +	function adjust_item(&$item_row, &$row)
   1.825 +	{
   1.826 +		parent::adjust_item($item_row, $row);
   1.827 +
   1.828 +		$item_row['title'] = (isset($row['forum_name']) && $row['forum_name'] !== '') ? $row['forum_name'] . ' ' . $this->separator . ' ' . $item_row['title'] : $item_row['title'];
   1.829 +	}
   1.830 +}
   1.831 +
   1.832 +/**
   1.833 +* Forum feed
   1.834 +*
   1.835 +* This will give you the last {$this->num_items} posts made
   1.836 +* within a specific forum.
   1.837 +*
   1.838 +* @package phpBB3
   1.839 +*/
   1.840 +class phpbb_feed_forum extends phpbb_feed_post_base
   1.841 +{
   1.842 +	var $forum_id		= 0;
   1.843 +	var $forum_data		= array();
   1.844 +
   1.845 +	function phpbb_feed_forum($forum_id)
   1.846 +	{
   1.847 +		parent::phpbb_feed_base();
   1.848 +
   1.849 +		$this->forum_id = (int) $forum_id;
   1.850 +	}
   1.851 +
   1.852 +	function open()
   1.853 +	{
   1.854 +		global $db, $auth;
   1.855 +
   1.856 +		// Check if forum exists
   1.857 +		$sql = 'SELECT forum_id, forum_name, forum_password, forum_type, forum_options
   1.858 +			FROM ' . FORUMS_TABLE . '
   1.859 +			WHERE forum_id = ' . $this->forum_id;
   1.860 +		$result = $db->sql_query($sql);
   1.861 +		$this->forum_data = $db->sql_fetchrow($result);
   1.862 +		$db->sql_freeresult($result);
   1.863 +
   1.864 +		if (empty($this->forum_data))
   1.865 +		{
   1.866 +			trigger_error('NO_FORUM');
   1.867 +		}
   1.868 +
   1.869 +		// Forum needs to be postable
   1.870 +		if ($this->forum_data['forum_type'] != FORUM_POST)
   1.871 +		{
   1.872 +			trigger_error('NO_FEED');
   1.873 +		}
   1.874 +
   1.875 +		// Make sure forum is not excluded from feed
   1.876 +		if (phpbb_optionget(FORUM_OPTION_FEED_EXCLUDE, $this->forum_data['forum_options']))
   1.877 +		{
   1.878 +			trigger_error('NO_FEED');
   1.879 +		}
   1.880 +
   1.881 +		// Make sure we can read this forum
   1.882 +		if (!$auth->acl_get('f_read', $this->forum_id))
   1.883 +		{
   1.884 +			trigger_error('SORRY_AUTH_READ');
   1.885 +		}
   1.886 +
   1.887 +		// Make sure forum is not passworded or user is authed
   1.888 +		if ($this->forum_data['forum_password'])
   1.889 +		{
   1.890 +			$forum_ids_passworded = $this->get_passworded_forums();
   1.891 +
   1.892 +			if (isset($forum_ids_passworded[$this->forum_id]))
   1.893 +			{
   1.894 +				trigger_error('SORRY_AUTH_READ');
   1.895 +			}
   1.896 +
   1.897 +			unset($forum_ids_passworded);
   1.898 +		}
   1.899 +	}
   1.900 +
   1.901 +	function get_sql()
   1.902 +	{
   1.903 +		global $auth, $db;
   1.904 +
   1.905 +		$m_approve = ($auth->acl_get('m_approve', $this->forum_id)) ? true : false;
   1.906 +		$forum_ids = array(0, $this->forum_id);
   1.907 +
   1.908 +		// Determine topics with recent activity
   1.909 +		$sql = 'SELECT topic_id, topic_last_post_time
   1.910 +			FROM ' . TOPICS_TABLE . '
   1.911 +			WHERE ' . $db->sql_in_set('forum_id', $forum_ids) . '
   1.912 +				AND topic_moved_id = 0
   1.913 +				' . ((!$m_approve) ? 'AND topic_approved = 1' : '') . '
   1.914 +			ORDER BY topic_last_post_time DESC';
   1.915 +		$result = $db->sql_query_limit($sql, $this->num_items);
   1.916 +
   1.917 +		$topic_ids = array();
   1.918 +		$min_post_time = 0;
   1.919 +		while ($row = $db->sql_fetchrow())
   1.920 +		{
   1.921 +			$topic_ids[] = (int) $row['topic_id'];
   1.922 +
   1.923 +			$min_post_time = (int) $row['topic_last_post_time'];
   1.924 +		}
   1.925 +		$db->sql_freeresult($result);
   1.926 +
   1.927 +		if (empty($topic_ids))
   1.928 +		{
   1.929 +			return false;
   1.930 +		}
   1.931 +
   1.932 +		$this->sql = array(
   1.933 +			'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, ' .
   1.934 +							'u.username, u.user_id',
   1.935 +			'FROM'		=> array(
   1.936 +				POSTS_TABLE		=> 'p',
   1.937 +				USERS_TABLE		=> 'u',
   1.938 +			),
   1.939 +			'WHERE'		=> $db->sql_in_set('p.topic_id', $topic_ids) . '
   1.940 +							' . ((!$m_approve) ? 'AND p.post_approved = 1' : '') . '
   1.941 +							AND p.post_time >= ' . $min_post_time . '
   1.942 +							AND p.poster_id = u.user_id',
   1.943 +			'ORDER_BY'	=> 'p.post_time DESC',
   1.944 +		);
   1.945 +
   1.946 +		return true;
   1.947 +	}
   1.948 +
   1.949 +	function adjust_item(&$item_row, &$row)
   1.950 +	{
   1.951 +		parent::adjust_item($item_row, $row);
   1.952 +
   1.953 +		$item_row['title'] = (isset($row['forum_name']) && $row['forum_name'] !== '') ? $row['forum_name'] . ' ' . $this->separator . ' ' . $item_row['title'] : $item_row['title'];
   1.954 +	}
   1.955 +
   1.956 +	function get_item()
   1.957 +	{
   1.958 +		return ($row = parent::get_item()) ? array_merge($this->forum_data, $row) : $row;
   1.959 +	}
   1.960 +}
   1.961 +
   1.962 +/**
   1.963 +* Topic feed for a specific topic
   1.964 +*
   1.965 +* This will give you the last {$this->num_items} posts made within this topic.
   1.966 +*
   1.967 +* @package phpBB3
   1.968 +*/
   1.969 +class phpbb_feed_topic extends phpbb_feed_post_base
   1.970 +{
   1.971 +	var $topic_id		= 0;
   1.972 +	var $forum_id		= 0;
   1.973 +	var $topic_data		= array();
   1.974 +
   1.975 +	function phpbb_feed_topic($topic_id)
   1.976 +	{
   1.977 +		parent::phpbb_feed_base();
   1.978 +
   1.979 +		$this->topic_id = (int) $topic_id;
   1.980 +	}
   1.981 +
   1.982 +	function open()
   1.983 +	{
   1.984 +		global $auth, $db, $user;
   1.985 +
   1.986 +		$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
   1.987 +			FROM ' . TOPICS_TABLE . ' t
   1.988 +			LEFT JOIN ' . FORUMS_TABLE . ' f
   1.989 +				ON (f.forum_id = t.forum_id)
   1.990 +			WHERE t.topic_id = ' . $this->topic_id;
   1.991 +		$result = $db->sql_query($sql);
   1.992 +		$this->topic_data = $db->sql_fetchrow($result);
   1.993 +		$db->sql_freeresult($result);
   1.994 +
   1.995 +		if (empty($this->topic_data))
   1.996 +		{
   1.997 +			trigger_error('NO_TOPIC');
   1.998 +		}
   1.999 +
  1.1000 +		if ($this->topic_data['topic_type'] == POST_GLOBAL)
  1.1001 +		{
  1.1002 +			// We need to find at least one postable forum where feeds are enabled,
  1.1003 +			// that the user can read and maybe also has approve permissions.
  1.1004 +			$in_fid_ary = $this->get_readable_forums();
  1.1005 +
  1.1006 +			if (empty($in_fid_ary))
  1.1007 +			{
  1.1008 +				// User cannot read any forums
  1.1009 +				trigger_error('SORRY_AUTH_READ');
  1.1010 +			}
  1.1011 +
  1.1012 +			if (!$this->topic_data['topic_approved'])
  1.1013 +			{
  1.1014 +				// Also require m_approve
  1.1015 +				$in_fid_ary = array_intersect($in_fid_ary, $this->get_moderator_approve_forums());
  1.1016 +
  1.1017 +				if (empty($in_fid_ary))
  1.1018 +				{
  1.1019 +					trigger_error('SORRY_AUTH_READ');
  1.1020 +				}
  1.1021 +			}
  1.1022 +
  1.1023 +			// Diff excluded forums
  1.1024 +			$in_fid_ary = array_diff($in_fid_ary, $this->get_excluded_forums());
  1.1025 +
  1.1026 +			if (empty($in_fid_ary))
  1.1027 +			{
  1.1028 +				trigger_error('SORRY_AUTH_READ');
  1.1029 +			}
  1.1030 +
  1.1031 +			// Also exclude passworded forums
  1.1032 +			$in_fid_ary = array_diff($in_fid_ary, $this->get_passworded_forums());
  1.1033 +
  1.1034 +			if (empty($in_fid_ary))
  1.1035 +			{
  1.1036 +				trigger_error('SORRY_AUTH_READ');
  1.1037 +			}
  1.1038 +
  1.1039 +			$sql = 'SELECT forum_id, left_id
  1.1040 +				FROM ' . FORUMS_TABLE . '
  1.1041 +				WHERE forum_type = ' . FORUM_POST . '
  1.1042 +					AND ' . $db->sql_in_set('forum_id', $in_fid_ary) . '
  1.1043 +				ORDER BY left_id ASC';
  1.1044 +			$result = $db->sql_query_limit($sql, 1);
  1.1045 +			$this->forum_data = $db->sql_fetchrow($result);
  1.1046 +			$db->sql_freeresult($result);
  1.1047 +
  1.1048 +			if (empty($this->forum_data))
  1.1049 +			{
  1.1050 +				// No forum found.
  1.1051 +				trigger_error('SORRY_AUTH_READ');
  1.1052 +			}
  1.1053 +
  1.1054 +			unset($in_fid_ary);
  1.1055 +		}
  1.1056 +		else
  1.1057 +		{
  1.1058 +			$this->forum_id = (int) $this->topic_data['forum_id'];
  1.1059 +
  1.1060 +			// Make sure topic is either approved or user authed
  1.1061 +			if (!$this->topic_data['topic_approved'] && !$auth->acl_get('m_approve', $this->forum_id))
  1.1062 +			{
  1.1063 +				trigger_error('SORRY_AUTH_READ');
  1.1064 +			}
  1.1065 +
  1.1066 +			// Make sure forum is not excluded from feed
  1.1067 +			if (phpbb_optionget(FORUM_OPTION_FEED_EXCLUDE, $this->topic_data['forum_options']))
  1.1068 +			{
  1.1069 +				trigger_error('NO_FEED');
  1.1070 +			}
  1.1071 +
  1.1072 +			// Make sure we can read this forum
  1.1073 +			if (!$auth->acl_get('f_read', $this->forum_id))
  1.1074 +			{
  1.1075 +				trigger_error('SORRY_AUTH_READ');
  1.1076 +			}
  1.1077 +
  1.1078 +			// Make sure forum is not passworded or user is authed
  1.1079 +			if ($this->topic_data['forum_password'])
  1.1080 +			{
  1.1081 +				$forum_ids_passworded = $this->get_passworded_forums();
  1.1082 +
  1.1083 +				if (isset($forum_ids_passworded[$this->forum_id]))
  1.1084 +				{
  1.1085 +					trigger_error('SORRY_AUTH_READ');
  1.1086 +				}
  1.1087 +
  1.1088 +				unset($forum_ids_passworded);
  1.1089 +			}
  1.1090 +		}
  1.1091 +	}
  1.1092 +
  1.1093 +	function get_sql()
  1.1094 +	{
  1.1095 +		global $auth, $db;
  1.1096 +
  1.1097 +		$this->sql = array(
  1.1098 +			'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, ' .
  1.1099 +							'u.username, u.user_id',
  1.1100 +			'FROM'		=> array(
  1.1101 +				POSTS_TABLE		=> 'p',
  1.1102 +				USERS_TABLE		=> 'u',
  1.1103 +			),
  1.1104 +			'WHERE'		=> 'p.topic_id = ' . $this->topic_id . '
  1.1105 +								' . ($this->forum_id && !$auth->acl_get('m_approve', $this->forum_id) ? 'AND p.post_approved = 1' : '') . '
  1.1106 +								AND p.poster_id = u.user_id',
  1.1107 +			'ORDER_BY'	=> 'p.post_time DESC',
  1.1108 +		);
  1.1109 +
  1.1110 +		return true;
  1.1111 +	}
  1.1112 +
  1.1113 +	function get_item()
  1.1114 +	{
  1.1115 +		return ($row = parent::get_item()) ? array_merge($this->topic_data, $row) : $row;
  1.1116 +	}
  1.1117 +}
  1.1118 +
  1.1119 +/**
  1.1120 +* 'All Forums' feed
  1.1121 +*
  1.1122 +* This will give you a list of all postable forums where feeds are enabled
  1.1123 +* including forum description, topic stats and post stats
  1.1124 +*
  1.1125 +* @package phpBB3
  1.1126 +*/
  1.1127 +class phpbb_feed_forums extends phpbb_feed_base
  1.1128 +{
  1.1129 +	var $num_items	= 0;
  1.1130 +
  1.1131 +	function set_keys()
  1.1132 +	{
  1.1133 +		$this->set('title',		'forum_name');
  1.1134 +		$this->set('text',		'forum_desc');
  1.1135 +		$this->set('bitfield',	'forum_desc_bitfield');
  1.1136 +		$this->set('bbcode_uid','forum_desc_uid');
  1.1137 +		$this->set('updated',	'forum_last_post_time');
  1.1138 +		$this->set('options',	'forum_desc_options');
  1.1139 +	}
  1.1140 +
  1.1141 +	function get_sql()
  1.1142 +	{
  1.1143 +		global $auth, $db;
  1.1144 +
  1.1145 +		$in_fid_ary = array_diff($this->get_readable_forums(), $this->get_excluded_forums());
  1.1146 +		if (empty($in_fid_ary))
  1.1147 +		{
  1.1148 +			return false;
  1.1149 +		}
  1.1150 +
  1.1151 +		// Build SQL Query
  1.1152 +		$this->sql = array(
  1.1153 +			'SELECT'	=> 'f.forum_id, f.left_id, f.forum_name, f.forum_last_post_time,
  1.1154 +							f.forum_desc, f.forum_desc_bitfield, f.forum_desc_uid, f.forum_desc_options,
  1.1155 +							f.forum_topics, f.forum_posts',
  1.1156 +			'FROM'		=> array(FORUMS_TABLE => 'f'),
  1.1157 +			'WHERE'		=> 'f.forum_type = ' . FORUM_POST . '
  1.1158 +							AND ' . $db->sql_in_set('f.forum_id', $in_fid_ary),
  1.1159 +			'ORDER_BY'	=> 'f.left_id ASC',
  1.1160 +		);
  1.1161 +
  1.1162 +		return true;
  1.1163 +	}
  1.1164 +
  1.1165 +	function adjust_item(&$item_row, &$row)
  1.1166 +	{
  1.1167 +		global $phpEx, $config;
  1.1168 +
  1.1169 +		$item_row['link'] = feed_append_sid('/viewforum.' . $phpEx, 'f=' . $row['forum_id']);
  1.1170 +
  1.1171 +		if ($config['feed_item_statistics'])
  1.1172 +		{
  1.1173 +			global $user;
  1.1174 +
  1.1175 +			$item_row['statistics'] = sprintf($user->lang['TOTAL_TOPICS_OTHER'], $row['forum_topics'])
  1.1176 +				. ' ' . $this->separator_stats . ' ' . sprintf($user->lang['TOTAL_POSTS_OTHER'], $row['forum_posts']);
  1.1177 +		}
  1.1178 +	}
  1.1179 +}
  1.1180 +
  1.1181 +/**
  1.1182 +* News feed
  1.1183 +*
  1.1184 +* This will give you {$this->num_items} first posts
  1.1185 +* of all topics in the selected news forums.
  1.1186 +*
  1.1187 +* @package phpBB3
  1.1188 +*/
  1.1189 +class phpbb_feed_news extends phpbb_feed_topic_base
  1.1190 +{
  1.1191 +	function get_news_forums()
  1.1192 +	{
  1.1193 +		global $db, $cache;
  1.1194 +		static $forum_ids;
  1.1195 +
  1.1196 +		// Matches acp/acp_board.php
  1.1197 +		$cache_name	= 'feed_news_forum_ids';
  1.1198 +
  1.1199 +		if (!isset($forum_ids) && ($forum_ids = $cache->get('_' . $cache_name)) === false)
  1.1200 +		{
  1.1201 +			$sql = 'SELECT forum_id
  1.1202 +				FROM ' . FORUMS_TABLE . '
  1.1203 +				WHERE ' . $db->sql_bit_and('forum_options', FORUM_OPTION_FEED_NEWS, '<> 0');
  1.1204 +			$result = $db->sql_query($sql);
  1.1205 +
  1.1206 +			$forum_ids = array();
  1.1207 +			while ($forum_id = (int) $db->sql_fetchfield('forum_id'))
  1.1208 +			{
  1.1209 +				$forum_ids[$forum_id] = $forum_id;
  1.1210 +			}
  1.1211 +			$db->sql_freeresult($result);
  1.1212 +
  1.1213 +			$cache->put('_' . $cache_name, $forum_ids);
  1.1214 +		}
  1.1215 +
  1.1216 +		return $forum_ids;
  1.1217 +	}
  1.1218 +
  1.1219 +	function get_sql()
  1.1220 +	{
  1.1221 +		global $auth, $config, $db;
  1.1222 +
  1.1223 +		// Determine forum ids
  1.1224 +		$in_fid_ary = array_intersect($this->get_news_forums(), $this->get_readable_forums());
  1.1225 +		if (empty($in_fid_ary))
  1.1226 +		{
  1.1227 +			return false;
  1.1228 +		}
  1.1229 +
  1.1230 +		$in_fid_ary = array_diff($in_fid_ary, $this->get_passworded_forums());
  1.1231 +		if (empty($in_fid_ary))
  1.1232 +		{
  1.1233 +			return false;
  1.1234 +		}
  1.1235 +
  1.1236 +		// Add global forum
  1.1237 +		$in_fid_ary[] = 0;
  1.1238 +
  1.1239 +		// We really have to get the post ids first!
  1.1240 +		$sql = 'SELECT topic_first_post_id, topic_time
  1.1241 +			FROM ' . TOPICS_TABLE . '
  1.1242 +			WHERE ' . $db->sql_in_set('forum_id', $in_fid_ary) . '
  1.1243 +				AND topic_moved_id = 0
  1.1244 +				AND topic_approved = 1
  1.1245 +			ORDER BY topic_time DESC';
  1.1246 +		$result = $db->sql_query_limit($sql, $this->num_items);
  1.1247 +
  1.1248 +		$post_ids = array();
  1.1249 +		while ($row = $db->sql_fetchrow($result))
  1.1250 +		{
  1.1251 +			$post_ids[] = (int) $row['topic_first_post_id'];
  1.1252 +		}
  1.1253 +		$db->sql_freeresult($result);
  1.1254 +
  1.1255 +		if (empty($post_ids))
  1.1256 +		{
  1.1257 +			return false;
  1.1258 +		}
  1.1259 +
  1.1260 +		$this->sql = array(
  1.1261 +			'SELECT'	=> 'f.forum_id, f.forum_name,
  1.1262 +							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,
  1.1263 +							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',
  1.1264 +			'FROM'		=> array(
  1.1265 +				TOPICS_TABLE	=> 't',
  1.1266 +				POSTS_TABLE		=> 'p',
  1.1267 +			),
  1.1268 +			'LEFT_JOIN'	=> array(
  1.1269 +				array(
  1.1270 +					'FROM'	=> array(FORUMS_TABLE => 'f'),
  1.1271 +					'ON'	=> 'p.forum_id = f.forum_id',
  1.1272 +				),
  1.1273 +			),
  1.1274 +			'WHERE'		=> 'p.topic_id = t.topic_id
  1.1275 +							AND ' . $db->sql_in_set('p.post_id', $post_ids),
  1.1276 +			'ORDER_BY'	=> 'p.post_time DESC',
  1.1277 +		);
  1.1278 +
  1.1279 +		return true;
  1.1280 +	}
  1.1281 +}
  1.1282 +
  1.1283 +/**
  1.1284 +* New Topics feed
  1.1285 +*
  1.1286 +* This will give you the last {$this->num_items} created topics
  1.1287 +* including the first post.
  1.1288 +*
  1.1289 +* @package phpBB3
  1.1290 +*/
  1.1291 +class phpbb_feed_topics extends phpbb_feed_topic_base
  1.1292 +{
  1.1293 +	function get_sql()
  1.1294 +	{
  1.1295 +		global $db, $config;
  1.1296 +
  1.1297 +		$forum_ids_read = $this->get_readable_forums();
  1.1298 +		if (empty($forum_ids_read))
  1.1299 +		{
  1.1300 +			return false;
  1.1301 +		}
  1.1302 +
  1.1303 +		$in_fid_ary = array_diff($forum_ids_read, $this->get_excluded_forums(), $this->get_passworded_forums());
  1.1304 +		if (empty($in_fid_ary))
  1.1305 +		{
  1.1306 +			return false;
  1.1307 +		}
  1.1308 +
  1.1309 +		// Add global forum
  1.1310 +		$in_fid_ary[] = 0;
  1.1311 +
  1.1312 +		// We really have to get the post ids first!
  1.1313 +		$sql = 'SELECT topic_first_post_id, topic_time
  1.1314 +			FROM ' . TOPICS_TABLE . '
  1.1315 +			WHERE ' . $db->sql_in_set('forum_id', $in_fid_ary) . '
  1.1316 +				AND topic_moved_id = 0
  1.1317 +				AND topic_approved = 1
  1.1318 +			ORDER BY topic_time DESC';
  1.1319 +		$result = $db->sql_query_limit($sql, $this->num_items);
  1.1320 +
  1.1321 +		$post_ids = array();
  1.1322 +		while ($row = $db->sql_fetchrow($result))
  1.1323 +		{
  1.1324 +			$post_ids[] = (int) $row['topic_first_post_id'];
  1.1325 +		}
  1.1326 +		$db->sql_freeresult($result);
  1.1327 +
  1.1328 +		if (empty($post_ids))
  1.1329 +		{
  1.1330 +			return false;
  1.1331 +		}
  1.1332 +
  1.1333 +		$this->sql = array(
  1.1334 +			'SELECT'	=> 'f.forum_id, f.forum_name,
  1.1335 +							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,
  1.1336 +							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',
  1.1337 +			'FROM'		=> array(
  1.1338 +				TOPICS_TABLE	=> 't',
  1.1339 +				POSTS_TABLE		=> 'p',
  1.1340 +			),
  1.1341 +			'LEFT_JOIN'	=> array(
  1.1342 +				array(
  1.1343 +					'FROM'	=> array(FORUMS_TABLE => 'f'),
  1.1344 +					'ON'	=> 'p.forum_id = f.forum_id',
  1.1345 +				),
  1.1346 +			),
  1.1347 +			'WHERE'		=> 'p.topic_id = t.topic_id
  1.1348 +							AND ' . $db->sql_in_set('p.post_id', $post_ids),
  1.1349 +			'ORDER_BY'	=> 'p.post_time DESC',
  1.1350 +		);
  1.1351 +
  1.1352 +		return true;
  1.1353 +	}
  1.1354 +
  1.1355 +	function adjust_item(&$item_row, &$row)
  1.1356 +	{
  1.1357 +		parent::adjust_item($item_row, $row);
  1.1358 +
  1.1359 +		$item_row['title'] = (isset($row['forum_name']) && $row['forum_name'] !== '') ? $row['forum_name'] . ' ' . $this->separator . ' ' . $item_row['title'] : $item_row['title'];
  1.1360 +	}
  1.1361 +}
  1.1362 +
  1.1363 +/**
  1.1364 +* Active Topics feed
  1.1365 +*
  1.1366 +* This will give you the last {$this->num_items} topics
  1.1367 +* with replies made withing the last {$this->sort_days} days
  1.1368 +* including the last post.
  1.1369 +*
  1.1370 +* @package phpBB3
  1.1371 +*/
  1.1372 +class phpbb_feed_topics_active extends phpbb_feed_topic_base
  1.1373 +{
  1.1374 +	var $sort_days = 7;
  1.1375 +
  1.1376 +	function set_keys()
  1.1377 +	{
  1.1378 +		parent::set_keys();
  1.1379 +
  1.1380 +		$this->set('author_id',	'topic_last_poster_id');
  1.1381 +		$this->set('creator',	'topic_last_poster_name');
  1.1382 +	}
  1.1383 +
  1.1384 +	function get_sql()
  1.1385 +	{
  1.1386 +		global $db, $config;
  1.1387 +
  1.1388 +		$forum_ids_read = $this->get_readable_forums();
  1.1389 +		if (empty($forum_ids_read))
  1.1390 +		{
  1.1391 +			return false;
  1.1392 +		}
  1.1393 +
  1.1394 +		$in_fid_ary = array_intersect($forum_ids_read, $this->get_forum_ids());
  1.1395 +		$in_fid_ary = array_diff($in_fid_ary, $this->get_passworded_forums());
  1.1396 +		if (empty($in_fid_ary))
  1.1397 +		{
  1.1398 +			return false;
  1.1399 +		}
  1.1400 +
  1.1401 +		// Add global forum
  1.1402 +		$in_fid_ary[] = 0;
  1.1403 +
  1.1404 +		// Search for topics in last X days
  1.1405 +		$last_post_time_sql = ($this->sort_days) ? ' AND topic_last_post_time > ' . (time() - ($this->sort_days * 24 * 3600)) : '';
  1.1406 +
  1.1407 +		// We really have to get the post ids first!
  1.1408 +		$sql = 'SELECT topic_last_post_id, topic_last_post_time
  1.1409 +			FROM ' . TOPICS_TABLE . '
  1.1410 +			WHERE ' . $db->sql_in_set('forum_id', $in_fid_ary) . '
  1.1411 +				AND topic_moved_id = 0
  1.1412 +				AND topic_approved = 1
  1.1413 +				' . $last_post_time_sql . '
  1.1414 +			ORDER BY topic_last_post_time DESC';
  1.1415 +		$result = $db->sql_query_limit($sql, $this->num_items);
  1.1416 +
  1.1417 +		$post_ids = array();
  1.1418 +		while ($row = $db->sql_fetchrow($result))
  1.1419 +		{
  1.1420 +			$post_ids[] = (int) $row['topic_last_post_id'];
  1.1421 +		}
  1.1422 +		$db->sql_freeresult($result);
  1.1423 +
  1.1424 +		if (empty($post_ids))
  1.1425 +		{
  1.1426 +			return false;
  1.1427 +		}
  1.1428 +
  1.1429 +		$this->sql = array(
  1.1430 +			'SELECT'	=> 'f.forum_id, f.forum_name,
  1.1431 +							t.topic_id, t.topic_title, t.topic_replies, t.topic_replies_real, t.topic_views,
  1.1432 +							t.topic_last_poster_id, t.topic_last_poster_name, t.topic_last_post_time,
  1.1433 +							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',
  1.1434 +			'FROM'		=> array(
  1.1435 +				TOPICS_TABLE	=> 't',
  1.1436 +				POSTS_TABLE		=> 'p',
  1.1437 +			),
  1.1438 +			'LEFT_JOIN'	=> array(
  1.1439 +				array(
  1.1440 +					'FROM'	=> array(FORUMS_TABLE => 'f'),
  1.1441 +					'ON'	=> 'p.forum_id = f.forum_id',
  1.1442 +				),
  1.1443 +			),
  1.1444 +			'WHERE'		=> 'p.topic_id = t.topic_id
  1.1445 +							AND ' . $db->sql_in_set('p.post_id', $post_ids),
  1.1446 +			'ORDER_BY'	=> 'p.post_time DESC',
  1.1447 +		);
  1.1448 +
  1.1449 +		return true;
  1.1450 +	}
  1.1451 +
  1.1452 +	function get_forum_ids()
  1.1453 +	{
  1.1454 +		global $db, $cache;
  1.1455 +		static $forum_ids;
  1.1456 +
  1.1457 +		$cache_name	= 'feed_topic_active_forum_ids';
  1.1458 +
  1.1459 +		if (!isset($forum_ids) && ($forum_ids = $cache->get('_' . $cache_name)) === false)
  1.1460 +		{
  1.1461 +			$sql = 'SELECT forum_id
  1.1462 +				FROM ' . FORUMS_TABLE . '
  1.1463 +				WHERE forum_type = ' . FORUM_POST . '
  1.1464 +					AND ' . $db->sql_bit_and('forum_options', FORUM_OPTION_FEED_EXCLUDE, '= 0') . '
  1.1465 +					AND ' . $db->sql_bit_and('forum_flags', log(FORUM_FLAG_ACTIVE_TOPICS, 2), '<> 0');
  1.1466 +			$result = $db->sql_query($sql);
  1.1467 +
  1.1468 +			$forum_ids = array();
  1.1469 +			while ($forum_id = (int) $db->sql_fetchfield('forum_id'))
  1.1470 +			{
  1.1471 +				$forum_ids[$forum_id] = $forum_id;
  1.1472 +			}
  1.1473 +			$db->sql_freeresult($result);
  1.1474 +
  1.1475 +			$cache->put('_' . $cache_name, $forum_ids, 180);
  1.1476 +		}
  1.1477 +
  1.1478 +		return $forum_ids;
  1.1479 +	}
  1.1480 +
  1.1481 +	function adjust_item(&$item_row, &$row)
  1.1482 +	{
  1.1483 +		parent::adjust_item($item_row, $row);
  1.1484 +
  1.1485 +		$item_row['title'] = (isset($row['forum_name']) && $row['forum_name'] !== '') ? $row['forum_name'] . ' ' . $this->separator . ' ' . $item_row['title'] : $item_row['title'];
  1.1486 +	}
  1.1487 +}
  1.1488 +
  1.1489 +
  1.1490 +?>
  1.1491 \ No newline at end of file