<?php
define('POSITION', 'sitefont');
define('CURSCRIPT', 'post');//仅仅主题涉及到附件上传权限问题;
require_once "./engine/common.inc.php";
require_once "./engine/post.func.php";
require_once "./engine/attachment.func.php";

if($action=='savevote'){
	upsurveycount2db();
}

if($action=='postregister'){
	regmember();
}

if($action=='mailverify'){
	$q=$db->query("select uid from g_member_mailcode where emailcode='$mailcode'");
	if($mailverify=$db->fetch_array($q)){;
	$mailverify=$db->query("update g_members SET state=0 where uid = {$mailverify['uid']}");
	$db->query("delete from g_member_mailcode where emailcode='$mailcode'");

	showmsg("用户激活成功，你现在可以通过会员方式登录本网站","index.php",1);
	}else{
	    showmsg("用户已经激活或该激活链接已经失效","index.php",1);
	}
}

if($action=='savereply'){
		reply2db($bid,$g_editor_doc,$typeid,$uid,$userid);
	}

if($_GSESSION['gid']==4 || !$_GSESSION['gid']){
	showmsg("访问被禁止");
}

if($action=='updatearticle'){
	uparticle2db($articletitle,$g_editor_doc,$tag,$aid);
}

if($action=='updateproduct'){
	upproduct2db($producttitle,$productintro,$g_editor_doc,$tag,$productid);
}

if($action=='updatethread'){
	upthread2db($posttitle,$g_editor_doc,$tid);
}

if($action=='updatereply'){
	upreply2db($g_editor_doc,$rid);
}


//发布或修改文章信息时逻辑
if($action=='pubarticle' || $ipost=='modifyarticle'){
	if(!$pervalue{17}){
		showmsg("该用户组无此权限");
	}
	if($ipost=='modifyarticle'){
		$modarticleattachs=array();
		if($action=='delete'){
			modifydelattach($mid,$fid,1);
		}
		$q = $db->query("select * from g_articles a,g_article_content b where a.aid=b.aid and a.aid= $mid limit 1");
		$aq = $db->query("select * from g_attachments where bid = $mid and typeid=1");
		$modifyinfo=$db->fetch_array($q);
			while($modarticleattach=$db->fetch_array($aq)){
				$modarticleattachs[$modarticleattach['fid']]=$modarticleattach;
			}
	}
	if($action=='pubarticle'){
		$siteheadtitle=implode(' ' , array('发布文章',$sitename));
	}elseif($ipost=='modifyarticle'){
		$siteheadtitle=implode(' ' , array('修改文章',$sitename));	
	}
	include($templatedir."postarticle.tpl.php");
	exit();

//发布或修改产品信息时逻辑
}elseif($action=='pubproduct' || $ipost=='modifyproduct'){
	if(!$pervalue{16}){
		showmsg("该用户组无此权限");
	}
	if($ipost=='modifyproduct'){
		$modproductattachs=array();
		if($action=='delete'){
			modifydelattach($mid,$fid,2);
		}
		$q = $db->query("select * from g_product_content b , g_products a left join g_attachments c On a.productid = c.bid and  c.typeid=2 and c.islogo=1 where a.productid=b.productid and a.productid= $mid limit 1");
		$aq = $db->query("select * from g_attachments where bid = $mid and typeid=2 and islogo=0");
		$modifyinfo=$db->fetch_array($q);
			while($modproductattach=$db->fetch_array($aq)){
				$modproductattachs[$modproductattach['fid']]=$modproductattach;
			}
	}
	if($action=='pubproduct'){
		$siteheadtitle=implode(' ' , array('发布产品',$productname,$sitename));
	}elseif($ipost=='modifyproduct'){
		$siteheadtitle=implode(' ' , array('修改产品',$productname,$sitename));	
	}
	include($templatedir."postproduct.tpl.php");
	exit();
}elseif($action=='pubthread' || $ipost=='modifythread'){
	if(!$pervalue{10} && $action=='pubthread'){
		showmsg("该用户组无此权限");
	}
	if((!$pervalue{3} || !$pervalue{15}) && $ipost=='modifythread'){
		showmsg("该用户组无权修改别人的主题");
	}
	if($ipost=='modifythread'){
		$modthreadattachs=array();
		if(!$uid){
			showmsg("该用户组无此权限");
		}
		$q = $db->query("select * from g_thread_content a , g_threads b where a.tid=b.tid and a.tid= $mid limit 1");
		$modifyinfo=$db->fetch_array($q);
		if($pervalue{3} && !$pervalue{15}) {
			if($modifyinfo['uid']!=$uid){
			showmsg("该用户组不能修改别人的主题");
			}
		}
		if($action=='delete'){
			modifydelattach($mid,$fid,3);
		}
		$aq = $db->query("select * from g_attachments where bid = $mid and typeid=3");
		while($modthreadattach=$db->fetch_array($aq)){
				$modthreadattachs[$modthreadattach['fid']]=$modthreadattach;
			}
	}
	if($action=='pubthread'){
		$siteheadtitle=implode(' ' , array('发布主题',$forumname,$sitename));
	}elseif($ipost=='modifythread'){
		$siteheadtitle=implode(' ' , array('修改主题',$forumname,$sitename));	
	}
	include($templatedir."postthread.tpl.php");
	exit();
}elseif($action=="pubreply" || $ipost=='modifyreply'){

	if($ipost=='modifyreply'){
		$modreplyattachs=array();
		if($gid==4){
			showmsg("该用户组无此权限");
		}
		$q = $db->query("select * from g_replys where rid= $mid limit 1");
		$replyinfo=$db->fetch_array($q);
		$modifyinfo=$replyinfo;
		if($replyinfo['typeid']==1){
			if(!$pervalue{1} && !$pervalue{17}){
				showmsg("该用户组没有修改的权限");
			}
			if($pervalue{1} && $replyinfo['reply_uid']!=$uid && !$pervalue{17}){
				showmsg("该用户组没有修改别人回复的权限");
			}
		}elseif($replyinfo['typeid']==2){
			if(!$pervalue{1} && !$pervalue{16}){
				showmsg("该用户组没有修改的权限");
			}
			if($pervalue{1} && $replyinfo['reply_uid']!=$uid && !$pervalue{16}){
				showmsg("该用户组没有修改别人回复的权限");
			}
		}elseif($replyinfo['typeid']==3){
			if(!$pervalue{1} && !$pervalue{15}){
				showmsg("该用户组没有修改的权限");
			}
			if($pervalue{1} && $replyinfo['reply_uid']!=$uid && !$pervalue{15}){
				showmsg("该用户组没有修改别人回复的权限");
			}
		}
		if($action=='delete'){
			modifydelattach($mid,$fid,4);
		}
		$aq = $db->query("select * from g_attachments where bid = $mid and typeid=4");
			while($modreplyattach=$db->fetch_array($aq)){
				$modreplyattachs[$modreplyattach['fid']]=$modreplyattach;
			}
	}
	if($action=='pubthread'){
		$siteheadtitle=implode(' ' , array('发布主题',$forumname,$sitename));
	}elseif($ipost=='modifythread'){
		$siteheadtitle=implode(' ' , array('修改主题',$forumname,$sitename));	
	}
	include($templatedir."postreply.tpl.php");
	exit();
}

