<?php
defined('IN_HS') or exit('Access Denied');
set_time_limit(0);
$articleid = intval($articleid);
$articleid or showmessage($LANG['empty_article_id'], $referer);


require HS_ROOT.'/include/attachment.class.php';
$att = new attachment;

require HS_ROOT.'/admin/include/position.class.php';
$pos = new position($channelid);

$referer = isset($referer) ? $referer : "?mod=$mod&file=$file&action=manage&channelid=$channelid";

if($dosubmit)
{
	if(empty($article['title']))
	{
		showmessage($LANG['short_title_can_not_be_blank'],'goback');
	}
	if(!isset($article['islink']) && empty($article['content']))
	{
		showmessage($LANG['content_can_not_be_blank'],'goback');
	}

	if(isset($save_remotepic)) 
	{
		require HS_ROOT.'/include/get_remotefiles.func.php';
		$article['content'] = get_remotepics($article['content'], $CHA['channeldir']."/".$CHA['uploaddir']);
	}
	
	if($ishtmled) $art->delete($articleid, 1);

	$introcude_length = isset($introcude_length) ? intval($introcude_length) : 0;
	if(empty($article['introduce']) && isset($add_introduce) && $introcude_length) $article['introduce'] = str_cut(strip_tags($article['content']), $introcude_length);

  $article['showcommentlink'] = isset($article['showcommentlink']) ? $article['showcommentlink'] : 0;
	$article['islink'] = isset($article['islink']) ? 1 : 0;
	$article['arrgroupidview'] = empty($article['arrgroupidview']) ? '' : implode(',',$article['arrgroupidview']);
	$article['catid'] = $catid;
	$article['editor'] = $_username;
	$article['urlruleid'] = $article['ishtml'] ? $html_urlrule : $php_urlrule;
	$article['addtime'] = preg_match('/^([0-9]{4})-([0-9]{1,2})-([0-9]{1,2}) ([0-9]{2}):([0-9]{2}):([0-9]{2})/', $article['addtime']) ? strtotime($article['addtime']) : $PHP_TIME;	
	$article['edittime'] = $PHP_TIME;
	if(isset($article['arrposid']))
	{
		$arrposid = $article['arrposid'];
		$article['arrposid'] = ','.implode(',', $arrposid).',';
	}
	else
	{
        $arrposid = array();
		$article['arrposid'] = '';
	}

	if($art->edit($article))
	{
		if($freelink)
		{
			$r = $db->get_one("select title,thumb,linkurl,style from ".channel_table('article', $channelid)." where articleid='$articleid' ");
			$f = array();
			$f['title'] = $r['title'];
			$f['url'] = linkurl($r['linkurl']);
			$f['image'] = imgurl($r['thumb']);
			$f['style'] = $r['style'];
			add_freelink(trim($freelink), $f);
		}
		$att->attachment($articleid, $channelid, $catid);
		$att->add($article['content']);

		if($arrposid || $old_arrposid)
		{
			$old_posid_arr = $old_arrposid ? array_filter(explode(',', $old_arrposid)) : array();
			$pos->edit($articleid, $old_posid_arr, $arrposid);
		}

		if($article['status'] == 3)
		{
			require HS_ROOT.'/include/create_related_html.inc.php';
		}
		showmessage($LANG['modify_article_success'], $referer);
	}
	else
	{
		showmessage($LANG['modify_article_failure'],'goback');
	}
}
else
{
	@extract(new_htmlspecialchars($art->get_one()));
	$CAT = cache_read("category_$catid.php");
	$addtime = date('Y-m-d H:i:s',$addtime);
	$type_select = type_select('article[typeid]', $LANG['type'], $typeid);
	$style_edit = style_edit("article[style]", $style);

	$showgroup = showgroup('checkbox', 'article[arrgroupidview][]', $arrgroupidview);
	$showskin = showskin('article[skinid]', $skinid);
	$showtpl = showtpl($mod,'content', 'article[templateid]', $templateid);
	$html_urlrule = urlrule_select('html_urlrule', 'html', 'item', $urlruleid);
	$php_urlrule = urlrule_select('php_urlrule', 'php', 'item', $urlruleid);
	$position = $pos->checkbox('article[arrposid][]', $arrposid);
	
	include admintpl($mod.'_edit');
}
?>