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





/********************** Not very useful settings are contained here. *****************************/

$content_outblurt_handle = 'content_main_outblurt';

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






/********************************************************************/
/* This is the default 404 file. If you want a custom 404 message,  */
/* instead of changing this, instead include a file in the content  */
/* directory with a file name that includes "error404", and that    */
/* will be served instead of this file. For example,                */
/*   error_the_file_cannot_be_not_found.error404.dontlist.html      */
/* might be a logical filename for a 404 eror message.              */
/********************************************************************/
$file = new FileWrapper();
$file->title = '404: File Not Found';
$file->directory = $content_directory;
$file->dont_list = TRUE;
$file->is_capable = FALSE;
$file->file_name = 'error404';
$file->type = POWER_HTML;

main_add_magic_file ($file );

function magicfile_error404 () {
	return
	'
<h3>404 - File not found</h3>
Uh oh! It looks like the page that you were looking for does not exist.<br />
';
}

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


/*


An "auto" function is automatically executed before a module function is executed. In other words, 
if you want some code to be executed wherever your module is used, regardless of which function 
is being used, make a function with the "[module]_auto" name. If none exist, Planet Mongo will 
not try to execute it again.
It will be executed only ONCE, before the FIRST use of that module.

content_auto loads the content file and gets information about it.

*/
function content_auto () {
	// First we globalize all of the content options that we need access to.
	global $content_file, $content_directory, $content_404error_filename, 
		$content_404_for_404, $content_index_filename, $content_request_get_variable, 
		$content_request, $content_directory;
	
	// This grabs the request from the GET variables
	$request = $_GET[$content_request_get_variable];
	if (strlen($request) < 1) { // Hmm! There's not request in the get variable.
		if (strlen ($content_request) > 1 ) // Is there a a request from the $content_request variable?
			$request = $content_request;
		else // Okay, it looks like the user didn't specify at all what the request was to be, 
			$request = $content_index_filename; // and thus should be served the index file. 
	}
	$content_file = content_get_file_or_404($request, $content_directory); // now we serve up the file	
}

// A couple aliases... I should probably remove these since I don't like aliases very much
//function content_navigation_doaction () { content_link_box_doaction(); }
//function content_auto_navigation_doaction () { content_link_box_doaction(); } 
/*
This much-accaimed function displays the auto-generated link box that Planet Mango is so famous for.
(the above statement was sarcastic)
*/
function content_link_box_doaction ($arg = '') {
	global $theme_link_begin, $theme_link_middle, $theme_link_end;
	global $content_request_get_variable, $content_directory;
	global $theme_link_navigation;
	if (strlen($theme_link_navigation) < 1) { // They don't have anything meaninful specified.
		global $theme_link_basic;
		$theme_link_navigation = $theme_link_basic;
	}
	
	$filelist = main_dir_contents ($content_directory);
	
	if ($filelist !== FALSE) { // checks to make sure there is a content_directory
		foreach ( $filelist as $file ) {
			$title = $file->title;
			$dont_list = $file->dont_list;
			$filename = $file->file_name;
			
			if ($dont_list !== TRUE && ((strlen($arg) > 0) ? strpos($filename, $arg) : TRUE)) {
				$replace_array = Array( 
					'%url%' => 'index.php?' . $content_request_get_variable . '=' . $filename ,  // TODO: fix this nonsense
					'%title%' => $title );	
				$text = main_replace ($theme_link_navigation, $replace_array);
				outblurt($text, POWER_DEFAULT);
			}
		}
	}
	else echo "<small>Error [content_3]. Content directory could not be found.</small>";
}



function content_page_title_doaction ($arg = '') {
	global $content_file;
	outblurt ($content_file->title);
}
function content_template_doaction ($arg) {
	global $content_file;
	$content_file->outblurtWithTemplate($arg, FALSE);
}
function content_main_doaction( $arg = '' ) {
	global $content_template, $content_outblurt_handle;
	$file = FALSE;
	if (strlen($arg) < 1) {
		global $content_file;
		$file = $content_file;
	} else {
		global $content_directory;
		$file = content_get_file_or_404 ($arg, $content_directory);
	}
	$file->outblurtWithTemplate ($content_template, TRUE, $content_outblurt_handle );
}

function content_get_file_or_404 ( $file, $dir ) {
	global $content_404error_filename, $content_directory;
	$file = main_get_file ($file, $dir);
	if ($file === FALSE) { // This means that there is no file with that name
		$file = main_get_file ($content_404error_filename, $content_directory);
		if ( $file === FALSE) {
			$file = new FileWrapper('double_error_404.php');
			$file->contents = '<b>404x2 - "File Not Found" Not Found</b><br />Uh oh. The file could not be found. 
		<!-- error [content_1]: ironically a 404 error occured when looking for the 404 error page.-->';
		}
	}
	return $file;	
}



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