<?php
//v1.0.4 www.bcoos.net 2006/10 GPL
// www.bcoos.net 2006-09 1.0.2 GPL
//v1.4  www.exoops.info 2006/05
//v1.3 www.exoops.info 2006/03 
// $Id: common.php,v 1.25 2003/08/11 07:45:23 half-dead Exp $
// ------------------------------------------------------------------------- //
//               E-Xoops: Content Management for the Masses                  //
//                       < http://www.e-xoops.com >                          //
// ------------------------------------------------------------------------- //
if ( !defined('XOOPS_MAINFILE_INCLUDED') ) {
	exit();
	} else {
	error_reporting(2039);
	set_magic_quotes_runtime(0);
	$objCache = new stdClass();
// #################### Include global config files ###############
	include_once(XOOPS_ROOT_PATH.'/modules/system/cache/config.php');
	$bcoosConfig['language'] = !empty($bcoosConfig['language']) ? $bcoosConfig['language'] : $bcoosConfig['default_language'];
	if ($bcoosConfig['gzip_compression']) {
		ob_start('ob_gzhandler');
		} else {
			ob_start();
		}
	// Some backwards compatibility settings
	$bcoosConfig['xoops_url'] = XOOPS_URL;
	$bcoosConfig['root_path'] = XOOPS_ROOT_PATH.'/';
	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');
// #################### Detect variables now, reuse later ###########################
define('_PHP_SELF', $_SERVER['PHP_SELF']);
// SERVER followed by ENV comparison :: apache/iis, cgi/isapi :: ..so check'em all :)
!empty($_SERVER['HTTP_HOST'])       ? define('_HTTP_HOST'      , $_SERVER['HTTP_HOST'])       : define('_HTTP_HOST'      , $_ENV['HTTP_HOST']);
!empty($_SERVER['QUERY_STRING'])    ? define('_QUERY_STRING'   , $_SERVER['QUERY_STRING'])    : define('_QUERY_STRING'   , $_ENV['QUERY_STRING']);
!empty($_SERVER['SCRIPT_NAME'])     ? define('_SCRIPT_NAME'    , $_SERVER['SCRIPT_NAME'])     : define('_SCRIPT_NAME'    , $_ENV['SCRIPT_NAME']);
!empty($_SERVER['HTTP_REFERER'])    ? define('_HTTP_REFERER'   , $_SERVER['HTTP_REFERER'])    : define('_HTTP_REFERER'   , $_ENV['HTTP_REFERER']);
!empty($_SERVER['REQUEST_METHOD'])  ? define('_REQUEST_METHOD' , $_SERVER['REQUEST_METHOD'])  : define('_REQUEST_METHOD' , $_ENV['REQUEST_METHOD']);
!empty($_SERVER['HTTP_USER_AGENT']) ? define('_HTTP_USER_AGENT', $_SERVER['HTTP_USER_AGENT']) : define('_HTTP_USER_AGENT', $_ENV['HTTP_USER_AGENT']);
$R_URI = !empty($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : $_ENV['REQUEST_URI'];
!empty($R_URI) ? define('_REQUEST_URI', $R_URI) : define('_REQUEST_URI', _PHP_SELF._QUERY_STRING);
// Ordered by priority ..let's fetch that buggers IP
$tmp_ip[1] = $_SERVER['HTTP_X_FORWARDED_FOR'] ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_ENV['HTTP_X_FORWARDED_FOR'];
$tmp_ip[2] = $_SERVER['HTTP_CLIENT_IP']       ? $_SERVER['HTTP_CLIENT_IP']       : $_ENV['HTTP_CLIENT_IP'];
$tmp_ip[3] = $_SERVER['REMOTE_ADDR']          ? $_SERVER['REMOTE_ADDR']          : $_ENV['REMOTE_ADDR'];
switch(TRUE) {
	case !empty($tmp_ip[1]):
		$tmp_ip[1] = preg_replace('/,.*/', '', $tmp_ip[1]);
		define('_REMOTE_ADDR', $tmp_ip[1]);
		break;
	case !empty($tmp_ip[2]):
		define('_REMOTE_ADDR', $tmp_ip[2]);
		break;
	case !empty($tmp_ip[3]):
		define('_REMOTE_ADDR', $tmp_ip[3]);
		break;
	default:
		define('_REMOTE_ADDR', '0.0.0.0'); // empty or filled ..dunno yet
}
// Check if https is on
$tmp_https = $_SERVER['HTTPS'] ? $_SERVER['HTTPS'] : $_ENV['HTTPS'];
(strtoupper($tmp_https) == 'ON') ? define('_HTTPS', TRUE) : define('_HTTPS', FALSE);
// Detect platform
switch(strtoupper(PHP_OS)) {
	case 'WINDOWS':
	case 'CYGWIN':
	case 'WINNT':
	case 'WIN32':
		define('_OS', 'W');
		break;
	case 'DARWIN':
	case 'OSX':
		define('_OS', 'M');
		break;
	default:
		define('_OS', 'U');
}
// ################# :: Register Globals Compatibility :: #################
$globals_test = @ini_get('register_globals');
if ( isset($globals_test) && empty($globals_test) ) {
	// These still need some work :: Cookie|Server|Env are ok now.
	if ( !empty($_GET) )  { extract($_GET, EXTR_SKIP);  }
	if ( !empty($_POST) ) { extract($_POST, EXTR_OVERWRITE); }
	define('_GLOBALS', FALSE);
	} else {
		define('_GLOBALS', TRUE);
	}
// #################### Error reporting settings ##################
include_once(XOOPS_ROOT_PATH.'/class/debug/debug.php');
// ############## Include common functions file ##############
include_once(XOOPS_ROOT_PATH.'/include/functions.php');
// ################ Include unallowed words/hosts list ###############
// Will be moved to core api
$xoopsBadUnames = file(XOOPS_ROOT_PATH.'/modules/system/cache/badunames.php');
$xoopsBadEmails = file(XOOPS_ROOT_PATH.'/modules/system/cache/bademails.php');
$xoopsBadWords  = file(XOOPS_ROOT_PATH.'/modules/system/cache/badwords.php');
$xoopsBadIps    = file(XOOPS_ROOT_PATH.'/modules/system/cache/badips.php');
$xoopsBadAgents = file(XOOPS_ROOT_PATH.'/modules/system/cache/badagents.php');
// ############## Ban unwanted IP address ##############
if ( hasMatch($xoopsBadIps, _REMOTE_ADDR) || hasMatch($xoopsBadAgents, _HTTP_USER_AGENT) ) {
	header('Status: 404 Not Found');
	exit();
}
// ################# Include version info file ##########################
include_once(XOOPS_ROOT_PATH.'/include/version.php');
// #################### Include site-wide lang file ##################
if ( @file_exists(XOOPS_ROOT_PATH.'/language/'.$bcoosConfig['language'].'/global.php') ) {
	include_once(XOOPS_ROOT_PATH.'/language/'.$bcoosConfig['language'].'/global.php');
	} else {
		include_once(XOOPS_ROOT_PATH.'/language/english/global.php');
	}
set_error_handler('ex_error_handler');
// ################ Include page-specific lang file ################
if ( isset($xoopsOption['pagetype']) ) {
	if ( @file_exists(XOOPS_ROOT_PATH.'/language/'.$bcoosConfig['language'].'/'.$xoopsOption['pagetype'].'.php') ) {
		include_once(XOOPS_ROOT_PATH.'/language/'.$bcoosConfig['language'].'/'.$xoopsOption['pagetype'].'.php');
		} else {
			include_once(XOOPS_ROOT_PATH.'/language/english/'.$xoopsOption['pagetype'].'.php');
		}
	}
if ( !defined('_USE_MULTIBYTES') ) {
	define('_USE_MULTIBYTES', 0);
}
// #################### Include xoops check ##################
include_once(XOOPS_ROOT_PATH.'/include/xoopscheck.php');
// #################### Connect to DB ##################
if ($bcoosConfig['prefix'] && $bcoosConfig['dbhost'] && $bcoosConfig['dbname'] && $bcoosConfig['database']) {
	include_once(XOOPS_ROOT_PATH.'/class/database/'.$bcoosConfig['database'].'.php');
	$db = new Database();
	$db->setPrefix($bcoosConfig['prefix']);
	$result1 = @$db->connect($bcoosConfig['dbhost'], $bcoosConfig['dbuname'], $bcoosConfig['dbpass'],$bcoosConfig['dbname'], $bcoosConfig['db_pconnect']);
	if (!$result1) {
		echo $db->error();
		exit();
	}
	
	$db->setDebug($bcoosConfig['debug_mode']);
}
// #################### Include text sanitizer ##################
include_once(XOOPS_ROOT_PATH.'/class/module.textsanitizer.php');
$myts = new MyTextSanitizer();
$myts->setAllowImage($bcoosConfig['allow_image']);
$myts->setAllowLibrary($bcoosConfig['allow_library']);
// ############## Include basic constructor classes ##############
unset($xoopsUser);
include_once(XOOPS_ROOT_PATH.'/class/xoopsobject.php');
include_once(XOOPS_ROOT_PATH.'/class/xoopsgroup.php');
include_once(XOOPS_ROOT_PATH.'/class/xoopsuser.php');
include_once(XOOPS_ROOT_PATH.'/class/xoopsmodule.php');
include_once(XOOPS_ROOT_PATH.'/class/xoopsblock.php');
// ############## Login a user if there is a valid session ##############
if ($bcoosConfig['use_sessions'] == 1) {
	session_set_cookie_params ($bcoosConfig['session_expire'] , "/");
	if ( $_COOKIE[$bcoosConfig['session_name']] ) {
		session_id($_COOKIE[$bcoosConfig['session_name']]);
	}
	if (function_exists('session_cache_expire')) {
		$session_cache_expire = intval($bcoosConfig['session_expire']/60);
		@session_cache_expire($session_cache_expire);
	}
	session_start();
	$session_var = $_SESSION[$bcoosConfig['session_name']];
	} else {
		$session_var = $_COOKIE[$bcoosConfig['session_name']];
	}
if ( !empty($session_var) ) {
	include_once(XOOPS_ROOT_PATH.'/class/sessions.class.php');
	$session = new XoopsUserSession($session_var);
	if ($session->isValid()) {
		$session->update();
		$xoopsUser = new XoopsUser($session->uid());
	}
}
// ############## Setup module specific stuff if in a module dir ##############
if ( @file_exists('include/bcoos_module.php') ) {
	$module_dir  = basename(dirname(_PHP_SELF));
	$xoopsModule =& XoopsModule::getByDirname($module_dir);
if ( !$xoopsModule || !$xoopsModule->isActivated() ) {
	include_once(XOOPS_ROOT_PATH.'/header.php');
	echo '<h4>'._MODULENOEXIST.'</h4>';
	include_once(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/'.$bcoosConfig['language'].'/language.php') ) {
	include_once(XOOPS_ROOT_PATH.'/modules/'.$xoopsModule->dirname().'/language/'.$bcoosConfig['language'].'/language.php');
	} else {
		if ( @file_exists(XOOPS_ROOT_PATH.'/modules/'.$xoopsModule->dirname().'/language/english/languge.php') ) {
			include_once(XOOPS_ROOT_PATH.'/modules/'.$xoopsModule->dirname().'/language/english/language.php');
		}
	}
}
//---------------------------------------------------------------------------------------//
} // END XOOPS_MAINFILE_INCLUDED
?>
