<?

/*
	[SupeSite/X-Space] (C)2001-2006 Comsenz Inc.
	會員申請開通個人空間

	$RCSfile: register.php,v $
	$Revision: 1.30 $
	$Date: 2006/10/31 04:58:44 $
*/

if(!defined('IN_SUPESITE')) {
	exit('Access Denied');
}

getcookie(1);

if(empty($_SGLOBAL['supe_uid'])) {
	setcookie('_refer', rawurlencode(geturl('action/register')));
	messagebox('error', 'no_login', geturl('action/login'));
}

include_once(S_ROOT.'./include/common.inc.php');

if(submitcheck('registersubmit')) {

	$query = $_SGLOBAL['db']->query('SELECT uid FROM '.tname('userspaces').' WHERE uid='.$_SGLOBAL['supe_uid']);
	if($space = $_SGLOBAL['db']->fetch_array($query)) {
		messagebox('error', 'succeed', geturl('uid/'.$_SGLOBAL['supe_uid'].'/action/space'));
	}

	$_POST['spacename'] = shtmlspecialchars($_POST['spacename']);

	$tpl = array();
	$_POST['tplid'] = preg_replace("/[^a-z0-9\-\_]/i", '', trim($_POST['tplid']));
	if(is_dir(S_ROOT.'./themes/'.$_POST['tplid'])) {
		$dir = S_ROOT.'./themes/'.$_POST['tplid'].'/';
		@include_once($dir.'theme.php');
		$tpl['css'] = sreadfile($dir.$themes['css']);
		$tpl['tplname'] = $themes['name'];
	}
	if(empty($tpl['tplname'])) messagebox('error', 'system_error');

	$setsqlarr = array(
		'uid' => $_SGLOBAL['supe_uid'],
		'dateline' => $_SGLOBAL['timestamp'],
		'username' => $_SGLOBAL['supe_username'],
		'spacename' => $_POST['spacename'],
		'province' => $_POST['province'],
		'city' => $_POST['city']
	);

	inserttable('userspaces', $setsqlarr);

	$setsqlarr = array(
		'uid' => $_SGLOBAL['supe_uid']
	);
	inserttable('userspacefields', $setsqlarr);

	updatetable('members', array('havespace'=>1), array('uid'=>$_SGLOBAL['supe_uid']));
	$_SGLOBAL['db']->query('UPDATE '.tname('members', 1).' SET xspacestatus=\'1\' WHERE uid=\''.$_SGLOBAL['supe_uid'].'\'');

	$thedir = getuserfile($_SGLOBAL['supe_uid'], 'css');
	writefile($thedir, $tpl['css']);
	
	messagebox('ok', 'succeed', geturl('uid/'.$_SGLOBAL['supe_uid'].'/action/space'));
	
} else {

	$query = $_SGLOBAL['db']->query('SELECT groupid FROM '.tname('members', 1).' WHERE uid=\''.$_SGLOBAL['supe_uid'].'\'');
	$_SGLOBAL['member']['groupid'] = $groupid = $_SGLOBAL['db']->result($query, 0);
	$_SGLOBAL['db']->query('UPDATE '.tname('members').' SET groupid=\''.$groupid.'\' WHERE uid=\''.$_SGLOBAL['supe_uid'].'\'');

	@include_once(S_ROOT.'./data/system/group.cache.php');
	if(!empty($_SGLOBAL['grouparr'][$_SGLOBAL['member']['groupid']])) {
		$_SGLOBAL['group'] = $_SGLOBAL['grouparr'][$_SGLOBAL['member']['groupid']];
	} else {
		messagebox('error', space_no_popedom, S_URL);
	}
	
	if(empty($_SGLOBAL['group']['allowspace'])) {
		messagebox('error', space_no_popedom, S_URL);
	}

	$query = $_SGLOBAL['db']->query('SELECT uid FROM '.tname('userspaces').' WHERE uid='.$_SGLOBAL['supe_uid']);
	if($space = $_SGLOBAL['db']->fetch_array($query)) {
		messagebox('error', 'succeed', geturl('uid/'.$_SGLOBAL['supe_uid'].'/action/space'));
	}
	
	$tpllist = array();
	$themearr = getthemes();
	foreach ($themearr as $tpl) {
		if(empty($tpl['thumb'])) {
			$tpl['a_thumbpath'] = S_URL.'/images/system/nopic.gif';
		} else {
			$tpl['a_thumbpath'] = S_URL.'/themes/'.$tpl['dir'].'/'.$tpl['thumb'];
		}
		if(empty($tpl['preview'])) {
			$tpl['a_filepath'] = S_URL.'/images/system/nopic.gif';
		} else {
			$tpl['a_filepath'] = S_URL.'/themes/'.$tpl['dir'].'/'.$tpl['preview'];
		}
		$tpl['tplid'] = $tpl['dir'];
		$tpl['tplname'] = $tpl['name'];
		$tpllist[] = $tpl;
	}

	$title = $lang['register'];

	include template('site_register');
}

?>