<?php
if (MING != "merciless") require ('index.php');
else {

// These are the files that it will require no matter what. If you want to add your own files, just add them to the array.
$header_first_includes = Array(
	'config/config.php',
	'modules/core.php');

/*
This function removes the escape characters ("magic quotes") which are automatically inserted in some PHP configurations.
That means that you will have no need to worry about magic quotes when writing your modules and PHP. They are removed before any other PHP is executed.
*/
# This header_fix_magic_quotes is based on the function by the New York PHP community ( http://nyphp.org/ )
function header_fix_magic_quotes ($var = NULL, $sybase = NULL)
{
	// if sybase style quoting isn't specified, use ini setting
	if ( !isset ($sybase) )	$sybase = ini_get ('magic_quotes_sybase');
	// if no var is specified, fix all affected superglobals
	if ( !isset ($var) )
	{
		// if magic quotes is enabled
		if ( get_magic_quotes_gpc () )
		{
			// workaround because magic_quotes does not change $_SERVER['argv']
			$argv = isset($_SERVER['argv']) ? $_SERVER['argv'] : NULL; 
			// fix all affected arrays
			foreach ( array ('_ENV', '_REQUEST', '_GET', '_POST', '_COOKIE', '_SERVER') as $var )
				$GLOBALS[$var] = header_fix_magic_quotes ($GLOBALS[$var], $sybase);
			$_SERVER['argv'] = $argv;
			// turn off magic quotes, this is so scripts which
			// are sensitive to the setting will work correctly
			ini_set ('magic_quotes_gpc', 0);
		}
		// disable magic_quotes_sybase
		if ( $sybase ) ini_set ('magic_quotes_sybase', 0);
		// disable magic_quotes_runtime
		set_magic_quotes_runtime (0);
		return TRUE;
	}
	// if var is an array, fix each element
	if ( is_array ($var) )
	{
		foreach ( $var as $key => $val )
			$var[$key] = header_fix_magic_quotes ($val, $sybase);
		return $var;
	}
	// if var is a string, strip slashes.
	// This is the only actual part that strips slashes
	if ( is_string ($var) )
		return $sybase ? str_replace ('\'\'', '\'', $var) : stripslashes ($var);
	// otherwise ignore
	return $var;
}

header_fix_magic_quotes (); // This gets rid of all those horrific magic quotes. Grrr. I hate them.

/*
   /*
   All these global variables are slash-encoded by default,
   because    magic_quotes_gpc is set by default!
   (And magic_quotes_gpc affects more than just $_GET, $_POST, and $_COOKIE)
   * /  
   $_SERVER = stripslashes_array($_SERVER);
   $_GET = stripslashes_array($_GET);
   $_POST = stripslashes_array($_POST);
   $_COOKIE = stripslashes_array($_COOKIE);
   $_FILES = stripslashes_array($_FILES);
   $_ENV = stripslashes_array($_ENV);
   $_REQUEST = stripslashes_array($_REQUEST);
   $HTTP_SERVER_VARS = stripslashes_array($HTTP_SERVER_VARS);
   $HTTP_GET_VARS = stripslashes_array($HTTP_GET_VARS);
   $HTTP_POST_VARS = stripslashes_array($HTTP_POST_VARS);
   $HTTP_COOKIE_VARS = stripslashes_array($HTTP_COOKIE_VARS);
   $HTTP_POST_FILES = stripslashes_array($HTTP_POST_FILES);
   $HTTP_ENV_VARS = stripslashes_array($HTTP_ENV_VARS);
*/
function header_include_files ()
{
	$include_files = Array();
	global $main_autodetect_modules;
	if ($main_autodetect_modules === TRUE)	{
		global $main_include_directories, $header_include_file_wrappers;
		$header_include_file_wrappers = Array();
		foreach($main_include_directories as $directory) {
			$dir_contents = main_dir_contents($directory, FALSE, FALSE);
			// We add to the array an array of the contents of the current folder.
			$header_include_file_wrappers =  array_merge($header_include_file_wrappers, $dir_contents);
		}
		foreach ($header_include_file_wrappers as $wrapper) { 
			// Now we loop through the list of FileWrappers processing them and adding them to 
			// the list of paths ($include_files).
			if ($wrapper->type == POWER_PHP && $wrapper->raw_title != 'index')
				$include_files[] = $wrapper->directory . '/' . $wrapper->file_name;
		}
	} else {
		// If autodetection of modules is off, we just have all of the files listed here instead.
		// More secure, etc.
		$include_files = Array (
			'config/config.php',
			'config/users.php',
			'modules/core.php',
			'modules/theme.php',
			'modules/content.php',
			'modules/user.php',
			'modules/news.php',
			'modules/help.php',
			'modules/license.php',
			'modules/xml_database.php',
			'modules/comment.php',
			'modules/editor.php',
			'modules/random.php',
		);
	}
	return $include_files;
}

foreach ($header_first_includes as $file_path) require ($file_path); // includes each file.
$header_other_includes = header_include_files();
$header_first_includes[] = 'modules/header.php'; // Add header.php to first includes so that it wont be included in the next for loop
foreach ($header_other_includes as $file_path)
{
	if (!in_array($file_path, $header_first_includes) ) include ($file_path); // includes each file, only if it has not been already included
}

} // for the "else" clause
?>
