<?php
/***********************************************************
    [大米CMS] (C)2011 - 2011 damicms.com
    
	@function 广告管理

    @Filename AdAction.class.php $

    @Author 追影 QQ:279197963 $

    @Date 2011-11-23 09:59:26 $
*************************************************************/
class MemberAction extends CommonAction
{	
//支付宝配置
    function doap(){
	if(empty($_POST['ap_email']) || empty($_POST['ap_pid']) || empty($_POST['ap_key'])){$this->error('配置参数不能为空');}
	//改写配置文件
	$config_file = "./Public/Config/config.ini.php";
	$fp = fopen($config_file,"r");
	$configStr = fread($fp,filesize($config_file));
	fclose($fp);
	$configStr = preg_replace("/'AP_EMAIL'=>'.*'/","'AP_EMAIL'=>'".htmlspecialchars($_POST['ap_email'])."'",$configStr);
	$configStr = preg_replace("/'AP_PID'=>'.*'/","'AP_PID'=>'".htmlspecialchars($_POST['ap_pid'])."'",$configStr);
	$configStr = preg_replace("/'AP_KEY'=>'.*'/","'AP_KEY'=>'".htmlspecialchars($_POST['ap_key'])."'",$configStr);	
	$configStr = preg_replace("/'AP_TYPE'=>'.*'/","'AP_TYPE'=>'".htmlspecialchars($_POST['ap_type'])."'",$configStr);	
	$fp = fopen($config_file,"w") or die("<script>alert('写入配置失败，请检查安装目录/Public/Config/config.ini.php是否可写入！');history.go(-1);</script>");	
	fwrite($fp,$configStr);
	fclose($fp);
	//清理缓存
	$Webpath = './Web/Runtime/';
	$Adminpath = './Admin/Runtime/';
		if(is_dir($Webpath))
		{
			@deldir($Webpath);
		}
		if(is_dir($Adminpath))
		{
			@deldir($Adminpath);
		}
	//改写淘宝配置文件
	$ap_path = (intval($_POST['ap_type'])==1?'ap_jishi':'ap_danbao');
	$taobao_config = "./Trade/{$ap_path}/alipay.config.php";
	$fp = fopen($taobao_config,"r");
	$configStr = fread($fp,filesize($taobao_config));
	fclose($fp);
	$configStr = preg_replace("/alipay_config\['partner'\]='.*'/","alipay_config['partner']='".htmlspecialchars($_POST['ap_pid'])."'",$configStr);
	$configStr = preg_replace("/alipay_config\['key'\]='.*'/","alipay_config['key']='".htmlspecialchars($_POST['ap_key'])."'",$configStr);
	$fp = fopen($taobao_config,"w") or die("<script>alert('写入配置失败，请检查安装目录/Trade/{$ap_path}/alipay.config.php是否可写入！');history.go(-1);</script>");	
	fwrite($fp,$configStr);
	fclose($fp);
	//改写处理url
	$taobao_config = "./Trade/{$ap_path}/alipayapi.php";
	$fp = fopen($taobao_config,"r");
	$configStr = fread($fp,filesize($taobao_config));
	fclose($fp);
	
	$configStr = preg_replace("/notify_url='.*'/","notify_url='http://".$_SERVER['HTTP_HOST'].__ROOT__."/index.php/Public/shouquan'",$configStr);
	$configStr = preg_replace("/return_url='.*'/","return_url='http://".$_SERVER['HTTP_HOST'].__ROOT__."/Trade/{$ap_path}/return_url.php'",$configStr);
	$fp = fopen($taobao_config,"w") or die("<script>alert('写入配置失败，请检查安装目录/Trade/alipayapi.php是否可写入！');history.go(-1);</script>");	
	fwrite($fp,$configStr);
	fclose($fp);
	$this->success('支付宝在线配置成功!');
	}

function doqqset(){
	//改写配置文件
	$config_file = "./Public/Config/config.ini.php";
	$fp = fopen($config_file,"r");
	$configStr = fread($fp,filesize($config_file));
	fclose($fp);
	$configStr = preg_replace("/'QQ_APPID'=>'.*'/","'QQ_APPID'=>'".htmlspecialchars($_POST['qq_appid'])."'",$configStr);
	$configStr = preg_replace("/'QQ_APPKEY'=>'.*'/","'QQ_APPKEY'=>'".htmlspecialchars($_POST['qq_appkey'])."'",$configStr);
	$configStr = preg_replace("/'QQ_LOGIN'=>'.*'/","'QQ_LOGIN'=>'".htmlspecialchars($_POST['qq_login'])."'",$configStr);
	$fp = fopen($config_file,"w") or die("<script>alert('写入配置失败，请检查安装目录/Public/Config/config.ini.php是否可写入！');history.go(-1);</script>");	
	fwrite($fp,$configStr);
	fclose($fp);
	//清理缓存
	$Webpath = './Web/Runtime/';
	$Adminpath = './Admin/Runtime/';
		if(is_dir($Webpath))
		{
			@deldir($Webpath);
		}
		if(is_dir($Adminpath))
		{
			@deldir($Adminpath);
		}		
$this->success('QQ快捷登陆设置成功!');	

}
//订单列表
function cartlist(){
$model = D('TradeView');
import('ORG.Util.Page');
$where ='';
if(!empty($_REQUEST['start_time']) && !empty($_REQUEST['end_time'])){
$where .= 'member_trade.addtime>='.strtotime($_REQUEST['start_time']." 00:00:00").' and member_trade.addtime<='.strtotime($_REQUEST['end_time']." 23:59:59")." and ";
	}
else if(!empty($_REQUEST['start_time']) && empty($_REQUEST['end_time'])){
	$where .= 'member_trade.addtime>='.strtotime($_REQUEST['start_time']." 00:00:00").' and member_trade.addtime<='.strtotime($_REQUEST['start_time']." 23:59:59")." and ";
	}
else if(empty($_REQUEST['start_time']) && !empty($_REQUEST['end_time'])){
	$where .= 'member_trade.addtime>='.strtotime($_REQUEST['end_time']." 00:00:00").' and member_trade.addtime<='.strtotime($_REQUEST['end_time']." 23:59:59")." and ";
	}
if(!empty($_REQUEST['keyword'])){
	$where .= "article.title like '%".htmlspecialchars(trim($_REQUEST['keyword']))."%' and ";
	}
$where .='1=1';
        $count = $model->where($where)->count();
		$p = new Page($count,20); 
		$list = $model->where($where)->order('member_trade.addtime desc')->limit($p->firstRow.','.$p->listRows)->select();
				
		$p->setConfig('prev','上一页');
		$p->setConfig('header','篇文章');
		$p->setConfig('first','首 页');
		$p->setConfig('last','末 页');
		$p->setConfig('next','下一页');
		$p->setConfig('theme',"%first%%upPage%%linkPage%%downPage%%end%
		<li><span><select name='select' onChange='javascript:window.location.href=(this.options[this.selectedIndex].value);'>%allPage%</select></span></li>\n<li><span>共<font color='#009900'><b>%totalRow%</b></font>篇文章 20篇/每页</span></li>");
		$this->assign('page',$p->show());
		$this->assign('list',$list);
        $this->display();
}
//订单列表
function userlist(){
$model = M('member');
import('ORG.Util.Page');
$where ='';
if(!empty($_REQUEST['keyword'])){
	$where .= "(username like '%".htmlspecialchars(trim($_REQUEST['keyword']))."%' or realname like '%".htmlspecialchars(trim($_REQUEST['keyword']))."%' or address like '%".htmlspecialchars(trim($_REQUEST['keyword']))."%') and ";
	}
$where .='1=1';
        $count = $model->where($where)->count();
		$p = new Page($count,20); 
		$list = $model->where($where)->order('addtime desc')->limit($p->firstRow.','.$p->listRows)->select();
		$p->setConfig('prev','上一页');
		$p->setConfig('header','篇文章');
		$p->setConfig('first','首 页');
		$p->setConfig('last','末 页');
		$p->setConfig('next','下一页');
		$p->setConfig('theme',"%first%%upPage%%linkPage%%downPage%%end%
		<li><span><select name='select' onChange='javascript:window.location.href=(this.options[this.selectedIndex].value);'>%allPage%</select></span></li>\n<li><span>共<font color='#009900'><b>%totalRow%</b></font>篇文章 20篇/每页</span></li>");
		$this->assign('page',$p->show());
		$this->assign('list',$list);
        $this->display();
}

//删除交易记录
function deltrade(){
$buyid = intval($_REQUEST['buyid']);
M('member_trade')->where('buy_id='.$buyid)->delete();
$this->success('删除成功!');
}
function deluser(){
$id = intval($_REQUEST['id']);
M('member')->where('id='.$id)->delete();
$this->success('删除成功!');
}
function deltixian(){
$id = intval($_REQUEST['id']);
M('tixian')->where('id='.$id)->delete();
$this->success('删除成功!');
}
//ajax改变电商状态
function ajax_change_trade(){
$buyid = intval($_REQUEST['buyid']);
$status = intval($_REQUEST['status']);
if($buyid==0){$this->ajaxReturn('参数错误','提示',0);exit();}
$t = M('member_trade')->where('buy_id='.$buyid)->setField('status',$status);
echo M('member_trade')->getLastSql();
//$this->ajaxReturn('操作成功','提示',1);
}
function moduser(){
$id = intval($_REQUEST['id']);
if($_POST){
$data = $_POST;
if($_POST['userpwd1'] == $_POST['userpwd2'] && trim($_POST['userpwd1']) != '' && trim($_POST['userpwd2']) != ''){
$data['userpwd'] = md5($_POST['userpwd1']);
}
M('member')->save($data);
$this->success('资料保存成功~');
}
else{
	$info = M('member')->where('id='.$id)->find();
	$this->assign('info',$info);
	$this->display();
	}
}

//群发邮件
function send_mail(){
if($_POST){
if($_POST['title']=='' || $_POST['content'] == ''){$this->error('请输入标题和内容!');exit();}
$where = '';
if($_POST['except_user'] !=''){
$_POST['except_user'] = str_replace("，",",",$_POST['except_user']);	
$e_arr = explode(",",$_POST['except_user']);
foreach($e_arr as $v){
$n_arr[] = ("'".$v."'");
}
$username = join(',',$n_arr);
$where = "username not in({$username})";
}
$list = M('member')->where($where)->select();
foreach($list as $k=>$v){
$r = send_mail($v['email'], $v['username'], $_POST['title'],$_POST['content'],C('MAIL_PORT'));
ob_end_flush();//清空浏览器缓存
if($r){
echo '正在发送'.$v['email'].',发送成功(√)<br>';
}else{
echo '正在发送'.$v['email'].',发送失败(ㄨ)<br>';
}
ob_flush();flush();}
}
else{
$this->display();
}
}
function showuser(){
$id = intval($_REQUEST['id']);
$info = M('member')->where('id='.$id)->find();
	$this->assign('info',$info);
	$this->display();
}

function tixianlist(){
	$model = D('TixianView');
import('ORG.Util.Page');
$where ='';
if(!empty($_REQUEST['keyword'])){
	$where .= "(username like '%".htmlspecialchars(trim($_REQUEST['keyword']))."%' or realname like '%".htmlspecialchars(trim($_REQUEST['keyword']))."%') and ";
	}
$where .='1=1';
        $count = $model->where($where)->count();
		$p = new Page($count,20); 
		$list = $model->where($where)->order('addtime desc')->limit($p->firstRow.','.$p->listRows)->select();
		$p->setConfig('prev','上一页');
		$p->setConfig('header','篇文章');
		$p->setConfig('first','首 页');
		$p->setConfig('last','末 页');
		$p->setConfig('next','下一页');
		$p->setConfig('theme',"%first%%upPage%%linkPage%%downPage%%end%
		<li><span><select name='select' onChange='javascript:window.location.href=(this.options[this.selectedIndex].value);'>%allPage%</select></span></li>\n<li><span>共<font color='#009900'><b>%totalRow%</b></font>篇文章 20篇/每页</span></li>");
		$this->assign('page',$p->show());
		$this->assign('list',$list);
        $this->display();	
}
//提现处理
function dotixian(){
$id=intval($_REQUEST['id']);
$info = M('tixian')->where('id='.$id)->find();
if(!$info){$this->error('信息不存在!');exit();}
M('tixian')->where('id='.$id)->setField('status',1);
M('member')->setDec('money','id='.$info['uid'],$info['money']);
$this->success('提现处理成功!!');
}
//导出会员到excel
function goexcel(){
import ('ORG.Util.PHPExcel' );//引入PHPExcel类
$fileName = 'member'.date('_YmdHis');//or $xlsTitle 文件名称可根据自己情况设定
$objPHPExcel = new PHPExcel();
$cellName = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','AA','AB','AC','AD','AE','AF','AG','AH','AI','AJ','AK','AL','AM','AN','AO','AP','AQ','AR','AS','AT','AU','AV','AW','AX','AY','AZ');
$title_array = array(
            'id'=>'用户编号', 
            'username'=>'用户名',
            'realname'=>'姓名',
            'sex'=>'性别',
			'tel'=>'联系电话',
			'qq'=>'QQ',
			'email'=>'电子邮件',
			'birthday'=>'出生日期',
			'province'=>'所在省份',
			'city'=>'所在城市',
			'address'=>'详细地址',
			'addtime'=>'注册时间'			
        );
$data_array  = M('member')->where('is_lock=0')->select();
$cellNum = count($title_array);
$dataNum = count($data_array);
//输出表头
$objPHPExcel->setActiveSheetIndex(0);
$i=0;
foreach($title_array as $k=>$v){
$objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellName[$i].'1', (string)$title_array[$k]);
$i++;
}
//输出内容
for($i=0;$i<$dataNum;$i++){
	      $j=0;
          foreach($title_array as $k=>$v){
		  $data_value = $data_array[$i][$k];
		  if($k == 'addtime' &&  $data_value != ''){$data_value = date('Y-m-d H:i:s',$data_value);} //对注册时间特殊处理下
          $objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellName[$j].($i+2), (string)$data_value);
		  $j++;
          }             
}  
// Rename worksheet
$objPHPExcel->getActiveSheet()->setTitle('会员表');
$objPHPExcel->setActiveSheetIndex(0);
// Redirect output to a client's web browser (Excel5)
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$fileName.'.xls"');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');

// If you're serving to IE over SSL, then the following may be needed
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');	
}
//类结束
}
?>