<?php
/**
 * @version $Id: register.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', 'register');
require_once('include/common.inc.php');
require_once(PBDIGG_ROOT.'data/cache/cache_reg.php');
require_once(PBDIGG_ROOT.'include/member.class.php');
require_once (PBDIGG_ROOT.'include/mail.inc.php');
require_once(loadLang('register'));

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

if (!$reg_status)
{
	showMsg($reg_closereason, $forward);
}

if ($customer['groupid'] != '5')
{
	showMsg($register_message['register_already'], $forward);
}

if ($reg_allowsameip)
{
	$ipcachefile = PBDIGG_ROOT.'data/cache/ip_cache.php';
	if (file_exists($ipcachefile))
	{
		$ipcache = PReadFile($ipcachefile);
		if ($ipcache)
		{
			$lastregtime = (int)substr($ipcache, 12, 10);
			$lastregip = substr($ipcache, strpos($ipcache, ',') + 1);
			if (($timestamp < ($lastregtime + 3600 * $reg_allowsameip)) && ($_PBDEV['PB_IP'] == $lastregip))
			{
				showMsg(sprintf($register_message['same_ip_reg_limit'], $reg_allowsameip), $forward);
			}	
		}
	}
}

if ($action == 'reg')
{
	if ($pb_gdcheck['login'] && !ckgdcode($captcha))
	{
		showMsg($u_message['checkcode_error'], 'register.php');
	}
	if ($reg_invite)
	{
		!$invite && showMsg('请输入邀请码', 'register.php');
		$rt = $DB->db_fetch_one_array("SELECT COUNT(*) num FROM {$db_prefix}invitecode WHERE invitecode = '".$_POST['invite']."' AND (endtime >= {$timestamp} OR endtime = -1) ");
		if ($rt['num'])
		{
			$DB->db_query("DELETE FROM {$db_prefix}invitecode WHERE invitecode = '".$_POST['invite']."' OR (endtime <> -1 AND endtime <= $timestamp)", 0);
		}
		else
		{
			showMsg('您输入的邀请码不正确，请重新输入。', 'register.php');
		}
	}
	$reg_groupid = $reg_emailactive ? '6' : '-1';
	$Member = new Member();
	$Member->Action('add', array('username'=>trim($_POST['username']), 'password'=>trim($_POST['password']), 'rpassword' => trim($_POST['rpassword']), 'email'=>trim($_POST['email']), 'groupid'=>$reg_groupid, 'exp'=>array('ctsig'=>0)));
	$reg_success = $register_message['register_success'];
	$newmember = $Member->getNewUser();
	if ($reg_emailactive)
	{
		//send active email
		$activecode = $newmember[0].'|'.$timestamp.'|'.random(8);
		$activecode = rawurlencode(PEncode($activecode, $pb_sitehash));
		$mail_content = str_replace(array('[!--username--]','[!--activecode--]'), array($newmember[1],$activecode), $register_message['register_active_body']);
		PMail($email, $register_message['register_active_subject'], $mail_content);
		$reg_success = $register_message['register_check_email'];
	}
	if ($reg_sendemail)
	{
		//send welcome email
		$mail_content = str_replace(array('{!--username--}','{!--sitename--}','{!--password--}','{!--siteurl--}','{!--adminmail--}'), array($newmember[1],$pb_sitename,$newmember[2],$_PBENV['PB_URL'], $pb_adminmail), $reg_emailcontent);
		PMail($email, $register_message['register_welcome_subject'], '<pre>'.$mail_content.'</pre>');
	}
	sCookie('pb_auth', PEncode($newmember[0]."\t".$newmember[3], $pb_sitehash), 0);
	$TPL->assign('uclogin', $newmember['uc_login']);
	if ($reg_allowsameip)
	{
		PWriteFile(PBDIGG_ROOT.'data/cache/ip_cache.php', '<?php exit?>'.$timestamp.','.$_PBDEV['PB_IP'], 'wb');
	}
	redirect($reg_success, $_PBENV['PB_URL']);
}
else
{
	$seo['pagetitle'] = $seo['keywords'] = $seo['description'] = $register_message['register'];
	$profile['reg_minname'] = $reg_minname;
	$profile['reg_maxname'] = $reg_maxname;
	if ($reg_invite)
	{
		$TPL->assign('invite', '1');
	}
	$TPL->assign('profile', $profile);
}

$TPL->assign('regmsg', $register_message);
$TPL->assign('seo', $seo);
PBOutPut('register.html');
?>
