<?php
/***************************************************************************
                              smartfeed_url.php
                             -------------------
    begin                : Thurs Dec 29 2005
    copyright            : (C) 2000 The phpBB Group
    email                : mhamill@computer.org

    $Id: $

***************************************************************************/

/***************************************************************************
 *
 *   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.
 *
 ***************************************************************************/

// Written by Mark D. Hamill, mhamill@computer.org
// This software is designed to work with phpBB Version 2.0.19

// This is the user interface for the Smartfeed software. Users can use it to create a URL they can use 
// with newsfeed readers like FeedReader or web based personal news aggregators like bloglines.com.
// Unlike most RSS newsfeeds this one can dip into member-only and user group permission based
// forums which should not be accessible to the world and return posts in these topics. It handles
// RSS and Atom feeds.

define('IN_PHPBB', true);
$phpbb_root_path = './';

include($phpbb_root_path . 'extension.inc');
include($phpbb_root_path . 'common.'.$phpEx);
 
//
// Start session management
//

$userdata = session_pagestart($user_ip, PAGE_INDEX);
init_userprefs($userdata);

include($phpbb_root_path . 'includes/smartfeed_constants.'.$phpEx);
include($phpbb_root_path . 'language/lang_' . $board_config['default_lang'] . '/lang_smartfeed.' . $phpEx);

//
// End session management
//

$page_title = $lang['smartfeed_page_title'];
include($phpbb_root_path . 'includes/page_header.'.$phpEx);

$auth_restrict = ($userdata['session_logged_in'] == true) ? AUTH_ALL . ',' . AUTH_REG : AUTH_ALL;

$template->set_filenames(array('smartfeed' => 'smartfeed_url_body.tpl'));

// Retrieve a list of forum_ids that all members can access
$sql = 'select f.forum_id, f.forum_name, c.cat_order, f.forum_order, f.auth_read
	from ' . FORUMS_TABLE . ' f, ' . CATEGORIES_TABLE . ' c
	where f.cat_id = c.cat_id and auth_read in (' . $auth_restrict .') 
	order by c.cat_order, f.forum_order';

if ( !($result = $db->sql_query($sql)))
{
	message_die(GENERAL_ERROR, 'Could not query forum information', '', __LINE__, __FILE__, $sql);
}

// We have to do a lot of array processing mainly because MySQL 3.x can't handle unions or 
// intersections. Basically we need to figure out: of all forums, which are those this 
// user can potentially read? We only want to show posts for forums for which a user
// has read privileges.
$forum_ids = array();
$forum_names = array();
$cat_orders = array();
$forum_orders = array();
$auth_read = array();
  
$i=0;
while ($row = $db->sql_fetchrow ($result)) 
{ 
	$forum_ids [$i] = $row['forum_id'];
	$forum_names [$i] = $row['forum_name'];
	$cat_orders [$i] = $row['cat_order'];
	$forum_orders [$i] = $row['forum_order'];
	$auth_read [$i] = $row['auth_read'];
	$i++;
}
$db->sql_freeresult ($result);

// Now we need to add to our forums array other forums that may be private for which
// the user has access.

if ($userdata['session_logged_in'])
{
	$sql = 'select distinct a.forum_id, f.forum_name, c.cat_order, f.forum_order, f.auth_read
		from ' . AUTH_ACCESS_TABLE . ' a, ' . USER_GROUP_TABLE . ' ug, ' . FORUMS_TABLE . ' f, ' . CATEGORIES_TABLE . ' c
		where ug.user_id = ' . $userdata['session_user_id']
		. ' and ug.user_pending = 0 
		and a.group_id = ug.group_id and 
		a.forum_id = f.forum_id and f.cat_id = c.cat_id';

	if ( !($result = $db->sql_query($sql)))
	{
		message_die(GENERAL_ERROR, 'Could not query forum information', '', __LINE__, __FILE__, $sql);
	}

	while ($row = $db->sql_fetchrow ($result)) 
	{ 
		$forum_ids [$i] = $row['forum_id'];
		$forum_names [$i] = $row['forum_name'];
		$cat_orders [$i] = $row['cat_order'];
		$forum_orders [$i] = $row['forum_order'];
		$auth_read [$i] = $row['auth_read'];
		$i++;
	}
	$db->sql_freeresult ($result);
}
$i--;

