<?php
if(!defined('IN_GATEWEI')) {
	exit('Access Denied');
}
function upsurveycount2db(){
	global $db , $get_id , $post_sub_survey_count , $votevalid , $timestamp, $sid;
	if(!$post_sub_survey_count){
			showmsg("没有选项被选中","viewsurvey.php?get_id=".$get_id);
	}
	$votevalname="votevalid_".$get_id;
	global ${$votevalname};

	/*控制灌水*/
	if($$votevalname){
		showmsg("对不起,五分钟内只能投一票","viewsurvey.php?get_id=".$get_id);
	}
	/*控制灌水*/
	@SetCookie($votevalname,$sid,$timestamp+300);
	if(!is_array($post_sub_survey_count)){
		$post_count=array($post_sub_survey_count);
	}else{
		$post_count=$post_sub_survey_count;
	}
		foreach($post_count as $val){
		$count=$db->result($db->query("select sub_survey_count from g_subsurveys where sub_survey_id = $val"),0);
		$count++;
		$db->query("update g_subsurveys set sub_survey_count = $count where sub_survey_id =$val limit 1");
		}
		$db->query("update g_surveys set survey_total_count = (select SUM(sub_survey_count) from g_subsurveys where survey_id =$get_id) where survey_id =$get_id limit 1 ");
	showmsg("投票成功","viewsurvey.php?get_id=".$get_id);
}




function attach2db($dbattachments , $bid , $typeid , $g_editor_doc , $islogo=0){
	//$attachid是从表单里提交上来的标示变量
	global $db , $timestamp , $attachid ;
	$islogo=0;
		if(!empty($dbattachments) && is_array($dbattachments)){
			foreach($dbattachments as $key => $dbattach){
				@extract($dbattach);
				$db->query("INSERT INTO g_attachments ( fid , bid , typeid , name , storename , size , islogo , isimage , type , downtimes , subtime ) VALUES ( NULL , $bid , $typeid , '$name' , '$storename' , $size , 0 , $isimage , '$type' , 0 , $timestamp )");
				$replacearray[] = '[attach]'.$db->insert_id().'[/attach]';
				$searcharray[] = '[attach]'.$attachid[$key].'[/attach]';
					 }
				$g_editor_doc = str_replace($searcharray, $replacearray, $g_editor_doc);
			}
		return $g_editor_doc;
}

function attachupdb($dbattachments,$typeid){
	global $db;
	$attachdir=array(1=>'article',2=>'product',3=>'thread');
	if(!empty($dbattachments) && is_array($dbattachments)){
			foreach($dbattachments as $key => $dbattach){
				@extract($dbattach);
				//从原来的数据库中查出原附件的存储名，是否为Logo的信息
				$oldattach = $db->fetch_array($db->query("select storename,islogo from g_attachments where fid= $key limit 1"));
				$deltarget="./clientdata/attachment/".$attachdir[$typeid]."/".$oldattach['storename'];
				$db->query("update g_attachments set name='$name' ,storename = '$storename' ,type='$type' ,size=$size, isimage=$isimage where fid =$key limit 1");
				@unlink($deltarget);
				//查询原表中如果(islogo==1)，就对修改的图片进行压缩,并删除原来是logo图片的压缩图
				if($oldattach['islogo']){
					$deltarget="./clientdata/attachment/".$attachdir[$typeid]."/".replaceimg($oldattach['storename']);
					@unlink($deltarget);
					$target="./clientdata/attachment/".$attachdir[$typeid]."/".$storename;
					thumbimage($target);
				}
			 }
		}
}

function logoattach2db($dbattachments , $bid , $typeid){

	global $db , $timestamp;
		if(!empty($dbattachments) && is_array($dbattachments)){
			foreach($dbattachments as $key => $dbattach){
				@extract($dbattach);
				$db->query("INSERT INTO g_attachments ( fid , bid , typeid , name , storename , size , islogo , isimage , type , downtimes , subtime ) VALUES ( NULL , $bid , $typeid , '$name' , '$storename' , $size , 1 , $isimage , '$type' , 0 , $timestamp )");
				 }
		}
}

function article2db($articletitle,$g_editor_doc,$tag,$pid,$categoryid,$uid,$userid){
	global $db , $currentip , $timestamp , $action ,$pervalue ,$iscustom,$g_calendar_year_subtime,$g_calendar_month_subtime,$g_calendar_day_subtime;
	 if(empty($articletitle) || strlen($articletitle)>120){
		 if(POSITION=='sitefont'){
				showmsg("文章标题不能为空且最大字符数不能超过120字符");	
			}else{
				gadminmessage("文章标题不能为空且最大字符数不能超过120字符");	
			}
	}
	if(empty($g_editor_doc) || strlen($g_editor_doc)>10000){
		if(POSITION=='sitefont'){
				showmsg("文章内容不能为空且最大字符数不能超过10000字符");	
			}else{
				gadminmessage("文章内容不能为空且最大字符数不能超过10000字符");	
			}
	}
	if($iscustom){
		$timestamp=gmmktime(0,0,0,$g_calendar_month_subtime,$g_calendar_day_subtime,$g_calendar_year_subtime);
	}else{
		$timestamp=$timestamp;
		}
	if(!$pervalue{17}){
		if(POSITION=='sitefont'){
				showmsg("该用户组无此权限");
			}else{
				gadminmessage("该用户组无此权限");
			}
	}
	$articletitle = ghtmlspecialchars(trim($articletitle));
	$g_editor_doc = trim($g_editor_doc);
    require_once './engine/upload.func.php';
	$ip=explode('.',$currentip);
	$dbattachments=uploadattach('attach','article');
	$db->query("INSERT INTO g_articles (aid , uid , useraccount , ip1 , ip2 , ip3 , ip4 , status , ord , tag , hlight , articletitle , pid , categoryid , top , view , review , subtime) VALUES (NULL , $uid , '$userid' , $ip[0] , $ip[1] , $ip[2] , $ip[3] , 0 , $timestamp , $tag , 0 ,'$articletitle', $pid , $categoryid , 0 , 0 , 0 , $timestamp)");
	$bid=$db->insert_id();
	$g_editor_doc=attach2db($dbattachments , $bid , 1 , $g_editor_doc);
	$db->query("INSERT INTO g_article_content (aid, content) VALUES ($bid, '$g_editor_doc')");
	if(POSITION=='sitefont'){
				showmsg("非常感谢，您的文章已经提交，现在将转入文章列表页面","articledisplay.php?pid=".$pid."&categoryid=".$categoryid);
			}else{
				gadminmessage("非常感谢，您的文章已经成功发布","siteadmin.php?iframe=publisharticle",1);
			}
	
}



