<?php
/***************************************************************************
 *                              archive
 *                            -----------
 *		Version			: 1.0.0
 *		Email			: austin@phpbb-amod.com
 *		Site			: http://phpbb-tweaks.com
 *		Copyright		: aUsTiN-Inc 2003/6
 *
 ***************************************************************************/
 
define('IN_PHPBB', true);
$phpbb_root_path = './';
include($phpbb_root_path .'extension.inc');
include($phpbb_root_path .'common.'. $phpEx);

$userdata = session_pagestart($user_ip, PAGE_INDEX);
init_userprefs($userdata);

	define('ARCHIVE_FORUM', 'f');
	define('ARCHIVE_TOPIC', 't');
	define('ARCHIVE_POST', 'p');	
	define('ARCHIVE_CAT', 'c');
	define('ARCHIVE_EXT', '.html');
	define('ARCHIVE_TP', '%s Posts in %s Topics');
	define('ARCHIVE_P', '%s Posts');	
	define('ARCHIVE_PT', 'Post %s of %s');
	define('ARCHIVE_W', '%s Wrote:');
	define('ARCHIVE_T', '%s Archives');
	// If you remove this, you have no respect for others work & therefor can expect no support.
	define('ARCHIVE_C', '<div align="center">phpBB Archives Mod &copy; <a href="http://phpbb-tweaks.com">phpBB-TweakS</a></div>');
	
	$uri	= $HTTP_SERVER_VARS['REQUEST_URI'];
	$id 	= str_replace(ARCHIVE_EXT, '', substr(strrchr($uri, '-'), 1));
	$var	= preg_replace("'(.*)\?([a-z])\-(.*)'", "\\2", $uri);	

	$q = "SELECT cat_id, cat_title, cat_order
		  FROM ". CATEGORIES_TABLE ." 
		  ORDER BY cat_order";
	$r 		= $db->sql_query($q);
	$cats 	= $db->sql_fetchrowset($r);
		
	$q = "SELECT *
	      FROM ". FORUMS_TABLE ."
		  WHERE forum_id > 0
	      ORDER BY cat_id ASC, forum_order ASC";
	$r 		= $db->sql_query($q);
	$forums = $db->sql_fetchrowset($r);		
	
	$q = "SELECT *
		  FROM ". TOPICS_TABLE .'
		  ORDER BY topic_time DESC';
	$r 		= $db->sql_query($q);
	$topics = $db->sql_fetchrowset($r);
	
	$q = "SELECT *
		  FROM ". POSTS_TABLE;
	$r 		= $db->sql_query($q);
	$posts 	= $db->sql_fetchrowset($r);
		
	$q = "SELECT *
		  FROM ". POSTS_TEXT_TABLE;
	$r 		= $db->sql_query($q);
	$text	= $db->sql_fetchrowset($r);
			
	$q = "SELECT *
		  FROM ". USERS_TABLE;
	$r 		= $db->sql_query($q);
	$users	= $db->sql_fetchrowset($r);
				
	$forums_auth 	= array();
	$forums_auth 	= auth(AUTH_VIEW, AUTH_LIST_ALL, $userdata, $forums);

echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">';
echo '<html>';
echo '	<head>';		
echo '	<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">';
echo '	<meta name="robots"content="index,follow">';
echo '	<meta name="revisit-after" content="1">';
echo '	<title>'. sprintf(ARCHIVE_T, $board_config['sitename']) .'</title>';
echo '	</head>';
echo '<body>';

