<?php
/***************************************************************************
 *                                ticker.php
 *                            -------------------
 *   begin                : Monday, Dec 30, 2002
 *   copyright            : (C) 2002 Chris Merrett
 *					Matthijs van de Water
 *					Sascha Carlin
 *   email                : chrisfu@the-fus.com
 *				    matthijs@beryllium.net
 *                          sc@itst.org
 *
 *   $Id: ticker.php,v 1.0 2002/30/12 02:20:41 cmerrett Exp $
 *
 *
 ***************************************************************************/

/***************************************************************************
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 ***************************************************************************/

/***************************************************************************
 *
 *   PHPBB 2.0.x XML NEWS TICKER
 *   Shows last active topics XML form
 *
 ***************************************************************************
 *
 *   Put this file in your phpBB2 directory.
 *   You can call this script without parameters, what will
 *   result in an XML with the 10 latest posts from all your forums.
 *   You can specify the number of posts via the url parameter count:
 *
 *   http://www.domain.com/phpBB2/ticker.php?count=50
 *   
 *   This will result in an XML file with the latest 50 posts from
 *   all your forums.
 *   
 *   You can also specify to look only at a specified forum using the
 *   fid parameter:
 *   
 *   http://www.domain.com/phpBB2/ticker.php?fid=9
 *   
 *   This will result in an XML file with the latest 10 posts from
 *   your forum with the id 9.
 *   
 *   You can also mix the paramters:
 *   
 *   http://www.domain.com/phpBB2/ticker.php?fid=5&count=3
 *   
 *   will show you the latest 3 posts from forum 5.
 *
 *   THIS SCRIPT WILL ONLY SHOW POSTS FROM PUBLIC FORUMS
 *   
 *   See the included file README for detailed instructions.
 *
 ***************************************************************************/

/***************************************************************************
 * 
 *   Below are the variables you'll need to edit and customise the ticker.
 *   If you're not sure what any of them do, leave them.
 *
 ***************************************************************************/

// Main Cosmetic Variables
$pause = "true"; 		// Pausable? (true or false)
$timeout = "4"; 		// Time before show next post (in seconds)
$border = "3"; 		// Border width (in pixels)
$borderc = "#555555"; 	// Border colour (HTML colour hex code)
$bg = "#535D63"; 		// Background colour (HTML colour hex code)
$wdth = "350"; 		// Box width (in pixels)
$hght = "30"; 		// Box height (in pixels)

// Text Formatting Variables
$font = "verdana,arial,helvetica";	// Choose fonts in order of pref.
$moutc = "#FF9900"; 	// Standard text colour (HTML colour hex code)
$moutd = "none"; 		// Standard text decor (underline, strikeout)
$moutw = "bold"; 		// Standard text weight (bold, italic)
$mouts = "8pt"; 		// Stardard text size (8pt, 10pt, 12pt)
$movrc = "#999999"; 	// Mouseover text colour (HTML colour hex code)
$movrd = "underline"; 	// Mouseover text decor (underline, strikeout)
$movrw = "bold"; 		// Mouseover text weight (bold, italic)
$movrs = "8pt"; 		// Mouseover text size (8pt, 10pt, 12pt)

// Other stuff
$target = "_blank"; 	// Link destination window (_blank, _parent etc.)

/***************************************************************************
 * 
 *   Don't edit anything below this point unless you know what you're doing!
 *
 ***************************************************************************/

// XML and nocaching headers
header ('Cache-Control: private, pre-check=0, post-check=0, max-age=0');
header ('Expires: ' . gmdate('D, d M Y H:i:s', time()) . ' GMT');
header ('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header ('Content-Type: text/xml');

// Includes of phpBB scripts
define ('IN_PHPBB', true);
$phpbb_root_path = './';
include($phpbb_root_path . 'extension.inc');
include($phpbb_root_path . 'common.'.$phpEx);

// If not set, set the output count to 10
$count = ( !isset($HTTP_GET_VARS['count']) ) ? 10 : 
intval($HTTP_GET_VARS['count']);
$count = ( $count == 0 ) ? 10 : $count;

// Create main board url (some code borrowed from functions_post.php)
$script_name = preg_replace('/^\/?(.*?)\/?$/', '\1', 
trim($board_config['script_path']));
$viewtopic = ( $script_name != '' ) ? $script_name . '/viewtopic.' . $phpEx : 
'viewtopic.'. $phpEx;
$index = ( $script_name != '' ) ? $script_name . '/index.' . $phpEx : 'index.'. 
$phpEx;
$server_name = trim($board_config['server_name']);
$server_protocol = ( $board_config['cookie_secure'] ) ? 'https://' : 'http://';
$server_port = ( $board_config['server_port'] <> 80 ) ? ':' . 
trim($board_config['server_port']) . '/' : '/';

$index_url = $server_protocol . $server_name . $server_port . $index;
$viewtopic_url = $server_protocol . $server_name . $server_port . $viewtopic;

$ticker = "<?xml version=\"1.0\" ?>
<ticker>
	<tickerstyle pause=\"$pause\" timeout=\"$timeout\" border=\"$border\" bordercolor=\"$borderc\" background=\"$bg\" width=\"$wdth\" height=\"$hght\" />
	<tickerlinksyle>
		<mouseout font=\"$font\" color=\"$moutc\" decoration=\"$moutd\" weight=\"$moutw\" size=\"$mouts\" /> 
  		<mouseover font=\"$font\" color=\"$movrc\" decoration=\"$movrd\" weight=\"$movrw\" size=\"$movrs\" />
	</tickerlinksyle>
";

$fid = ( isset($HTTP_GET_VARS['fid']) ) ? intval($HTTP_GET_VARS['fid']) : '';
$sql_where = ( !empty($fid) ) ? " AND f.forum_id = $fid " : " ";

// SQL statement to fetch active topics of public forums
$sql = "SELECT DISTINCT t.topic_title, t.topic_last_post_id, p.post_time, 
f.forum_name
    FROM " . TOPICS_TABLE . " AS t, " . POSTS_TABLE . " AS p, " . FORUMS_TABLE . 
" AS f
    WHERE
        t.forum_id = f.forum_id
            AND f.auth_view = " . AUTH_ALL . "
            AND p.topic_id = t.topic_id
            AND p.post_id = t.topic_last_post_id
            $sql_where
    ORDER BY p.post_time DESC LIMIT $count";
$topics_query = $db->sql_query($sql);

if ( !$topics_query )
{
    die("Failed obtaining list of active topics");
}
else
{
    $topics = $db->sql_fetchrowset($topics_query);
}

if ( count($topics) == 0 )
{
    die("No topics found");
}
else
{
    // $topics contains all interesting data
    for ($i = 0; $i < count($topics); $i++)
    {
        $title = $topics[$i]['topic_title'];
        $url = $viewtopic_url . "?" . POST_POST_URL . "=" . 
$topics[$i]['topic_last_post_id'] . "#" . $topics[$i]['topic_last_post_id'];

        $ticker .= "
<tickeritem URL=\"" . $url . "\" target=\"$target\">" . $title . "</tickeritem>
";
    }
}

// Create XML footer
$ticker .= "
</ticker>";

// Output the XML
echo $ticker;

?> 