<?php
/*******************************************************************
 * 版权协议：2009-2019
 * 版权所有: PHP-北京金南曦网络技术有限公司旗下PHPWMS，PHP商城系统技术支持中心http://www.php188.net，并保留所有权利。 
 * PHP商城系统网址: http://www.php188.com
 * PHPWMS系统网址：http://www.phpwms.com
 * 销售电话：010-87875088
 * 销售电话：010-87875188 
*------------------------------------------------------------------
 * 这不是一个完全免费软件！您只能在不用于商业目的的前提下对程序代码进
 * 行修改和使用；不允许对程序代码以任何形式任何目的的再发布。
 *------------------------------------------------------------------
 * 文件名称：admin_core.php
 * 摘　　要：会员
 * 适用范围：PHPWMS 1.0
 * 版　　本：1.0
 *------------------------------------------------------------------
 * Script Written By phpwms项目组
 * 最后更新： 2009-09-24 phpwms 由PHP金南曦网络技术有限公司整理发布
 *******************************************************************/
if(!defined('IN_SYS'))exit('access die');
include('../include/classes/cls.sys_crypt.php'); 
$do		= trim($_REQUEST['do']);
$type	= trim($_REQUEST['type']);
$action = trim($_REQUEST['action']);
$tpl -> assign("do",$do);
$tpl -> assign("type",$type);
$tpl -> assign("action",$action);
$id = abs(intval(trim($_GET['id'])));
require SYS_ROOT.'include/member.prive.php';
if(is_post()){
	$task = $_POST['task'];
	$data = $_POST['data'];
	$id = to_int($_POST['id']);
	$type = 'core';
	switch ($task){
		case 'search'://会员搜索
			$name = $_POST['login_name'];
			//if(empty($name))exit($sp->bgmsg('请输入要搜索的用户名.','admincp.php?action=core'));
			$sex = $_POST['sex'];
			$email  = $_POST['email'];
			if(!empty($email) && !is_email($email))exit($sp->bgmsg('请输入正确的邮箱格式.','admincp.php?action=core'));
			$group_id = $_POST['group_id'];
			if(!empty($group_id) && is_array($group_id)){
				foreach ($group_id as &$g)$g = intval(trim($g));
				$group_id = array_filter($group_id);
				$group_ids = !empty($group_id)?join(',',$group_id):'';
			}
			$sql = "SELECT * FROM ##__user_info as u LEFT JOIN ##__user_group AS g ON u.`group_id` = g.`id` WHERE  1=1 ";
			$c = "SELECT COUNT(*) AS T FROM ##__user_info as u LEFT JOIN ##__user_group AS g ON u.`group_id` = g.`id` WHERE  1=1 ";
			$s = !empty($sex)?"AND  u.sex = '$sex' ":'';
			$e  =  !empty($email)?"AND  u.email like '%$email%' ":' ';
			$g = !empty($group_ids)?"AND g.id in ($group_ids)":'';
			if(empty($s) && empty($g) && empty($e)){
				$sql .= " u.`login_name` LIKE  '%$name%' ";
				$c .= " u.`login_name` LIKE  '%$name%' ";
			}else{
				$sql .=  $s.$e.$g." AND u.`login_name` LIKE '%$name%' ";
				$c .=  $s.$e.$g." AND u.`login_name` LIKE '%$name%' ";
			}
			$data = $db->fetch($sql);
			$tpl->assign('data',$data);
			$tpl->assign('do','search_usr');
			$pn = '会员搜索';
			$tpl -> assign("admin_nav",shownav('<a href="?action='.$action.'&type=list">会员搜索</a>&nbsp;&raquo;&nbsp;'.$pn));
			$sp -> outPut('core.tpl',$pn);
			break;
		case 'user_add'://增加用户
			if(!getAdminPri('7101'))exit(NO_AUTH_LOGIN);
					$sqls= 'SELECT * FROM ##__user_info WHERE login_name = "'.$data['login_name'].'"';
					$ree  = $db -> sql_query($sqls);
					$arrc = $db -> sql_fetchrowset($ree);
					if(count($arrc))die($sp -> bgmsg('此用户已被注册！',"admincp.php?action=core&do=user_add"));
					$data['birthday'] = strtotime($data['birthday']);
					if(!empty($data['telephone']) && !preg_match("/^1(3[0-9]|5[0,1,2,3,6,8,9]|8[6,8,9])[0-9]{8}$/", $data['telephone']))die($sp -> bgmsg('电话号码格式错误',"admincp.php?action=core&do=user_add"));
					$qq = $data['qq'];
					if(!is_numeric($qq) && !empty($qq))die($sp -> bgmsg('QQ号格式错误.',"admincp.php?action=".$type."&do=user_add"));
					$formname='head_pic';
					if(!empty($_FILES['head_pic']['size'])){
						require_once SYS_ROOT.'include/classes/upload.class.php';
						$upload = new Upload();
						$upload->upload_type = array('jpg','gif','png');
						$upload->upload_maxsize = 1024*1024;//1M
						$upload->upload_save_path = 'files/upfiles/member/';
						$u = $upload->up('head_pic');
						if(!$u){
							$msg = $upload->getMsg();
								die($sp -> bgmsg($msg['msg'],"admincp.php?action=".$type."&do=user_add"));
						}else{
							$data['head_pic']= $u['upload_part'];
						}
					}
					if(!$data['login_name']  ||  !$data['login_pwd'] || !$data['email'])die($sp -> bgmsg('打星号的为必填项！',"admincp.php?action=core&do=user_add"));
					$data['reg_time']=time();
					$data['login_ip']= get_ip();
					$data['login_pwd']=md5($data['login_pwd']);
					if(!$data['birthday'])unset($data['birthday']);
					if(!$data['mobile'])unset($data['mobile']);
					if(!$data['zipcode'])unset($data['zipcode']);
					if(!$data['qq'])unset($data['qq']);
					$data['reg_time'] = time();
					if($data['email'] && !is_email($data['email']))die($sp -> bgmsg('E-Mail不正确！',"admincp.php?action=core&do=user_add"));
					$db->insert($data,'user_info');
					$msg = "用户添加成功！";
					$sp -> bgmsg($msg,"admincp.php?action=core&do=associator");
			break;
		case 'user_edit':
			if(!getAdminPri('7101'))exit(NO_AUTH_LOGIN);
					$msg = "更新用户成功！";
					$formname='head_pic';
					$data['birthday'] = strtotime($data['birthday']);
					if(!empty($data['telephone']) && !preg_match("/^1(3[0-9]|5[0,1,2,3,6,8,9]|8[6,8,9])[0-9]{8}$/", $data['telephone'])){
						die($sp -> bgmsg('电话号码格式错误',"admincp.php?action=core&do=associator"));
					}
					$id = abs(intval($data['id']));
					if(empty($id))die($sp -> bgmsg('非法操作',"admincp.php?action=core&do=associator"));;
					$sqls= 'SELECT * FROM ##__user_info WHERE id = '.$id;
					$ree  = $db -> sql_query($sqls);
					$arrc = $db -> sql_fetchrowset($ree);
					$formname='head_pic';
					$qq = $data['qq'];
					if(!is_numeric($qq) && !empty($qq)){
						die($sp -> bgmsg('QQ号格式错误.',"admincp.php?action=".$type."&do=user_edit&id=".$id));
					}
					if(!empty($_FILES['head_pic']['size'])){
						require_once SYS_ROOT.'include/classes/upload.class.php';
						$upload = new Upload();
						$upload->upload_type = array('jpg','gif','png');
						$upload->upload_maxsize = 1024*1024;//1M
						$upload->upload_save_path = 'files/upfiles/member/';
						$u = $upload->up('head_pic');
						if(!$u){
							$msg = $upload->getMsg();
								die($sp -> bgmsg($msg['msg'],"admincp.php?action=".$type."&do=user_edit&id=".$id));
						}else{
							$data['head_pic']= $u['upload_part'];
							$file = $arrc[0]['head_pic'];
						   @unlink(SYS_ROOT.$file);
						}
					}
					if(!$data['login_name'] or  !$data['email'])
					{
						die($sp -> bgmsg('打星号的为必填项！',"admincp.php?action=".$type."&do=user_edit&id=".$id));
					}
					$data['mobile'] = intval($data['mobile']);
					$data['zipcode'] = $data['zipcode'];
					$data['login_time']=time();
					$data['login_ip']= get_ip();
					$db->update($data,'user_info',array('id'=>$id));
					$sp -> bgmsg($msg,"admincp.php?action=".$type."&do=associator");
			break;
			case "group_edit":
				if(!getAdminPri('7102'))exit(NO_AUTH_LOGIN);
					$msg = "用户组更新成功！";
					$group_id = to_int($_POST['group_id']);
					//用户私有权限
					$priv_config = $_POST['group'];
					$member_priv_string = '';
					if(!empty($priv_config) && is_array($priv_config)){
						foreach ($priv_config as &$p){
							$p = abs(intval(trim($p)));
						}
						$member_priv_string = join('|',$priv_config); 
					}
					$data['pri_config'] = $member_priv_string;
					$name = html_encode($data['name']);
					if(empty($group_id)) die($sp -> bgmsg('参数错误',"admincp.php?action=core&do=popedom"));
					if(empty($name))die($sp -> bgmsg('请输入用户组名称.',"admincp.php?action=core&do=group_edit&id=".$group_id));
					$check_sql = "SELECT * FROM ##__user_group WHERE `name` = '$name' AND `id` <> '$group_id' ";
					$check_data = $db->fetch($check_sql,TRUE);
					if(!empty($check_data))die($sp -> bgmsg('存在相同的分组.',"admincp.php?action=".$type."&do=group_edit&id=".$group_id));
					if($check_data['is_system']=='1')unset($data['name']);			
					$sql = 'UPDATE ##__user_group SET ' .$db -> sql_build_array('UPDATE', $data).' WHERE id = '.$group_id;
					$re  = $db -> sql_query($sql);
					$sp -> bgmsg($msg,"admincp.php?action=".$type."&do=popedom");
				break;
			case "group_add":
				if(!getAdminPri('7102'))exit(NO_AUTH_LOGIN);
					$msg = "用户组添加成功！";
					$name = html_encode($data['name']);
					if(empty($name))die($sp -> bgmsg('请输入用户组名称.',"admincp.php?action=".$type."&do=group_add"));
					$check = $db->fetch("SELECT * FROM ##__user_group WHERE `name` ='$name' ;");
					if(!empty($check))die($sp -> bgmsg('已存在相同的分组.',"admincp.php?action=".$type."&do=group_add"));
					$sql = "INSERT INTO `##__user_group` (`name`, `pri_config`, `state`) VALUES ('".$data['name']."', '".$member_priv_string."', '".$data['state']."')";
					$re  = $db -> sql_query($sql);
					exit($sp -> bgmsg($msg,"admincp.php?action=".$type."&do=popedom"));
				break;
			default:$sp->bgmsg('参数错误','admincp.php?action=core');
	}
	exit();
}
//for get
switch ($do){
	case 'associator'://会员列表
			if(!getAdminPri('7101'))exit(NO_AUTH_LOGIN);
			$sql = 'SELECT a.login_name,a.integral,a.login_time,a.reg_time,a.id ,b.name FROM ##__user_info a inner join ##__user_group b on a.group_id=b.id order by a.id desc';
			$page = new ShowPage();
			$page->PageSize = 25;
			$page->Total = $db -> getCount($sql);
			$sql .= " limit  ".$page->OffSet();
			$re  = $db -> sql_query($sql);
			$showpage = $page->ShowLink();
			$arr = $db -> sql_fetchrowset($re);
			for($i=0;$i<count($arr);$i++)
			{
				if($arr[$i]['login_time'])
				{
					$arr[$i]['login_time']=date("Y-m-d H:i:s",$arr[$i]['login_time']);
				}
			}
	        $tpl -> assign("data",$arr);
	        $pn = '用户列表';
			$tpl -> assign("show_page",$showpage);
		break;
	case 'popedom'://用户组
			if(!getAdminPri('7102'))exit(NO_AUTH_LOGIN);
			$sql = 'SELECT* FROM ##__user_group order by id desc';
			$arr = $db->fetch($sql);
			$pn = '会员权限管理';
			$tpl -> assign("data",$arr);
		break;
	case 'group_edit'://编辑用户组
			if(empty($id))exit('crack die');
			$sql = 'SELECT* FROM ##__user_group where id='.$id;
			$arr = $db->fetch($sql,TRUE);
			if(empty($arr))die($sp -> bgmsg('无可用数据！',"admincp.php?action=core&do=popedom"));
			$group_list_cfg = $arr['pri_config'];
			$list = array_filter(array_map("trim",explode('|',$group_list_cfg)));
			foreach ($_member_priv as &$m){
				$m['checked'] = in_array($m['p'],$list)?'1':'0';
			}
			$group_tree = array_to_tree($_member_priv,'id','parent');
			$pn = '编辑用户组';
			$tpl -> assign("data",$arr);
		break;
	case 'deleteGroup'://删除用户组
		if(!getAdminPri('7102'))exit(NO_AUTH_LOGIN);
		$group_id = abs(intval(trim($_GET['id'])));
		if(empty($group_id))exit('非法操作');
		$check = $db->fetch("SELECT * FROM ##__user_group WHERE `id` = '$group_id' ;",TRUE);
		if($check['is_system']=='1')exit($sp->bgmsg('系统默认分组,不允许删除','admincp.php?action=core&do=associator'));
		if(empty($check))exit('1|无可用数据');
		$db->sql_query("DELETE FROM ##__user_group WHERE `id` = '$group_id';");
		exit('2|操作成功');
		break;
	case 'user_delete'://删除用户
			if(!getAdminPri('7101'))exit(NO_AUTH_LOGIN);
			if(empty($id))exit($sp->bgmsg('参数错误','admincp.php?action=core&do=associator'));
			$check_data = $db->fetch("SELECT * FROM ##__user_info WHERE `id` = '$id';",TRUE);
			if(empty($check_data))exit($sp->bgmsg('无可用数据','admincp.php?action=core&do=associator'));
			$f = $db->fetch("SELECT * FROM ##__user_info WHERE `id` = '$id';",TRUE);
			@unlink(SYS_ROOT.'/'.$f['head_pic']);
			$db->query("DELETE FROM ##__user_info WHERE `id` = '$id';");
			exit($sp->bgmsg('操作成功','admincp.php?action=core&do=associator'));
		break;
	case 'user_add'://添加用户
			if(!getAdminPri('7101'))exit(NO_AUTH_LOGIN);
			$sqll = 'SELECT* FROM ##__user_group';
			$ree  = $db -> sql_query($sqll);
			$arrr = $db -> sql_fetchrowset($ree);
			for($i=0;$i<count($arrr);$i++)
			{
				$sel.="<option value='".$arrr[$i]['id']."' >".$arrr[$i]['name']."</option>";
			}
			$pn = '添加用户';
			$tpl -> assign("user_group",$sel);
		break;
	case 'user_edit'://编辑用户
		if(!getAdminPri('7101'))exit(NO_AUTH_LOGIN);
			$id = abs(intval($_GET['id']));
			if(empty($id))exit($sp->bgmsg('参数错误.','admincp.php?action=core&do=associator'));
			$sql = 'SELECT* FROM ##__user_info where id='.$id;
			$re  = $db -> sql_query($sql);
			$arr = $db -> sql_fetchrowset($re);
			if(empty($arr))exit($sp->bgmsg('无可用数据.','admincp.php?action=core&do=associator'));
			$sqll = 'SELECT* FROM ##__user_group';
			$ree  = $db -> sql_query($sqll);
			$arrr = $db -> sql_fetchrowset($ree);
			if(empty($arrr))exit($sp->bgmsg('无分组数据,请先加分组.','admincp.php?action=core&do=group_add'));
			for($k=0;$k<count($arrr);$k++)
			{
				if($arrr[$k]['id']==$arr[0]['group_id'])
				{
					$selected='selected';
				}else{
					$selected='';
				}
				$sel.="<option value='".$arrr[$k]['id']."' $selected>".$arrr[$k]['name']."</option>";
			}
			$tpl -> assign("user_group",$sel);
			$tpl -> assign("data",$arr);
			$tpl -> assign("city",trim($arr[0]['city']));
			$pn  = '编辑用户';
			$tpl -> assign("province",trim($arr[0]['province']));
		break;
	default://会员搜索
		$pn = '会员搜索';
		if(!getAdminPri('7101'))exit(NO_AUTH_LOGIN);
		$sql ="SELECT * FROM `##__user_group` WHERE `state`=1";
	    $re  = $db -> sql_query($sql);
	    $arr = $db -> sql_fetchrowset($re);
	    $tpl -> assign("user_group",$arr);
		$db -> sql_freeresult($re);
		$db -> sql_close();
}
if($do=='group_add'){
	$group_tree = array_to_tree($_member_priv,'id','parent');
}
$tpl->assign('group_list',$group_tree);
$tpl -> assign("admin_nav",shownav('<a href="?action='.$action.'&type=list">会员管理</a>&nbsp;&raquo;&nbsp;'.$pn));
$sp -> outPut('core.tpl',$pn);
?>
