<?php
// +----------------------------------------------------------------------
// | 天方夜谭 [ 世界上最好用的QQ秒赞系统 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2014~2019 http://www.eirds.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 天方夜谭 <17404785@qq.com>
// +----------------------------------------------------------------------

//页面标题
define('Title', '我的资料');

//修改资料
if (POST('action')=='edit'){
    //声明返回json格式的信息
    header('Content-Type:application/json; charset=utf-8');
    $name = POST('name');
    $qq = POST('qq');
    $email = POST('email');
    $music_id = POST('music_id');
    $cqyzt = POST('cqyzt');
    $aqproblem = POST('aqproblem');
    $aqanswer = POST('aqanswer');
    $pwd = POST('pwd');
    $chat_bg = POST('chat_bg');
    if (empty($name) || empty($qq) || empty($email) || empty($music_id) || empty($cqyzt) || empty($aqproblem) || empty($aqanswer)){
        $arr = array('state'=>'no','msg'=>'所有带*号的都必须填写！');
    }elseif (strlen($name)>18){
        $arr = array('state'=>'no','msg'=>'昵称太长！');
    }elseif (strlen($qq)<5 || strlen($qq)>11){
        $arr = array('state'=>'no','msg'=>'QQ号码不正确！');
    }elseif (!preg_match("/^[a-z0-9][a-z0-9\.\_\-]+@[a-z0-9]+\.[a-z]{2,4}$/i",$email)){
        $arr = array('state'=>'no','msg'=>'邮箱地址不正确！');
    }elseif ($TFYT_DB->get_row("select uid from ".PREFIX."users where qq='{$qq}' and uid!='{$GLOBALS['users']['uid']}' limit 1")){
        $arr = array('state'=>'no','msg'=>'QQ号已被其他用户绑定，换一个吧！');
    }elseif ($TFYT_DB->get_row("select uid from ".PREFIX."users where email='{$email}' and uid!='{$GLOBALS['users']['uid']}' limit 1")){
        $arr = array('state'=>'no','msg'=>'该邮箱已被其他用户绑定，换一个吧！');
    }else{
        $set2 = "music_id='{$music_id}',cqyzt='{$cqyzt}',chat_bg='{$chat_bg}'";
        if (!empty($pwd)){
            if ((strlen($pwd)<5 || strlen($pwd)>18)){
                $arr = array('state'=>'no','msg'=>'密码不能小于5位并且不能大于18位！');
            }else{
                $password = md5Pwd($pwd);
                $set = "name='{$name}',qq='{$qq}',email='{$email}',aqproblem='{$aqproblem}',aqanswer='{$aqanswer}',password='{$password}'";
                $TFYT_DB->query("update ".PREFIX."users set {$set} where uid='{$GLOBALS['users']['uid']}'");
                $TFYT_DB->query("update ".PREFIX."users_service set {$set2} where only='{$GLOBALS['users']['only']}'");
                $arr = array('state'=>'ok','msg'=>'修改成功！');
            }
        }else{
            $set = "name='{$name}',qq='{$qq}',email='{$email}',aqproblem='{$aqproblem}',aqanswer='{$aqanswer}'";
            $TFYT_DB->query("update ".PREFIX."users set {$set} where uid='{$GLOBALS['users']['uid']}'");
            $TFYT_DB->query("update ".PREFIX."users_service set {$set2} where only='{$GLOBALS['users']['only']}'");
            $arr = array('state'=>'ok','msg'=>'修改成功！');
        }
    }
    exit(json_encode($arr));
}elseif (!empty(GET('access_token'))){  //绑定QQ
    $qqlogin = new qqlogin(GET('access_token'));
    $openid = $qqlogin->get_auth_info()['openid'];
    $TFYT_DB->query("update ".PREFIX."users set qqopenid='{$openid}' where uid='{$GLOBALS['users']['uid']}' ");
    exit("<script language='javascript'>alert('绑定成功！');window.location.href='/Application/Mains.php?Mould=User';</script>");
}

//加载头部
Load_Template(Mains.'head.php');

//加载视图
Load_Template(Mains.'user.php');

//加载底部
Load_Template(Mains.'foot.php');