function uparticle2db($articletitle,$g_editor_doc,$tag,$mid){
	global $db , $pervalue , $cid ,$pid, $categoryid,$iscustom,$g_calendar_year_subtime,$g_calendar_month_subtime,$g_calendar_day_subtime;;
	 if(empty($articletitle) || strlen($articletitle)>120){
		 if(POSITION=='sitefont'){
				showmsg("文章标题不能为空且最大字符数不能超过120字符");
			}else{
				gadminmessage("文章标题不能为空且最大字符数不能超过120字符");
			}
	}
	if(empty($g_editor_doc) || strlen($g_editor_doc)>10000){
		 if(POSITION=='sitefont'){
				showmsg("文章内容不能为空且最大字符数不能超过10000字符");	
			}else{
				gadminmessage("文章内容不能为空且最大字符数不能超过10000字符");
			}
	}
	if(!$pervalue{17}){
		 if(POSITION=='sitefont'){
				showmsg("该用户组无此权限");
			}else{
				gadminmessage("该用户组无此权限");
			}
	}
	if(!empty($cid)){
		list($pid,$categoryid)=explode(',',$cid);
		$upcategory=True;
		if($pid==0 && $categoryid==0){
			gadminmessage("不能为文章根分类");
		}
	}else{
		$upcategory=False;	
	}
	require_once './engine/upload.func.php';
	$dbattachments=uploadattach('attach','article');
	$g_editor_doc=attach2db($dbattachments , $mid , 1 , $g_editor_doc);
	$dbattachments=array();
	$dbattachments=uploadattach('attachmodify','article');
	attachupdb($dbattachments,1);//更新附件数据表中的附件名称
	$articletitle = ghtmlspecialchars(trim($articletitle));
	$g_editor_doc = trim($g_editor_doc);
	if($upcategory){
	$db->query("update g_articles set articletitle = '$articletitle', pid = $pid, categoryid=$categoryid, tag =$tag where aid =$mid limit 1");
	}else{
	$db->query("update g_articles set articletitle = '$articletitle', tag =$tag where aid =$mid limit 1");
	}
	$db->query("update g_article_content set content  = '$g_editor_doc' where aid =$mid limit 1");
	if($iscustom){
		$timestamp=gmmktime(0,0,0,$g_calendar_month_subtime,$g_calendar_day_subtime,$g_calendar_year_subtime);
		$db->query("update g_articles set ord = $timestamp, subtime =$timestamp where aid =$mid limit 1");
	}
	if(POSITION=='sitefont'){
		showmsg("非常感谢，您的文章已修改成功","viewarticle.php?aid=".$mid);
	}else{
		gadminmessage("非常感谢，您的文章已修改成功，现在将转入文章列表页面","siteadmin.php?iframe=articlemanagement",1);
	}
}



function product2db($producttitle,$productintro,$g_editor_doc,$tag,$pid,$categoryid,$uid,$userid){
	global $db , $currentip , $timestamp , $action ,$pervalue , $g_calendar_month_subtime , $g_calendar_day_subtime , $g_calendar_year_subtime , $iscustom;
	 if(empty($producttitle) || strlen($producttitle)>120){
		 if(POSITION=='sitefont'){
			showmsg("产品标题不能为空且最大字符数不能超过120字符");	
		}else{
			gadminmessage("产品标题不能为空且最大字符数不能超过120字符");	
		}
	}
	if(empty($productintro) || strlen($productintro)>255){
		 if(POSITION=='sitefont'){
			showmsg("产品简介不能为空且最大字符数不能超过255字符");	
		}else{
			gadminmessage("产品简介不能为空且最大字符数不能超过255字符");	
		}
	}
	if(empty($g_editor_doc) || strlen($g_editor_doc)>10000){
		if(POSITION=='sitefont'){
			showmsg("产品内容不能为空且最大字符数不能超过10000字符");	
		}else{
			gadminmessage("产品内容不能为空且最大字符数不能超过10000字符");	
		}
	}
	if(!$pervalue{16}){
		if(POSITION=='sitefont'){
			showmsg("该用户组无此权限");
		}else{
			gadminmessage("该用户组无此权限");	
		}
		
	}
	if($iscustom){
		$timestamp=gmmktime(0,0,0,$g_calendar_month_subtime,$g_calendar_day_subtime,$g_calendar_year_subtime);
	}
	$producttitle = ghtmlspecialchars(trim($producttitle));
	$productintro = ghtmlspecialchars(trim($productintro));
	$g_editor_doc = trim($g_editor_doc);
    require_once './engine/upload.func.php';
	$ip=explode('.',$currentip);
	$dbattachments=uploadattach('attach','product');
	$dblogoattachments=uploadattach('logoimg','product',true);
	$db->query("INSERT INTO g_products (productid , ord , tag , hlight , ip1 , ip2 , ip3 , ip4 , uid , useraccount , producttitle , productintro , pid , categoryid , top , view , review , subtime) VALUES (NULL , $timestamp , $tag , 0 , $ip[0] , $ip[1] , $ip[2] , $ip[3] , $uid , '$userid' , '$producttitle' , '$productintro' , $pid , $categoryid , 0 , 0 , 0 , $timestamp)");
	$bid=$db->insert_id();
	logoattach2db($dblogoattachments , $bid , 2);
	$g_editor_doc=attach2db($dbattachments , $bid , 2 , $g_editor_doc);
	$db->query("INSERT INTO g_product_content (productid, content) VALUES ($bid, '$g_editor_doc')");

	if(POSITION=='sitefont'){
		showmsg("非常感谢，您的产品信息已经提交，现在将转入产品列表页面","productdisplay.php?pid=".$pid."&categoryid=".$categoryid);
	}else{
		gadminmessage("非常感谢，您的产品信息已经提交，现在将转入产品列表页面","siteadmin.php?iframe=productmanagement",1);
	}
	
}



