<?php
define('THIS_SCRIPT', 'indivgroup_new');
require_once 'global.php';
include_once $fsetting['manageurl'].'inc/page.class.php';
$lang = load_lang($lang, 'indivgroup_new' );
global $title_num,$action,$reaction,$membername;

include_once INC_PATH.'DV_Encoding.class.php';
$objenc =& DV_Encoding::GetEncoding($charset);
require_once CACHE_PATH.'Forum_Group_top.php';

$title_num = array(0=>array('linktitle'=>$lang['template.strings0'],'linkurl'=>'indivgroup.php'),1=>array('linktitle'=>$lang['template.strings1']));

if(!empty($membername))
	$mygrouplink= '<a href="?action=mygrouplist">'.$lang['template.strings2'].'</a>';
else
	$mygrouplink='';

if($fsetting['indivgroup_show'] == 0)
{
	head(1,0,0,$title_num,$mygrouplink);
	showmsg($lang['error_close']);
	footer();
}
if($_GET['action'] == "appgroup"){
	head(1,0,0,$title_num,$mygrouplink);
	save_new();
	footer();
}
else if($_GET['action'] == "appgroupcheck"){
	save_check();
}
else{
	head(1,0,0,$title_num,$mygrouplink);
	include_once printout('indivgroup_new');
	footer();
}

function save_check(){
	global $db,$lang,$dv,$restriction,$page,$userid,$membername,$fsetting;
	$GroupName= trim($_POST['GroupName_c']);
	$GroupName= $db->escape_string($GroupName);
	$retStr= '';

	$query=$db->query("select id from {$dv}group_name where groupname='".$GroupName."'");
	if ($ts = $db->fetch_array($query)) 
	{
		$retStr= $lang['error_rename'];
	}
	
	echo "<script language=\"javascript\">parent.showinfo(\"".$retStr."\")</script>";
}

function save_new(){
	global $db,$lang,$dv,$restriction,$page,$userid,$membername,$fsetting;
	$GroupName= trim($_POST['GroupName']);
	$GroupInfo=$_POST['GroupInfo'];
	$Locked=intval($_POST['Locked']);
	$ViewFlag=intval($_POST['ViewFlag']);

	if($Locked == '')
		$Locked= '0';
	if($ViewFlag == '')
		$ViewFlag= '0';

	$retStr= "";

	if($fsetting['indivgroup_check'] == 1)
		$stats= 0;
	else
		$stats= 1;

	if($GroupName == '')
	{
		$retStr= $lang['error_name'];
	}
	else if($membername == '')
	{
		$retStr= $lang['error_app'];
	}
	else
	{
		$GroupName= $db->escape_string($GroupName);
		$GroupInfo= $db->escape_string($GroupInfo);
		$Locked= $db->escape_string($Locked);
		$ViewFlag= $db->escape_string($ViewFlag);

		$query=$db->query("select id from {$dv}group_name where groupname='".$GroupName."'");
		if ($ts = $db->fetch_array($query)) 
		{
			$retStr= $lang['error_rename'];
		}
		else
		{
			$query=$db->query("select id from {$dv}group_name where appusername='".$membername."'");
			if ($ts = $db->fetch_array($query)) 
			{
				$retStr= $lang['new_reapp'];
			}
			else
			{
				$faceurl= '';
				$upfileinfo =& $_FILES['groupface'];

				if (empty($upfileinfo) OR 4 == $upfileinfo['error']){
					$faceurl= '';
				}
				else{
					$upfileinfo['tmp_name'] = str_replace('\\\\', '\\', $upfileinfo['tmp_name']);
					if (false === is_uploaded_file(str_replace('\\\\', '\\', $upfileinfo['tmp_name']))) {
						//无法取得文件名
					}
					else{
						$fileext = str_replace('.','',strrchr($upfileinfo['name'],'.'));
						if (false === $fileext) {
							list(,$fileext) = explode('/',$upfileinfo['type']);
							if (strpos($fileext,'-') !== false) {
								$fileext = str_replace('-','',strstr($fileext,'-'));
							}
						}
						$lowerext = trim(strtolower($fileext));
						
						if (false === strpos(',gif,jpg,bmp,jpeg,png,', ",{$lowerext},")) {
							showmsg($lang['tpl.str45']);
							return;
						}
						if (($upfileinfo['size'] / 1024) > 100) {	//	图片不能超过100K！
							showmsg($lang['tpl.str59']);
							return;
						}

						$upbasedir = $fsetting['forumuploadname'];

						$len = strlen($upbasedir);
						if ('/' === $upbasedir{$len - 1}) {
							$upbasedir = substr($upbasedir, 0, $lent - 1);
						}

						$filename = $userid.'_'.md5(uniqid(rand())).'.'.$lowerext;
						$destfile = $upbasedir.'/faces/'.$filename;
						$tmpfile = $upbasedir.'/tempimage/'.$filename;
						if (move_uploaded_file($upfileinfo['tmp_name'], $destfile)) {
							$faceurl= $destfile;
						}
					}
				}

				if($faceurl == ''){
					$picsql=$db->query("select userface from {$dv}user where userid='{$userid}'");
					if($pics=$db->fetch_array($picsql)){
						$arrpic=explode("|",$pics['userface']);
						if(is_array($arrpic)){
							$faceurl=$arrpic[count($arrpic)-1];
						}
					}
				}

				$db->query("insert into {$dv}group_name(groupname,groupinfo,appuserid,appusername,usernum,stats,limituser,appdate,passdate,locked,viewflag,groupface) values ('".$GroupName."','".$GroupInfo."',".$userid.",'".$membername."',1,'".$stats."','".$fsetting['indivgroup_limit']."',now(),now(),".$Locked.",".$ViewFlag.",'".$faceurl."')");

				$groupid= $db->insert_id();

				if($groupid){
					$db->query("Insert Into {$dv}group_user(groupid,userid,username,islock) values(".$groupid.",".$userid.",'".$membername."',2)");
					$retStr= $lang['tpl.str46']."<script>setTimeout(\"document.location.href='indivgroup_index.php?groupid=$groupid';\",3000);</script>";
					require INC_PATH.'BoardCache.class.php';
					Create_Forum_Group_top_new($groupid,10);
				}
				else
				{
					$retStr= $lang['new_fai'];
				}
			}
		}
	}

	showmsg($retStr);
}

?>