<?php
// ------------------------------------------------------------------------- //
//                XOOPS - PHP Content Management System                      //
//                       <http://www.xoops.org/>                             //
// ------------------------------------------------------------------------- //
//  This program is free software; you can redistribute it and/or modify     //
//  it under the terms of the GNU General Public License as published by     //
//  the Free Software Foundation; either version 2 of the License, or        //
//  (at your option) any later version.                                      //
//                                                                           //
//  This program 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 General Public License for more details.                             //
//                                                                           //
//  You should have received a copy of the GNU General Public License        //
//  along with this program; if not, write to the Free Software              //
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
// ------------------------------------------------------------------------- //
if (!defined("XOOPS_MAINFILE_INCLUDED")) {
	exit();
} else {
	define("XOOPS_SIDEBLOCK_LEFT",0);
	define("XOOPS_SIDEBLOCK_RIGHT",1);
	define("XOOPS_SIDEBLOCK_BOTH",2);
	define("XOOPS_CENTERBLOCK_LEFT",3);
	define("XOOPS_CENTERBLOCK_RIGHT",4);
	define("XOOPS_CENTERBLOCK_CENTER",5);
	define("XOOPS_CENTERBLOCK_ALL",6);
	define("XOOPS_BLOCK_INVISIBLE",0);
	define("XOOPS_BLOCK_VISIBLE",1);
	define("XOOPS_MATCH_START",0);
	define("XOOPS_MATCH_END",1);
	define("XOOPS_MATCH_EQUAL",2);
	define("XOOPS_MATCH_CONTAIN",3);
	define("XOOPS_ADMIN_URL", XOOPS_URL."/admin.php");

	// ################# Include version info file ##############
	include(XOOPS_ROOT_PATH."/include/version.php");

	// for older versions...will be DEPRECATED!
	$xoopsConfig['xoops_url'] = XOOPS_URL;
	$xoopsConfig['root_path'] = XOOPS_ROOT_PATH."/";

	// #################### Error reporting settings ##################
	error_reporting (0);
 
	// uncomment the following lines to show all warning messages (for debug)
	//if ( $debug ) {
		//error_reporting (E_ALL); 
	//}
	set_magic_quotes_runtime(0);
	// #################### Include global config files ##################
	include(XOOPS_ROOT_PATH."/modules/system/cache/config.php");

	// ############## Include blocks construction class file ##############
	include_once(XOOPS_ROOT_PATH."/class/xoopsblock.php");

	// #################### Include site-wide lang file ##################
	if ( file_exists(XOOPS_ROOT_PATH."/language/".$xoopsConfig['language']."/global.php") ) {
		include(XOOPS_ROOT_PATH."/language/".$xoopsConfig['language']."/global.php");
	} else {
		include(XOOPS_ROOT_PATH."/language/english/global.php");
	}

	// ################ Include page-specific lang file ################
	if ( isset($xoopsOption['pagetype']) ) {
		if ( file_exists(XOOPS_ROOT_PATH."/language/".$xoopsConfig['language']."/".$xoopsOption['pagetype'].".php") ) {
			include(XOOPS_ROOT_PATH."/language/".$xoopsConfig['language']."/".$xoopsOption['pagetype'].".php");
		} else {
			include(XOOPS_ROOT_PATH."/language/english/".$xoopsOption['pagetype'].".php");
		}
	}

	if ( !defined("XOOPS_USE_MULTIBYTES") ) {
		define("XOOPS_USE_MULTIBYTES",0);
	}

	// ################ Include unallowed words/hosts list ###############
	include_once(XOOPS_ROOT_PATH."/modules/system/cache/badwords.php");
	include_once(XOOPS_ROOT_PATH."/modules/system/cache/badips.php");

	// #################### Include text sanitizer ##################
	include_once(XOOPS_ROOT_PATH."/class/module.textsanitizer.php");


	// #################### Include xoops check ##################
	include_once(XOOPS_ROOT_PATH."/include/xoopscheck.php");

	// #################### Include xoops mailer ##################
	include_once(XOOPS_ROOT_PATH."/class/xoopsmailer.php");
	if ( file_exists(XOOPS_ROOT_PATH."/language/".$xoopsConfig['language']."/xoopsmailerlocal.php") ) {
		include_once(XOOPS_ROOT_PATH."/language/".$xoopsConfig['language']."/xoopsmailerlocal.php");
	}

	// #################### Connect to DB ##################
	include(XOOPS_ROOT_PATH."/class/".$xoopsConfig['database'].".php");

	if ( $xoopsConfig['prefix'] && $xoopsConfig['dbhost'] && isset($xoopsConfig['dbuname']) && isset($xoopsConfig['dbpass']) && $xoopsConfig['dbname'] ) {
		$xoopsDB = new Database();
		if ( $xoopsConfig['debug_mode'] ==1 ) {
			$xoopsDB->setDebug(true); 
		}
		$xoopsDB->setPrefix($xoopsConfig['prefix']);
		$xoopsDB->connect($xoopsConfig['dbhost'], $xoopsConfig['dbuname'], $xoopsConfig['dbpass'], $xoopsConfig['dbname'], $xoopsConfig['db_pconnect']);
	}
	// ############## Login a user with a valid session ##############
	include_once(XOOPS_ROOT_PATH."/class/xoopsuser.php");
	$xoopsUser = "";
	if ( isset($HTTP_COOKIE_VARS[$xoopsConfig['sessioncookie']]) ) {
		$session = new XoopsUserSession($HTTP_COOKIE_VARS[$xoopsConfig['sessioncookie']]);
		// if the session is valid, update it and create a user object
		if($session->isValid()){
			$session->update();
			$xoopsUser = new XoopsUser($session->uid());
		}
	}

	// ############## Include common functions file ##############
	include(XOOPS_ROOT_PATH."/include/functions.php");


	// Ban unwanted IP address
	$ip = getenv("REMOTE_ADDR") ? getenv("REMOTE_ADDR") : $REMOTE_ADDR;
	if (!checkIp($ip)) {
		exit();
	}

	$xoopsMyts = new MyTextSanitizer();
	
	if ( file_exists("xoops_version.php") ) {
		include_once(XOOPS_ROOT_PATH."/class/xoopsmodule.php");
		include_once(XOOPS_ROOT_PATH."/class/xoopsgroup.php");
		$module_dir = basename(dirname($PHP_SELF));
		$xoopsModule = XoopsModule::getByDirname($module_dir);
		if ( !$xoopsModule || !$xoopsModule->isActivated() ) {
			include(XOOPS_ROOT_PATH."/header.php");
			echo "<h4>"._MODULENOEXIST."</h4>";
			include(XOOPS_ROOT_PATH."/footer.php");
			exit();
		}
		if ( $xoopsUser ) {
			if ( !XoopsGroup::checkRight("module", $xoopsModule->mid(), $xoopsUser->groups()) ) {
				redirect_header(XOOPS_URL."/",1,_NOPERM);
				exit();
			}
		} else {
			if ( !XoopsGroup::checkRight("module", $xoopsModule->mid(), 0) ) {
				redirect_header(XOOPS_URL."/",1,_NOPERM);
				exit();
			}
		}
		if ( file_exists(XOOPS_ROOT_PATH."/modules/".$xoopsModule->dirname()."/language/".$xoopsConfig['language']."/main.php") ) {
			include(XOOPS_ROOT_PATH."/modules/".$xoopsModule->dirname()."/language/".$xoopsConfig['language']."/main.php");
		} else {
			if ( file_exists(XOOPS_ROOT_PATH."/modules/".$xoopsModule->dirname()."/language/english/main.php") ) {
				include(XOOPS_ROOT_PATH."/modules/".$xoopsModule->dirname()."/language/english/main.php");
			}
		}
	}
}
?>