function upproduct2db($producttitle,$productintro,$g_editor_doc,$tag,$mid){
	global $db , $pervalue , $cid ,$pid, $categoryid,$iscustom,$g_calendar_year_subtime,$g_calendar_month_subtime,$g_calendar_day_subtime;
	 if(empty($producttitle) || strlen($producttitle)>120){
		 if(POSITION=='sitefont'){
				showmsg("产品名称不能为空且最大字符数不能超过120字符");
			}else{
				gadminmessage("产品名称不能为空且最大字符数不能超过120字符");
			}
	}
	if(empty($g_editor_doc) || strlen($g_editor_doc)>10000){
		 if(POSITION=='sitefont'){
				showmsg("文章内容不能为空且最大字符数不能超过10000字符");	
			}else{
				gadminmessage("文章内容不能为空且最大字符数不能超过10000字符");
			}
	}
	if(!$pervalue{16}){
		 if(POSITION=='sitefont'){
				showmsg("该用户组无此权限");
			}else{
				gadminmessage("该用户组无此权限");
			}
	}
	if(!empty($cid)){
		list($pid,$categoryid)=explode(',',$cid);
		$upcategory=True;
		if($pid==0 && $categoryid==0){
			gadminmessage("不能为产品根分类");
		}
	}else{
		$upcategory=False;	
	}
	require_once './engine/upload.func.php';

	$dbattachments=uploadattach('attach','product');
	$g_editor_doc=attach2db($dbattachments , $mid , 2 , $g_editor_doc);
	$dbattachments=array();
	$dbattachments=uploadattach('attachmodify','product');
	attachupdb($dbattachments,2); //更新附件数据表中的附件信息,包括更新logo附件

	//如果logo附件是新上传的就成功执行以下函数，否则以下函数执行后没有改变任何值
	$dblogoattachments=array();
	$dblogoattachments=uploadattach('logoimg','product',true);
	logoattach2db($dblogoattachments , $mid , 2);


	$producttitle = ghtmlspecialchars(trim($producttitle));
	$g_editor_doc = trim($g_editor_doc);
	if($upcategory){
	$db->query("update g_products set producttitle = '$producttitle', productintro = '$productintro' , pid = $pid, categoryid=$categoryid, tag =$tag where productid =$mid limit 1");
	}else{
	$db->query("update g_products set producttitle = '$producttitle', productintro = '$productintro' , tag =$tag where productid =$mid limit 1");
	}
	$db->query("update g_product_content set content  = '$g_editor_doc' where productid =$mid limit 1");
	if($iscustom){
		$timestamp=gmmktime(0,0,0,$g_calendar_month_subtime,$g_calendar_day_subtime,$g_calendar_year_subtime);
		$db->query("update g_products set ord = $timestamp, subtime =$timestamp where productid =$mid limit 1");
	}
	if(POSITION=='sitefont'){
		showmsg("非常感谢，您的产品已修改成功，现在将转入产品列表页面","productdisplay.php?pid=".$pid."&categoryid=".$categoryid);
	}else{
		gadminmessage("非常感谢，您的产品已修改成功，现在将转入产品列表页面","siteadmin.php?iframe=productmanagement",1);
	}
}





function thread2db($posttitle,$g_editor_doc,$forid,$uid,$userid){
	global $db , $currentip , $timestamp , $action ,$pervalue , $authcode , $pubthreadauthcode , $allowedauthcode;
	if($allowedauthcode{3}){
		if(strcmp($authcode,$pubthreadauthcode)){
			showmsg("验证码输入不正确");
		}
	}
	
	 if(empty($posttitle) || strlen($posttitle)>120){
		showmsg("主题标题不能为空且最大字符数不能超过120字符");	
	}
	if(empty($g_editor_doc) || strlen($g_editor_doc)>10000){
		showmsg("主题内容不能为空且最大字符数不能超过10000字符");	
	}
	if(!$pervalue{10}){
		showmsg("该用户组无此权限");
	}
	$posttitle = ghtmlspecialchars(trim($posttitle));
	$g_editor_doc = trim($g_editor_doc);
    require_once './engine/upload.func.php';
	$ip=explode('.',$currentip);
	$dbattachments=uploadattach('attach','thread');
	$db->query("INSERT INTO g_threads (tid , forid , uid , useraccount , ip1 , ip2 , ip3 , ip4 , ord , hlight , title , top , status , view , review , subtime) VALUES (NULL , $forid , $uid , '$userid' , $ip[0] , $ip[1] , $ip[2] , $ip[3] , $timestamp , 0 ,'$posttitle' , 0 , 0 , 0 , 0 , $timestamp)");
	$bid=$db->insert_id();
	$g_editor_doc=attach2db($dbattachments , $bid , 3 , $g_editor_doc);
	$db->query("INSERT INTO g_thread_content (tid, content) VALUES ($bid, '$g_editor_doc')");

	$foruminfo=$db->fetch_array($db->query("select * from g_forumcategory where forid=$forid"));
	$threads=$foruminfo['threads']+1;//主题数计数机
	$lastpost=trim($bid."\t".$posttitle."\t".$userid."\t".$timestamp);
	if($timestamp-$foruminfo['upstatus']>86400){
		$db->query("UPDATE g_forumcategory SET upstatus = $timestamp , todayposts = 1 , threads=$threads , lastpost='$lastpost' where forid =$forid LIMIT 1");
		}else{
		$todayposts=$foruminfo['todayposts']+1;
		$db->query("UPDATE g_forumcategory SET todayposts = $todayposts , threads=$threads , lastpost='$lastpost' where forid =$forid LIMIT 1");
		}
		showmsg("非常感谢，您的主题已经提交，现在将返回论坛列表页面","forumdisplay.php?forid=".$forid);
}

