<?php
require 'libs/init.php';
require 'libs/init_db.php';//数据库
$tab_on = 'club';

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') {
        $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;
}

//默认为北京
$tab_id = isset($_GET['tab_id']) ? intval($_GET['tab_id']) : 0;
$province_id = isset($_POST['province']) ? $_POST['province'] : 2;
$city_id = isset($_POST['city']) ? $_POST['city'] : null;

$date = date("Y-m-d", time());
if ($_REQUEST['act'] == 'check_username') {
    $username = trim($_REQUEST['username']);
	$username = mysql_real_escape_string($username);
	$username = replace_specialChar($username);
	
    $sql = "SELECT 1 FROM oppo_bd.bd_club_user WHERE user_name = '{$username}' LIMIT 1";
    $user = $db->getOne($sql);
    echo json_encode($user);
    exit;
}
if ($_POST['act'] == 'insert') {
    if (strtolower(trim($_POST['code'])) != strtolower($_SESSION['seccode'])) {
        header("Location: club_register.php?res_code=wrong");
        exit();
    } else {
        if (isset($_POST['username']) && trim($_POST['username']) == '') {
            alert_back("請輸入您的用戶名", "club_register.php");
        }
        if (isset($_POST['username']) && trim($_POST['username']) != '') {
            $username = trim($_REQUEST['username']);
            $username = mysql_real_escape_string($username);
            $username = replace_specialChar($username);

            $sql = "SELECT 1 FROM oppo_bd.bd_club_user WHERE user_name = '{$username}' LIMIT 1";
            $user = $db->getOne($sql);
            if(!empty($user)) {
                alert_back("用户名已存在", "club_register.php");
            }
        }
//         if (isset($_POST['email']) && (trim($_POST['email']) == '') || !is_email(trim($_POST['email']))) {
//             alert_back("请输入合法的email地址", "club_register.php");
//         }
        if (isset($_POST['password']) && isset($_POST['repeat_password'])) {
            if ( trim($_POST['repeat_password']) != trim($_POST['repeat_password'])) {
                alert_back("请输入密码和确认密码", "club_register.php");
            }
        }
        
        if (isset($_POST['true_name']) && trim($_POST['true_name']) == '') {
            alert_back("请输入姓名", "club_register.php");
        }
        if (isset($_POST['mobile']) && (trim($_POST['mobile']) == '')){
            alert_back("请输入合法的手机号码", "club_register.php");
        }
        
        $user_name = trim($_POST['username']); 
        $e_mail = trim($_POST['email']);
        $password = md5(trim($_POST['password']));
        $sex = (trim($_POST['sex']) == -1) ? null : trim($_POST['sex']);
        $true_name = trim($_POST['true_name']);
        $mobile_phone = trim($_POST['mobile']);
        $tel_phone = trim($_POST['tel']);
        $birthday = trim($_POST['birthday']);
        $education = trim($_POST['education']);
        $marriage_status = (trim($_POST['marriage_status']) == -1) ? null : trim($_POST['marriage_status']);
        $profession = (trim($_POST['profession']) == -1) ? null : trim($_POST['profession']);
        $personal_income = (trim($_POST['personal_income']) == -1 ) ? null : trim($_POST['personal_income']);
        $province = trim($_POST['province']);
        $city = trim($_POST['city']);
        $address = trim($_POST['address']);
        $zipcode = trim($_POST['zipcode']);
        $recommended_email = trim($_POST['recommended_email']);
        $type = trim($_POST['type']);
        $purchase_reason = $_POST['purchase_reason'];
        $media = $_POST['media']; 
        $connection_type = $_POST['connection_type'];
        if (!empty($purchase_reason)) {
            $purchase_reason = implode(",", $purchase_reason);
        } 
        if (!empty($media)) {
            $media = implode(",", $media);
        }
        if (!empty($connection_type)) {
            $connection_type = implode(",", $connection_type);
        }
		
		$user_name = mysql_real_escape_string($user_name);
        $e_mail = mysql_real_escape_string($e_mail);
        $password = mysql_real_escape_string($password);
        $mobile_phone = mysql_real_escape_string($mobile_phone);
		
        $user_name = replace_specialChar($user_name);
        $e_mail = replace_specialChar($e_mail);
        $password = replace_specialChar($password);
        $mobile_phone = replace_specialChar($mobile_phone);
        
        
        $sql = "
            INSERT INTO oppo_bd.bd_club_user 
                (`user_name`, `e_mail`, `password`, `mobile_phone` ) 
            VALUES ('{$user_name}', '{$e_mail}', '{$password}','{$mobile_phone}')";
        $result = $db->exec($sql);
        if ($result) {
            //注册成功
            //header("Location: club_login.php");
           header("Location:club_register.php?tab_id=1");
            exit();
//             alert_back("注册成功，请登录", "club_login.php");
        }
    }
} 

function replace_specialChar($strParam){
	$regex = "/\/|\~|\#|\\$|\%|\^|\&|\(|\)|\{|\}|\:|\<|\>|\?|\[|\]|\/|\;|\'|\`|\=|\\\|\|/";
	return preg_replace($regex,"",$strParam);
}


require_once BD_ROOT . 'libs/Template.php';
include Zandy_Template::outCache('club_register', $siteConf['tplDir'], $siteConf['cacheDir']);