<?php

/*
	[Discuz!] (C)2001-2006 Comsenz Inc.
	This is NOT a freeware, use is subject to license terms

	$RCSfile: admincp.php,v $
	$Revision: 1.53.2.3 $
	$Date: 2006/09/28 02:34:15 $
*/

require_once './include/common.inc.php';
require_once DISCUZ_ROOT.'./admin/global.func.php';
@include_once DISCUZ_ROOT.'./forumdata/cache/cache_bbcodes.php';
require_once DISCUZ_ROOT.'./include/cache.func.php';
$discuz_action = 211;
include language('admincp');
if($adminid <= 0) {

	$cpaccess = 0;

} else {

	if(!$discuz_secques && $forcesecques) {
		cpheader();
		cpmsg('secques_invalid');
	}

	if($adminipaccess && $adminid == 1 && !ipaccess($onlineip, $adminipaccess)) {
		$cpaccess = 2;
	} else {
		$query = $db->query("SELECT errorcount FROM {$tablepre}adminsessions WHERE uid='$discuz_uid' AND ip='$onlineip' AND dateline+1800>'$timestamp'", 'SILENT');
		if($db->error()) {
			$db->query("DROP TABLE IF EXISTS {$tablepre}adminsessions");
			$db->query("CREATE TABLE {$tablepre}adminsessions (uid mediumint(8) UNSIGNED NOT NULL default '0', ip char(15) NOT NULL default '', dateline int(10) unsigned NOT NULL default '0', errorcount tinyint(1) NOT NULL default '0')");
			$cpaccess = 1;
		} else {
			if($session = $db->fetch_array($query)) {
				if($session['errorcount'] == -1) {
					$db->query("UPDATE {$tablepre}adminsessions SET dateline='$timestamp' WHERE uid='$discuz_uid'", 'UNBUFFERED');
					$cpaccess = 3;
				} elseif($session['errorcount'] <= 3) {
					$cpaccess = 1;
				} else {
					$cpaccess = 0;
				}
			} else {
				$db->query("DELETE FROM {$tablepre}adminsessions WHERE uid='$discuz_uid' OR dateline+1800<'$timestamp'");
				$db->query("INSERT INTO {$tablepre}adminsessions (uid, ip, dateline, errorcount)
					VALUES ('$discuz_uid', '$onlineip', '$timestamp', '0')");
				$cpaccess = 1;
			}
		}
	}

}

if($action && !in_array($action, array('main', 'header', 'menu', 'illegallog', 'ratelog', 'modslog', 'banlog', 'cplog', 'errorlog'))) {
	switch($cpaccess) {
		case 0:
			$extra = 'PERMISSION DENIED';
			break;
		case 1:
			$extra = 'AUTHENTIFICATION(ERROR #'.intval($session['errorcount']).')';
			break;
		case 2:
			$extra = 'IP ACCESS DENIED';
			break;
		case 3:
			$extra = $semicolon = '';
			if(is_array($_GET)) {
				foreach(array_merge($_GET, $_POST) as $key => $val) {
					if(!in_array($key, array('action', 'sid', 'formhash', 'admin_password')) && $val) {
						$extra .= $semicolon.$key.'=';
						if(is_array($val)) {
							$extra .= 'Array(';
							foreach($val as $arraykey => $arrayval) {
								$extra .= $arraykey.'='.cutstr($arrayval, 15).'; ';
							}
							$extra .= ')';
						} else {
							$extra .= cutstr($val, 15);
						}
						$semicolon = '; ';
					}
				}
				$extra = nl2br(dhtmlspecialchars($extra));
			}
			break;
	}
	@$fp = fopen(DISCUZ_ROOT.'./forumdata/cplog.php', 'a');
	@flock($fp, 2);
	@fwrite($fp, "<?PHP exit('Access Denied'); ?>\t$timestamp\t".dhtmlspecialchars($discuz_userss)."\t$adminid\t$onlineip\t".dhtmlspecialchars($action)."\t$extra\n");
	@fclose($fp);
}

if($cpaccess == 0) {

	clearcookies();
	loginmsg('noaccess');
} elseif($cpaccess == 1) {
	if(!$admin_password || md5($admin_password) != $discuz_pw) {
		if($admin_password) {
			$db->query("UPDATE {$tablepre}adminsessions SET errorcount=errorcount+1 WHERE uid='$discuz_uid'");
		}
		//$action = empty($action) ? 'home' : $action;

		loginmsg('', '', 'login');
	} else {
		$db->query("UPDATE {$tablepre}adminsessions SET errorcount='-1' WHERE uid='$discuz_uid'");
		loginmsg('login_succeed', 'admincp.php?'.$_SERVER['QUERY_STRING'].'');
		if(!empty($url_forward)) {
			echo "<meta http-equiv=refresh content=\"0;URL=$url_forward\">";exit;
		}
	}
} elseif($cpaccess == 2) {

	loginmsg('noaccess_ip');

}
if(empty($action) || isset($frames)) {
	parse_str($_SERVER['QUERY_STRING'], $getarray);

	$extra = $and = '';
	foreach($getarray as $key => $value) {
		if($key == 'action' && in_array($value, array('header', 'menu'))) {
			$extra .= $and.$key.'=home';
		}elseif(!in_array($key, array('sid', 'frames'))) {
			$extra .= $and.$key.'='.rawurlencode($value);
			$and = '&';
		}
	}

	$extra = $extra && $action ? $extra : (!empty($runwizard) ? 'action=runwizard' : 'action=home');

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Discuz! Administrator's Control Panel</title>
<meta http-equiv="Content-Type" content="text/html; charset=<?=$charset?>">
</head>

<frameset rows="63,*" cols="*" frameborder="no" border="0" framespacing="0">
	<frame name="header" noresize scrolling="no" src="admincp.php?action=header&sid=<?=$sid?>">
	<frameset cols="165,*" rows="*" frameborder="no" border="0" framespacing="0">
		<frame name="menu" noresize scrolling="yes" src="admincp.php?action=menu&sid=<?=$sid?>">
		<frame name="main" noresize scrolling="yes" src="admincp.php?<?=$extra?>&sid=<?=$sid?>">
	</frameset>
</frameset>

</html>
<?

	exit();

}
if($action == 'menu') {
	require_once DISCUZ_ROOT.'./admin/menu.inc.php';
} elseif($action == 'header') {
	require_once DISCUZ_ROOT.'./admin/header.inc.php';
} elseif($action == 'logout') {
	$db->query("DELETE FROM {$tablepre}adminsessions WHERE uid='$discuz_uid' AND errorcount='-1'");
	loginmsg('logout_succeed', 'index.php');
} else {
	$cpscript = '';
	$isfounder = isfounder();
	if($adminid == 1) {

		if($action == 'home') {
			$cpscript = 'home';
		} elseif($action == 'runwizard') {
			$cpscript = 'runwizard';
		} elseif($action == 'settings') {
			$cpscript = 'settings';
		} elseif($action == 'xspace') {
			$cpscript = 'supesite';
		} elseif($action == 'passport' || $action == 'siteengine' || $action == 'shopex') {
			$cpscript = 'passport';
		} elseif($action == 'avatarshow_config' || $action == 'avatarshow_register') {
			$cpscript = 'avatarshow';
		} elseif($action == 'qihoo_config' || $action == 'qihoo_topics') {
			$cpscript = 'qihoo';
		} elseif($action == 'forumadd' || $action == 'forumsedit' || $action == 'forumsmerge' || $action == 'forumdetail' || $action == 'forumdelete' || $action == 'moderators' || $action == 'threadtypes' || $action=='forumcopy') {
			$cpscript = 'forums';
		} elseif($action == 'editmember' || $action == 'memberadd' || $action == 'members' || $action == 'membersmerge' || $action == 'editgroups' || $action == 'access' || $action == 'editcredits' || $action == 'editmedals' || $action == 'memberprofile' || $action == 'profilefields' || $action == 'ipban' ||  $action == 'banmember') {
			$cpscript = 'members';
		} elseif($action == 'usergroups' || $action == 'admingroups' || $action == 'ranks') {
			$cpscript = 'groups';
		} elseif($action == 'announcements') {
			$cpscript = 'announcements';
		} elseif($action == 'styles') {
			$cpscript = 'styles';
		} elseif($isfounder && ($action == 'templates' || $action == 'tpladd' || $action == 'tpledit')) {
			$cpscript = 'templates';
		} elseif($action == 'modmembers' || $action == 'modthreads' || $action == 'modreplies') {
			$cpscript = 'moderate';
		} elseif($action == 'recyclebin') {
			$cpscript = 'recyclebin';
		} elseif($action == 'alipay' || $action == 'orders') {
			$cpscript = 'ecommerce';
		} elseif($action == 'forumlinks' || $action == 'onlinelist' || $action == 'medals' || $action == 'censor' || $action == 'discuzcodes' || $action == 'smilies' || $action == 'attachtypes' || $action == 'crons' || $action == 'creditslog') {
			$cpscript = 'misc';
		} elseif($action == 'adv' || $action == 'advadd' || $action == 'advedit') {
			$cpscript = 'advertisements';
		} elseif($isfounder && ($action == 'export' || $action == 'import' || $action == 'runquery' || $action == 'optimize')) {
			$cpscript = 'database';
		} elseif($action == 'attachments') {
			$cpscript = 'attachments';
		} elseif($action == 'counter') {
			$cpscript = 'counter';
		} elseif($action == 'threads') {
			$cpscript = 'threads';
		} elseif($action == 'pushthreads') {
			$cpscript = 'pushthreads';
		} elseif($action == 'prune' || $action == 'pmprune') {
			$cpscript = 'prune';
		} elseif($action == 'updatecache' || $action == 'jswizard' || $action == 'fileperms') {
			$cpscript = 'tools';
		} elseif($action == 'plugins' || $action == 'pluginsconfig' || $action == 'pluginsedit' || $action == 'pluginhooks' || $action == 'pluginvars') {
			$cpscript = 'plugins';
		} elseif($action == 'illegallog' || $action == 'ratelog' || $action == 'modslog' || $action == 'medalslog' || $action == 'banlog' || $action == 'cplog' || $action == 'errorlog') {
			$cpscript = 'logs';
		} elseif($action == 'tradelog') {
			$cpscript = 'tradelog';
		}

		if($radminid != $groupid) {
			$query = $db->query("SELECT disabledactions FROM {$tablepre}adminactions WHERE admingid='$groupid'");
			$dactionarray = ($dactionarray = unserialize($db->result($query, 0))) ? $dactionarray : array();
			if(in_array($action, $dactionarray)) {
				cpheader();
				cpmsg('action_noaccess');
			}
		}
	} elseif($adminid == 2 || $adminid == 3) {

		if($action == 'home') {
			$cpscript = 'home';
		} elseif((($allowedituser || $allowbanuser) && ($action == 'editmember' || $action == 'banmember')) || ($allowbanip && $action == 'ipban')) {
			$cpscript = 'members';
		} elseif($action == 'forumrules') {
			$cpscript = 'forums';
		} elseif($allowpostannounce && $action == 'announcements') {
			$cpscript = 'announcements';
		} elseif(($allowmoduser && $action == 'modmembers') || ($allowmodpost && ($action == 'modthreads' || $action == 'modreplies'))) {
			$cpscript = 'moderate';
		} elseif(($allowcensorword && $action == 'censor') || $action == 'logout') {
			$cpscript = 'misc';
		} elseif($allowmassprune && $action == 'prune') {
			$cpscript = 'prune';
		} elseif($action == 'plugins') {
			$cpscript = 'plugins';
		} elseif($allowviewlog && ($action == 'ratelog' || $action == 'modslog' || $action == 'banlog')) {
			$cpscript = 'logs';
		}

	}
	if($cpscript) {
		require_once DISCUZ_ROOT.'./admin/'.$cpscript.'.inc.php';
	} else {
		cpheader();
		cpmsg('noaccess');
	}
	if($action != 'menu' && $action != 'header') {
		cpfooter();
	}

}
output();

function loginmsg($message, $url_forward = '', $msgtype = 'message') {
	extract($GLOBALS, EXTR_SKIP);
	$message = isset($msglang[$message]) ? $msglang[$message] : $message;

	if($msgtype == 'message') {
		$message = '<tr><td>&nbsp;</td><td align="center" colspan="3" >'.$message;
		if($url_forward) {
			$message .= "<br><br><a href=\"$url_forward\">$lang[message_redirect]</a>";
			$url_forward = transsid($url_forward);
			$message .= "<script>setTimeout(\"redirect('$url_forward');\", 1250);</script><br><br><br></td><td>&nbsp;</td></tr>";
		} else {
			$message .= '<br><br><br>';
		}
	} else {
		$extra = isset($action) && empty($frames) && $action != 'logout' ? '?frames=yes&'.$_SERVER['QUERY_STRING'] : (in_array($action, array('header', 'menu', 'logout')) ? '' : '?'.$_SERVER['QUERY_STRING']);
		$message = '<form method="post" name="login" action="admincp.php'.$extra.'">'.
			'<input type="hidden" name="sid" value="'.$sid.'">'.
			'<input type="hidden" name="frames" value="yes>'.
			'<input type="hidden" name="url_forward" value="'.$url_forward.'">'.
			'<tr><td>&nbsp;</td><td align="right">'.$lang['username'].':</td>'.
    			'<td>'.$discuz_user.'</td><td><a href="'.$link_logout.'&referer='.$indexname.'" target="_blank">'.$lang['menu_logout'].'</a></td>'.
    			'<td>&nbsp;</td></tr>'.
    			'<tr><td>&nbsp;</td><td align="right">'.$lang['password'].':</td><td><input type="password" name="admin_password" size="25"></td>'.
    			'<td>&nbsp;</td><td>&nbsp;</td></tr>'.
  			'<tr><td>&nbsp;</td><td class="line1">&nbsp;</td>'.
    			'<td class="line1" align="center"><input type="submit" class="button" value="'.$lang['submit'].'" /></form><script language="JavaScript">document.login.admin_password.focus();</script></td>'.
    			'<td class="line1">&nbsp;</td><td>&nbsp;</td></tr>';

	}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Discuz! Administrator's Control Panel</title>
<meta http-equiv="Content-Type" content="text/html; charset=<?=$charset?>">
<link href="./images/admincp/login.css" rel="stylesheet" type="text/css" />
</head>
<script language="JavaScript">
if (self.parent.frames.length != 0) {
	self.parent.location=document.location;
}
function redirect(url) {
	window.location.replace(url);
}
</script>
<br /><br /><br /><br />
<table width="600" border="0" cellpadding="8" cellspacing="0" class="logintable">
<tr class="loginheader"><td width="80"></td><td width="100"></td><td></td><td width="120"></td><td width="80"></td></tr>
<tr style="height:40px"><td>&nbsp;</td>
<td class="line1"><span style="color:#ffff66;font-size:14px;font-weight: bold;"><?=$lang['admin_page']?></span></td>
<td class="line1">&nbsp;</td>
<td class="line1">&nbsp;</td>
<td>&nbsp;</td></tr>
<tr><td>&nbsp;</td><td class="line2">&nbsp;</td><td class="line2">&nbsp;</td><td class="line2">&nbsp;</td><td>&nbsp;</td></tr>
<?=$message?>
<tr><td>&nbsp;</td><td class="line2">&nbsp;</td><td class="line2">&nbsp;</td><td class="line2">&nbsp;</td><td>&nbsp;</td></tr>
<tr><td colspan="5" align="center">Powered by <a href="http://www.discuz.net" target="_blank" style="color: #fff"><b>Discuz!</b></a>
&nbsp;&copy; 2001-2006 <a href="http://www.comsenz.com" target="_blank" style="color: #fff">Comsenz Inc.</a></td></tr>
<tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr></table>
</html>
<?
	dexit();
}

?>