function upthread2db($threadtitle,$g_editor_doc,$mid){
	global $db , $pervalue , $forid ,$uid;
	 if(empty($threadtitle) || strlen($threadtitle)>120){
				showmsg("主题不能为空且最大字符数不能超过120字符");
	}
	if(empty($g_editor_doc) || strlen($g_editor_doc)>10000){
				showmsg("内容不能为空且最大字符数不能超过10000字符");	
	}
	if(!$pervalue{15}||!$pervalue{3}){
				showmsg("该用户组无此权限");
	}
	if(!$pervalue{15}&&$pervalue{3}){
			$checkinfo=$db->fetch_array($db->query("select * from g_threads where tid= $mid"));
			if($uid!=$checkinfo['uid'] && $uid){//确保uid是存在的，同时如果uid跟帖子主人不一致，须提示权限错误，并退出
				showmsg("不能修改别人的主题");
			}

	}
	require_once './engine/upload.func.php';
	$dbattachments=uploadattach('attach','thread');
	$g_editor_doc=attach2db($dbattachments , $mid , 3 , $g_editor_doc);
	$dbattachments=array();
	$dbattachments=uploadattach('attachmodify','thread');
	attachupdb($dbattachments,3);//更新附件数据表中的附件信息
	$threadtitle = ghtmlspecialchars(trim($threadtitle));
	$g_editor_doc = trim($g_editor_doc);
	$db->query("update g_threads set title = '$threadtitle' where tid =$mid limit 1");
	$db->query("update g_thread_content set content  = '$g_editor_doc' where tid =$mid limit 1");
	if(POSITION=='sitefont'){
		showmsg("非常感谢，您的主题已修改成功，现在将转入文章列表页面","forumdisplay.php?forid=".$forid);
	}else{
		gadminmessage("非常感谢，您的主题已修改成功，现在将转入文章列表页面","siteadmin.php?iframe=threadmanagement",1);
	}
}














function reply2db($bid,$g_editor_doc,$typeid,$uid,$userid){
global $db , $currentip , $timestamp , $allowedauthcode , $authcode , $replyarticleauthcode , $replyproductauthcode , $replythreadauthcode;
	if(!in_array($typeid,array(1,2,3))){
		showmsg("回复类型有误");	
	}
	if($allowedauthcode{1}&&$typeid==1){
		if(strcmp($authcode,$replyarticleauthcode)){
			showmsg("验证码输入不正确");
		}
	}elseif($allowedauthcode{0}&&$typeid==2){
		if(strcmp($authcode,$replyproductauthcode)){
			showmsg("验证码输入不正确");
		}
	}elseif($allowedauthcode{2}&&$typeid==3){
		if(strcmp($authcode,$replythreadauthcode)){
			showmsg("验证码输入不正确");
		}
	}

$replytable=array(1=>'g_articles',2=>'g_products',3=>'g_threads');
$replyid=array(1=>'aid',2=>'productid',3=>'tid');
$ip=explode('.',$currentip);
	require_once './engine/upload.func.php';
	if(empty($g_editor_doc) || strlen($g_editor_doc)>10000){
		showmsg("留言内容不能为空且最大字符数不能超过10000字符");	
	}
	$dbattachments=uploadattach('attach','reply');
	$g_editor_doc = trim($g_editor_doc);
	$db->query("INSERT INTO g_replys (rid , reply_uid , postname , content ,typeid , bid ,ip1,ip2,ip3,ip4,subtime)VALUES (NULL , $uid , '$userid' ,'' , $typeid, $bid, $ip[0],$ip[1],$ip[2],$ip[3],$timestamp)");
	$b_id=$db->insert_id();//$b_id 是回复记录标识号,$bid 是上级标示号(比如文章、产品、论坛标示号)
	$g_editor_doc=attach2db($dbattachments , $b_id , 4 , $g_editor_doc);
	$db->query("UPDATE g_replys SET content = '$g_editor_doc' WHERE rid =$b_id LIMIT 1");

	$review=$db->result($db->query("SELECT count(*) FROM g_replys WHERE typeid = $typeid and bid = $b_id"),0);
	/* update g_artcle , g_product , g_thread's review count */
	$db->query("UPDATE {$replytable[$typeid]} SET review = (SELECT count(*) FROM g_replys WHERE typeid = $typeid and bid = $bid) WHERE {$replyid[$typeid]} =$bid LIMIT 1");
	$postcount=$db->result($db->query("select a.ac+b.bc from (select count(*) as ac from g_threads WHERE uid=$uid) a,(select count(*) as bc from g_replys where reply_uid=$uid) b "),0);
	if($typeid==3){
		$forumid=$db->result($db->query("select forid as forumid from g_threads t where tid=$bid limit 1"),0);
		$db->query("UPDATE g_forumcategory SET posts = (select sum(review) from g_threads where forid = $forumid) WHERE forid =$forumid LIMIT 1");
		$db->query("UPDATE g_threads SET ord = $timestamp WHERE tid =$bid LIMIT 1");
	}
	$db->query("UPDATE g_members SET postcount = $postcount WHERE uid =$uid LIMIT 1");
	$parturi=$typeid==1?'viewarticle.php?aid':(($typeid==2)?'viewproduct.php?productid':'viewthread.php?tid');
	showmsg("非常感谢，您的回复已经发布，2秒后自动转回",$parturi."=".$bid);
}



