Location: PHPKode > projects > OpenNitro > trunk/Nitro/Base.inc.php
<?php
//
// +---------------------------------------------------------------------------+
// | Nitro :: Base                                                             |
// +---------------------------------------------------------------------------+
// | Copyright (c) 2003 June Systems BV                                        |
// +---------------------------------------------------------------------------+
// | This library is free software; you can redistribute it and/or modify it   |
// | under the terms of the GNU Lesser General Public License as published by  |
// | the Free Software Foundation; either version 2.1 of the License, or (at   |
// | your option) any later version.                                           |
// |                                                                           |
// | This library is distributed in the hope that it will be useful, but       |
// | WITHOUT ANY WARRANTY; without even the implied warranty of                |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser   |
// | General Public License for more details.                                  |
// |                                                                           |
// | You should have received a copy of the GNU Lesser General Public License  |
// | along with this library; if not, write to the Free Software Foundation,   |
// | Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA            |
// +---------------------------------------------------------------------------+
// | Authors: Siggi Oskarsson <hide@address.com>                          |
// +---------------------------------------------------------------------------+
//
// $Id: Base.inc.php 229 2008-04-17 09:20:31Z oli $
//
// This file contains the base functions and initialization for Nitro
//

/**
 * This file contains the Base functions and init of Nitro
 *
 * @author 		Siggi Oskarsson
 * @version 	$Revision: 1.39 $
 * @copyright		June Systems BV, 2006
 * @package	Nitro
 * @subpackage	Base
 */

/**
 * Define Nitro version information and PHP requirements
 */
define("NITRO_VERSION", "###OpenNitroVersion###");
define("NITRO_PHP_MIN_VERSION", "4.3.0");
define("NITRO_PHP_VERSION", "4.4.1");

/**
 * Include common functions
 */
require_once "Nitro/Common.inc.php";
require_once "Nitro/Error.inc.php";
require_once "Nitro/DebugLevels.inc.php";
require_once "Nitro/MimeType.inc.php";

########################################################################3
#
# Initialization of Nitro environment
#
#########################################################################3

if(0) { //!zend_loader_file_licensed()) {
	echo 'This application does not have a valid license!<BR><BR>';
	echo 'Application: Nitro<BR>';
	echo 'Version: '.NITRO_VERSION.'<BR>';
	echo '<BR>Please contact June Systems for further information: hide@address.com<BR>';
	exit;
}

// set debugging on/off, debug level and debug port
if (isset($_GET['SessionDebug'])) {
	if ($_GET['SessionDebug']) {
		setcookie('SessionDebug', TRUE);
		$_COOKIE["SessionDebug"] = TRUE;
	} else {
		setcookie('SessionDebug', FALSE);
		$_COOKIE["SessionDebug"] = FALSE;
	}
}
if (isset($_GET['RunTimeDebug'])) {
	if ($_GET['RunTimeDebug']) {
		setcookie('RunTimeDebug', TRUE);
		$_COOKIE["RunTimeDebug"] = TRUE;
	} else {
		setcookie('RunTimeDebug', FALSE);
		$_COOKIE["RunTimeDebug"] = FALSE;
	}
}
if (isset($_GET['SetDebugPort'])) {
	if ((int)$_GET['SetDebugPort']) {
		setcookie('SetDebugPort', (int)$_GET['SetDebugPort']);
		$_COOKIE["SetDebugPort"] = (int)$_GET['SetDebugPort'];
	} else {
		setcookie('SetDebugPort', FALSE);
		$_COOKIE["SetDebugPort"] = FALSE;
	}
}
if (isset($_GET['SetMaxOutputLevel'])) {
	if ((int)$_GET['SetMaxOutputLevel']) {
		setcookie('SetMaxOutputLevel', (int)$_GET['SetMaxOutputLevel']);
		$_COOKIE["SetMaxOutputLevel"] = (int)$_GET['SetMaxOutputLevel'];
	} else {
		setcookie('SetMaxOutputLevel', FALSE);
		$_COOKIE["SetMaxOutputLevel"] = FALSE;
	}
}
if (isset($_GET['SetDebugHost'])) {
	// only allow hosts on local networks to debug OR hosts in $__debug_hosts_allow array (must be set in NitroBaseConfig.inc.php or index.php
	if (!is_array($__debug_hosts_allow)) {
		$__debug_hosts_allow = Array();
	}
	if (in_array($_GET['SetDebugHost'], $__debug_hosts_allow) || ereg('^(10.[0-9]+.[0-9]+.[0-9]+)|(192.168.[0-9]+.[0-9]+)|(127.0.0.1)$', $_GET['SetDebugHost'])) {
		setcookie('SetDebugHost', $_GET['SetDebugHost']);
		$_COOKIE["SetDebugHost"] = $_GET['SetDebugHost'];
	} else {
		setcookie('SetDebugHost', FALSE);
		$_COOKIE["SetDebugHost"] = FALSE;
	}
}
if (isset($_GET['SetDebugLevel'])) {
	if ($_GET['SetDebugLevel']) {
		// Clean _GET string, make sure only real DEBUG_ defines are being passed
		$tmp = explode("|", $_GET['SetDebugLevel']);
		$d = Array();
		foreach($tmp AS $t) {
			if (ereg('DEBUG_', $t) && defined($t)) { $d[] = $t; }
		}
		$debuglevel = eval('return '.(implode('|', $d)).';');
		setcookie('SetDebugLevel', $debuglevel);
		$_COOKIE["SetDebugLevel"] = $debuglevel;
	} else {
		setcookie('SetDebugLevel', FALSE);
		$_COOKIE["SetDebugLevel"] = FALSE;
	}
}
if (file_exists($_SERVER['DOCUMENT_ROOT'].'NitroDebug') OR $_COOKIE["SessionDebug"]) {
	/**
	 * Debugging is ON, define NITRO_DEBUG and NITRO_RUNTIME_DEBUG TRUE
	 */
	define("NITRO_DEBUG", TRUE);
	define("NITRO_RUNTIME_DEBUG", TRUE);
} else {
	/**
	 * Debugging off
	 * @ignore
	 */
	define("NITRO_DEBUG", FALSE);
	if ($_COOKIE['RunTimeDebug']) {
		/**
		 * Runtime debugging on
		 * @ignore
		 */
		define("NITRO_RUNTIME_DEBUG", TRUE);
	} else {
		/**
		 * Runtime debugging off
		 * @ignore
		 */
		define("NITRO_RUNTIME_DEBUG", FALSE);
	}
}

