<?php
error_reporting(E_ERROR | E_WARNING | E_PARSE);
set_magic_quotes_runtime(0);
$mtime = explode(' ', microtime());
$mm_starttime = $mtime[1] + $mtime[0];
define('MODEPATH', substr(dirname(__FILE__), 0, -8));
define('MONKEYMAO',true);
if(PHP_VERSION < '4.1.0') {
	$_GET = &$HTTP_GET_VARS;
	$_POST = &$HTTP_POST_VARS;
	$_COOKIE = &$HTTP_COOKIE_VARS;
	$_SERVER = &$HTTP_SERVER_VARS;
	$_ENV = &$HTTP_ENV_VARS;
	$_FILES = &$HTTP_POST_FILES;
}

require_once MODEPATH.'./includes/global.func.php';

$magic_quotes_gpc = get_magic_quotes_gpc();
$register_globals = @ini_get('register_globals');

if(!$register_globals || !$magic_quotes_gpc) {
	@extract(filter($_POST));
	@extract(filter($_GET));
	if(!$magic_quotes_gpc) {
		$_FILES = filter($_FILES);
	}
}

require_once MODEPATH.'./config.inc.php';
require_once MODEPATH.'./includes/db_mysql.class.php';
$time = time();
$db = new db_class();
$db -> mysql_con($dbhost ,$dbuser, $dbpassword,$dbname,$connectmode);
unset($dbhost ,$dbuser, $dbpassword,$dbname,$connectmode);
$PHP_SELF = $_SERVER['PHP_SELF'] ? $_SERVER['PHP_SELF'] : $_SERVER['SCRIPT_NAME'];
$SCRIPT_FILENAME = str_replace('\\\\', '/', ($_SERVER['PATH_TRANSLATED'] ? $_SERVER['PATH_TRANSLATED'] : $_SERVER['SCRIPT_FILENAME']));
$homeurl = 'http://'.$_SERVER['HTTP_HOST'].preg_replace("/\/+(api|archiver|wap)?\/*$/i", '', substr($PHP_SELF, 0, strrpos($PHP_SELF, '/'))).'/';


$extrahead = '';
$_DCOOKIE = $_DSESSION = $_DCACHE = $_DPLUGIN = array();

$prelength = strlen($tablepre);
foreach($_COOKIE as $key => $val) {
	if(substr($key, 0, $prelength) == $tablepre) {
		$_DCOOKIE[(substr($key, $prelength))] = filter($val);
	}
}

unset($prelength);
$cachemiss = '';

$cachemiss .= (@include MODEPATH.'./cache/cache_settings.php') ? '' : 'settings';
if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
	$onlineip = getenv('HTTP_CLIENT_IP');
} elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {
	$onlineip = getenv('HTTP_X_FORWARDED_FOR');
} elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {
	$onlineip = getenv('REMOTE_ADDR');
} elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {
	$onlineip = $_SERVER['REMOTE_ADDR'];
}
$onlineip = preg_replace("/^([\d\.]+).*/", "\\1", $onlineip);

$sid = filter($transsidstatus && (isset($_GET['sid']) || isset($_POST['sid'])) ?
	(isset($_GET['sid']) ? $_GET['sid'] : $_POST['sid']) :
	(isset($_DCOOKIE['sid']) ? $_DCOOKIE['sid'] : ''));

$mm_auth_key = md5($_DCACHE['settings']['authkey'].$_SERVER['HTTP_USER_AGENT']);
list($mm_pw, $mm_mid) = isset($_DCOOKIE['auth']) ? explode("\t", authcode($_DCOOKIE['auth'], 'DECODE')) : array('', '');

if(isset($_DCOOKIE['auth']) && !$mm_mid) {
	clearcookies();
}

$newpm = $newpmexists = $sessionexists = $seccode = $adminid =0;