function upreply2db($g_editor_doc,$rid){
	global $db , $pervalue ,$uid;
	if(empty($g_editor_doc) || strlen($g_editor_doc)>10000){
				showmsg("内容不能为空且最大字符数不能超过10000字符");	
	}
	if(!$pervalue{1}){
		showmsg("该用户组无此权限");
	}
	if(!$uid){
			showmsg("该用户组无此权限");
		}
	$checkinfo=$db->fetch_array($db->query("select * from g_replys where rid= $rid"));
	$arnotallowed=$pervalue{1} && !$pervalue{17} && $checkinfo['typeid']==1;
	$prnotallowed=$pervalue{1} && !$pervalue{16} && $checkinfo['typeid']==2;
	$trnotallowed=$pervalue{1} && !$pervalue{15} && $checkinfo['typeid']==3;
	$isallowed=$arnotallowed || $prnotallowed || $trnotallowed;
	if($isallowed && $uid!=$checkinfo['reply_uid']){
		showmsg("不能修改别人的回复");
	}
	require_once './engine/upload.func.php';
	$dbattachments=uploadattach('attach','reply');
	$g_editor_doc=attach2db($dbattachments , $rid , 4 , $g_editor_doc);
	$dbattachments=array();
	$dbattachments=uploadattach('attachmodify','reply');
	attachupdb($dbattachments,4);//更新附件数据表中的附件信息
	$g_editor_doc = trim($g_editor_doc);
	$db->query("update g_replys set content  = '$g_editor_doc' where rid =$rid limit 1");
	$parturi=$checkinfo['typeid']==1?'viewarticle.php?aid':(($checkinfo['typeid']==2)?'viewproduct.php?productid':'viewthread.php?tid');
	showmsg("非常感谢，您的回复已经修改，2秒后自动转回",$parturi."=".$checkinfo['bid']);
}



function regmember(){
	global $db , $uid , $allowedregister , $groupnames , $membermoreinfo , $timestamp , $currentip , $reg_userid , $reg_username , $reg_email , $reg_userpwd, $reg_f_userpwd , $reg_gid , $reg_sex , $reg_phone , $reg_mobile, $reg_qq , $reg_address , $authcode , $registerauthcode , $allowedauthcode , $emailtitle , $emailmsg , $emailaddress ,$sitename;
	$reg_userid=strtolower(trim($reg_userid));
	$reg_userpwd=trim($reg_userpwd);
	if(!$uid && !$allowedregister){
		showmsg("管理员禁止用户注册");
	}
	$memberformat=preg_match("/(^[a-z0-9_]+$)/", $reg_userid);
	if(strlen($reg_userid)>16 || strlen($reg_userid)<4){
			$memberformat=false;
		}
	if(!$memberformat){
		showmsg("用户账号只能是字母、数字或下划线,且字符数大于3位小于16位".$reg_userid);
	}

	if(!isemail($reg_email)){
		showmsg("Email地址输入不正确");
	}
	$q=$db->query("SELECT uid FROM g_members WHERE email='$reg_email'");
	if($db->num_rows($q)){
		showmsg("email账号已经存在");
	}
	if(strlen($reg_userpwd)<4){
		showmsg("密码不能小于4个字符");
	}
	if(strcmp($reg_userpwd,$reg_f_userpwd)){
		showmsg("两次密码输入不一致");	
	}
	if(!intval($reg_gid)){
		showmsg("用户组不能为空");	
	}
	if($allowedauthcode{4}){
		if($authcode!=$registerauthcode){
			showmsg('验证码输入不正确');
		}
	}
	$q=$db->query("SELECT uid FROM g_members WHERE userid='$reg_userid'");
	if($db->num_rows($q)){
		showmsg("用户名已经存在");
	}
		$reg_userpwd=md5($reg_userpwd);
		$state=intval($groupnames[$reg_gid][2]);
		$db->query("INSERT INTO g_members (uid , userid , username , userface , email , gid, postcount , userpwd , logintimes , registerip , state , registertime )VALUES ( NULL , '$reg_userid' , '$reg_username' , '01.png' , '$reg_email' , $reg_gid , 0 , '$reg_userpwd' , 0 , '$currentip' , $state , $timestamp )");
		$uid=$db->insert_id();
		$db->query("INSERT INTO g_member_info (uid , msgboxcount , lastlogintime , logintimes , sex , phone ,mobile ,qq ,address)VALUES ( $uid , 0 , $timestamp , 0 , $reg_sex , '$reg_phone' , '$reg_mobile' , '$reg_qq' , '$reg_address')");
	if($state==0){
		if(intval($allowedemailmsg)){
			$emailtitle=str_replace(array('{name}','{sitename}'),array($reg_userid,$sitename),$emailtitle);
			$emailmsg=str_replace(array('{name}','{sitename}','{adminemail}'),array($reg_userid,$sitename,$emailaddress),$emailmsg);
			$emailtitle=iconv("UTF-8","GB2312//IGNORE",$emailtitle);
			$emailmsg=iconv("UTF-8","GB2312//IGNORE",$emailmsg);
			@gsendmail($reg_email,$emailtitle,$emailmsg);
		}
	showmsg("非常感谢，你已经注册成为会员","index.php",1);
	}elseif($state==1){
	$emailcode=($state==1)?md5(random(6)):'';
	$db->query("INSERT INTO g_member_mailcode (uid ,emailcode )VALUES ($uid, '$emailcode')");
	$msg_title="你好，感谢你注册为会员，请点击激活链接激活账号";
	$msg_content="你好感谢你注册成为会员，请点击链接激活账号 \r\n ".cururldir()."post.php?action=mailverify&mailcode=".$emailcode;
	$msg_title=iconv("UTF-8","GB2312//IGNORE",$msg_title);
	$msg_content=iconv("UTF-8","GB2312//IGNORE",$msg_content);
	gsendmail($reg_email,$msg_title,$msg_content);//此处有修改
	showmsg("激活邮件已经发送至您的邮箱，请点击邮箱中的链接激活用户账号","index.php",1);
	}elseif($state==2){
	showmsg("注册信息已经提交，请等待管理员审核","index.php",1);
	}
}


