<?php
// $Id: common.php,v 1.3 2004/07/21 10:20:07 Farsus Exp $
// -------------------------------------------------------------------------//
//			       RUNCMS
//
//	 Relaible - Unique - Nocost &  Simplicity & ease off use
//                       < http://www.runcms.org >
// -------------------------------------------------------------------------//
// abuse hack 
// original suply by Alhpalogic e-xoops.alphalogic.org

define('_PHP_SELF', $HTTP_SERVER_VARS['PHP_SELF']);

// SERVER followed by ENV comparison :: apache/iis, cgi/isapi :: ..so check'em all :)
!empty($HTTP_SERVER_VARS['HTTP_HOST'])       ? define('_HTTP_HOST'      , $HTTP_SERVER_VARS['HTTP_HOST'])       : define('_HTTP_HOST'      , $HTTP_ENV_VARS['HTTP_HOST']);
!empty($HTTP_SERVER_VARS['QUERY_STRING'])    ? define('_QUERY_STRING'   , $HTTP_SERVER_VARS['QUERY_STRING'])    : define('_QUERY_STRING'   , $HTTP_ENV_VARS['QUERY_STRING']);
!empty($HTTP_SERVER_VARS['SCRIPT_NAME'])     ? define('_SCRIPT_NAME'    , $HTTP_SERVER_VARS['SCRIPT_NAME'])     : define('_SCRIPT_NAME'    , $HTTP_ENV_VARS['SCRIPT_NAME']);
!empty($HTTP_SERVER_VARS['HTTP_REFERER'])    ? define('_HTTP_REFERER'   , $HTTP_SERVER_VARS['HTTP_REFERER'])    : define('_HTTP_REFERER'   , $HTTP_ENV_VARS['HTTP_REFERER']);
!empty($HTTP_SERVER_VARS['REQUEST_METHOD'])  ? define('_REQUEST_METHOD' , $HTTP_SERVER_VARS['REQUEST_METHOD'])  : define('_REQUEST_METHOD' , $HTTP_ENV_VARS['REQUEST_METHOD']);
!empty($HTTP_SERVER_VARS['HTTP_USER_AGENT']) ? define('_HTTP_USER_AGENT', $HTTP_SERVER_VARS['HTTP_USER_AGENT']) : define('_HTTP_USER_AGENT', $HTTP_ENV_VARS['HTTP_USER_AGENT']);

// Ordered by priority ..let's fetch that buggers IP
$tmp_ip[1] = $HTTP_SERVER_VARS['HTTP_X_FORWARDED_FOR'] ? $HTTP_SERVER_VARS['HTTP_X_FORWARDED_FOR'] : $HTTP_ENV_VARS['HTTP_X_FORWARDED_FOR'];
$tmp_ip[2] = $HTTP_SERVER_VARS['HTTP_CLIENT_IP']       ? $HTTP_SERVER_VARS['HTTP_CLIENT_IP']       : $HTTP_ENV_VARS['HTTP_CLIENT_IP'];
$tmp_ip[3] = $HTTP_SERVER_VARS['REMOTE_ADDR']          ? $HTTP_SERVER_VARS['REMOTE_ADDR']          : $HTTP_ENV_VARS['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
}

//CHECKING FOR GET VARS ABUSERS
//CHECK $_GET
$abuse = false;
$my_get_vars = is_array($_GET) ? $_GET : $HTTP_GET_VARS;
foreach ($my_get_vars as $key => $content) {
	if (preg_match('/<(.*?)>/s', $content)) {
		$abuse = true;
		$ab_array[$key] = $content;
	}
	if (preg_match('/cd (.*?)>/s',$content)) {
		$abuse = true;
		$ab_array[$key] = $content;		
	}
	if (preg_match('/chmod (.*?)/s',$content)) {
		$abuse = true;
		$ab_array[$key] = $content;		
	}	
	if (preg_match('/ls (.*?)/s',$content)) {
		$abuse = true;
		$ab_array[$key] = $content;		
	}	
	if (preg_match('/uname (.*?)/s',$content)) {
		$abuse = true;
		$ab_array[$key] = $content;		
	}	
}
if ($abuse){
	$ab_time  = date("Y_m_d-H_i_s");
	$content = _REMOTE_ADDR.' tried to abuse the system:'."\n";
	foreach ($ab_array as $key => $var_content) {
		$content .= 'With trying to set the get var: '.$key.' to '.$var_content."\n";
	}
	$content .= "Query String: "._QUERY_STRING." Location: "._PHP_SELF;
	$filename = XOOPS_ROOT_PATH."/cache/abuse-".$ab_time.".log";
	if ( $file = fopen($filename, "w") ) {
		fwrite($file, $content);
		fclose($file);
	} 
	header('Status: 302 Found');
	header("Location: ".XOOPS_URL."/abuse.php");
}
// abuse hack 