// Sort forums so they appear as they would appear on the main index. This makes for a more 
// natural presentation.

array_multisort($cat_orders, SORT_ASC, $forum_orders, SORT_ASC, $forum_ids, SORT_ASC, $forum_names, SORT_ASC, $auth_read, SORT_ASC);

// now print the forums on the web page, each forum being a checkbox with appropriate label
for ($j=0; $j<=$i; $j++) 
{

	// Don't print if a duplicate

	if (!(($j>0) && ($cat_orders[$j] == $cat_orders[$j-1]) && ($forum_orders[$j] == $forum_orders[$j-1]))) 

	{    

		switch($auth_read[$j])
		{
			case AUTH_REG:
				$auth_label = $lang['smartfeed_auth_reg_text'];
				break;
			case AUTH_ACL:
				$auth_label = $lang['smartfeed_auth_acl_text'];
				break;
			case AUTH_MOD:
				$auth_label = $lang['smartfeed_auth_mod_text'];
				break;
			case AUTH_ADMIN:
				$auth_label = $lang['smartfeed_auth_admin_text'];
				break;
			default:
				$auth_label = '';
		}
			

		$template->assign_block_vars('forums', array( 
			'FORUM_NAME' => 'forum_' . $forum_ids [$j],
			'FORUM_LABEL' => $forum_names[$j],
			'FORUM_AUTH' => $auth_label));
	}

}

// The encoded password as stored in the database is encrypted then placed on the URL string for authentication.
if ($userdata['session_logged_in'])
{
	$sql = "SELECT user_password
		FROM " . USERS_TABLE . "
		WHERE user_id = '" . $userdata['session_user_id'] . "'";

	if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, 'Error in obtaining userdata', '', __LINE__, __FILE__, $sql);
	}

	$row = $db->sql_fetchrow($result);

	// Here is a modest way to encrypt the already encoded user_password. The $dbpasswd is unique to each site, so it would be hard to decrypt.
	include($phpbb_root_path . 'config.'.$phpEx);
	$encrypted_data = encrypt($row['user_password'], $dbpasswd);
	// Here is a modest way to encrypt the already encrypted user_password and the current IP. The $dbpasswd is unique to each site, so it would be hard to decrypt.
	$encrypted_data_with_ip = encrypt($row['user_password'] . '~' . $HTTP_SERVER_VARS['REMOTE_ADDR'],$dbpasswd);	
	unset($dbpasswd);
}
else
{
	$encrypted_data = 'NONE';
	$encrypted_data_with_ip = 'NONE';
}

// fill template with labels and values

if (!$userdata['session_logged_in'])
{
	$template->assign_block_vars('switch_not_logged_in', array());
	$template->assign_vars(array('NOT_LOGGED_IN_MSG' => $lang['smartfeed_not_logged_in']));
}
else
{
	$template->assign_block_vars('switch_logged_in', array());
	$template->assign_vars(array('L_LASTVISIT' => $lang['smartfeed_lastvisit'],
		'L_IP_AUTHENTICATION' => $lang['smartfeed_ip_auth']));
}