function articlesetting($action,$article_check){
	global $db,$timestamp,$pid,$categoryid,$move_select_cid;
	if($action=='settop'){
			foreach($article_check as $val){
				$db->query("update g_articles set ord=$timestamp , top = 1 where aid =$val limit 1");
			}
		}

		if($action=='unsettop'){
			foreach($article_check as $val){// 此处$article_check为函数内部变量
				$articletime=$db->result($db->query("select subtime from g_articles where aid=$val"),0);
				$db->query("update g_articles set ord = $articletime , top = 0 where aid =$val limit 1");
			}
		}

		if($action=='priority'){
			foreach($article_check as $val){
				$db->query("update g_articles set ord=$timestamp where aid =$val limit 1");
			}
		}

		if($action=='unpriority'){
			foreach($article_check as $val){
				$articletime=$db->result($db->query("select subtime from g_articles where aid=$val"),0);
				$db->query("update g_articles set ord= $articletime where aid =$val limit 1");
			}
		}

		if($action=='sethlight'){
			foreach($article_check as $val){
				$db->query("update g_articles set hlight=1 where aid =$val limit 1");
			}
		}

		if($action=='unsethlight'){
			foreach($article_check as $val){
				$db->query("update g_articles set hlight=0 where aid =$val limit 1");
			}
		}

		if($action=='move'){
			list($new_pid,$new_categoryid)=explode(',',$move_select_cid);
			if(!$new_pid&&!$new_categoryid){
				if(POSITION=='sitefont'){
					showmsg("不能为根分类","articledisplay.php?pid=".$pid."&categoryid=".$categoryid);
				}else{
					gadminmessage("不能为根分类","siteadmin.php?iframe=articlemanagement");
				}
			}
			foreach($article_check as $val){
				$db->query("update g_articles set pid = $new_pid , categoryid=$new_categoryid where aid =$val limit 1");
			}
			$db->query("update g_articlecategory set articlecount=(select count(*) from g_articles where categoryid=$categoryid) where acid=$categoryid");
			$db->query("update g_articlecategory set articlecount=(select count(*) from g_articles where categoryid=$new_categoryid) where acid=$new_categoryid");
		}

		if($action=='delete'){
			foreach($article_check as $val){
				$dq = $db->query("select storename from g_attachments where bid= $val and typeid=1");
				while($storenames=$db->fetch_array($dq)){
					$storename=$storenames['storename'];
					$attach_subdir=substr($storename,0,strpos($storename,'_'));
					$deltarget="./clientdata/attachment/article/".$storename;
					@unlink($deltarget);
				}
				$db->query("delete from g_article_content where aid = $val limit 1");
				$db->query("delete from g_articles where aid = $val limit 1");
				$db->query("delete from g_attachments where bid = $val and typeid=1");
				$db->query("delete from g_replys where bid = $val and typeid=1");
			}
			$db->query("update g_articlecategory set articlecount=(select count(*) from g_articles where categoryid=$categoryid) where acid=$categoryid");
			if(POSITION=='sitefont'){
				showmsg("被选中文章所有相关信息成功删除","articledisplay.php?pid=".$pid."&categoryid=".$categoryid);
			}else{
				gadminmessage("被选中文章所有相关信息成功删除","siteadmin.php?iframe=articlemanagement",1);
			}
		}


		if(POSITION=='sitefont'){
				showmsg("设置成功","articledisplay.php?pid=".$pid."&categoryid=".$categoryid);
		}else{
			gadminmessage("设置成功","siteadmin.php?iframe=articlemanagement",1);
		}

}

function modifydelattach($mid,$fid,$typeid){
		global $db;
		$ipostpage=array(1=>'modifyarticle',2=>'modifyproduct',3=>'modifythread',4=>'modifyreply');
		$backpage=array(
			1=>'articlemanagement',
			2=>'productmanagement',
			3=>'threadmanagement'
		);
		$attachdir=array(1=>'article', 2=>'product', 3=>'thread');
			$fid=isset($fid)?intval($fid):0;
			$attachinfo=$db->fetch_array($db->query("select storename , islogo from g_attachments where fid= $fid"));
			$deltarget="./clientdata/attachment/".$attachdir[$typeid]."/".$attachinfo['storename'];
			@unlink($deltarget);
			if($attachinfo['islogo']){
				$thumbimgname=replaceimg($attachinfo['storename']);
				$deltarget="./clientdata/attachment/".$attachdir[$typeid]."/".$thumbimgname;
				@unlink($deltarget);
			}
			$db->query("delete from g_attachments where fid =$fid limit 1");
			if(POSITION=='sitefont'){
					showmsg("被选中附件所有信息成功删除","post.php?ipost=".$ipostpage[$typeid]."&mid=".$mid);
			}else{
					gadminmessage("被选中附件所有信息成功删除","siteadmin.php?iframe=".$backpage[$typeid]."&ipost=modify&mid=".$mid,1);
			}
}


