<?php
	define('IN_PHPMPS', true);
	ob_start();
	require dirname(__FILE__) . '/include/common.inc.php';
	require PHPMPS_ROOT . 'include/pay.fun.php';
	$act = $_REQUEST['act'] ? trim($_REQUEST['act']) : 'index';
	switch($act){
		case 'post_comment':
			get_login();
			$comid = intval($_POST['cid']);
			$eid = intval($_POST['eid']);
			$formid = $_POST['formid'] ? htmlspecialchars(trim($_POST['formid'])) : '';
			if(empty($comid))showwap('CID参数错误！');
			if(empty($eid))showwap('EID参数错误！');
			$ceid = $db->getOne("SELECT count(*) FROM {$table}com_comment WHERE comid='$comid' and userid='$_userid' and eid='$eid'");
			if($ceid>0)showwap('你已点评过本次消费了！');
			$dpuser = $db->getOne("SELECT userid FROM {$table}pay_exchange WHERE comid='$comid' and exchangeid='$eid'");
			if($dpuser!=$_userid)showwap('参数错误！');
			$cm_fw  = intval($_POST['star_fw']);
			$cm_hj  = intval($_POST['star_hj']);
			$cm_pz  = intval($_POST['star_pz']);
			$content = htmlspecialchars(trim($_POST['content']));
			if(strlen($content)>1000)showwap('你的点评太长了！');
			if(($cm_fw+$cm_hj+$cm_pz)<1)showwap('最低给一分吧！');
			$ip = get_ip();
			if(($cm_fw+$cm_hj+$cm_pz)<=0)showwap('一分都不给?最少给1分吧！');
			if(empty($content))showwap('评价就说点什么吧！');
			check_words($who=array('content'));
			$postdate = time();
			$ip = get_ip();
			$check = $CFG['comment_check'] == '0' ? '1' : '0' ;
			$sql = "INSERT INTO {$table}com_comment (eid,comid,userid,username,cm_pz,cm_fw,cm_hj,content,postdate,is_check,ip,formid) VALUES ('$eid','$comid','$_userid','$_username','$cm_pz','$cm_fw','$cm_hj','$content','$postdate','$check','$ip','$formid')";
			$res = $db->query($sql);
			$db->query("update {$table}pay_exchange set dp='1' where comid='$comid' and userid='$_userid' and exchangeid='$eid'");
			/*if($_username) {
				$credit_count = $db->getOne("select count(*) from {$table}pay_exchange where username='$_username' and addtime>".mktime(0,0,0)." and note='post_comment_credit' ");
				if($credit_count < $CFG['max_comment_credit']) {
					if(!empty($CFG['post_comment_credit']))credit_add($_username, $CFG['post_comment_credit'],'post_comment_credit');
				}
			}*/
			header("Location: member.php?act=exchange");
		break;
	
		case 'login':
			if(!empty($_userid)) {
				showwap("你已经登录了，请不要再次登录！", "index.php");
			}
			$refers = trim(htmlspecialchars($_SERVER['HTTP_REFERER']));
			$seo['title'] = "用户登录 - " . $CFG['webname'];
			include tpl('login');
		break;
		
		case 'act_login':
			if(!submitcheck('submit')) showwap('提交错误！');
			$username = $_POST['username'] ? htmlspecialchars(trim($_POST['username'])) : '';
			$password = $_POST['password'] ? trim($_POST['password']) : '';
			$password=iconv("UTF-8","GB2312",$password);
			$md5_password = MD5($password);
			if(empty($username))showwap("用户名");
			if(empty($password))showwap("密码不能为空");
			
			$username = $db->getOne("SELECT username FROM {$table}member WHERE (username='$username' or email='$username' or phone='$username')");
		    $userid = $db->getOne("SELECT userid FROM {$table}member WHERE username='$username'");
            if(empty($username) && empty($userid))showwap("用户名不存在");
			
			if(login($username,$md5_password)) {
				$url= $_POST['refer'] ? rawurldecode($_POST['refer']) : 'index.php';
				showwap('登录成功！',$url);
			} else {
				showwap('登录失败！','login.php');
			}
		break;
		
		case 'logout':
			logout();
			//$refer = urlencode(trim(htmlspecialchars($_SERVER['HTTP_REFERER'])));
			header("Location: index.php");
		break;
		
		case 'edit_pass':
			$seo['title'] = "修改密码 - " . $CFG['webname'];
			include tpl('edit_pass');
		break;

		case 'act_edit_pass':
			$password = $_POST['password'] ? trim($_POST['password']) : '';
			$repassword = $_POST['repassword'] ? trim($_POST['repassword']) : '';
			if(empty($password) || empty($repassword))showwap('密码项不能为空！');
			if($password!=$repassword)showwap('两次输入的密码不一致！');
			$password = MD5($password);
			$query = $db->query("UPDATE {$table}member SET password='$password' WHERE userid='$_userid' ");
			showwap('密码修改成功!','com.php');
		break;
		
		case 'exchange':
		get_login();
		$comid = intval($_REQUEST['comid']);
		$page = intval($_GET['page']);
		$page = isset($page) ? intval($page) : 1;
		$pagesize = 7;
		if(!empty($comid)){
			$sqls = " AND comid='$comid'";
		}
		$_username = addslashes(stripslashes($_username));
		$get_device = get_device_type();
		$r = $db->getRow("SELECT count(*) as number,sum(value) as moneys FROM {$table}pay_exchange WHERE userid='$_userid' $sqls");
		$pager['search'] = array('act' => 'exchange','comid'=>$comid);
		$pager = get_pager('member.php', $pager['search'], $r['number'], $page, $pagesize);
        $moneys = $r['moneys'] ? number_format(str_replace('-','', $r['moneys']),2) : '0';
		$exchanges = array();
		$result = $db->query("SELECT * FROM {$table}pay_exchange WHERE userid='$_userid' $sqls ORDER BY addtime DESC LIMIT $pager[start],$pager[size]");
		while($r = $db->fetchrow($result)) {
			$r['value'] = number_format(str_replace('-','', $r['value']),2);
			$r['comname'] = com_info("comname",$r['comid'],1);
			$r['addtime'] = date('m-d', $r['addtime']);
			$r['dp'] = $r['dp']=='1' ? '' : '未评';
			$exchanges[] = $r;
		}
			include tpl('member_exchange');
		break;
	}
?>