<?php
define('THIS_SCRIPT', 'spaceapply');
require 'space_global.php';
require_once INC_PATH.'space.functions.php';
$fsetting['styletpl'] = $_DCACHE['stylecss'][0];
$fsetting['styletpl_prev'] = '';
$lang = load_lang($lang,'space.error');
$lang = load_lang($lang,'spaceapply');
if (1 != $spaceSystemSetting[0]) {
	spaceHeader(array('stats'=>$lang['HeaderTitle1_1'],'isSystem'=>true,'topEnable'=>true,'navEnable'=>true));
	spaceShowMsg($lang['spaceError.str39']);
	exit;
} else {
	$action = strtolower($action);
	if ('savereg' === $action) {
		if (0 == $userid) {
			spaceHeader(array('stats'=>$lang['HeaderTitle1_1'],'isSystem'=>true,'topEnable'=>true,'navEnable'=>true));
			spaceShowMsg($lang['spaceError.str42']);
			exit;
		}
		if (0 < $spaceUserID) {
			spaceHeader(array('stats'=>$lang['HeaderTitle1_1'],'isSystem'=>true,'topEnable'=>true,'navEnable'=>true));
			spaceShowMsg($lang['spaceError.str41']);
			exit;
		} else {
			spaceHeader(array('stats'=>$lang['space.header.str6'],'isSystem'=>true,'topEnable'=>true,'navEnable'=>true));
			if (false === ChkPost()) {
				spaceShowMsg($lang['spaceError.str0']);
				exit;
			}
			$nick_name = trim($nick_name);
			$space_name = trim($space_name);
			$space_password = trim($space_password);
			$space_title = trim($space_title);
			$space_ctitle = trim($space_ctitle);
			require_once INC_PATH.'DV_Encoding.class.php';
			$_objEnc =& DV_Encoding::GetEncoding($charset);
			if ('' === $nick_name) {
				$nick_name = $membername;
			} else {
				$_intLength = $_objEnc->StrLength($nick_name);
				if (50 < $_intLength OR 1 > $_intLength) {
					spaceShowMsg($lang['spaceError.str6']);
					exit;
				} else {
					$nick_name = htmlspecialchars($nick_name);
				}
			}
			//Check nick name
			if (false === checkText($nick_name)) {
				spaceShowMsg($lang['spaceError.str7']);
				exit;
			}
			//Check space name
			if(1 === preg_match('#[^a-zA-Z0-9_]#i',$space_name) OR false === checkText($space_name)) {
				spaceShowMsg($lang['spaceError.str8']);
				exit;
			}
			//Check password
			$rawspacePassword = $space_password;
			if ('' !== $space_password) {
				$space_password = dv_md5($space_password);
			} else {
				$space_password = $userinfo['userpassword'];
			}
			if ('' !==  $space_title) {
				$_intLength = $_objEnc->StrLength($space_title);
				if(150 < $_intLength OR 1 > $_intLength) {
					spaceShowMsg($lang['spaceError.str10']);
					exit;
				} else {
					$space_title = htmlspecialchars($space_title);
				}
			} else{
				$space_title = $membername.$lang['tpl.str0'];
			}
			if ('' !== $space_ctitle) {
				$_intLength = $_objEnc->StrLength($space_ctitle);
				if (250 < $_intLength OR 1 > $_intLength) {
					spaceShowMsg($lang['spaceError.str10']);
					exit;
				} else {
					$space_ctitle = htmlspecialchars($space_ctitle);
				}
			} else {
				$space_ctitle = $membername.$lang['tpl.str1'];
			}
			$_intCount = $db->scalar("SELECT COUNT(1) FROM {$dv}space_user WHERE userid='{$userid}'");
			if(0 >= $_intCount) {
				$_intCount = $db->scalar("SELECT COUNT(1) FROM {$dv}space_user WHERE spacename='{$space_name}'");
			}
			if (0 < $_intCount) {
				spaceShowMsg($lang['spaceError.str11']);
				exit;
			}
			$_result = array();
			if ($query = $db->query("SELECT scatid,scattitle,scatnote,ucatnum,topicnum,postnum,todaynum,stype,lastuptime FROM {$dv}space_syscat ORDER BY scatid", array('pageSize'=>1))) {
				if ($_result = $db->fetch_array($query)) {
					$db->query("UPDATE {$dv}space_syscat SET ucatnum=ucatnum+1 WHERE scatid='".$db->escape_string($_result['scatid'])."'");
				}
			}
			$_intSpaceSize = 0;
			if(is_numeric($spaceSystemSetting[15])) {
				$_intSpaceSize = $spaceSystemSetting[15];
			}
			if (!empty($spaceConfig_Default['spaceUserInfo']['spacesetting'])) {
				$_strspaceSetting = $spaceConfig_Default['spaceUserInfo']['spacesetting'];
			} else {
				$_strspaceSetting = '1,0,120,15,1,2,15,15,20,12,15,10,space/images/sex/footmark1.gif,space/images/sex/footmark2.gif,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1';
			}
			$db->query("INSERT INTO {$dv}space_user(userid,username,nickname,spacename,password,spacetitle,spacechildtitle,spacesize,spacesetting,syscatid,skinid,joinspacetime) VALUES('{$userid}','{$spaceUserName}','{$nick_name}','{$space_name}','{$space_password}','{$space_title}','{$space_ctitle}','{$_intSpaceSize}','{$_strspaceSetting}','".$db->escape_string($_result['scatid'])."','{$spaceConfig_System['skinid']}','".TIME_NOW."')");
			$db->query("UPDATE {$dv}space_system SET s_usernum=s_usernum+1");
			require_once INC_PATH.'BoardCache.class.php';
			$mycache =& new BoardCache($db);
			$mycache->createspaceConfig();
			spaceShowMsg($lang['spaceError.str44'],1,'');
			exit;
		}
	} elseif ('logout' === $action) {
		session_start();
		unset($_SESSION['spaceManage']);
		header('Location:space.php');
		exit;
	} else {
		if (0 == $userid) {
			spaceHeader(array('stats'=>$lang['HeaderTitle1_1'],'isSystem'=>true,'topEnable'=>true,'navEnable'=>true));
			spaceShowMsg($lang['spaceError.str42']);
			exit;
		}
		if (0 < $spaceUserID) {
			spaceHeader(array('stats'=>$lang['HeaderTitle1_1'],'isSystem'=>true,'topEnable'=>true,'navEnable'=>true));
			spaceShowMsg($lang['spaceError.str41']);
			exit;
		} else {
			spaceHeader(array('stats'=>$lang['space.header.str6'],'isSystem'=>true,'topEnable'=>true,'navEnable'=>true));
			require printout('spaceapply');
		}
	}
}

spaceFooter();

?>