function productsetting($action,$product_check){
	global $db,$timestamp,$pid,$categoryid,$move_select_cid;
	if($action=='settop'){
			foreach($product_check as $val){
				$db->query("update g_products set ord=$timestamp , top = 1 where productid =$val limit 1");
			}
		}

		if($action=='unsettop'){
			foreach($product_check as $val){// 此处$product_check为函数内部变量
				$producttime=$db->result($db->query("select subtime from g_products where productid=$val"),0);
				$db->query("update g_products set ord = $producttime , top = 0 where productid =$val limit 1");
			}
		}

		if($action=='priority'){
			foreach($product_check as $val){
				$db->query("update g_products set ord=$timestamp where productid =$val limit 1");
			}
		}

		if($action=='unpriority'){
			foreach($product_check as $val){
				$producttime=$db->result($db->query("select subtime from g_products where productid=$val"),0);
				$db->query("update g_products set ord= $producttime where productid =$val limit 1");
			}
		}

		if($action=='sethlight'){
			foreach($product_check as $val){
				$db->query("update g_products set hlight=1 where productid =$val limit 1");
			}
		}

		if($action=='unsethlight'){
			foreach($product_check as $val){
				$db->query("update g_products set hlight=0 where productid =$val limit 1");
			}
		}

		if($action=='move'){
			list($new_pid,$new_categoryid)=explode(',',$move_select_cid);
			if(!$new_pid&&!$new_categoryid){
				if(POSITION=='sitefont'){
					if($pid||$categoryid){
						showmsg("不能为根分类","productdisplay.php?pid=".$pid."&categoryid=".$categoryid);
					}elseif(!$pid&&!$categoryid){
						showmsg("不能为根分类","product.php");
					}
				}else{
					gadminmessage("不能为根分类","siteadmin.php?iframe=productmanagement");
				}
			}
			foreach($product_check as $val){
				$db->query("update g_products set pid = $new_pid , categoryid=$new_categoryid where productid =$val limit 1");  
			}
			$db->query("update g_productcategory set productcount=(select count(*) from g_products where categoryid=$categoryid) where pcid=$categoryid");
			$db->query("update g_productcategory set productcount=(select count(*) from g_products where categoryid=$new_categoryid) where pcid=$new_categoryid");
		}

		if($action=='delete'){
			foreach($product_check as $val){
				$dq = $db->query("select * from g_attachments where bid= $val and typeid=2");
				while($storenames=$db->fetch_array($dq)){
					$deltarget="./clientdata/attachment/product/".$storenames['storename'];
					@unlink($deltarget);
					if($storenames['islogo']){
						$deltarget="./clientdata/attachment/product/".replaceimg($storenames['storename']);
						@unlink($deltarget);
					}
				}
				$db->query("delete from g_product_content where productid = $val limit 1");
				$db->query("delete from g_products where productid = $val limit 1");
				$db->query("delete from g_attachments where bid = $val and typeid=2");
				$db->query("delete from g_replys where bid = $val and typeid=2");
			}
			$db->query("update g_productcategory set productcount=(select count(*) from g_products where categoryid=$categoryid) where pcid=$categoryid");
			if(POSITION=='sitefont'){
				if($pid||$categoryid){
					showmsg("被选中产品所有相关信息成功删除","productdisplay.php?pid=".$pid."&categoryid=".$categoryid);
				}elseif(!$pid&&!$categoryid){
					showmsg("被选中产品所有相关信息成功删除","product.php");
				}
			}else{
				gadminmessage("被选中产品所有相关信息成功删除","siteadmin.php?iframe=productmanagement",1);
			}
		}


		if(POSITION=='sitefont'){
			if($pid||$categoryid){
				showmsg("设置成功","productdisplay.php?pid=".$pid."&categoryid=".$categoryid);
			}elseif(!$pid&&!$categoryid){
				showmsg("设置成功","product.php");
			}
		}else{
			gadminmessage("设置成功","siteadmin.php?iframe=productmanagement",1);
		}

}


