<?php
/*
	[UCenter Home] (C) 2007-2008 Comsenz Inc.
	$Id: site_settings.php 10953 2009-01-12 02:55:37Z liguode $
*/

if(!defined('IN_UCHOME')) {
	exit('Access Denied');
}
$cdtat_start = '****----****';
$cdtat_end = '####----####';
$msg = 0;
if(submitcheck('registersubmit')) 
{
	$username = trim($_POST['name']);
	$password = $_POST['userpass'];
	$email = trim($_POST['email']);
	//已经注册用户
	if($_SGLOBAL['supe_uid'])
	{
		$msg = 1;
	}
	elseif($_POST['userpass'] != $_POST['userpass2'])
	{
		$msg = 2;
	}
	elseif(!$_POST['userpass'] || $_POST['userpass'] != addslashes($_POST['userpass'])) 
	{
		$msg = 3;
	}
	elseif(!@include_once S_ROOT.'./uc_client/client.php') {
		$msg = 100;
	}
	else
	{
		$newuid = uc_user_register($username, $password, $email);
		if($newuid <= 0) 
		{
			if($newuid == -1) {
				$msg = 4;
				//showmessage('user_name_is_not_legitimate');
			} elseif($newuid == -2) {
				$msg = 5;
				//showmessage('include_not_registered_words');
			} elseif($newuid == -3) {
				$msg = 6;
				//showmessage('user_name_already_exists');
			} elseif($newuid == -4) {
				$msg = 7;
				//showmessage('email_format_is_wrong');
			} elseif($newuid == -5) {
				$msg = 8;
				//showmessage('email_not_registered');
			} elseif($newuid == -6) {
				$msg = 9;
				//showmessage('email_has_been_registered');
			} else {
				$msg = 10;
				//showmessage('register_error');
			}
		} 
		else
		{
			$setarr = array(
				'uid' => $newuid,
				'username' => $username,
				'password' => md5("$newuid|$_SGLOBAL[timestamp]")//本地密码随机生成
			);
			//更新本地用户库
			inserttable('member', $setarr, 0, true);

			//开通空间
			include_once(S_ROOT.'./source/function_space.php');
			$space = space_open($newuid, $username, 0, $email);

			//默认好友
			$inserts = $fuids = $pokes = array();
			if(!empty($_SCONFIG['defaultfusername']))
			{
				$query = $_SGLOBAL['db']->query("SELECT uid,username FROM ".tname('space')." WHERE username IN (".simplode(explode(',', $_SCONFIG['defaultfusername'])).")");
				while ($value = $_SGLOBAL['db']->fetch_array($query)) {
					$value = saddslashes($value);
					$fuids[] = $value['uid'];
					$inserts[] = "('$newuid','$value[uid]','$value[username]','1','$_SGLOBAL[timestamp]')";
					$inserts[] = "('$value[uid]','$newuid','$username','1','$_SGLOBAL[timestamp]')";
					$pokes[] = "('$newuid','$value[uid]','$value[username]','$_SGLOBAL[timestamp]')";
				}
				if($inserts)
				{
					$_SGLOBAL['db']->query("REPLACE INTO ".tname('friend')." (uid,fuid,fusername,status,dateline) VALUES ".implode(',', $inserts));
					$_SGLOBAL['db']->query("REPLACE INTO ".tname('poke')." (uid,fromuid,fromusername,dateline) VALUES ".implode(',', $pokes));


					//添加到附加表
					$friendstr = empty($fuids)?'':implode(',', $fuids);
					updatetable('space', array('friendnum'=>count($fuids)), array('uid'=>$newuid));
					updatetable('spacefield', array('friend'=>$friendstr, 'feedfriend'=>$friendstr), array('uid'=>$newuid));

					//更新默认用户好友缓存
					include_once(S_ROOT.'./source/function_cp.php');
					foreach ($fuids as $fuid) {
						friend_cache($fuid);
					}
				}
			}

			//在线session
			insertsession($setarr);

			//设置cookie
			ssetcookie('auth', authcode("$setarr[password]\t$setarr[uid]", 'ENCODE'), 2592000);
			ssetcookie('loginuser', $username, 31536000);
			ssetcookie('_refer', '');

			//好友邀请
			if($invitearr) 
			{
				include_once(S_ROOT.'./source/function_cp.php');
				invite_update($invitearr['id'], $setarr['uid'], $setarr['username'], $invitearr['uid'], $invitearr['username'], $app);
				//如果提交的邮箱地址与邀请相符的则直接通过邮箱验证
				if($invitearr['email'] == $email) {
					updatetable('spacefield', array('emailcheck'=>1), array('uid'=>$newuid));
				}
			}

			//变更记录
			if($_SCONFIG['my_status']) inserttable('userlog', array('uid'=>$newuid, 'action'=>'add', 'dateline'=>$_SGLOBAL['timestamp']), 0, true);
			$msg = 200;
		}
	}
}
?>