<?php
include_once __DIR__ . '/../common/function.php';
$row = Db::table('app_web')
         ->find();
 $code=$_GET['code'];
$type=$_GET['type'];
$http_typex=get_http_type();
$appid=$row['APPID'];
$appkey=$row['APPKEY'];
$url ="https://login.1atech.cn/connect.php?act=callback&appid=".$appid."&appkey=".$appkey."&code=".$code."&type=".$type;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HEADER,0);
    $output = curl_exec($ch);
    $data=  json_decode($output, true);
   // echo $data['social_uid'];
    //print_r($data) ;
    curl_close($ch); 
    if($data['code']!=0){
        exit(ReturnError('Sever Error!!'));
    }

    if ($redisResult->code === 201) {
        exit(ReturnError($redisResult->msg));
    }


   
    $username  = $data['social_uid'];
    $username=str_replace("-","",$username);
    $email     = 'ccc@1atech.cn';
    $password  = '123456';
    $password2 = '123456';
    $password  = md5($password);
    $password2 = md5($password2);
    

    if ( !checkEmail($email)) {
        exit(ReturnError('邮箱不合法'));
    }


    if ($password !== $password2) {
        exit(ReturnError('两次密码不一致'));
    }

    if (__REG_LIMIT__) {
        $chat_limit = __REG_LIMIT__;
    } else {
        $chat_limit = 0;
    }
   
    $password  = md5($password);
    $password2 = md5($password2);
    if (checkUsernameExistence($username)) {
        $COOKIE = md5($username. $password . time());
        if ( !Db::table('app_user')
                ->where('username' , $username)
                ->update(['cookie' => $COOKIE])) {
           // exit(ReturnError('Sever Error!!'));
        }
        setcookie('authentication' , $COOKIE , time() + 3600 * 24 , '/');
        Header("Location:".$http_typex.$_SERVER['HTTP_HOST']."/index.php?msg=success");
        echo $COOKIE;        
       //exit(ReturnError('Sever Error!!'));
    }else{
         $add = Db::table('app_user')
             ->add([
                 'username'   => $username ,
                 'password'   => $password ,
                 'email'      => $email ,
                 'cookie'     => md5($username . $password . time()) ,
                 'salt'       => encryptPassword($password) ,
                 'chat_limit' => $chat_limit
             ]);
    if ($add) {
        $COOKIE = md5($username. $password . time());
        if ( !Db::table('app_user')
                ->where('username' , $username)
                ->update(['cookie' => $COOKIE])) {
           // exit(ReturnError('Sever Error!!'));
        }
        setcookie('authentication' , $COOKIE , time() + 3600 * 24 , '/');
        Header("Location:".$http_typex.$_SERVER['HTTP_HOST']."/index.php?msg=success"); 
        echo $COOKIE;
        exit(ReturnError('Sever Error!!'));
    } else {
        exit(ReturnError('注册失败'));
    }}
function get_http_type()
{
    $http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://';
    return $http_type;
}
