<?php
	!defined('DIRCMS_MEMBER') && exit('Access Denied!');
	$action=empty($action)?'editphoto':trim($action);
	require_once(DIRCMS_ROOT.'include/member.input.class.php');
	include(DIRCMS_ROOT.'include/member.class.php');
	$member=new member();
	require_once(DIRCMS_ROOT.'include/select.class.php');
	$s=new select();
	require_once(DIRCMS_ROOT.'include/html.class.php');
	$html=new html();
	require_once(DIRCMS_ROOT.'include/list.class.php');
	switch($action)
	{
		case 'editphoto':
			if(isset($do_submit)) 
			{
				if(!$uptype)
				{
					$t=$info['facephoto'];
					if(!preg_match('/^[a-z0-9\.\/_]{2,100}$/i',$t))showmsg('头像修改失败!'); /// 防止意外
				}
				else
				{
					require_once(DIRCMS_ROOT.'include/upload.class.php');
					$userphotoupload=new upload('userphoto','images/userphoto','userphoto_'.$_userid,array('jpg','jpeg','gif','bmp','png'));
					$imagearr=$userphotoupload->files;
					$t=$imagearr[0];
				}
				unset($info);
				$info['facephoto']=$t;
				$db->update(DB_PRE.'member',$info,'id='.intval($_userid));
				$db->update(DB_PRE.'member_cache',$info,'id='.intval($_userid));
				showmsg('头像修改成功!',$DIRCMS['site_url'].'member/index.php?file=user&action=editphoto');
			}
			$faceselect='<select name="info[facephoto]" onchange="document.getElementById(\'showface\').src=\''.DIRCMS_PATH.'\'+this.value">';
			$flist=glob(DIRCMS_ROOT.'member/images/face/*.*');
			foreach($flist as $filename)
			{
				if(in_array(get_fileext($filename),array('gif','jpg','jpeg','bmp','png')))
				{	
					$isselected=basename($filename)==basename($_facephoto)?'selected="selected"':'';
					$faceselect.='<option value="member/images/face/'.basename($filename).'" '.$isselected.'>'.basename($filename).'</option>';
				}
			}
			$faceselect.='</select>';
			include member_tlp('user_editphoto');
			break;

		case 'myyp':
			createYp();
			header("location:$_companyurl");
			break;
		case 'edit':
			$r=cache_read('cache_membermodel'.$_modelid.'.cache.php',DIRCMS_ROOT.'data/cache_model/');
			//if(!$r['iscompany'])$_flag=true;
			$_flag=$r['iscompany']?false:true;
			if(isset($do_submit)) 
			{
				$member_input=new member_input($_modelid);
				$info=$member_input->check($info);
				$r=cache_read('cache_membermodel'.$_modelid.'.cache.php',DIRCMS_ROOT.'data/cache_model/');
				$m_table=$r['name'];
				if(isset($info['id']))unset($info['id']);
				if(isset($info['username']))unset($info['username']);
				if(isset($info['groupid']))unset($info['groupid']);
				if(isset($info['amount']))unset($info['amount']);
				if(isset($info['point']))unset($info['point']);
				$db->update(DB_PRE.'member',$info,'id='.intval($_userid));
				$db->update(DB_PRE.'member_cache',$info,'id='.intval($_userid));
				$db->update(DB_PRE.$m_table,$info,'userid='.intval($_userid));

				if(UC)
				{
					$username = $_username;
					$email = $info['email'];
					$password = $_password;
					$action = 'edit';
					require DIRCMS_ROOT.'api/passport_server_ucenter.php';
				}
				createYp();
				showmsg('会员信息修改成功!',$DIRCMS['site_url'].'member/index.php?file=user&action=edit');
			}
			$r=cache_read('cache_membermodel'.$_modelid.'.cache.php',DIRCMS_ROOT.'data/cache_model/');
			$m_table=$r['name'];
			$c=$db->fetch_one("SELECT a.*,b.* FROM ".DB_PRE."member a LEFT JOIN ".DB_PRE.$m_table." b ON a.id=b.userid WHERE a.id=".intval($_userid));
			require_once(DIRCMS_ROOT.'include/select.class.php');
			$s=new select();
			require_once(DIRCMS_ROOT.'include/form.class.php');	
			$f=new form('_m'.$_modelid);
			$base_r=$_flag?$f->get($c):array();
			include member_tlp('user_edit');
			break;
		case 'editpsw':
			if(isset($do_submit)) 
			{
				$psw=$member->get($_userid,'password');
				if($psw['password']!=$member->password($old_password))showmsg('旧密码输入不正确!');
				if(!is_pwd($old_password))showmsg('新密码输入不正确!');
				if($new_password!=$chk_password)showmsg('两次密码输入不一致!');
				if(UC)
				{
					$username = $_username;
					$email = $_email;
					$action = 'editpwd';
					require DIRCMS_ROOT.'api/passport_server_ucenter.php';
				}
				$db->update(DB_PRE.'member',array('password'=>$member->password($new_password)),' id='.intval($_userid));
				$db->update(DB_PRE.'member_cache',array('password'=>$member->password($new_password)),' id='.intval($_userid));
				showmsg('会员密码修改成功!');
			}
			include member_tlp('user_editpsw');
			break;
		case 'collect':
			if(isset($do_submit)) 
			{
				if(!$id)showmsg('未选中任何文件!');
				$id=array_map('intval',$id);
				foreach($id as $_id)
				{
					$db->query("DELETE FROM `".DB_PRE."collect` WHERE `".DB_PRE."collect`.`id`=$_id AND `".DB_PRE."collect`.`userid`=$_userid",true);
				}
				showmsg('删除成功!');
			}
			$l=new dircmslist();	
			$where="`userid`={$_userid}";
			$orderby="id DESC";
			$page=max(isset($page)?intval($page):1,1);
			$result=$l->getlist(DB_PRE.'collect',$where,$orderby,$page,18);
			$pages=$l->pages;
			include member_tlp('user_collect');
			break;
		case 'add_collect':
			if(!$verify || $verify!=md5(AUTH_KEY.$id.md5($title).$url) || !is_numeric($id))showmsg('Bad Request!!!',$DIRCMS['site_url']);
			if(!$_userid)showmsg('请先登录!','index.php?file=login');
			$info=array();
			$info['id']=intval($id);
			$info['userid']=$_userid;
			$info['title']=$title;
			$info['url']=$url;
			$info['time']=TIME;
			$db->insert(DB_PRE.'collect',$info,true);
			unset($info);
			showmsg('添加收藏成功!');
			break;
		case 'space':
			include member_tlp('user_space_info');
			break;
		case 'space_template':
			$list=glob(TPL_ROOT.'comtmp/*');
			$files=glob(TPL_ROOT.'comtmp/*.*');
			$dirs=array_diff($list,$files);
			$r=array();
			foreach($dirs as $v)
			{
				if(basename($v)!='person')$r[]=basename($v);
			}
			if($do)
			{

				if(in_array($_gradeid,$templatepriv) || $_groupid==1)
				{
					if(!$_iscompmayed)showmsg('您尚未开通店铺或者店铺尚未通过审核!','?file=user&action=space_template');
					if(!in_array($template,$r))showmsg('指定模板不存在!','?file=user&action=space_template');
					$db->query("UPDATE `".DB_PRE."member` SET `".DB_PRE."member`.`template`='{$template}' WHERE `".DB_PRE."member`.`id`=".intval($_userid));
					$db->query("UPDATE `".DB_PRE."member_cache` SET `".DB_PRE."member_cache`.`template`='{$template}' WHERE `".DB_PRE."member_cache`.`id`=".intval($_userid));
					createYp();
					showmsg('黄页模板设置成功!','index.php?file=user&action=space_template');
				}
				else
				{
					showmsg('只有Vip商家才能使用自选模板功能!','?file=user&action=space_template');
				}
			}
			include member_tlp('user_template');
			break;
		case 'space_edit':
			if(isset($do_submit)) 
			{
				$info['typeid']=intval($info['typeid']);
				if(!$info['typeid'])showmsg('请选择商家类型!');
				$member_input=new member_input($_modelid);
				$info=$member_input->check($info);
				$r=cache_read('cache_membermodel'.$_modelid.'.cache.php',DIRCMS_ROOT.'data/cache_model/');
				$_iscompany=$r['iscompany']?true:false;
				if(!$_iscompany)showmsg($r['modelname'].'禁止开通黄页店铺!');
				$m_table=$r['name'];
				if(isset($info['id']))unset($info['id']);
				if(isset($info['username']))unset($info['username']);
				if(isset($info['groupid']))unset($info['groupid']);
				if(isset($info['amount']))unset($info['amount']);
				if(isset($info['point']))unset($info['point']);
				if(isset($info['gradeid']))unset($info['gradeid']);
				$info['gradeid']=$_gradeid?$_gradeid:10;
				$info['iscompmayed']=COMPANYCHECK&&!$_iscompmayed?0:1;
				if(!in_array($_gradeid,$templatepriv)) //////// 2010-11-2
				{
					$info['template']='company';
				}
				if(!$_iscompmayed)$info['iscompmayed']=COMPANYCHECK?0:1;
				$db->update(DB_PRE.'member',$info,'id='.intval($_userid));
				$db->update(DB_PRE.'member_cache',$info,'id='.intval($_userid));
				$db->update(DB_PRE.$m_table,$info,'userid='.intval($_userid));
				if($_iscompmayed)
				{
					createYp();
					showmsg('黄页店铺信息编辑成功!');
				}
				else
				{
					createYp();
					if(COMPANYCHECK)
					{
						showmsg('黄页店铺成功开通，请等待管理员审核!');
					}
					else
					{
						showmsg('黄页店铺成功开通!');
					}
				}
			}

			$r=cache_read('cache_membermodel'.$_modelid.'.cache.php',DIRCMS_ROOT.'data/cache_model/');
			$_iscompany=$r['iscompany']?true:false;
			if($_iscompany)
			{
				$m_table=$r['name'];
				$c=$db->fetch_one("SELECT a.*,b.* FROM ".DB_PRE."member a LEFT JOIN ".DB_PRE.$m_table." b ON a.id=b.userid WHERE a.id=".intval($_userid));
				require_once(DIRCMS_ROOT.'include/select.class.php');
				$s=new select();
				require_once(DIRCMS_ROOT.'include/form.class.php');	
				$f=new form('_m'.$_modelid);
				$base_r=$f->get($c);
				$select=$s->getSelect('info[typeid]','请选择商家类型',DB_PRE.'member_type',true,'type');
				include member_tlp('user_space');
			}
			else
			{
				$r=$db->fetch_one("SELECT userid FROM ".DB_PRE."member_convert WHERE userid=".$_userid);
				if(!$r)
				{
					$r=cache_read('cache_membermodel'.$_modelid.'.cache.php',DIRCMS_ROOT.'data/cache_model/');
					$modelname=$r['modelname'];
					$r=cache_read('cache_membermodel.cache.php',DIRCMS_ROOT.'data/cache_model/');
					$modelselect='<select name="convertid">';
					if($r)foreach($r as $_r)
					{
						if($_r['iscompany'])$modelselect.='<option value='.$_r['id'].'>'.$_r['modelname'].'</option>';
					}
					$modelselect.='</select>';
					include member_tlp('user_space_notice');
				}
				else
				{
					include member_tlp('user_space_convert');
				}
			}
			break;
		case 'requestconvert':
			$r=$db->fetch_one("SELECT id FROM ".DB_PRE."member_convert WHERE userid=".$_userid);
			if(!$r)
			{
				$r=cache_read('cache_membermodel'.$_modelid.'.cache.php',DIRCMS_ROOT.'data/cache_model/');
				$_iscompany=$r['iscompany']?true:false;
				if($_iscompany)showmsg('您已经具备开通黄页资格，无需转型!');
				$info=array();
				$info['userid']=$_userid;
				$info['username']=$_username;
				$info['amount']=$_amount;
				$info['point']=$_point;
				$info['regtime']=$_regtime;
				$info['logintime']=$_logintime;
				$info['loginip']=$_loginip;
				$info['modelid']=intval($convertid);
				$info['nowmodelid']=intval($_modelid);
				$r=cache_read('cache_membermodel'.$info['modelid'].'.cache.php',DIRCMS_ROOT.'data/cache_model/');
				if(!$r)showmsg('该模型不存在!');
				if($db->insert(DB_PRE.'member_convert',$info))showmsg('提交申请成功，请等待管理员审核!','index.php?file=user&action=space_edit');
			}
			else
			{
				showmsg('您已经申请转型，请等待管理员审核!');
			}
			break;
		case 'upgrade':
			$gradeid=intval($gradeid);
			$r=cache_read('membergrade'.$gradeid.'.cache.php',DIRCMS_ROOT.'data/cache/');
			if(!$r || $gradeid<=$_gradeid || !$gradeid)showmsg('会员升级失败!');
			if(UPGRADEMETHOD=='point') //扣点
			{
				if($_point<intval($r['point']))showmsg('会员点数不足,升级失败!');
				$db->update(DB_PRE.'member',array('gradeid'=>intval($r['grade']),'template'=>'default','vipstart'=>TIME,'vipto'=>(TIME+24*365*3600),'point'=>($_point-intval($r['point']))),'id='.$_userid);
				$db->update(DB_PRE.'member_cache',array('gradeid'=>intval($r['grade']),'template'=>'default','vipstart'=>TIME,'vipto'=>(TIME+24*365*3600),'point'=>($_point-intval($r['point']))),'id='.$_userid);
			}
			else
			{
				if($_amount<intval($r['amount']))showmsg('会员余额不足,升级失败!');
				$db->update(DB_PRE.'member',array('gradeid'=>intval($r['grade']),'template'=>'default','vipstart'=>TIME,'vipto'=>(TIME+24*365*3600),'amount'=>($_amount-floatval($r['amount']))),'id='.$_userid);
				$db->update(DB_PRE.'member_cache',array('gradeid'=>intval($r['grade']),'template'=>'default','vipstart'=>TIME,'vipto'=>(TIME+24*365*3600),'amount'=>($_amount-floatval($r['amount']))),'id='.$_userid);
			}
			showmsg('升级到'.$r['gradename'].'成功!','index.php?file=user&action=space');
			break;
		case 'space_menu':
			if(isset($do_submit))
			{
				$t=array();
				foreach($key as $k=>$v)
				{
					$t[$v]=array('name'=>$name[$k],'url'=>$url[$k]);
				}
				cache_write('menu'.$_userid.'.cache.php',$t,DIRCMS_ROOT.'company/cache/menu/');
				showmsg('菜单编辑成功!');
			}
			$menu=cache_read('menu'.$_userid.'.cache.php',DIRCMS_ROOT.'company/cache/menu/');
			$menu=$menu?$menu:array (
			   1 => array('name'=>'网站首页','url'=>$DIRCMS['site_url'].'company/company_'.$_userid.'.html'),
			  2 => array('name'=>'公司简介','url'=>$DIRCMS['site_url'].'company/about_'.$_userid.'.html'),
			  3 => array('name'=>'新闻动态','url'=>$DIRCMS['site_url'].'company/news_'.$_userid.'.html'),
			  4 => array('name'=>'优惠信息','url'=>$DIRCMS['site_url'].'company/coupon_'.$_userid.'.html'),
			  5 => array('name'=>'产品展示','url'=>$DIRCMS['site_url'].'company/product_'.$_userid.'.html'),
			  6 => array('name'=>'地图位置','url'=>$DIRCMS['site_url'].'company/map_'.$_userid.'.html'),
			  7 => array('name'=>'联系方式','url'=>$DIRCMS['site_url'].'company/contact_'.$_userid.'.html'),
			  8 => array('name'=>'在线留言','url'=>$DIRCMS['site_url'].'company/guestbook_'.$_userid.'.html'),
			);
			include member_tlp('user_space_menu');
			break;

		default:
			showmsg('Bad Request!!!',$DIRCMS['site_url']);
			break;
	}
?>