<?php
defined('IN_HS') or exit('Access Denied');
require HS_ROOT.'/module/'.$mod.'/include/common.inc.php';

sajax_init();
sajax_export("codedocheck");
sajax_handle_client_request();
//$GLOBALS['sajax_debug_mode'] = 1;

if(!$_userid) header('location:/member/login.php?forward='.urlencode($PHP_URL));
if($CHA['channeldomain'] && strpos($PHP_URL, $CHA['channeldomain'])!==false)
{
   header('Location:'.$HS['siteurl'].$CHA['channeldir'].'/myitem.php?'.$PHP_QUERYSTRING);
   exit;
}
$CHA['enablecontribute'] or showmessage($LANG['not_allowed_to_add_an_article'], 'goback');
//if(!$_userid) showmessage('请登陆后再发布', '/member/login.php?forward='.urlencode($PHP_URL));



require_once HS_ROOT.'/include/formselect.func.php';
require HS_ROOT.'/include/tree.class.php';
$tree = new tree();

$enableaddalways = false;
$GROUP = cache_read('member_group.php');
$gid[] = $_groupid;
$gids = array_merge($_arrgroupid, $gid);
foreach($gids as $gid)
{
	if($GROUP[$gid]['enableaddalways'])
	{
		$enableaddalways = true;
		break;
	}
}

$articleid = isset($articleid) ? intval($articleid) : 0;
$catid = isset($catid) ? intval($catid) : 0;
if($catid) $CAT = cache_read('category_'.$catid.'.php');

$category_select = category_select('catid', $LANG['please_select'], $catid, 'id="catid"');
$pagesize = $HS['pagesize'];
$referer = isset($referer) ? $referer : $PHP_REFERER;
$action = isset($action) ? $action : 'manage';
if(!isset($checkcodestr)) $checkcodestr = '';
if ($_groupid ==1)
{
$status =3;
}else{
$status =1;
}