if($action=='deletereply'){
	if(!$uid){
			showmsg("该用户组无此权限");
	}
	$q = $db->query("select * from g_replys where rid= $rid limit 1");
	$replyinfo=$db->fetch_array($q);
	if($replyinfo['typeid']==1){
			if($pervalue{0} && !$pervalue{17}){
				showmsg("该用户组没有删除别人回复的权限");
			}
			if(!$pervalue{0} && !$pervalue{17}){
				showmsg("该用户组没有删除的权限");
			}
	}elseif($replyinfo['typeid']==2){
			if($pervalue{0} && !$pervalue{16}){
				showmsg("该用户组没有删除别人回复的权限");
			}
			if(!$pervalue{0} && !$pervalue{16}){
				showmsg("该用户组没有删除的权限");
			}
	}elseif($replyinfo['typeid']==3){
			if($pervalue{0} && !$pervalue{15}){
				showmsg("该用户组没有删除别人回复的权限");
			}
			if(!$pervalue{0} && !$pervalue{15}){
				showmsg("该用户组没有删除的权限");
			}
	}
	deletereply($rid,$replyinfo);
}

if($ipost=='setarticle'){
	if(!$pervalue{17}){
		showmsg("该用户组无此权限");
	}
	if($action=='unsettop'){
		articlesetting($action,$top_article_check);
	}else{
		$article_check = isset($article_check)?$article_check:array();
		$top_article_check = isset($top_article_check)?$top_article_check:array();
		$merge_check=array_merge($article_check,$top_article_check);
		articlesetting($action,$merge_check);
	}
}

if($action=='savearticle'){
		article2db($articletitle,$g_editor_doc,$tag,$pid,$categoryid,$uid,$userid);
	}elseif($action=='saveproduct'){
		product2db($producttitle , $productintro , $g_editor_doc , $tag , $pid , $categoryid , $uid , $userid);
	}elseif($action=='savethread'){
		thread2db($posttitle , $g_editor_doc , $forid , $uid , $userid);
	}

if($ipost=='setproduct'){
	if(!$pervalue{16}){
		showmsg("该用户组无此权限");
	}
	if($action=='unsettop'){
		productsetting($action,$top_product_check);
	}else{
		$product_check = isset($product_check)?$product_check:array();
		$top_product_check = isset($top_product_check)?$top_product_check:array();
		$merge_check=array_merge($product_check,$top_product_check);
		productsetting($action,$merge_check);
	}
}

if($ipost=='setthread'){
	if(!$pervalue{15}){
		showmsg("该用户组无此权限");
	}
	if($action=='unsettop'){
		forumsetting($action,$top_thread_check);
	}else{
		$thread_check = isset($thread_check)?$thread_check:array();
		$top_thread_check = isset($top_thread_check)?$top_thread_check:array();
		$merge_check=array_merge($thread_check,$top_thread_check);
		forumsetting($action,$merge_check);
	}
}

?>