<?php
/**
 * oppo会员登录页面
 */ 
require 'libs/init.php';
require 'libs/init_db.php';
require_once BD_ROOT . 'libs/Template.php';
$tab_on = 'club';
if (isset($message)) {
    unset($message);
}
$act = isset($_GET['act']) ? trim($_GET['act']) : false;

global  $cookiepre;
$bdauth = !empty($_COOKIE[$cookiepre . 'logging_status']) ? $_COOKIE[$cookiepre . 'logging_status'] : '';
$login_title = array();
if (!empty($bdauth)) {
    list($id, $name, $value) = explode("\t", dz_authcode($bdauth,'DECODE'));
    if ($id == $_SESSION['u_id'] && $name == 'login' && $value == 'true') {
        //页面刷新重新设置时间
        $bdauth = dz_authcode($_SESSION['u_id'] . "\t" . 'login' . "\t" . 'true', "ENCODE") ;
        bdsetcookie('logging_status', $bdauth, time()+60*10);
        //检查是否为管理员
        $path = !empty($_COOKIE[$cookiepre . 'status']) ? $_COOKIE[$cookiepre . 'status'] : '';
        if (!empty($path)) {
            list($id, $user, $v) = explode("\t", dz_authcode($path,'DECODE'));
            if ($user == 'admin' && $v == 'oppo' && $act == 'product') {
                $bdauth1 = dz_authcode($_SESSION['u_id'] . "\t" . 'admin' . "\t" . 'oppo', "ENCODE") ;
                bdsetcookie('status', $bdauth1, time()+60*60*2);
                header("Location: product-manager-main.php");
                exit();
            } elseif (!$act) {
                header("Location: ask.php");
                exit();
            }
        } else {
            if (!$act) {
                header("Location: ask.php");
                exit();
            } elseif ($act == 'user-info') {
                header("Location:product-user-info.php");
                exit();
            } elseif ($act == 'product') {
                header("Location:product-certification.php");
                exit();
            }
        }
        $login_title = array('<a href="product-user-info.php" class="on">個人賬戶</a>', 
            '<a href="product-user-info.php?act=loginout" class="on">退出</a>');
        $login_status = 1;
    }
} else {
    $login_title = array(
         '<a href="club_register.php" class="on">註冊</a>', 
         '<a href="club_login.php" class="on">登錄</a>',
         '<a href="product-user-info.php?act=user-info" class="on">個人賬戶</a>',
    );
    $login_status = 0;
}

//先判断上次是否选择自动登录
$bdauth = !empty($_COOKIE[$cookiepre . 'autologin']) ? $_COOKIE[$cookiepre . 'autologin'] : '';
if (!empty($bdauth)) {
    list($id, $username, $password) = explode("\t", dz_authcode($bdauth,'DECODE'));
    if (!empty($username) && !empty($password)) {
    	$username = replace_specialChar($username);
    	$password = replace_specialChar($password);
    	
        $_SESSION['username'] = $username;
        $_SESSION['password'] = $password;
    }
}
$tab_id = isset($_GET['tab_id']) ? intval($_GET['tab_id']) : 0; 
if ($tab_id == 0) {
    if ($_POST['act'] == 'club') {
        $seccode  = !empty($_POST['seccode']) ? $_POST['seccode'] :  '';
        if(!$seccode || strtolower($seccode) != strtolower($_SESSION['seccode'])){
            $seccode = '';
        }

        $username = isset($_POST['username']) ? trim($_POST['username']) : '';
        $password = isset($_POST['password']) ? trim($_POST['password']) : '';
        
        $username = replace_specialChar($username);
        $password = replace_specialChar($password);
        
        $type = isset($_POST['type']) ? trim($_POST['type']) : 0;
        $password = md5($password);
        $_SESSION['username'] = $username;
        $_SESSION['password'] = $password;

        //是否下次自动登录
        if ($type == 1) {
            $bdauth = dz_authcode('a'. "\t" . $username . "\t" . $password, "ENCODE") ;
            bdsetcookie('autologin', $bdauth, time()+60*60*12);
        }

        if(empty($seccode)){
            $message = '驗證碼有誤，請重新輸入！';
        } else if ($username && $password) {
            $sql = "SELECT u_id, status FROM  oppo_bd.bd_club_user
        WHERE user_name = '{$username}' AND password = '{$password}' LIMIT 1";
            $row = $db->getRow($sql);
            if ($row['u_id']) {
                $_SESSION['u_id'] = $row['u_id'];
                //登录之后创建cookie
                $bdauth = dz_authcode($_SESSION['u_id'] . "\t" . 'login' . "\t" . 'true', "ENCODE") ;
                bdsetcookie('logging_status', $bdauth, time()+60*10);
                $bdauth1 = dz_authcode($_SESSION['u_id'] . "\t" . 'admin' . "\t" . 'oppo', "ENCODE") ;
                //管理员查看产品注册

                if ($row['status'] == 1) {
                    bdsetcookie('status', $bdauth1, time()+60*60*2);
                    if ($act == 'product') {
                        header("Location: product-manager-main.php");
                        exit();
                    } else {
                        header("Location: ask.php");
                        exit();
                    }
                } else {
                    //页面跳转
                    bdsetcookie('status');
                    if ($act == 'product') {
                        header("Location:product-certification.php");
                        exit();
                    } elseif ($act == 'user-info') {
                        header("Location:product-user-info.php");
                        exit();
                    } else {
                        header("Location: ask.php");
                        exit();
                    }
                }
            } else {
                $message = '用户名或密码有误，请重新登录';
            }
        }
    }

}

function replace_specialChar($strParam){
	$regex = "/\/|\~|\#|\\$|\%|\^|\&|\(|\)|\{|\}|\:|\<|\>|\?|\[|\]|\/|\;|\'|\`|\=|\\\|\|/";
	return preg_replace($regex,"",$strParam);
}

include Zandy_Template::outCache('club_login', $siteConf['tplDir'], $siteConf['cacheDir']);

if (isset($message) && $message) {
    alert($message);
}

?>