<?php
defined('PHP168_PATH') or die();

/**
* 登录
**/

if(empty($_GET['style']) && ($inte = &$core->integrate()) && !empty($inte->CONFIG['client'])){
	header('HTTP/1.1 301 Moved Permanently');
	header('Location: '. $inte->CONFIG['api'] . $inte->CONFIG['param']);
	exit;
}

if(REQUEST_METHOD == 'GET'){
	//前台登录框
	
	if(isset($_GET['username']) || isset($_GET['password'])){
		exit('attack');
	}
	$style = !empty($_GET['style']) ? xss_clear($_GET['style']) : '';
	if($style){
		include "loginstyle.php";
		exit;
	}
	if($UID){
		header('Location: '. $core->U_controller);
		exit;
	}
	if(isset($_GET['forward'])){
		$forward = html_entities($_GET['forward']);
	}else if(isset($forward)){

	}else{
		$forward = $this_module->U_controller;
	}

    $forward = $forward ? $forward : $core->U_controller;
    $forward = substr($forward,-13) == 'member-logout' ?  $core->U_controller : $forward;
    $sites_flag = in_array('s.php',explode('/',$HTTP_REFERER)) ? true : false;
    $site_name = !empty($_GET['site']) ? xss_clear(trim($_GET['site'])) : '';
    if(isset($core->systems['sites']) && !empty($core->systems['sites']['enabled'])){
        $site_controller = &$core->load_system('sites');
        $sites_flag2 = $site_controller->check_domain($HTTP_REFERER);
        $site_name = $site_name ? $site_name : (($sites_flag || $sites_flag2) ? $site_controller->SITE : '');
    }
    if($site_name) $forward = $forward . (strpos($forward, '?') ? '&' : '?') . 'site=' . $site_name;

	include template($this_module, 'login', 'member/default');
	
}else if(REQUEST_METHOD == 'POST'){
	if(P8_AJAX_REQUEST)$_POST = from_utf8($_POST);
	if(!empty($this_module->CONFIG['login_with_captcha'])){
		captcha(isset($_POST['captcha']) ? $_POST['captcha'] : '') or message('captcha_incorrect');
	}

	$forward = empty($_POST['forward']) ? $core->U_controller : xss_url($_POST['forward']);
	$site_name = empty($_POST['site_name']) ? '' : xss_url($_POST['site_name']);
	$type = isset($_POST['type'])? $_POST['type'] : 'username';
	if(isset($_POST['username']) && isset($_POST['password'])){
		$_POST['username'] = p8_addslashes2($_POST['username']);
		$_POST['password'] = $this_module->authcode($_POST['password']);
		$_POST['password'] or message('password_decode_error'); 		
		$data = $this_module->login($_POST['username'], $_POST['password'], 0, false, $type);		
		switch($data['status']){
			case 0:
				if(P8_AJAX_REQUEST)exit(p8_json($data));
                $uc_url = $site_name ? $core->U_controller.'?site='.$site_name : $core->U_controller;
				message(p8lang($P8LANG['login_success'],$uc_url,$forward). $data['message'], $forward, 5);
			break;
			case 1:
				if(P8_AJAX_REQUEST)exit("['user_no_verify']");
				message($P8LANG['user_no_verify'] . $data['message'], $forward, 5);
			break;
			case -1:
				if(P8_AJAX_REQUEST)exit("['no_such_username']");
				message('no_such_username', HTTP_REFERER);
			break;
			
			case -2:
				if(P8_AJAX_REQUEST)exit("['wrong_password']");
				message('wrong_password', HTTP_REFERER);
			break;
		}		
	}
	
}