function forumsetting($action,$thread_check){
	global $db,$timestamp,$forid,$move_select_cid,$pervalue;
	if($action=='settop'){
			if(!$pervalue{6}||!$pervalue{15}){
				showmsg("该用户组无此权限");
			}
			foreach($thread_check as $val){
				$db->query("update g_threads set ord=$timestamp , top = 1 where tid =$val limit 1");
			}
		}
		if($action=='unsettop'){
			if(!$pervalue{6}||!$pervalue{15}){
				showmsg("该用户组无此权限");
			}
			foreach($thread_check as $val){// 此处$thread_check为函数内部变量
				$threadtime=$db->result($db->query("select subtime from g_threads where tid=$val"),0);
				$db->query("update g_threads set ord = $threadtime , top = 0 where tid =$val limit 1");
			}
		}

		if($action=='priority'){
			if(!$pervalue{5}||!$pervalue{15}){
				showmsg("该用户组无此权限");
			}
			foreach($thread_check as $val){
				$db->query("update g_threads set ord=$timestamp where tid =$val limit 1");
			}
		}

		if($action=='unpriority'){
			if(!$pervalue{5}||!$pervalue{15}){
				showmsg("该用户组无此权限");
			}
			foreach($thread_check as $val){
				$producttime=$db->result($db->query("select subtime from g_threads where tid=$val"),0);
				$db->query("update g_threads set ord= $producttime where tid =$val limit 1");
			}
		}

		if($action=='sethlight'){
			if(!$pervalue{4}||!$pervalue{15}){
				showmsg("该用户组无此权限");
			}
			foreach($thread_check as $val){
				$db->query("update g_threads set hlight=1 where tid =$val limit 1");
			}
		}

		if($action=='unsethlight'){
			if(!$pervalue{4}||!$pervalue{15}){
				showmsg("该用户组无此权限");
			}
			foreach($thread_check as $val){
				$db->query("update g_threads set hlight=0 where tid =$val limit 1");
			}
		}

		if($action=='move'){
			if(!$move_select_cid){
				if(POSITION=='sitefont'){
					showmsg("不能为根分类","forumdisplay.php?forid=".$forid);
				}else{
					gadminmessage("不能为根分类","siteadmin.php?iframe=threadmanagement");
				}
			}
			list($new_pid,$new_forid)=explode(',',$move_select_cid);
			$new_forid=intval($new_forid);
			echo $new_forid;
			foreach($thread_check as $val){
				$db->query("update g_threads set forid=$new_forid where tid =$val limit 1");  
			}

			// ****更新论坛栏目中最后发表帖子的内容****
			$cache_forum_q=$db->query("select tid , title , useraccount , subtime  from g_threads where forid = $forid order by subtime DESC limit 1");
			$newlastpost=$db->fetch_array($cache_forum_q);
			$updatelastpost=trim($newlastpost['tid']."\t".$newlastpost['title']."\t".$newlastpost['useraccount']."\t".$newlastpost['subtime']);
			// ********************************
			$db->query("update g_forumcategory set threads=(select count(*) from g_threads where forid=$forid) , posts=(select count(*) from g_replys a, g_threads b where a.bid=b.tid and a.typeid=3 and b.forid = $forid) , lastpost = '$updatelastpost' where forid=$forid");


			// ****更新论坛栏目中最后发表帖子的内容****
			$cache_forum_q=$db->query("select tid , title , useraccount , subtime  from g_threads where forid = $new_forid order by subtime DESC limit 1");
			$newlastpost=$db->fetch_array($cache_forum_q);
			$updatelastpost=trim($newlastpost['tid']."\t".$newlastpost['title']."\t".$newlastpost['useraccount']."\t".$newlastpost['subtime']);
			// ********************************
			$db->query("update g_forumcategory set threads=(select count(*) from g_threads where forid=$new_forid) , posts=(select count(*) from g_replys a, g_threads b where a.bid=b.tid and a.typeid=3 and b.forid = $new_forid) , lastpost = '$updatelastpost' where forid=$new_forid");
		}


		if($action=='delete'){
			if(!$pervalue{2}||!$pervalue{15}){
				showmsg("该用户组无此权限");
			}
			foreach($thread_check as $val){
				$dq = $db->query("select * from g_attachments where bid= $val and typeid=3");
				while($storenames=$db->fetch_array($dq)){
					$deltarget="./clientdata/attachment/thread/".$storenames['storename'];
					@unlink($deltarget);
				}
				$db->query("delete from g_thread_content where tid = $val limit 1");
				$db->query("delete from g_threads where tid = $val limit 1");
				$db->query("delete from g_attachments where bid = $val and typeid=3");
				$db->query("delete from g_replys where bid = $val and typeid=3");
			}

			//更新论坛栏目中最后发表帖子的内容
			$cache_forum_q=$db->query("select tid , title , useraccount , subtime  from g_threads where forid = $forid order by subtime DESC limit 1");
			$newlastpost=$db->fetch_array($cache_forum_q);
			$updatelastpost=trim($newlastpost['tid']."\t".$newlastpost['title']."\t".$newlastpost['useraccount']."\t".$newlastpost['subtime']);
			$db->query("update g_forumcategory set threads=(select count(*) from g_threads where forid=$forid) , posts=(select count(*) from g_replys a, g_threads b where b.tid=a.bid and typeid=3 and b.forid = $forid) , lastpost = '$updatelastpost' where forid=$forid  limit 1");
			if(POSITION=='sitefont'){
				showmsg("被选中主题所有相关信息成功删除","forumdisplay.php?forid=".$forid);
			}else{
				gadminmessage("被选中主题所有相关信息成功删除","siteadmin.php?iframe=threadmanagement",1);
			}
		}
		if(POSITION=='sitefont'){
			showmsg("设置成功","forumdisplay.php?forid=".$forid);
		}else{
			gadminmessage("设置成功","siteadmin.php?iframe=threadmanagement",1);
		}

}
function deletereply($rid,$replyinfo){
	global $db;
	$bid=$replyinfo['bid'];
	$db->query("delete from g_replys where rid=$rid limit 1");
	$db->query("delete from g_attachments where bid = $rid and typeid=4");
	$parturi=$replyinfo['typeid']==1?'viewarticle.php?aid=':(($replyinfo['typeid']==2)?'viewproduct.php?productid=':'viewthread.php?tid=');

	if($replyinfo['typeid']==1){
		$parturi='viewarticle.php?aid=';
		$db->query("update g_articles set review=(select count(*) from g_replys where bid=$bid and typeid=1) where aid=$bid");
	}elseif($replyinfo['typeid']==2){
		$parturi='viewproduct.php?productid=';
		$db->query("update g_products set review=(select count(*) from g_replys where bid=$bid and typeid=2) where productid=$bid");
	}elseif($replyinfo['typeid']==3){
		$parturi='viewthread.php?tid=';
		$db->query("update g_threads set review=(select count(*) from g_replys where bid=$bid and typeid=3) where tid=$bid");
	}
	showmsg("帖子信息删除成功",$parturi.$replyinfo['bid']);
}





function autolink($msg)    
{   
	$msg = eregi_replace('(((f|ht){1}tp://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)', '[url]\1[/url]', $msg);   
	if( strpos($msg, "http") === FALSE ){   
	$msg = eregi_replace('(www.[-a-zA-Z0-9@:%_\+.~#?&//=]+)', '[url]\1[/url]', $msg);   
	}   
return $msg;   
}
?>