if (NITRO_RUNTIME_DEBUG) {
	require_once "Nitro/RuntimeDebug.inc.php";
}

if (NITRO_DEBUG || NITRO_RUNTIME_DEBUG) {
	// Include debug environment and initalize Debug class
	include_once "Nitro/Debug.inc.php";
	$_DEBUG = new NitroDebug();
	DebugGroup("Base", "Base", "Index file calls", __FILE__, __LINE__, DEBUG_APPL_OK);
} else {
	// Include empty debug functions and start timer for page total
	include_once "Nitro/DebugFake.inc.php";
	$NitroPageStartTime = NitroGetMicroTime();
}

// Check PHP version used on the server against Nitro versions
$PHPVersion = phpversion();
if (!version_compare($PHPVersion, NITRO_PHP_MIN_VERSION, ">=")) {
	echo "The PHP version you are using is too old to use with Nitro!<BR><BR>";
	echo "PHP version: ".phpversion()."<BR>";
	echo "Nitro min version: ".NITRO_PHP_MIN_VERSION."<BR>";
	exit;
} elseif (!version_compare($PHPVersion, NITRO_PHP_VERSION, ">=")) {
	Debug("Base", "Base", "PHP version used is older than the preferred version! (PHP: ".$PHPVersion.", Nitro: ".NITRO_PHP_VERSION.")", __FILE__, __LINE__, DEBUG_APPL_ERR);
}

// Start session
Debug("Base", "Base", "Starting session", __FILE__, __LINE__, DEBUG_APPL_OK);
if (defined('NITRO_SESSION_HANDLER')) {
	// use other session handler, than default file handler
	// if the include failes (handler does not exist), file handler is used
	$handler = strtolower(NITRO_SESSION_HANDLER);
	@include_once('SessionHandler/'.$handler.'.inc.php');
} else {
	@include_once('SessionHandler/files.inc.php');
}
if (!function_exists('NitroSession_SaveToDisk')) {
	// Session handler not properly loaded!
	// retry file based sessions ....
	if (!@include_once('SessionHandler/files.inc.php')) {
		echo "Session handler could not be properly loaded!";
		exit;
	}
}
include_once "Nitro/Session.inc.php";
session_start();

// Read ini and set php, session and application values
Debug("Base", "Base", "Loading Config", __FILE__, __LINE__, DEBUG_APPL_OK);
include_once "Nitro/Config.inc.php";
$__NitroConf = new NitroConfig(NITRO_CONFIG);
define("NITRO_ERROR_File", $__NitroConf->GetErrorFile());

