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

/**
* 登录
**/

if(empty($_GET['style']) && ($inte = &$core->integrate()) && !empty($inte->CONFIG['client'])){
	//header('HTTP/1.1 301 Moved Permanently');
    $url = strpos(REQUEST_URI,'login')===false?REQUEST_URI:HTTP_REFERER;
    $url .= (strpos($url,'?')!=false?'&':'?').'_='.time();
	header('Location: '. $inte->CONFIG['api'] . $inte->CONFIG['param'].'url='.urldecode($url));
	exit;
}

if(REQUEST_METHOD == 'GET'){
	//前台登录框
	
	if(isset($_GET['username']) || isset($_GET['password'])){
		exit('attack');
	}
	$style = !empty($_GET['style']) ? xss_clear($_GET['style']) : '';
	$style = in_array($style,array('1','admin','admin_main','banshi','banshi2','com','gov','gov18','interview','jianhua','list','loginbox','loginbox2','opi','opinion','qiyue','sites')) ? $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'){
	
	$username = isset($_POST['username']) ? html_entities($_POST['username']) : '';
	if(!empty($core->CONFIG['admin_login_false'])){		
        $alfData = $core->CACHE->read('', 'core', 'admin_login_false');	
        if(isset($alfData[$username]) 
            && $alfData[$username]['count']>= intval($core->CONFIG['admin_login_false']) 
            && $alfData[$username]['last']> P8_TIME - intval($core->CONFIG['admin_login_false_lock'])*60){
			message(p8lang($P8LANG['admin_login_false'],$core->CONFIG['admin_login_false'],$core->CONFIG['admin_login_false_lock'])); 
        }
    }
	
	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'] = $this_module->authcode(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));
				admin_login_false($username);
                $uc_url = $site_name ? $core->U_controller.'?site='.$site_name : $core->U_controller;
				if($site_name){
					message(p8lang($P8LANG['login_success'],$uc_url,$forward). $data['message'], $forward, 5);
				}else{
					$sites_system = $core->load_system('sites');
					$mysites = $sites_system->get_manage_sites($data['id']);						
					if(empty($sites_system) || empty($mysites)){
						message(p8lang($P8LANG['login_success'],$uc_url,$forward). $data['message'], $forward, 5);
					}else{						
						message($P8LANG['login_success'],$this_router.'-mysites' , 0);					
					}
				}
			break;
			case 1:
				if(P8_AJAX_REQUEST)exit("['user_no_verify']");
				admin_login_false($username,false);
				message($P8LANG['user_no_verify'] . $data['message'], $forward, 5);
			break;
			case -1:
				if(P8_AJAX_REQUEST)exit("['no_such_username']");
				admin_login_false($username,false);
				message('no_such_username', HTTP_REFERER);
			break;
			
			case -2:
				if(P8_AJAX_REQUEST)exit("['wrong_password']");
				admin_login_false($username,false);
				message('wrong_password', HTTP_REFERER);
			break;
		}		
	}
	
}

function admin_login_false($username, $success=true){
    global $core;
    $count = intval($core->CONFIG['admin_login_false']);
    if(!$count)return;
    
    $data = $core->CACHE->read('', 'core', 'admin_login_false');
    $data = $data?$data:array();
    /*
	foreach($data as $name=>$row){
        if($row['last']<P8_TIME - intval($core->CONFIG['admin_login_false_lock'])*60){
            unset($data[$username]);
        }
    }
	*/
    if($success){
        if(empty($data[$username]))return;
        unset($data[$username]);
        
    }
    $adminlog = array();
    if(!$success){        
        if(empty($data[$username])){
            $adminlog = array(
                'begin' => P8_TIME,
                'last' => P8_TIME,
                'count' => 1
            );
        }else{
            $adminlog = $data[$username];
            $adminlog['last'] = P8_TIME;
            $adminlog['count'] += 1;
        }        
    }
    if($adminlog)$data[$username] = $adminlog;
    
    $core->CACHE->write('', 'core', 'admin_login_false',$data);
}