<?php
require_once 'include/contain.class.php';

if(isset($_COOKIE['login_auth'])) {
    list($User_uid, $User_username) = explode("\t", uc_authcode($_COOKIE['login_auth'], 'DECODE'));
    $tem_url_return=$lang[system][url_return][index];
    $state=$lang[system][login]['success'].$ucsynlogin;
    $style->display('login/status.html');
}
else {

    if($_GET['m']=='user' and $_GET['a']=='login'){
        list($uid, $username, $password, $email) = uc_user_login($_POST['username'], $_POST['password']);

        setcookie('login_auth', '', -86400);
        if($uid > 0) {
            if(!isset($_POST['cookietime']) or $_POST['cookietime']==''){
                $cookietime='1800';
            }
            else{
                $cookietime=$_POST['cookietime'];
            }
            setcookie('login_auth', uc_authcode($uid."\t".$username, 'ENCODE'),time()+$cookietime);
            $ucsynlogin = uc_user_synlogin($uid);
            $get=mysql_query("SELECT * FROM ".$table."members 
WHERE `uid`='".$uid."'",$con);
        $unum=mysql_num_rows($get);
        if($unum ==0){
        $_admincp_power[maxqq] = mysql_fetch_array(mysql_query("SELECT * FROM ".$table."admincp_power
WHERE name='initialmaxqq'"));
        $_admincp_power[group] = mysql_fetch_array(mysql_query("SELECT * FROM ".$table."admincp_power
WHERE name='initialgroup'"));
        $_admincp_power[integral] = mysql_fetch_array(mysql_query("SELECT * FROM ".$table."admincp_power
WHERE name='initialintegral'"));
        $_initialmaxqq = $_admincp_power[maxqq][power];
        $_initialintegral = $_admincp_power[integral][power];
        $_initialgroup =$_admincp_power[group][power];
            $in=mysql_query("INSERT INTO ".$table."members (uid,user,largest,`group`,integral,noticeqq,`status`) VALUES ('".$uid."','".$username."','".$_initialmaxqq."','$_initialgroup','$_initialintegral','0','1')",$con);
            echo mysql_error();
        }
            $tem_url_return=$lang[system][url_return][index];
            $state=$lang[system][login]['success'].$ucsynlogin;
        } elseif($uid == -1) {
            $tem_url_return=$lang[system][url_return][login];
            $state=$lang[system][login]['none'];
        } elseif($uid == -2) {
            $tem_url_return=$lang[system][url_return][login];
            $state=$lang[system][login]['passworderror'];
        } else {
            $tem_url_return=$lang[system][url_return][login];
            $state=$lang[system][login]['false'];
        }
        $style->display('login/status.html');
    }
    else if(!empty($_GET['token'])){
                $api = new Denglu(DL_APPID,DL_APIKEY,'utf-8');
            try{
                $userInfo = $api->getUserInfoByToken($_GET['token']);
                if($data = uc_get_user(substr($userInfo[personID].$userInfo[mediaUserID],0,15),0)){
                    list($uid, $username, $email) = $data;
                    setcookie('login_auth', uc_authcode($uid."\t".$username, 'ENCODE'), time()+1800);
                    $ucsynlogin = uc_user_synlogin($data[0]);
                    $get=mysql_query("SELECT * FROM ".$table."members 
WHERE `uid`='".$uid."'",$con);
                    $unum=mysql_num_rows($get);
                    if($unum ==0){
                        $_admincp_power[maxqq] = mysql_fetch_array(mysql_query("SELECT * FROM ".$table."admincp_power
WHERE name='initialmaxqq'"));
                        $_admincp_power[group] = mysql_fetch_array(mysql_query("SELECT * FROM ".$table."admincp_power
WHERE name='initialgroup'"));
                        $_admincp_power[integral] = mysql_fetch_array(mysql_query("SELECT * FROM ".$table."admincp_power
WHERE name='initialintegral'"));
                        $_initialmaxqq = $_admincp_power[maxqq][power];
                        $_initialintegral = $_admincp_power[integral][power];
                        $_initialgroup =$_admincp_power[group][power];
                        $in=mysql_query("INSERT INTO ".$table."members (uid,user,largest,`group`,integral,noticeqq,`status`) VALUES ('".$uid."','".$username."','".$_initialmaxqq."','$_initialgroup','$_initialintegral','0','1')",$con);
                        echo mysql_error();
                    }
                    $tem_url_return=$lang[system][url_return][index];
                    $state=$lang[system][login]['success'].$ucsynlogin;
                }
                else{
                        $_admincp_power[allowreg] = mysql_fetch_array(mysql_query("SELECT * FROM ".$table."admincp_power
WHERE name='allowreg'"));
                        if($_admincp_power[allowreg][power]=='yes'){
                            $pwd =md5(rand(1,9999).$userInfo[mediaUserID].$userInfo[createTime]);
                            $Uname=substr($userInfo[personID].$userInfo[mediaUserID],0,15);
                            $salt = substr(uniqid(rand()), -6);
                            $pword = md5(md5($pwd).$salt);
                            $email=$Uname.'@test.izzx';
                            $phptime=strtotime("now");
                            mysql_query("INSERT INTO ".UC_DBTABLEPRE."members SET username='$Uname', password='$pword', email='$email', regip='".get_client_ip()."', regdate='$phptime', salt='$salt'");
                        if($data = uc_get_user($Uname,0)){
                            list($uid, $username, $email) = $data;
                            setcookie('login_auth', uc_authcode($uid."\t".$username, 'ENCODE'), time()+1800);
                            $ucsynlogin = uc_user_synlogin($uid);
                            $get=mysql_query("SELECT * FROM ".$table."members 
WHERE `uid`='".$uid."'",$con);
                            $unum=mysql_num_rows($get);
                            if($unum ==0){
                                $_admincp_power[maxqq] = mysql_fetch_array(mysql_query("SELECT * FROM ".$table."admincp_power
WHERE name='initialmaxqq'"));
                                $_admincp_power[group] = mysql_fetch_array(mysql_query("SELECT * FROM ".$table."admincp_power
WHERE name='initialgroup'"));
                                $_admincp_power[integral] = mysql_fetch_array(mysql_query("SELECT * FROM ".$table."admincp_power
WHERE name='initialintegral'"));
                                $_initialmaxqq = $_admincp_power[maxqq][power];
                                $_initialintegral = $_admincp_power[integral][power];
                                $_initialgroup =$_admincp_power[group][power];
                                $in=mysql_query("INSERT INTO ".$table."members (uid,user,largest,`group`,integral,noticeqq,`status`) VALUES ('".$uid."','".$username."','".$_initialmaxqq."','$_initialgroup','$_initialintegral','0','1')",$con);
                                echo mysql_error();
                            }
                            $tem_url_return=$lang[system][url_return][index];
                            $state=$lang[system][login]['success'].$ucsynlogin;
                            }
                            else{
                                $tem_url_return=$lang[system][url_return][index];
                                $state=$lang[system][login]['return'];
                            }
                  }
                  else{
                        $tem_url_return=$lang[system][url_return][index];
                        $state=$lang[system][reg]['noallowreg'];
                    }
                }
                $style->display('login/status.html');
            }catch(DengluException $e){//获取异常后的处理办法(请自定义)
                $tem_url_return=$lang[system][url_return][login];
                $state = '错误编号:'.$e->geterrorCode();  //返回错误编号
                $state.= '<br>错误信息:'.$e->geterrorDescription();  //返回错误信息
                $style->display('login/status.html');
            }
    }
    else {
    $style->display('login.html');
    }
}