if ($__NitroConf->isLoaded) {
	//Load internationalization class
	include_once "Nitro/Text.inc.php";

	// Set session defaults
	//Debug("Base", "Session", "Setting session defaults", __FILE__, __LINE__, DEBUG_APPL_OK);
	// Added ini to session array to seperate sites on same domain (same cookie == same php session array)
	if (!is_object($_SESSION['Nitro'][NITRO_CONFIG])) {
		Debug("Base", "Session", "Session object does not exist, creating a new one", __FILE__, __LINE__, DEBUG_APPL_OK);
		if (!is_array($_SESSION['Nitro'])) $_SESSION['Nitro'] = Array();
		$_SESSION['Nitro'][NITRO_CONFIG] = new NitroSession();
		$_SESSION['Nitro'][NITRO_CONFIG]->SecurityGroups = array(($__NitroConf->CONF["Default"]["SecurityGroup"] ? $__NitroConf->CONF["Default"]["SecurityGroup"] : 0));
		$_SESSION['Nitro'][NITRO_CONFIG]->SecurityGroupsLogout = $_SESSION["Nitro"][NITRO_CONFIG]->SecurityGroups;
		$_SESSION['Nitro'][NITRO_CONFIG]->Language = ($__NitroConf->CONF["Default"]["Language"] ? $__NitroConf->CONF["Default"]["Language"] : 'eng');
	}
	$__NSess =& $_SESSION["Nitro"][NITRO_CONFIG];
	$__NSess->NitroSession(); // reinitalize object, this is needed so PHP can use $this->DB and $this->Conf !

	// include main files
	DebugGroup("Base", "Base", "Include DB support and make connections", __FILE__, __LINE__, DEBUG_APPL_OK);
	include_once "Nitro/NitroDB.inc.php";
	// Try to create connections (initialize object) to all db's defined in the ini
	$DB = Array();
	if (is_array($__NitroConf->CONF["DB"])) {
		foreach($__NitroConf->CONF["DB"] AS $origID => $DSN) {
			$ID = ereg_replace('^\*', '', $origID);
			if($ID == $origID) $autoconnect = 1;
			else $autoconnect = 0;
			if (DB::isDBCon($DB[$DSN])) {
				// Alias for another database
				$DB[$ID] =& $DB[$DSN];
			} else {
				if (($id = ereg_replace('^[0-9]+\:', '', $ID)) != $ID && DB::isDBCon($DB[$id])) {
					// Additional handler for database connection, load balancing
					$DB[$id]->AddHandler($DSN, FALSE, $autoconnect);
				} else {
					// New database connection
					$DB[$ID] = new DB($DSN, FALSE, $autoconnect);
					if($DB[$ID]->Error) {
						DB::RaiseFatalError($DB[$ID]);
					}
				}
			}
		}
	}
	// Check if we have a Nitro DB connection
	if (!DB::isDBCon($DB['Nitro'])) {
		DB::RaiseFatalError($DB['Nitro']);
	}
	DebugCloseGroup(DEBUG_APPL_OK);

	// Login procedure
	// Check whether the session is still valid
	// this is expressely done before the login is checked
	$__NSess->CheckSession();

	Debug("Base", "Base", "Checking if login attempted", __FILE__, __LINE__, DEBUG_APPL_OK);
	if ($_POST["_Nitro_Login_username"] && $_POST["_Nitro_Login_password"]) {
		$sessionExpired = $__NSess->SessionExpired;
		$posted = strlen($_POST['SessionPOST']);
		if ($__NSess->Login($_POST["_Nitro_Login_username"], $_POST["_Nitro_Login_password"], $_POST["salt"])) {
			Debug("Base", "Base", "Login successful", __FILE__, __LINE__, DEBUG_APPL_OK);
			// Logged in, redirect to clear POST data,
			// only if we did not just get logged off
			if (!($sessionExpired && $posted)) {
				Debug("Base", "Base", "Closing session for writing and redirecting", __FILE__, __LINE__, DEBUG_APPL_OK);
				session_write_close(); // force writing of session
				Header("Location: ".$_SERVER["REQUEST_URI"]);
				exit;
			}
		}
	}
	//TODO, do this always by default? with explicit re-opening of session when needed?
	Debug("Base", "Base", "Closing session for writing", __FILE__, __LINE__, DEBUG_APPL_OK);
	if ($__Nitro_ReadOnly_Session) { session_write_close(); }
} else {
	echo "This application does not have a valid configuration file!<BR><BR>";
	echo "Application: Nitro<BR>";
	echo "Version: ".NITRO_VERSION."<BR>";
	echo "Configuration: ".NITRO_CONFIG."<BR>";
	echo "<BR>Please contact June Systems for further information: hide@address.com<BR>";
	exit;
}
?>
Return current item: OpenNitro