if($sid) {
	if($mm_mid) {	
		$query = $db->query("SELECT s.sid, s.pageviews AS spageviews, s.lastolupdate, s.seccode, m.mid AS mm_mid,
			m.username AS mm_user, m.password AS mm_pw, m.adminid, m.groupid, m.email, m.pageviews, m.invisible, m.lastvisit, m.lastactivity
			FROM {$tablepre}sessions s, {$tablepre}members m
			WHERE m.mid=s.mid AND s.sid='$sid' AND CONCAT_WS('.',s.ip1,s.ip2,s.ip3,s.ip4)='$onlineip' AND m.mid='$mm_mid'
			AND m.password='$mm_pw'");
			
	} else {
		$query = $db->query("SELECT sid, mid AS sessionmid, adminid, groupid, pageviews AS spageviews, lastolupdate, seccode
			FROM {$tablepre}sessions WHERE sid='$sid' AND CONCAT_WS('.',ip1,ip2,ip3,ip4)='$onlineip'");

	}
	if($_DSESSION = $db->fetch_array($query)) {

		$sessionexists = 1;
		if(!empty($_DSESSION['sessionmid'])) {
			$query = $db->query("SELECT m.mid AS mm_mid, m.username AS mm_user, m.password AS mm_pw, m.adminid, m.groupid, m.email,m.pageviews, m.invisible,
				m.lastvisit, m.lastactivity
				FROM {$tablepre}members m WHERE mid='$_DSESSION[sessionmid]'");
			$_DSESSION = array_merge($_DSESSION, $db->fetch_array($query));
		}
	} else {
		$query = $db->query("SELECT sid, adminid,groupid, pageviews AS spageviews, lastolupdate, seccode
			FROM {$tablepre}sessions WHERE sid='$sid' AND CONCAT_WS('.',ip1,ip2,ip3,ip4)='$onlineip'");
		if($_DSESSION = $db->fetch_array($query)) {
			clearcookies();
			$sessionexists = 1;
		}
	}
}
if(!$sessionexists) {

	if($mm_mid) {
		$query = $db->query("SELECT mid AS mm_mid, username AS mm_user, password AS mm_pw,
			adminid, groupid, email,  pageviews, invisible, lastvisit, lastactivity
			FROM {$tablepre}members WHERE mid='$mm_mid' AND password='$mm_pw'");
		if(!($_DSESSION = $db->fetch_array($query))) {
			clearcookies();
		}
	}



	$_DSESSION['sid'] = random(6);
	$_DSESSION['seccode'] = random(4, 1);
}

$_DSESSION['dateformat'] = empty($_DSESSION['dateformat']) ? $dateformat : $_DSESSION['dateformat'];
$_DSESSION['timeformat'] = empty($_DSESSION['timeformat']) ? $timeformat : ($_DSESSION['timeformat'] == 1 ? 'h:i A' : 'H:i');
$_DSESSION['timeoffset'] = isset($_DSESSION['timeoffset']) && $_DSESSION['timeoffset'] != 9999 ? $_DSESSION['timeoffset'] : $timeoffset;
@extract($_DSESSION);

$lastvisit = empty($lastvisit) ? $time - 86400 : $lastvisit;
$timenow = array('time' => gmdate("$dateformat $timeformat", $time + 3600 * $timeoffset),
	'offset' => ($timeoffset >= 0 ? ($timeoffset == 0 ? '' : '+'.$timeoffset) : $timeoffset));

if(empty($mm_mid) || empty($mm_user)) {
	$mm_user = $extgroupids = '';
	$mm_mid = $adminid = $posts = $upbizinfo = $allowdownload = $allowedvisit = $invisible=0;
	$groupid = empty($groupid) ? 5 : $groupid;
	$avatarshowid = 0;
} else {
	$mm_userss = $mm_user;
	$mm_user = addslashes($mm_user);
}
define('FORMHASH', formhash());
if(empty($_DCOOKIE['sid']) || $sid != $_DCOOKIE['sid']) {
	dsetcookie('sid', $sid, 604800);
}

$_DSESSION['groupid'] = $groupid = empty($groupid) ? 5 : $groupid;
$cachemiss .= (@include MODEPATH.'./cache/usergroup_'.intval($groupid).'.php') ? '' : ' usergroup_'.$groupid;
if($adminid){
	$cachemiss .= (@include MODEPATH.'./cache/cache_groups.php') ? '' : ' groups';
	if($cache_admingroups[$adminid]){
			$cachemiss .= (@include MODEPATH.'./cache/admingroups_'.$adminid.'.php') ? '' : ' admingroups';
	}
}
if($gzip && function_exists('ob_gzhandler')) {
	ob_start('ob_gzhandler');
} else {
	$gzip = 0;
	ob_start();
}
$system_version = array(
	'company' => ' inc, All rights reserved',
	'homesite' => 'pic.71116.com',
	'version' => '1.251',
	'system' => 'piccms!',

);
if($cachemiss){
	require_once MODEPATH.'./includes/cache.func.php';
	cache();
}

?>