<?php
!defined("MONKEYMAO") && die("Accessed Defined!");
$cache_usergroups = $cache_admingroups = array();
$query = $db -> query("SELECT groupid,grouptitle FROM {$tablepre}usergroups");
while($usergroup = $db -> fetch_array($query)){
	$cache_usergroups[$usergroup['groupid']]=$usergroup;
}
$query = $db -> query("SELECT adminid,grouptitle FROM {$tablepre}admingroups");
while($usergroup = $db -> fetch_array($query)){
	$cache_admingroups[$usergroup['adminid']]=$usergroup;
}
if($edit){
	$member = $db -> queryarray("SELECT * FROM {$tablepre}members WHERE mid='$edit'");
	if(!$member){
		cpmsg('error');
	}
	if(cpchecksubmit('edituser')){
		$newpassword = trim($newpassword);
		$newcompany=trim($newcompany);
		if($adminid !=1 && isset($newadminid)){
			cpmsg('error');
		}


		if($newpassword && $newpassword != $newpassword2) {
			cpmsg('member_password_twice');
		}
		if($newpassword && (!$newpassword || $newpassword != addslashes($newpassword))) {
			cpmsg('member_password_unaccepted');
		}
		$censorexp = '/('.str_replace("\r\n", '|', preg_quote($censoremail, '/')).')$/i';
		if(!isemail($newemail) || ($censoremail && preg_match($censorexp, $newemail))) {
			cpmsg('member_email_unaccepted');
		}
		$newpasswordmd=$newpassword ? md5($newpassword) : $member['password'];
		$newgroupid = ispage($newgroupid) ? $newgroupid : 3;
		$newgroupid = $newgroupid ? $newgroupid : 3;
		
		if(!$adminid){
			$newadminid = 0;		
		}
		$newadminid  = ispage($newadminid) ? $newadminid : 0;
		$db -> query("UPDATE {$tablepre}members SET password = '$newpasswordmd', adminid = '$newadminid', groupid = '$newgroupid', gender = '$newgender',  email = '$newemail', pageviews = '$newpageviews', allowedmode = '$sortkeys' WHERE mid='$edit'");		
		cpmsg('succeeded', 'index.php?action=members');
	}else{

		include theme('members_edit', 'themes/admin', 'admin');
	
	}
}elseif($menu == 'post'){
	if(cpchecksubmit('add')){
		$newusername = trim($newusername);
		$newpassword = trim($newpassword);
		$newcompany = trim($newcompany);
		if($adminid !=1 && isset($newadminid)){
			cpmsg("error");
		}
		$guestexp = '\xA1\xA1|\xAC\xA3|^Guest|^\xD3\xCE\xBF\xCD|\xB9\x43\xAB\xC8';		
		if(preg_match("/^\s*$|^c:\\con\\con$|[%,\*\"\s\t\<\>\&]|$guestexp/is", $newusername)) {
			cpmsg('member_username_unaccepted');
		}
		if(strlen($newusername) > 15) {
			cpmsg('member_username_toolong');
		}
		$query = $db -> query("SELECT mid FROM {$tablepre}members WHERE username='$newusername'");
		if($db -> num_rows($query)){
			cpmsg("member_username_ison");
		}
		if($newpassword != $newpassword2) {
			cpmsg('member_password_twice');
		}
		if(!$newpassword || $newpassword != addslashes($newpassword)) {
			cpmsg('member_password_unaccepted');
		}
		$censorexp = '/('.str_replace("\r\n", '|', preg_quote($censoremail, '/')).')$/i';
		if(!isemail($newemail) || ($censoremail && preg_match($censorexp, $newemail))) {
			cpmsg('member_email_unaccepted');
		}
		$newpasswordmd=md5($newpassword);

		$newgroupid = ispage($newgroupid) ? $newgroupid : 3;
		$newgroupid = $newgroupid ? $newgroupid : 3;
		
		if(!$adminid){
			$newadminid = 0;
		
		}
		$newadminid  = ispage($newadminid) ? $newadminid : 0;
		$db -> query("INSERT INTO {$tablepre}members (username, password, adminid, groupid, gender, regtime, email, pageviews,  allowedmode) VALUES ('$newusername', '$newpasswordmd', '$newadminid', '$newgroupid', '$newgender', '$time', '$newemail', '$newpageviews', '$sortkeys') ");
		$newmid = $db -> insert_id();		
		
		cpmsg('succeeded', 'index.php?action=members');

	}else{
		include theme('members_post', 'themes/admin', 'admin');	
	}	
}elseif(cpchecksubmit('updatelist')){
	if(is_array($delete) && count($delete)){
		$ids = idarray($delete);
		$query = $db -> query("SELECT m.username FROM {$tablepre}members m  WHERE m.mid IN($ids)");
		$users = $common='';
		while($member = $db -> fetch_array($query)){
			$users.=$common.$member['username'].'('.$member['company'].')';
			$common = ', ';
		}		
		$db -> query("DELETE FROM {$tablepre}members WHERE mid IN($ids)");		
		cpmsg('succeeded', 'index.php?action=members');		
	}else{
		cpmsg('error');
	}
}elseif($del){
	$user = $db -> queryarray("SELECT m.username FROM {$tablepre}members m WHERE m.mid ='$del'");
	if($del == $mm_mid || ($adminid !=1 && $user['adminid'])){
		cpmsg('error');
	}	
	$db -> query("DELETE FROM {$tablepre}members WHERE mid='$del'");
	cpmsg('succeeded', 'index.php?action=members');		
}else{
	$page = ispage($page) ? $page : 1;
	$pagenum = 30;
	$start_limit = ($page-1) * $pagenum;	
	$members = array();
	$query = $db -> query("SELECT COUNT(*) FROM {$tablepre}members");
	$lines = $db -> result($query, 0);
	$paging = paging($lines, $pagenum, $page, 'index.php?action=members');
	$query = $db -> query("SELECT m.mid, m.username, m.adminid, m.groupid, m.regtime FROM {$tablepre}members m   ORDER BY m.regtime DESC LIMIT $start_limit, $pagenum");
	while($member = $db -> fetch_array($query)){	
		$member['regtime'] = gmdate("$dateformat $timeformat", $member['regtime'] + ($timeoffset * 3600));
		$members[$member['mid']] = $member;
	}
	include theme('members', 'themes/admin', 'admin');

}

?>