<?php
!defined('MayiCMS') && exit('Access Denied!');
define('IN_MYMPS', true);
include dirname(__FILE__)."/../../include/global.php";
require_once MYMPS_DATA."/config.php";
require_once MYMPS_DATA."/config.db.php";

if(!empty($mymps_global['cfg_allow_adminip'])){
	$al=0;
	$ip=GetIP();
	$allow_adminip=explode("\r\n",trim($mymps_global['cfg_allow_adminip']));
	foreach($allow_adminip as $k =>$v){
		if(ipin($ip,$v)){
			$al=1;
			continue;
		}
	}
	if($al!=1){
		write_msg('根据后台设置规则，您的IP:<font color=red>'.$ip.'</font>无法登陆后台管理！<br><br>如有误判，请前往官网提交工单以获得帮助。','olmsg');
		exit;
	}
}

require MYMPS_INC."/db.class.php";
require MYMPS_INC."/admin.class.php";
$authcodesettings=read_static_cache('authcodesettings');
$url = trim($url);
if ($part == 'chk'){
	define(CURSCRIPT,'admin_login');
	$url = $url ? $url : '?mayicms=1';
	if($authcodesettings['adminlogin'] == 1 && !$randcode = mymps_chk_randcode($checkcode)){
		write_msg('验证码输入错误，请返回重新输入');
		exit;
	}
	$username = trim($username);
	$password = trim($password);
	$pubdate  = $timestamp ? $timestamp : time();
	$ip		  = GetIP();
	$row = $db->getRow("SELECT * FROM {$db_mymps}admin WHERE userid='".$username."' AND pwd='".md5($password)."'");
	if($row){
		$admin_id  	= $row['userid'];
		$admin_name = $row['uname'];
		$admin_cityid = $row['cityid'];
		$mymps_admin -> mymps_admin_login($admin_id,$admin_name,$admin_cityid);
		$db->query("UPDATE {$db_mymps}admin SET loginip='".GetIP()."',logintime='$timestamp' WHERE userid='$row[userid]'");
		$db->query("INSERT INTO `{$db_mymps}admin_record_login` (id,adminid,adminpwd,pubdate,ip,result) VALUES ('','$username','".md5($password)."','$pubdate','$ip','1')");
		$c=mymps_count("member","WHERE userid = '$admin_id'");
		if(empty($c)){
			if($row['typeid']==1){
				$mlevel=$db->getOne("SELECT MAX(id) FROM `{$db_mymps}member_level`");
				$levelm=$db->getRow("SELECT * FROM `{$db_mymps}member_level` WHERE id = '$mlevel'");
			}else{
				$levelm=$db->getRow("SELECT * FROM `{$db_mymps}member_level` WHERE id = '1'");
			}
			$db -> query("INSERT INTO `{$db_mymps}member`(id,userid,userpwd,logo,prelogo,email,safequestion,safeanswer,levelid,joinip,loginip,jointime,logintime,money_own,openid,openid_wx,cname,status,score,if_corp) VALUES ('','$row[userid]','$row[pwd]','','','$row[email]','','','$levelm[id]','$ip','$ip','$timestamp','$timestamp','$levelm[money_own]','','','$admin_name','1','$levelm[score]','0')");
		}else{
			$row['pwd']=$db->getOne("SELECT userpwd FROM `{$db_mymps}member` WHERE userid = '$admin_id'");
		}
		require MYMPS_INC."/member.class.php";
		$member_log -> in($admin_id,$row['pwd'],'off','noredirect');
		
		write_msg($admin_name."您已成功登陆系统管理中心",$url);
	}else{
		$db->query("INSERT INTO `{$db_mymps}admin_record_login` (id,adminid,adminpwd,pubdate,ip,result) VALUES ('','$username','***','$pubdate','$ip','0')");
		write_msg("您输入的用户名或密码错误，请返回重新输入");
	}
}elseif ($part == 'out'){
	define('IN_MYMPS', true);
	$mymps_admin -> mymps_admin_logout();
	write_msg("您已经安全退出系统管理中心","?");
}elseif ($part == 'default'){
	define('IN_MYMPS', true);
	$url 	 = trim($_GET['url']);
	if($mymps_admin -> mymps_admin_chk_getinfo()){
		write_msg("","?mayicms=1");
	}else{
		$dir = opendir(MYMPS_ROOT);
		$zipfile=array();
		while (($file = readdir($dir)) !== false)
		{
			$fileExt=pathinfo($file, PATHINFO_EXTENSION);
			if(in_array($fileExt,array('tar','gz','tar.gz','zip','rar'))){
				$zipfile[]= $file;
			}
		}
		closedir($dir);
		include(mayi_tpl("login"));
	}
}else{
	define('IN_MYMPS', true);
	write_msg("","?mayicms=1");
}

function ipin($ip1,$ips){
	$ips=str_replace('.*','',$ips);
	if(empty($ip1)){
		return false;
	}elseif(strstr($ip1,$ips)){  
		return true;
	}else{
		return false;  
	}
}
?>