function change_uri($current)
{
	unset($new);
	$allowed = "1234567890abcdefghijklmnopqrstuvwxyz_";
	$current = strtolower($current);
	
	for ($z = 0; $z < strlen($current); $z++)
	{
		if (strpos($allowed, $current[$z]) !== false)
			$new .= $current[$z]; // Allowed character
		if ( ($current[$z] == ' ') && ($current[strlen($new) - 1] != ' ') )
			$new .= ' '; // Not allowed character, replace with a space
	}
	
	$new = trim($new); // Remove unwanted spaces	
	$new = str_replace('  ', ' ', $new);	
	return str_replace(' ', '-', $new);
}

	// Display a cats forums
	if ($var == ARCHIVE_CAT)
	{
		$forum_ids = $forum_names = $forum_descs = $forum_posts = $forum_topics = array();
		
		for ($x = 0; $x < count($cats); $x++)
		{
			if (!$cats[$x]['cat_id'])
				break;
			
			if ($cats[$x]['cat_id'] == $id)
			{
				$cat_title 	= $cats[$x]['cat_title'];
				$cat_id		= $cats[$x]['cat_id'];
			}
			
			for ($y = 0; $y < count($forums); $y++)
			{
				if ( ($forums[$y]['cat_id'] == $id) && (!in_array($forums[$y]['forum_id'], $forum_ids)) )
				{
					$forum_ids[]	= $forums[$y]['forum_id'];
					$forum_names[] 	= $forums[$y]['forum_name'];
					$forum_descs[]	= $forums[$y]['forum_desc'];
					$forum_posts[]	= $forums[$y]['forum_posts'];
					$forum_topics[]	= $forums[$y]['forum_topics'];
				}
			}
		}
		
		echo '<fieldset class="fieldset" style="margin: 0px 0px 0px 0px;"><legend><b>&curren;</b>&nbsp;<a href="archive?c-'. change_uri($cat_title) .'-'. $cat_id . ARCHIVE_EXT .'">'. $cat_title .'</a></legend><br>';
		
		for ($x = 0; $x < count($forum_ids); $x++)
		{
			if ($forums_auth[$forum_ids[$x]]['auth_view'])
			{		
				echo '&ordm;&nbsp;<a href="archive?f-'. change_uri($forum_names[$x]) .'-'. $forum_ids[$x] . ARCHIVE_EXT .'">'. $forum_names[$x] .'</a>&nbsp;('. sprintf(ARCHIVE_TP, number_format($forum_posts[$x]), number_format($forum_topics[$x])) .')<br>';
				echo '&nbsp;'. $forum_descs[$x] .'<br>';
			}
		}
		
		echo '<br></fieldset><br><br>'. ARCHIVE_C .'<a href="archive">Back</a>';		
		exit();
	}
		
	// Display a forums topics
	if ($var == ARCHIVE_FORUM)
	{
		for ($x = 0; $x < count($forums); $x++)
		{
			if (!$forums[$x]['forum_id'])
				break;
			
			if ($forums[$x]['forum_id'] == $id)
			{
				$forum_name 	= $forums[$x]['forum_name'];
				$forum_id		= $forums[$x]['forum_id'];
				$forum_cat		= $forums[$x]['cat_id'];
			}		
		}
		
		for ($x = 0; $x < count($cats); $x++)
		{
			if (!$cats[$x]['cat_id'])
				break;
			
			if ($cats[$x]['cat_id'] == $forum_cat)
			{
				$cat_title 	= $cats[$x]['cat_title'];
				$cat_id		= $cats[$x]['cat_id'];
			}
		}
				
		echo '<fieldset class="fieldset" style="margin: 0px 0px 0px 0px;"><legend><b>&curren;</b>&nbsp;<a href="archive?c-'. change_uri($cat_title) .'-'. $cat_id . ARCHIVE_EXT .'">'. $cat_title .'</a>&nbsp;<b>::</b>&nbsp;<a href="archive?f-'. change_uri($forum_name) .'-'. $forum_id . ARCHIVE_EXT .'">'. $forum_name .'</a></legend><br>';

		for ($x = 0; $x < count($topics); $x++)
		{
			if ($forums_auth[$topics[$x]['forum_id']]['auth_view'])
			{
				if ($topics[$x]['forum_id'] == $id)
				{	
					echo '&ordm;&nbsp;<a href="archive?t-'. change_uri($topics[$x]['topic_title']) .'-'. $topics[$x]['topic_id'] . ARCHIVE_EXT .'">'. $topics[$x]['topic_title'] .'</a>&nbsp;('. sprintf(ARCHIVE_P, number_format($topics[$x]['topic_replies'] + 1)) .')<br>';
				}
			}
		}
					
		echo '<br></fieldset><br><br>'. ARCHIVE_C .'<a href="archive">Back</a>';	
		exit();	
	}
	
	// Display a topics posts
	if ($var == ARCHIVE_TOPIC)
	{
	define('IN_PHPBB', TRUE);
	include_once('includes/bbcode.'. $phpEx);
	
		for ($x = 0; $x < count($topics); $x++)
		{
			if (!$topics[$x]['topic_id'])
				break;
				
			if ($topics[$x]['topic_id'] == $id)
			{
				$forum_id 		= $topics[$x]['forum_id'];
				$topic_id		= $topics[$x]['topic_id'];
				$topic_title	= $topics[$x]['topic_title'];
				break;
			}
		}
		
		for ($x = 0; $x < count($forums); $x++)
		{
			if (!$forums[$x]['forum_id'])
				break;
			
			if ($forums[$x]['forum_id'] == $forum_id)
			{
				$forum_name 	= $forums[$x]['forum_name'];
				$forum_id		= $forums[$x]['forum_id'];
				$forum_cat		= $forums[$x]['cat_id'];
				break;
			}		
		}
		
		for ($x = 0; $x < count($cats); $x++)
		{
			if (!$cats[$x]['cat_id'])
				break;
			
			if ($cats[$x]['cat_id'] == $forum_cat)
			{
				$cat_title 	= $cats[$x]['cat_title'];
				$cat_id		= $cats[$x]['cat_id'];
			}
		}	
		
		echo '<fieldset class="fieldset" style="margin: 0px 0px 0px 0px;"><legend><b>&curren;</b>&nbsp;<a href="archive?c-'. change_uri($cat_title) .'-'. $cat_id . ARCHIVE_EXT .'">'. $cat_title .'</a>&nbsp;<b>::</b>&nbsp;<a href="archive?f-'. change_uri($forum_name) .'-'. $forum_id . ARCHIVE_EXT .'">'. $forum_name .'</a>&nbsp;<b>::</b>&nbsp;<a href="archive?t-'. change_uri($topic_title) .'-'. $topic_id . ARCHIVE_EXT .'">'. $topic_title .'</a></legend><br>';
		$total_posts = $this_post = 0;
		
		for ($x = 0; $x < count($posts); $x++)
		{
			if ($forums_auth[$posts[$x]['forum_id']]['auth_view'])
			{
				if ($posts[$x]['topic_id'] == $id)
				{
					$this_post++;
					if ($total_posts == 0)
					{
						for ($y = 0; $y < count($posts); $y++)
						{
							if ($posts[$y]['topic_id'] == $id)
								$total_posts++;
						}
					}
					
					for ($y = 0; $y < count($text); $y++)
					{							
						if ($text[$y]['post_id'] == $posts[$x]['post_id'])
						{
							if (!$re_subject)
								$re_subject = $y;
																					
							for ($z = 0; $z < count($users); $z++)
							{
								if ($users[$z]['user_id'] == $posts[$x]['poster_id'])
									$poster = $users[$z]['username'];
							}
								
							$text[$y]['post_subject'] = (empty($text[$y]['post_subject'])) ? 'RE: '. $text[$re_subject]['post_subject'] : $text[$y]['post_subject'];
							echo '<fieldset class="fieldset" style="margin: 0px 0px 0px 0px;"><legend>&ordm;&nbsp;<a href="archive?p-'. change_uri($text[$y]['post_subject']) .'-'. $posts[$x]['post_id'] . ARCHIVE_EXT .'">'. $text[$y]['post_subject'] .'</a>&nbsp;('. sprintf(ARCHIVE_PT, number_format($this_post), number_format($total_posts)) .')</legend><br>';
							echo sprintf(ARCHIVE_W, '<b>'. $poster .'</b>') .'<br><br>';
							
							// This is needed because phpBB's codes will not pass HTML validation.
							$in 	= array('<span class="genmed">', '<span class="postbody">', '</span>');
							$out 	= array('', '', '');
							
							echo str_replace($in, $out, bbencode_second_pass(str_replace("\n", '<br>', $text[$y]['post_text']), $text[$y]['bbcode_uid']));
							echo '<br></fieldset><br><br>';
						}
					}						
				}
			}
		}
							
		echo '<br></fieldset><br><br>'. ARCHIVE_C .'<a href="archive">Back</a>';		
		exit();	
	}	
	
	// Display a post
	if ($var == ARCHIVE_POST)
	{
	define('IN_PHPBB', TRUE);
	include_once('includes/bbcode.'. $phpEx);
	
		for ($x = 0; $x < count($posts); $x++)
		{
			if (!$posts[$x]['post_id'])
				break;
			
			if ($posts[$x]['post_id'] == $id)
			{
				$post_id 	= $posts[$x]['post_id'];
				$topic_id 	= $posts[$x]['topic_id'];
				break;
			}
		}
		
		for ($x = 0; $x < count($topics); $x++)
		{
			if (!$topics[$x]['topic_id'])
				break;
				
			if ($topics[$x]['topic_id'] == $topic_id)
			{
				$forum_id 		= $topics[$x]['forum_id'];
				$topic_id		= $topics[$x]['topic_id'];
				$topic_title	= $topics[$x]['topic_title'];
				break;
			}
		}
		
		for ($x = 0; $x < count($forums); $x++)
		{
			if (!$forums[$x]['forum_id'])
				break;
			
			if ($forums[$x]['forum_id'] == $forum_id)
			{
				$forum_name 	= $forums[$x]['forum_name'];
				$forum_id		= $forums[$x]['forum_id'];
				$forum_cat		= $forums[$x]['cat_id'];
				break;
			}		
		}
		
		for ($x = 0; $x < count($cats); $x++)
		{
			if (!$cats[$x]['cat_id'])
				break;
			
			if ($cats[$x]['cat_id'] == $forum_cat)
			{
				$cat_title 	= $cats[$x]['cat_title'];
				$cat_id		= $cats[$x]['cat_id'];
			}
		}	
		
		echo '<fieldset class="fieldset" style="margin: 0px 0px 0px 0px;"><legend><b>&curren;</b>&nbsp;<a href="archive?c-'. change_uri($cat_title) .'-'. $cat_id . ARCHIVE_EXT .'">'. $cat_title .'</a>&nbsp;<b>::</b>&nbsp;<a href="archive?f-'. change_uri($forum_name) .'-'. $forum_id . ARCHIVE_EXT .'">'. $forum_name .'</a>&nbsp;<b>::</b>&nbsp;<a href="archive?t-'. change_uri($topic_title) .'-'. $topic_id . ARCHIVE_EXT .'">'. $topic_title .'</a></legend><br>';
		$total_posts = $this_post = 0;
		
		for ($x = 0; $x < count($posts); $x++)
		{
			if ($forums_auth[$posts[$x]['forum_id']]['auth_view'])
			{
				if ($posts[$x]['post_id'] == $id)
				{					
					for ($y = 0; $y < count($text); $y++)
					{							
						if ($text[$y]['post_id'] == $posts[$x]['post_id'])
						{							
							for ($z = 0; $z < count($users); $z++)
							{
								if ($users[$z]['user_id'] == $posts[$x]['poster_id'])
									$poster = $users[$z]['username'];
							}
								
							$text[$y]['post_subject'] = (empty($text[$y]['post_subject'])) ? 'RE: '. $topic_title : $text[$y]['post_subject'];
							echo '<fieldset class="fieldset" style="margin: 0px 0px 0px 0px;"><legend>&ordm;&nbsp;<a href="archive?p-'. change_uri($text[$y]['post_subject']) .'-'. $posts[$x]['post_id'] . ARCHIVE_EXT .'">'. $text[$y]['post_subject'] .'</a></legend><br>';
							echo sprintf(ARCHIVE_W, '<b>'. $poster .'</b>') .'<br><br>';
							
							// This is needed because phpBB's codes will not pass HTML validation.
							$in 	= array('<span class="genmed">', '<span class="postbody">', '</span>');
							$out 	= array('', '', '');
							
							echo str_replace($in, $out, bbencode_second_pass(str_replace("\n", '<br>', $text[$y]['post_text']), $text[$y]['bbcode_uid']));
							echo '<br></fieldset><br><br>';
						}
					}
					break;						
				}
			}
		}
							
		echo '<br></fieldset><br><br>'. ARCHIVE_C .'<a href="archive">Back</a>';		
		exit();	
	}	
	
	// Display an index
	
	$displayed_cats = $cat_ids = $cat_names = $forum_cats = $forum_ids = $forum_names = $forum_descs = $forum_posts = $forum_topics = array();
		
	for ($x = 0; $x < count($cats); $x++)
	{
		if (!$cats[$x]['cat_id'])
			break;
			
		if ($cats[$x]['cat_id'])
		{
			if (!in_array($cats[$x]['cat_id'], $cat_ids))
				$cat_ids[] = $cats[$x]['cat_id'];
			if (!in_array($cats[$x]['cat_title'], $cat_names))						
				$cat_names[] = $cats[$x]['cat_title'];
		}
							
		for ($y = 0; $y < count($forums); $y++)
		{
			if (!in_array($forums[$y]['forum_id'], $forum_ids))
			{				
				$forum_cats[]	= $forums[$y]['cat_id'];	
				$forum_ids[]	= $forums[$y]['forum_id'];
				$forum_names[] 	= $forums[$y]['forum_name'];
				$forum_descs[]	= $forums[$y]['forum_desc'];
				$forum_posts[]	= $forums[$y]['forum_posts'];
				$forum_topics[]	= $forums[$y]['forum_topics'];
			}
		}
	}

	for ($x = 0; $x < count($forum_ids); $x++)
	{	
		for ($z = 0; $z < count($cat_ids); $z++)
		{
			if ( ($cat_ids[$z] == $forum_cats[$x]) && (!in_array($cat_ids[$z], $displayed_cats)) )
			{
				if ($x != 0)
					echo '<br></fieldset><br><br>';
								
				echo '<fieldset class="fieldset" style="margin: 0px 0px 0px 0px;"><legend><b>&curren;</b>&nbsp;<a href="archive?c-'. change_uri($cat_names[$z]) .'-'. $cat_ids[$z] . ARCHIVE_EXT .'">'. $cat_names[$z] .'</a></legend><br>';
				$displayed_cats[] = $cat_ids[$z];
				break;
			}
		}
			
		if ($forums_auth[$forum_ids[$x]]['auth_view'])
		{		
			echo '&ordm;&nbsp;<a href="archive?f-'. change_uri($forum_names[$x]) .'-'. $forum_ids[$x] . ARCHIVE_EXT .'">'. $forum_names[$x] .'</a>&nbsp;('. sprintf(ARCHIVE_TP, number_format($forum_posts[$x]), number_format($forum_topics[$x])) .')<br>';
			echo '&nbsp;'. $forum_descs[$x] .'<br>';
		}		
	}
echo '<br></fieldset>'. ARCHIVE_C;
echo '</body>';	
echo '</html>';	
exit();
?>