<?php
if (MING != "merciless") require ('index.php'); // this keeps people from executing "theme.php" directly
else {

// this adds the default theme file.
$file = new FileWrapper();
$file->title = 'Printer Friendly Theme';
$file->directory = $theme_directory;
$file->type = POWER_PHP;
$file->dont_list = TRUE;
$file->is_capable = TRUE;
$file->file_name = 'printer_friendly_theme';



main_add_magic_file ($file );



function magicfile_printer_friendly_theme()
{
	if ($theme_use_javascript_for_back === FALSE)
	{
		global $content_request;
		$back_link = '<a href="index.php?rqst=' . $content_request . '">Back</a>';
	}
	else
	{
		$back_link = '<a href="javascript:history.go(-1)">Back</a>';
	}
	$theme =	'
<?php
global $content_template;
$content_template = "%content%"	;


?>
<html>
<head>
	<title><!--[[ theme: page_title ]]--></title>
<body>
<h2> <!--[[ theme: page_title ]]--></h2>
<p><i> ' . $back_link . ' </i></p>
<!--[[ content : main ]]-->
<small><small> <!--[[ theme: footer ]]--> </small></small>
</body>
</html>
';
	return $theme;
}


// Default theme file
$file = new FileWrapper();
$file->title = 'Default theme';
$file->directory = $theme_directory;
$file->type = POWER_PHP;
$file->dont_list = TRUE;
$file->is_capable = TRUE;
$file->file_name = 'default_theme';
main_add_magic_file ($file );



function magicfile_default_theme()
{
	return
	'
<html>
<head>
	<title><!--[[ theme: page_title ]]--></title>
<body>
<i>This website is having difficulties, and is not being displayed properly. Please be patient!</i><br />
<i><small>(error [theme_02]: the specified theme could not be found)</small></i>
<!-- This is the default, failsafe theme. It is contained as a magic file in "theme.php" in your modules directory. 
It is being displayed because the theme you specified could not be found. --><br /><br />
<!--[[ theme: link_box ]]-->
<br />
<hr />
<!--[[ content: main ]]-->
<br />
<hr />
<small><small> <!--[[ theme: footer ]]--> </small></small>
</body>
</html>
';
}





// Site credits and misc.
$file = new FileWrapper();
$file->title = 'Site Credits';
$file->directory = $content_directory;
$file->type = POWER_HTML;
$file->dont_list = TRUE;
$file->is_capable = FALSE;
$file->file_name = 'site_credits';
main_add_magic_file ($file );


function magicfile_site_credits()
{
	global $theme_author, $theme_credits, $theme_license, $theme_more_credits, $theme_name;
	global $main_additional_credits;
	$site_credits = '
<p>This website was generated by <a href="http://planetmongo.sf.net">Planet Mongo</a>, a minimalist, free/libre content 
manager which values speedy deployment and ease of modification without using any databases. 
To find out more about Planet Mongo, and to see if Planet Mongo is the right content manager for <i>your</i> 
website, head on over to <a href="http://planetmongo.sf.net">the Highly-Official Planet Mongo Web Site</a>.</p> 
';
	$site_credits = $site_credits . '
	<hr />
	<ul>Information about this Planet Mongo installation</ul>
		<li>Version: <i>' . PLANET_MONGO_VERSION_STR . '  </i></li>
		<li>Website: <i><a href="http://planetmongo.sf.net">planetmongo.sf.net</a></i></li>
		<li>License: <a href="index.php?rqst=__gpl">GPL 2.0</a></li>
		<li><a href="index.php?rqst=__credits_and_version_history">Planet Mongo version history and credits</a></li>
	</ul>';
	if (strlen($theme_name) > 1)
	{
		if (strlen($theme_author) < 1) $theme_author = 'Unknown';
		if (strlen($theme_license) < 1) $theme_license = 'License unspecified';
		
		$site_credits = $site_credits . '
	<hr />
	<ul>Theme Credits</ul>
		<li>Theme name: <i>' . $theme_name . '</i></li>
		<li>Created by: <i>' . $theme_author . '</i></li>
		<li>' .$theme_license . '</li>
	</ul>';
	}
	if (strlen($theme_more_credits) > 1)
	{
		$site_credits = $site_credits . '<br />' . $theme_more_credits;
	}
	if (strlen($main_additional_credits) > 1)
	{
		$site_credits = $site_credits . '<hr />' . $main_additional_credits;
	}
	
	// TODO: add php credits function.

	return $site_credits;
	
}


/************************************************************/


function theme_site_title_doaction(){
	global $main_site_title;
	outblurt ($main_site_title);
}
function theme_page_title_doaction(){
	theme_site_title_doaction();
}

function theme_slogan_doaction() {
	global $main_slogan;
	outblurt ($main_slogan);	
}
/*
function theme_copyright_line_doaction() {
	theme_footer_doaction();
}
*/
function theme_logo_doaction()
{
	global $theme_logo, $main_site_title;
	if (strlen($theme_logo) > 3)
		outblurt ("<img src\"$theme_logo\" alt=\"$main_site_title\" />");
}

function theme_head_doaction()
{
	// where are YOU headed?	
}

function theme_main_doaction()
{	
	global $theme_directory, $theme, $theme_allow_printer_friendly_version, $theme_file_name, $theme_allow_multiple_themes, $theme_request_get_variable;
	$theme_default_theme2 = '';
	if ($theme_allow_multiple_themes === TRUE && strlen($_GET[$theme_request_get_variable]) > 1)
	{
		$theme_default_theme2 = $theme_default_theme;
		$theme_default_theme = $theme;
		$theme = $_GET[$theme_request_get_variable];
	}
	if (substr($theme, strlen($theme), 1) == '/') // checks the last digit
	{
		$theme = $theme . $theme_filename;
	}
	$theme_file = FALSE;
	if ( $theme_allow_printer_friendly_version === TRUE )
	{
		global $theme_printer_friendly_get_var, $theme_printer_friendly_theme;
		if ($_GET[$theme_printer_friendly_get_var] == 'y')
		{
			$theme_file = main_get_file($theme_printer_friendly_theme, $theme_directory);
		}
	}
	if ($theme_file === FALSE)
	{
		$theme_file = main_get_file($theme_directory . '/' . $theme);
	}
	
	if ($theme_file !== FALSE)
	{
		outblurt ($theme_file->contents , POWER_PHP, TRUE );
	}
	else
	{
		global $theme_default_theme;
		/*
		if (substr($theme_default_theme, strlen($theme_default_theme), 1) == '/')
		{
			$theme_default_theme = $theme . $theme_filename;
		}
		*/
		$failsafe_theme = main_get_file($theme_default_theme, $theme_directory);
		if ($failsafe_theme === FALSE && strlen($theme_default_theme2) > 1)
		{
			$failsafe_theme = main_get_file($theme_default_theme2, $theme_directory);
		}
		if ($failsafe_theme === FALSE)
		{
			echo '
<b>Uh oh. This website is experiencing some technical difficulties. Please try again later.</b><br />
<small>(error [theme_01], the theme could not be found, and the failsafe theme could not be found. This is likely the 
cause of a damaged configuration file.)</small>
				'; // This should be a fairly rare error.
				
		}
		else
		{
			outblurt ($failsafe_theme->contents, POWER_PHP, TRUE);
		}
	}
}

function theme_affiliates_doaction ( )
{
	global $theme_display_getfirefox_link, $theme_getfirefox_link, $theme_affilates;
	$text = '';
	if ( $theme_display_getfirefox_link == TRUE) $text = $theme_getfirefox_link;
	$text = $text . $theme_affiliates;
	outblurt ($theme_affiliates, POWER_DEFAULT2, TRUE);
}

function theme_block_begin_doaction ( $arg = '' )
{
	global $theme_block_name, $theme_block_begin;
	$theme_block_name = $arg;
	//echo ($theme_block_begin);
	// outblurt ( "<!--[[theme: block_title]]-->");
	outblurt ( $theme_block_begin, POWER_DEFAULT2, TRUE );
}

function theme_block_end_doaction ( $arg = '' )
{
	global $theme_block_end;
	outblurt ( $theme_block_end, POWER_DEFAULT2, TRUE );	
}
function theme_footer_doaction ( $arg = '')
{
	global $copyright_footer, $theme_display_credits_link, $theme_credits_link;
	global $main_enable_benchmark, $theme_benchmark_text;
	global $theme_footer_layout;
	
	$replacer = Array (
	'%copyright%' => $copyright_footer,
	'%credits%' => $theme_credits_link,
	'%benchmark%' => $theme_benchmark_text 
	);
	$text = main_replace($theme_footer_layout, $replacer);
	outblurt ($text, POWER_DEFAULT, TRUE);
}

function theme_link_box_doaction ( $arg = FALSE)
{
	// outblurt (' <!--[[ content: link_box ]]-->', POWER_HTML);
	if ($arg !== FALSE)
	{
		$varname = '$theme_link_box' . $arg;
		$val = eval ("global $varname; return $varname;");
	}
	else
	{
		global $theme_link_box;
		$val = $theme_link_box;
	}
	outblurt ($val, POWER_DEFAULT, TRUE );
	
}

function theme_block_title_doaction( $arg = '' )
{
	global $theme_block_name;
	outblurt ( $theme_block_name, POWER_DEFAULT, TRUE );
}

/*
Generates a string containing the date in a nice, human readable format.
*/

function theme_generate_date_line ($date, $template = FALSE)
{
	if ($template === FALSE)
	{
		global $theme_date_format;
		$template = $theme_date_format;		
	}
	$nice_date = getdate($date);
	$seconds = $nice_date['seconds'];
	$minutes = $nice_date['minutes'];
	$hours = $nice_date['hours'];
	$weekday = $nice_date['weekday'];
	$month = $nice_date['month'];
	$mday = $nice_date['mday'];
	$year = $nice_date['year'];
	$unix = $date;
	$replace_list = Array (
		'%seconds%' => $nice_date['seconds'], 
		'%minutes%' => $nice_date['minutes'], 
		'%hours%' => $nice_date['hours'], 
		'%weekday%' => $nice_date['weekday'], 
		'%month%' => $nice_date['month'], 
		'%mday%' => $nice_date['mday'], 
		'%mon%' => $nice_date['mon'], 
		'%year%' => $nice_date['year'], 
		'%yday%' => $nice_date['yday'],
		'%unix%' => $date
	);
	
	
	$posted_on = main_replace ($template, $replace_list);
	return $posted_on;
	
}

} // this closing brace is from the else clause in the bad_file checker above
?>