switch($action){

case 'add':

	if($dosubmit)
	{
		checkcode($checkcodestr, $MOD['check_code'], $PHP_REFERER);
		if(!$catid)	showmessage($LANG['please_choose_categories'], 'goback');
		if($catid < 5 || $catid > 6)	showmessage("你没有权限在此栏目发布", 'goback');
		if(empty($title)) showmessage($LANG['short_title_can_not_be_blank'], 'goback');
		if(empty($author)) showmessage("请填写游戏中昵称", 'goback');
		if(empty($copyfrom)) showmessage("请填写所在服务器", 'goback');
		if(empty($content))	showmessage($LANG['content_can_not_be_blank'], 'goback');
		if($CAT['child'] && !$CAT['enableadd']) showmessage($LANG['not_allowed_to_add_an_article'], 'goback');
		if($CAT['arrgroupid_add'] && strpos($CAT['arrgroupid_add'], "$_groupid") === false) showmessage($LANG['not_allowed_to_add_by_your_group'], 'goback');

		$inputstring=new_htmlspecialchars(array('title'=>$title,'author'=>$author,'copyfrom'=>$copyfrom));
		extract($inputstring,EXTR_OVERWRITE);
		$content = str_safe($content);
		$addtime = $PHP_TIME;


		$urlruleid = $CAT['ishtml'] ? $CAT['item_html_urlruleid'] : $CAT['item_php_urlruleid'];


		$query = "INSERT INTO ".channel_table('article',$channelid)."(catid,typeid,title,author,copyfrom,thumb,status,username,addtime,editor,edittime,ishtml,urlruleid,htmldir,prefix) VALUES('$catid','$typeid','$title','$author','$copyfrom','$thumb','$status','$_username','$addtime','$_username','$addtime','$CAT[ishtml]', '$urlruleid','$CAT[item_htmldir]','$CAT[item_prefix]')";
		$db->query($query);
		$articleid = $db->insert_id();
		require HS_ROOT.'/include/attachment.class.php';
		$att = new attachment;
		$att->attachment($articleid, $channelid, $catid);
		$att->add($content);

		$linkurl = item_url('url', $catid, $CAT['ishtml'], $CAT['item_html_urlruleid'], $CAT['item_htmldir'], $CAT['item_prefix'], $articleid, $addtime);
		$db->query("UPDATE ".channel_table('article', $channelid)." SET linkurl='$linkurl' WHERE articleid=$articleid ");

		if($MOD['storage_mode'] < 3) $db->query("INSERT INTO ".channel_table('article_data', $channelid)." (articleid,content) values('$articleid', '$content') ");
		if($MOD['storage_mode'] > 1) txt_update($channelid, $articleid, $content);
		if($MOD['storage_mode'] == 3) $db->query("INSERT INTO ".channel_table('article_data', $channelid)." (articleid,content) values('$articleid', '') ");

		showmessage($LANG['add_article_success'], $referer);
	}
	else
	{
		$type_select = type_select('typeid', $LANG['type']);
	}
break;

case 'edit':

    $articleid = intval($articleid);
	$articleid or showmessage($LANG['empty_article_id'], 'goback');
    if($dosubmit)
	{
		checkcode($checkcodestr, $MOD['check_code'], $PHP_REFERER);
		if(!$catid)	showmessage($LANG['please_choose_categories'], 'goback');
		if($catid < 5 || $catid > 6)	showmessage("你没有权限在此栏目发布", 'goback');
		if(empty($title)) showmessage($LANG['short_title_can_not_be_blank'], 'goback');
		if(empty($author)) showmessage("请填写游戏中昵称", 'goback');
		if(empty($copyfrom)) showmessage("请填写所在服务器", 'goback');
		if(empty($content))	showmessage($LANG['content_can_not_be_blank'], 'goback');
		if($CAT['child'] && !$CAT['enableadd']) showmessage($LANG['not_allowed_to_add_an_article'], 'goback');
		if($CAT['arrgroupid_add'] && strpos($CAT['arrgroupid_add'], "$_groupid") === false) showmessage($LANG['not_allowed_to_add_by_your_group'], 'goback');

		$inputstring = new_htmlspecialchars(array('title'=>$title,'author'=>$author,'copyfrom'=>$copyfrom));
		extract($inputstring, EXTR_OVERWRITE);
		$content = str_safe($content);
		$addtime = $PHP_TIME;


		$db->query("UPDATE ".channel_table('article', $channelid)." SET catid='$catid',typeid='$typeid',title='$title',author='$author',copyfrom='$copyfrom',status='$status',editor='$_username',edittime='$PHP_TIME' WHERE articleid=$articleid AND username='$_username'");
		if($db->affected_rows() == 1)
		{
			if($MOD['storage_mode'] < 3) $db->query("UPDATE ".channel_table('article_data', $channelid)." SET content='$content' WHERE articleid=$articleid ");
			if($MOD['storage_mode'] > 1) txt_update($channelid, $articleid, $content);

		}
		showmessage($LANG['modify_article_success'], $referer);
	}
	else
	{
		$r = $db->get_one("SELECT * FROM ".channel_table('article', $channelid)." WHERE articleid=$articleid AND username='$_username'");
		if(!$r['articleid']) showmessage($LANG['article_not_exists_orother'], 'goback');
		@extract($r);
		@extract($db->get_one("SELECT content FROM ".channel_table('article_data', $channelid)." WHERE articleid=$articleid "));
		$type_select = type_select('typeid', $LANG['type'], $typeid);
		$category_select = category_select('catid', $LANG['please_select'], $catid, 'id="catid"');
	}
break;

case 'delete':

  $articleid = intval($articleid);
	$articleid or showmessage($LANG['empty_article_id'], ' goback'); 
	$db->query("DELETE FROM ".channel_table('article', $channelid)." WHERE articleid=$articleid AND username='$_username' AND status!=3 ");
	if($MOD['storage_mode'] > 1) txt_delete($channelid, $articleid);
	if($db->affected_rows()>0)
	{
		showmessage($LANG['delete_article_success'],$referer);
	}
	else
	{
		showmessage($LANG['failure_to_delete_article'], 'goback');
	}
break;

case 'manage':


	if($catid && !array_key_exists($catid, $CATEGORY)) $catid = 0;
	if(!isset($page))
	{
		$page=1;
		$offset=0;
	}
	else
	{
		$offset=($page-1)*$pagesize;
	}


	$sql = '';

	if($catid)
	{
		$sql .=  $CATEGORY[$catid]['child'] ? " and catid in({$CATEGORY[$catid]['arrchildid']}) " : " and catid = $catid ";
	}


	$query="SELECT COUNT(articleid) as num FROM ".channel_table('article', $channelid)." WHERE username='$_username' $sql ";
	$result=$db->query($query);
	$r=$db->fetch_array($result);
	$number=$r["num"];

	$pages=phppages($number, $page, $pagesize);
	$articles = array();

	$query="SELECT * FROM ".channel_table('article', $channelid)." WHERE username='$_username' $sql ORDER BY status ASC LIMIT $offset,$pagesize ";
	$result=$db->query($query);
	if($db->num_rows($result)>0)
	{
		while($r=$db->fetch_array($result))
		{
			$r['linkurl'] = linkurl($r['linkurl']);
			$r['title'] = str_cut($r['title'], 46, '...');
			$r['title'] = style($r['title'], $r['style']);
			$r['adddate']=date("Y-m-d H:i:s",$r['addtime']);
			$r['checktime']=date("Y-m-d H:i:s",$r['checktime']);	
			$r['catname'] = $CATEGORY[$r['catid']]['catname'];
			$r['catlinkurl'] = $CATEGORY[$r['catid']]['linkurl'];
			$r['type'] = $r['typeid'] ? show_type($channelid, $r['typeid']) : '';
			$articles[]=$r;
		}
	}
break;
}

$head['title'] = $LANG['manage_my_news'];
$head['keywords'] = $HS['seo_keywords'];
$head['description'] = $HS['seo_description'];
/*
$servs = array();
$srs = $db->query("SELECT servername FROM ".TABLE_GAMESERVER." ORDER BY id ASC");
while($rs = $db->fetch_array($srs))
{
$servs[] = $rs;
}
*/

include template('article', 'myitem');

function codedocheck($chkcode)
{
	global $session;
	if($chkcode == '') return "nocode";
	if(!is_object($session)) $session = new hs_session();
	if(strtolower($_SESSION['checkcode']) != strtolower($chkcode))
	{
	  unset($_SESSION['checkcode']);
	  return "errcode";
	}else{
   return "true";
  }
}

?>