<?php
/**
 * @version $Id: login.php v2.0 $
 * @package PBDigg
 * @copyright Copyright (C) 2007 - 2008 PBDigg.com. All Rights Reserved.
 * @license PBDigg is free software and use is subject to license terms
 */

define('PB_CSCRIPT', 'login');
require_once('include/common.inc.php');

if (!$forward && strpos($_PBDEV['PB_REFERER'], 'login.php') === FALSE && strpos($_PBDEV['PB_REFERER'], 'register.php') === FALSE)
{
	$forward = $_PBDEV['PB_REFERER'];
}
else
{
	$forward = $_PBENV['PB_URL'];
}

if (($action == 'login') && ($_PBDEV['CK_HASH'] == $verifyhash))
{
	if ($pb_gdcheck['login'] && !ckgdcode($captcha))
	{
		showMsg($u_message['checkcode_error'], 'login.php');
	}
	$failedlogin = TRUE;
	$uc_return = array();
	$username = trim($_POST['username']);
	$post_pw = trim($_POST['password']);
	$password = md5($post_pw);
	$cookietime = isset ($persistent) ? 1 : 0;
	if ($pb_ucenable)
	{
		$uc_return = uc_user_login($username, $post_pw);
		if ($uc_return[0] > 0)
		{
			$pbuid = $DB->db_fetch_one_array("SELECT uid FROM {$db_prefix}memberdata WHERE ucuid = ".$uc_return[0]);
			if ($pbuid)
			{
				$autoid = $pbuid['uid'];
				$TPL->assign('uclogin', uc_user_synlogin($uc_return[0]));
			}
			else
			{
				$rs = $DB->db_fetch_one_array("SELECT COUNT(*) num FROM {$db_prefix}members WHERE username = '".$uc_return[1]."' OR email = '".$uc_return[3]."'");
				if ($rs['num']) showMsg('用户名已经被注册，无法在PBDigg登陆');
				$DB->db_query("INSERT INTO {$db_prefix}members (username, password, email, adminid, groupid, publicemail, regip, regdate) VALUES ('".$uc_return[1]."', '".md5($uc_return[2])."', '".$uc_return[3]."', 0, -1, 0, '".$_PBDEV['PB_IP']."', '$timestamp')");
				$autoid = $DB->db_insertID();
				$DB->db_query("INSERT INTO {$db_prefix}memberexp (uid) VALUES ($autoid)");
				$DB->db_query("INSERT INTO {$db_prefix}memberdata (uid, currency, lastip, currentip, lastvisit, ucuid) VALUES ($autoid, ".intval($reg_credit).", '".$_PBDEV['PB_IP']."', '".$_PBDEV['PB_IP']."', $timestamp, ".$uc_return[0].")");
				$DB->db_query("UPDATE {$db_prefix}sitestat SET newmember = '".$uc_return[1]."', membernum = membernum + 1 WHERE id = 1");
				$Cache->sitestat();
			}
			sCookie('pb_auth', PEncode(($autoid."\t".$password), $pb_sitehash), $cookietime);
			$DB->db_query("UPDATE {$db_prefix}memberdata SET visitcount = visitcount + 1 WHERE uid = $autoid LIMIT 1");
			$failedlogin = FALSE;
		}
	}
	else
	{
		$loginauth = $DB->db_fetch_one_array("SELECT m.uid AS login_uid, m.password AS login_pw, m.groupid AS login_gid FROM {$db_prefix}members m WHERE m.username = '$username'");
		if ($loginauth && ($loginauth['login_pw'] == $password))
		{
			extract($loginauth);
			$login_gid = ($login_gid == '-1') ? '7' : (int)$login_gid;
			@include_once(PBDIGG_ROOT.'data/cache/cache_usergroup_'.(int)$login_gid.'.php');
			if (!$allowvisit)
			{
				showMsg($u_message['login_visit_denied']);
			}
			sCookie('pb_auth', PEncode(($login_uid."\t".$login_pw), $pb_sitehash), $cookietime);
			$DB->db_query("UPDATE {$db_prefix}memberdata SET visitcount = visitcount + 1 WHERE uid = $login_uid LIMIT 1");
			if ($login_gid == '6')
			{
				redirect($u_message['login_avtive_account'], 'member.php');
			}
			else
			{
				redirect($u_message['login_success'], $forward);
			}
		}
	}
	if ($failedlogin)
	{
		$errorlog = '<?php exit(\'Access Denied!\'); ?>'."\t".$timestamp."\t".htmlspecialchars(stripslashes($_POST['username']))."\t".htmlspecialchars(stripslashes($_POST['password']))."\t".$_PBDEV['PB_IP']."\n";
		PWriteFile(PBDIGG_ROOT.'data/log/logerror.php', $errorlog);
		showMsg($u_message['login_failed'], $forward);
	}
	redirect($u_message['login_success'], $forward);
}
elseif ($action == 'logout')
{
	//quit
	sCookie('pb_auth', '', -1);
	sCookie('pb_sid', '', -1);
	$DB->db_query("DELETE FROM {$db_prefix}onlines WHERE sid = '".$customer['sid']."' LIMIT 1");
	unset($customer);
	if ($pb_ucenable) $TPL->assign('uclogin', uc_user_synlogout());
	redirect($u_message['logout_success'], $forward);
}
else
{
	$seo['pagetitle'] = $seo['keywords'] = $seo['description'] = $u_message['login'];
}

$TPL->assign('seo', $seo);
$TPL->assign('forward', rawurlencode($forward));
PBOutPut('login.html');

?>