$template->assign_vars(array(
	'PAGE_TITLE' => $lang['smartfeed_page_title'],
	'L_LAST_FETCH' => $lang['smartfeed_since_last_fetch_or_visit'],
	'L_LAST_FETCH_VALUE' => $lang['smartfeed_since_last_fetch_or_visit_value'],
	'L_FEED_TYPE' => $lang['smartfeed_feed_type'],
	'L_ATOM_10' => $lang['smartfeed_atom_10'],
	'L_RSS_20' => $lang['smartfeed_rss_20'],
	'L_RSS_10' => $lang['smartfeed_rss_10'],
	'L_RSS_091' => $lang['smartfeed_rss_091'],
	'L_ATOM_10_VALUE' => SMARTFEED_ATOM_10_VALUE,
	'L_RSS_20_VALUE' => SMARTFEED_RSS_20_VALUE,
	'L_RSS_10_VALUE' => SMARTFEED_RSS_10_VALUE,
	'L_RSS_091_VALUE' => SMARTFEED_RSS_091_VALUE,
	'NO_FORUMS_SELECTED' => $lang['smartfeed_no_forums_selected'],
	'SMARTFEED_EXPLANATION' => $lang['smartfeed_explanation'],
	'L_YES' => $lang['smartfeed_yes'],
	'L_NO' => $lang['smartfeed_no'],
	'L_FORUM_SELECTION' => $lang['smartfeed_select_forums'],
	'L_ALL_SUBSCRIBED_FORUMS' => $lang['smartfeed_all_forums'],
	'L_SUBMIT' => $lang['smartfeed_generate_url_text'],
	'L_RESET' => $lang['smartfeed_reset_text'],
	'L_LIMIT' => $lang['smartfeed_limit_text'],
	'L_WEEK' => $lang['smartfeed_last_week'],
	'L_WEEK_VALUE' => $lang['smartfeed_last_week_value'],
	'L_DAY' => $lang['smartfeed_last_day'],
	'L_DAY_VALUE' => $lang['smartfeed_last_day_value'],
	'L_12_HRS' => $lang['smartfeed_last_12_hours'],
	'L_12_HRS_VALUE' => $lang['smartfeed_last_12_hours_value'],
	'L_6_HRS' => $lang['smartfeed_last_6_hours'],
	'L_6_HRS_VALUE' => $lang['smartfeed_last_6_hours_value'],
	'L_3_HRS' => $lang['smartfeed_last_3_hours'],
	'L_3_HRS_VALUE' => $lang['smartfeed_last_3_hours_value'],
	'L_1_HRS' => $lang['smartfeed_last_1_hours'],
	'L_1_HRS_VALUE' => $lang['smartfeed_last_1_hours_value'],
	'L_30_MIN' => $lang['smartfeed_last_30_minutes'],
	'L_30_MIN_VALUE' => $lang['smartfeed_last_30_minutes_value'],
	'L_15_MIN' => $lang['smartfeed_last_15_minutes'],
	'L_15_MIN_VALUE' => $lang['smartfeed_last_15_minutes_value'],
	'L_TOPICSONLY' => $lang['smartfeed_topics_only'],
	'L_UNREAD' => $lang['smartfeed_size_all'],
	'L_SORTBY' => $lang['smartfeed_sort_by'],
	'L_FORUMTOPIC' => $lang['smartfeed_sort_forum_topic'],
	'L_POSTDATE' => $lang['smartfeed_sort_post_date'],
	'L_URL' => $lang['smartfeed_url_label'],
	'L_REMOVE_YOUR_POSTS' => $lang['smartfeed_remove_yours'],
	'SITE_URL' => SITE_URL,
	'USER_ID' => $userdata['user_id'],
	'PHPEXT' => $phpEx,
	'PWD' => $encrypted_data,
	'PWD_WITH_IP' => $encrypted_data_with_ip,
	'LOGGED_IN' => ($userdata['session_logged_in']) ? 'true' : 'false'));

$template->pparse('smartfeed');

include($phpbb_root_path . 'includes/page_tail.'.$phpEx);

function encrypt($string, $key) 
{

	$result = '';
	for($i=0; $i<strlen($string); $i++) 
	{
		$char = substr($string, $i, 1);
		$keychar = substr($key, ($i % strlen($key))-1, 1);
		$char = chr(ord($char)+ord($keychar));
		$result.=$char;
	}
	
	return base64_encode($result);
}
?>
