<?php
error_reporting(0);
global $db, $CcTvAi_com_userid;
/* if(!eregi($_SERVER['SERVER_NAME'], $_SERVER['HTTP_REFERER'])) {
    exit();
} */
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
header("Content-type: text/html;charset=gb2312");
$ac=SafeRequest("ac", "get");
if($ac == "login") { //登录
    if(cd_userlogin == "no") {
        exit("Msg_029");
    }
    $seccode=SafeRequest("code", "get");
    if($seccode != $_SESSION["codes"]) {
        exit("Msg_001");
    }
    $cd_username=unescape(SafeRequest("name", "get"));
    $cd_password=SafeRequest("pwd", "get");
    $cd_logintime=date('Y-m-d H:i:s');
    $cd_loginip=getonlineip();
    $cookietime=cd_userlogintime;
    $cd_password=substr(md5($cd_password), 8, 16);
    global $db;
    $sql="select cd_id from " . tname('user') . " where cd_name='" . $cd_username . "' and cd_password='" . $cd_password . "'";
    $cd_id=$db->Getone($sql);
    if(!IsNumID($cd_id)) {
        exit();
    }
    if($cd_id) {
        $row=$db->Getrow("select cd_id,cd_lock,cd_name,cd_password,cd_vipgrade from " . tname('user') . " where cd_id='" . $cd_id . "'");
        if($row["cd_lock"] == 1) {
            exit("Msg_002");
        } elseif($row["cd_lock"] == 2) {
            exit("Msg_003");
        } else {
            $cd_ids=$db->Getone("select cd_id from " . tname('session') . " where cd_uid='" . $cd_id . "'");
            if($cd_ids) {
                updatetable('session', array(
                    'cd_logintime'=>time()
                ), array(
                    'cd_id'=>$cd_ids
                ));
            } else {
                $setarr=array(
                    'cd_uid'=>$cd_id,
                    'cd_uname'=>$cd_username,
                    'cd_uip'=>$cd_loginip,
                    'cd_logintime'=>time()
                );
                inserttable('session', $setarr, 1);
            }
            if($db->getone("select cd_id from " . tname('user') . " where DateDiff(DATE(cd_logintime),'" . date('Y-m-d') . "')=0 and cd_id='" . $cd_id . "'")) {
                $db->query("update " . tname('user') . " set cd_loginnum=cd_loginnum+'1',cd_loginip='" . $cd_loginip . "',cd_logintime='" . $cd_logintime . "' where cd_id=cd_id='" . $cd_id . "'");
            } else {
                if(cd_userloginpoints >= 1) {
                    $setarr=array(
                        'cd_type'=>1,
                        'cd_uid'=>$cd_id,
                        'cd_uname'=>$cd_username,
                        'cd_icon'=>'app',
                        'cd_title'=>'每日登录',
                        'cd_points'=>cd_userloginpoints,
                        'cd_state'=>0,
                        'cd_addtime'=>date('Y-m-d H:i:s'),
                        'cd_endtime'=>getendtime()
                    );
                    inserttable('bill', $setarr, 1);
                }
                if($row["cd_vipgrade"] == 1) {
                    $cd_speed=5;
                } elseif($row["cd_vipgrade"] == 2) {
                    $cd_speed=10;
                } else {
                    $cd_speed=0;
                }
                $db->query("update " . tname('user') . " set cd_points=cd_points+'" . cd_userloginpoints . "',cd_rank=cd_rank+'" . cd_userloginrank . "',cd_viprank=cd_viprank+'" . $cd_speed . "',cd_loginnum=cd_loginnum+'1',cd_loginip='" . $cd_loginip . "',cd_logintime='" . $cd_logintime . "' where cd_id='" . $cd_id . "'");
            }
            setcookie("cd_id", $row["cd_id"], time()+$cookietime, cd_cookiepath);
            setcookie("cd_name", $row["cd_name"], time()+$cookietime, cd_cookiepath);
            setcookie("cd_password", $row["cd_password"], time()+$cookietime, cd_cookiepath);
            $_SESSION["codes"]=substr(md5(date('' . rand(1, 9) . 'msds' . rand(1, 9) . 'Hs' . rand(1, 9) . 's')), 8, 16);
			setcookie("login_referer");
            exit("Msg_005");
        }
    } else {
        exit("Msg_004");
    }
} elseif($ac == "dologin") { //登录
    if(cd_userlogin == "no") {
        exit($callback . '({"error":001,"errorMessage":""})');
    }
    $seccode=SafeRequest("vCode", "get");
    $cd_username=unescape(SafeRequest("loginName", "get"));
    $cd_password=SafeRequest("password", "get");
    $cd_logintime=date('Y-m-d H:i:s');
    $cd_loginip=getonlineip();
    $cookietime=cd_userlogintime;
    $cd_password=substr(md5($cd_password), 8, 16);
    $callback=SafeRequest("callback", "get");
    if($seccode != $_SESSION["codes"]) {
        exit($callback . '({"error":002,"errorMessage":""})');
    }
    global $db;
    $sql="select cd_id from " . tname('user') . " where cd_name='" . $cd_username . "' and cd_password='" . $cd_password . "'";
    $cd_id=$db->Getone($sql);
    if(!IsNumID($cd_id)) {
        exit();
    }
    if($cd_id) {
        $row=$db->Getrow("select cd_id,cd_lock,cd_name,cd_password,cd_vipgrade from " . tname('user') . " where cd_id='" . $cd_id . "'");
        if($row["cd_lock"] == 1) {
            exit($callback . '({"error":003,"errorMessage":""})');
        } elseif($row["cd_lock"] == 2) {
            exit($callback . '({"error":004,"errorMessage":""})');
        } else {
            $cd_ids=$db->Getone("select cd_id from " . tname('session') . " where cd_uid='" . $cd_id . "'");
            if($cd_ids) {
                updatetable('session', array(
                    'cd_logintime'=>time()
                ), array(
                    'cd_id'=>$cd_ids
                ));
            } else {
                $setarr=array(
                    'cd_uid'=>$cd_id,
                    'cd_uname'=>$cd_username,
                    'cd_uip'=>$cd_loginip,
                    'cd_logintime'=>time()
                );
                inserttable('session', $setarr, 1);
            }
            if($db->getone("select cd_id from " . tname('user') . " where DateDiff(DATE(cd_logintime),'" . date('Y-m-d') . "')=0 and cd_id='" . $cd_id . "'")) {
                $db->query("update " . tname('user') . " set cd_loginnum=cd_loginnum+'1',cd_loginip='" . $cd_loginip . "',cd_logintime='" . $cd_logintime . "' where cd_id='" . $cd_id . "'");
            } else {
                if(cd_userloginpoints >= 1) {
                    $setarr=array(
                        'cd_type'=>1,
                        'cd_uid'=>$cd_id,
                        'cd_uname'=>$cd_username,
                        'cd_icon'=>'app',
                        'cd_title'=>'每日登录',
                        'cd_points'=>cd_userloginpoints,
                        'cd_state'=>0,
                        'cd_addtime'=>date('Y-m-d H:i:s'),
                        'cd_endtime'=>getendtime()
                    );
                    inserttable('bill', $setarr, 1);
                }
                if($row["cd_vipgrade"] == 1) {
                    $cd_speed=5;
                } elseif($row["cd_vipgrade"] == 2) {
                    $cd_speed=10;
                } else {
                    $cd_speed=0;
                }
                $db->query("update " . tname('user') . " set cd_points=cd_points+'" . cd_userloginpoints . "',cd_rank=cd_rank+'" . cd_userloginrank . "',cd_viprank=cd_viprank+'" . $cd_speed . "',cd_loginnum=cd_loginnum+'1',cd_loginip='" . $cd_loginip . "',cd_logintime='" . $cd_logintime . "' where cd_id='" . $cd_id . "'");
            }
            setcookie("cd_id", $row["cd_id"], time()+$cookietime, cd_cookiepath);
            setcookie("cd_name", $row["cd_name"], time()+$cookietime, cd_cookiepath);
            setcookie("cd_password", $row["cd_password"], time()+$cookietime, cd_cookiepath);
            $_SESSION["codes"]=substr(md5(date('' . rand(1, 9) . 'msds' . rand(1, 9) . 'Hs' . rand(1, 9) . 's')), 8, 16);
            exit($callback . '({"error":005,"errorMessage":""})');
        }
    } else {
        exit($callback . '({"error":006,"errorMessage":""})');
    }
} elseif($ac == "logout") { //退出登录
    global $db;
    $db->query("delete from " . tname('session') . " where cd_uid=$CcTvAi_com_userid");
    setcookie("cd_id", "", time()-1, cd_cookiepath);
    setcookie("cd_name", "", time()-1, cd_cookiepath);
    setcookie("cd_password", "", time()-1, cd_cookiepath);
    echo "<script type=\"text/javascript\">location.href='" . $_SERVER['HTTP_REFERER'] . "';</script>";
} elseif($ac == "reg") { //注册
    global $db;
    $username=unescape(SafeRequest("name", "get"));
    $password=SafeRequest("pwd", "get");
    $nicheng=unescape(SafeRequest("nick", "get"));
    $sex=SafeRequest("sex", "get");
    if($sex == "1") {
        $sexs="帅哥";
    } elseif($sex == "0") {
        $sexs="靓女";
    } else {
        $sexs="靓女";
    }
    $year=SafeRequest("year", "get");
    $month=SafeRequest("month", "get");
    $day=SafeRequest("day", "get");
    $birthday=$year . "-" . $month . "-" . $day;
    $sheng=unescape(SafeRequest("sheng", "get"));
    $shi=unescape(SafeRequest("shi", "get"));
    $address=unescape($sheng . "-" . $shi);
    $email=SafeRequest("email", "get");
    $qq=SafeRequest("qq", "get");
    $points=cd_userpoints;
    $userrank=cd_userrank;
    $loginip=getonlineip();
    $seccode=SafeRequest("code", "get");
    $cookietime=cd_userlogintime;
	$logintime=date('Y-m-d H:i:s');
    $name=$db->getone("select cd_id from " . tname('user') . " where cd_name='" . $username . "'");
    $nick=$db->getone("select cd_id from " . tname('user') . " where cd_nicheng='" . $nicheng . "'");
    $mail=$db->getone("select cd_id from " . tname('user') . " where cd_email='" . $email . "'");
    if($seccode != $_SESSION["codes"]) {
        exit("Msg_001");
    } elseif($name) {
        exit("Msg_006");
    } elseif($nick) {
        exit("Msg_007");
    } elseif($mail) {
        exit("Msg_008");
    } else {
        $setarr=array(
            'cd_name'=>$username,
            'cd_nicheng'=>$nicheng,
            'cd_password'=>substr(md5($password), 8, 16),
            'cd_question'=>'',
            'cd_answer'=>'',
            'cd_email'=>$email,
            'cd_sex'=>$sexs,
            'cd_regdate'=>date('Y-m-d H:i:s'),
            'cd_loginip'=>$loginip,
            'cd_loginnum'=>0,
            'cd_qq'=>$qq,
            'cd_logintime'=>date('Y-m-d H:i:s'),
            'cd_grade'=>0,
            'cd_lock'=>0,
            'cd_points'=>$points,
            'cd_photo'=>'',
            'cd_sign'=>'',
            'cd_hidden'=>0,
            'cd_birthprovince'=>'',
            'cd_birthcity'=>$address,
            'cd_resideprovince'=>'',
            'cd_residecity'=>'',
            'cd_birthday'=>$birthday,
            'cd_vipindate'=>"0000-00-00 00:00:00",
            'cd_vipenddate'=>"0000-00-00 00:00:00",
            'cd_vipgrade'=>0,
            'cd_viprank'=>0,
            'cd_verified'=>0,
            'cd_djmub'=>0,
            'cd_ufav'=>0,
            'cd_ufans'=>0,
            'cd_hits'=>0,
            'cd_isbest'=>0,
            'cd_money'=>0,
            'cd_weblock'=>0,
            'cd_signd'=>0,
            'cd_signdcumu'=>0,
            'cd_signdtime'=>"0000-00-00 00:00:00",
            'cd_skin'=>0,
            'cd_friendnum'=>0,
            'cd_creditname'=>0,
            'cd_rank'=>$userrank,
            'cd_credit'=>0,
            'cd_qqopenid'=>'',
            'cd_checkmm'=>0,
            'cd_checkmusic'=>0,
            'cd_groupnum'=>0,
            'cd_level'=>0,
        );
        inserttable('user', $setarr, 1);
        //增加数据统计
        $tongji='select * from ' . tname('system') . '';
        $tj=$db->getrow($tongji);
        $sj=date('Ymd');
        if($tj['cd_userdaytime'] == $sj) {
            $db->query('update ' . tname('system') . " set cd_usermub=cd_usermub+1,cd_userdaymub=cd_userdaymub+1");
        } else {
            $db->query('update ' . tname('system') . " set cd_usermub=cd_usermub+1,cd_userdaymub='1',cd_djdaytime='" . $sj . "'");
        }
        //发送站内短消息
        if(cd_userlocalpm == "yes") {
            $rowlast=$db->getrow("select cd_id from " . tname('user') . " where cd_name='" . $username . "' order by cd_id desc");
            $cd_lastid=$rowlast['cd_id'];
            $setarrs=array(
                'cd_uid'=>0,
                'cd_uname'=>'系统消息',
                'cd_uids'=>$cd_lastid,
                'cd_unames'=>$username,
                'cd_dataid'=>0,
                'cd_readid'=>1,
                'cd_content'=>cd_userlocalpmtxt,
                'cd_addtime'=>date('Y-m-d H:i:s')
            );
            inserttable('message', $setarrs, 1);
        }
        //直接登录
        $sql="select cd_id from " . tname('user') . " where cd_name='" . $username . "' and cd_password='" . substr(md5($password), 8, 16) . "'";
        $cd_id=$db->getone($sql);
        if(!IsNumID($cd_id)) {
            exit();
        }
        if($cd_id) {
            $db->query("update " . tname('user') . " set cd_loginnum=cd_loginnum+'1',cd_loginip='" . $loginip . "',cd_logintime='" . $logintime . "',cd_weblock='1' where cd_id='" . $cd_id . "'");
            $db->query("delete from " . tname('session') . " where cd_uid='" . $cd_id . "'");
            $db->query("Insert " . tname('session') . " (cd_uid,cd_uname,cd_uip,cd_logintime) values ('" . $cd_id . "','" . $username . "','" . $loginip . "','" . time() . "')");
            setcookie("cd_id", $cd_id, time()+$cookietime, cd_cookiepath);
            setcookie("cd_name", $username, time()+$cookietime, cd_cookiepath);
            setcookie("cd_password", substr(md5($password), 8, 16), time()+$cookietime, cd_cookiepath);
            $_SESSION["codes"]=substr(md5(date('' . rand(1, 9) . 'msds' . rand(1, 9) . 'Hs' . rand(1, 9) . 's')), 8, 16);
            //奖励账单
            if($points >= 1) {
                $setarr=array(
                    'cd_type'=>1,
                    'cd_uid'=>$cd_id,
                    'cd_uname'=>$username,
                    'cd_icon'=>'logon',
                    'cd_title'=>'新用户注册',
                    'cd_points'=>$points,
                    'cd_state'=>0,
                    'cd_addtime'=>date('Y-m-d H:i:s'),
                    'cd_endtime'=>$loginip
                );
                inserttable('bill', $setarr, 1);
            }
            if(cd_userloginpoints >= 1) {
                $setarr=array(
                    'cd_type'=>1,
                    'cd_uid'=>$cd_id,
                    'cd_uname'=>$username,
                    'cd_icon'=>'app',
                    'cd_title'=>'每日登录',
                    'cd_points'=>cd_userloginpoints,
                    'cd_state'=>0,
                    'cd_addtime'=>date('Y-m-d H:i:s'),
                    'cd_endtime'=>$loginip
                );
                inserttable('bill', $setarr, 1);
            }
            $db->query("update " . tname('user') . " set cd_points=cd_points+'" . cd_userloginpoints . "',cd_rank=cd_rank+'" . cd_userloginrank . "' where cd_id='" . $cd_id . "'");
        }
        //发送邮件
        if(cd_usersmtppm == "yes") {
            include_once "../plug/mail/mail.php";
            $cd_usersmtppmtxt=cd_usersmtppmtxt;
            $cd_usersmtppmtxt=str_replace('[nicheng]', $nicheng, $cd_usersmtppmtxt);
            $cd_usersmtppmtxt=str_replace('[time]', date('Y-m-d H:i:s'), $cd_usersmtppmtxt);
            $smtp=cd_smtp;
            $youremail=cd_smtpemail;
            $password=cd_smtppassword;
            $mail=new PHPMailer();
            $mail->IsSMTP();
            $mail->SMTPAuth=true;
            $mail->Host=$smtp;
            $mail->Username=$youremail;
            $mail->Password=$password;
            $mail->From=$youremail;
            $mail->FromName=cd_webname;
            $mail->Subject=$nicheng . " - 注册信息 - " . cd_webname;
            $mail->AddAddress($email, $nicheng);
            $html=$cd_usersmtppmtxt;
            $mail->MsgHTML($html);
            $mail->IsHTML(true);
            if(!$mail->Send()) {
                echo "Msg_009"; //失败
            } else {
                echo "Msg_010"; //成功
            }
        } else {
            exit("Msg_009");
        }
    }
} elseif($ac == "lostpasswd") { //找回密码
    $username=unescape(SafeRequest("name", "get"));
    $question=unescape(SafeRequest("question", "get"));
    $answer=unescape(SafeRequest("answer", "get"));
    $password=SafeRequest("pwd", "get");
    $seccode=SafeRequest("code", "get");
    $cd_answer=substr(md5($answer), 8, 16);
    $cd_password=substr(md5($password), 8, 16);
    global $db;
    $sql="select cd_id from " . tname('user') . " where cd_name='" . $username . "'";
    $cd_id=$db->getone($sql);
    if($seccode != $_SESSION["codes"]) {
        exit("Msg_001");
    } elseif($cd_id) {
        $row=$db->getrow("select * from " . tname('user') . " where cd_id='" . $cd_id . "'");
        if($question <> $row["cd_question"]) {
            echo "Msg_011";
        } elseif($cd_answer <> $row["cd_answer"]) {
            echo "Msg_012";
        } else {
            $db->query("update " . tname('user') . " set cd_password='" . $cd_password . "' where cd_id='" . $cd_id . "'");
            echo "Msg_013";
        }
    } else {
        echo "Msg_014";
    }
}
?>