if ( !defined('XOOPS_MAINFILE_INCLUDED') ) {
	exit();

	} else {
	error_reporting(2039);
	set_magic_quotes_runtime(0);

	include_once(XOOPS_ROOT_PATH.'/class/core.php');
	$api = new core_api();

	$objCache = new stdClass();

// #################### Include global config files ###############
	include_once(XOOPS_ROOT_PATH.'/modules/system/cache/config.php');
	$xoopsConfig['language'] = !empty($xoopsConfig['language']) ? $xoopsConfig['language'] : $xoopsConfig['default_language'];

	if ($xoopsConfig['gzip_compression']) {
		ob_start('ob_gzhandler');
		} else {
			ob_start();
		}

	// Some backwards compatibility settings
	$xoopsConfig['xoops_url'] = XOOPS_URL;
	$xoopsConfig['root_path'] = XOOPS_ROOT_PATH.'/';

	define('XOOPS_SIDEBLOCK_LEFT' , 0);
	define('XOOPS_SIDEBLOCK_RIGHT', 1);
	define('XOOPS_SIDEBLOCK_BOTH' , 2);

	define('XOOPS_CENTERBLOCK_TOPLEFT'     , 3);
	define('XOOPS_CENTERBLOCK_TOPRIGHT'    , 4);
	define('XOOPS_CENTERBLOCK_TOPCENTER'   , 5);
	define('XOOPS_CENTERBLOCK_TOPALL'      , 6);

	define('XOOPS_CENTERBLOCK_BOTTOMLEFT'  , 7);
	define('XOOPS_CENTERBLOCK_BOTTOMRIGHT' , 8);
	define('XOOPS_CENTERBLOCK_BOTTOMCENTER', 9);
	define('XOOPS_CENTERBLOCK_BOTTOMALL'   , 10);


	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 ###########################
$R_URI = !empty($HTTP_SERVER_VARS['REQUEST_URI']) ? $HTTP_SERVER_VARS['REQUEST_URI'] : $HTTP_ENV_VARS['REQUEST_URI'];
!empty($R_URI) ? define('_REQUEST_URI', $R_URI) : define('_REQUEST_URI', _PHP_SELF._QUERY_STRING);



// Check if https is on
$tmp_https = $HTTP_SERVER_VARS['HTTPS'] ? $HTTP_SERVER_VARS['HTTPS'] : $HTTP_ENV_VARS['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($HTTP_GET_VARS) )  { extract($HTTP_GET_VARS, EXTR_SKIP);  }
	if ( !empty($HTTP_POST_VARS) ) { extract($HTTP_POST_VARS, 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_once(XOOPS_ROOT_PATH.'/modules/system/cache/meta.php');

// #################### Include site-wide lang file ##################
if ( @file_exists(XOOPS_ROOT_PATH.'/language/'.$xoopsConfig['language'].'/global.php') ) {
	include_once(XOOPS_ROOT_PATH.'/language/'.$xoopsConfig['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/'.$xoopsConfig['language'].'/'.$xoopsOption['pagetype'].'.php') ) {
		include_once(XOOPS_ROOT_PATH.'/language/'.$xoopsConfig['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 ($xoopsConfig['prefix'] && $xoopsConfig['dbhost'] && $xoopsConfig['dbname'] && $xoopsConfig['database']) {
	include_once(XOOPS_ROOT_PATH.'/class/database/'.$xoopsConfig['database'].'.php');
	$db = new Database();
	$db->setPrefix($xoopsConfig['prefix']);
	$result1 = @$db->connect($xoopsConfig['dbhost'], $xoopsConfig['dbuname'], $xoopsConfig['dbpass'], $xoopsConfig['db_pconnect']);
	if (!$result1) {
		echo $db->error();
		exit();
	}
	$result2 = @$db->select_db($xoopsConfig['dbname']);
	if (!$result2) {
		echo $db->error();
		exit();
	}
	$db->setDebug($xoopsConfig['debug_mode']);
}

// #################### Include text sanitizer ##################
include_once(XOOPS_ROOT_PATH.'/class/module.textsanitizer.php');
$myts = new MyTextSanitizer();
$myts->setAllowImage($xoopsConfig['allow_image']);
$myts->setAllowLibrary($xoopsConfig['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 ($xoopsConfig['use_sessions'] == 1) {
	session_set_cookie_params ($xoopsConfig['session_expire'], "/");
	if ( $HTTP_COOKIE_VARS[$xoopsConfig['session_name']] ) {
		session_id($HTTP_COOKIE_VARS[$xoopsConfig['session_name']]);
	}
	if (function_exists('session_cache_expire')) {
		$session_cache_expire = intval($xoopsConfig['session_expire']/60);
		@session_cache_expire($session_cache_expire);
	}
	session_start();
	$session_var = $HTTP_SESSION_VARS[$xoopsConfig['session_name']];
	} else {
		$session_var = $HTTP_COOKIE_VARS[$xoopsConfig['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('xoops_version.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 ( empty($xoopsOption['page_style']) && ($xoopsModule->dirname() == $xoopsConfig['startpage']) && (basename(_PHP_SELF) == 'index.php')) {
		$xoopsOption['page_style'] = 1; // start
		} elseif ( empty($xoopsOption['page_style']) && (basename(_PHP_SELF) == 'index.php') ) {
			$xoopsOption['page_style'] = 2; // index
			} elseif ( empty($xoopsOption['page_style']) ) {
				$xoopsOption['page_style'] = 4; // other
			}

	if ( @file_exists(XOOPS_ROOT_PATH.'/modules/'.$xoopsModule->dirname().'/language/'.$xoopsConfig['language'].'/main.php') ) {
		include_once(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_once(XOOPS_ROOT_PATH.'/modules/'.$xoopsModule->dirname().'/language/english/main.php');
			}
		}
}

if ( empty($xoopsOption['page_style']) && (basename(_PHP_SELF) == 'index.php')) {
	$xoopsOption['page_style'] = 2; // index
	} elseif (empty($xoopsOption['page_style'])) {
		$xoopsOption['page_style'] = 4; // other
	}

//---------------------------------------------------------------------------------------//
} // END XOOPS_MAINFILE_INCLUDED
?>
