<?php
session_start();

include('includes/ajax_check_login.php');

if( !isset($_POST['news_id']) 
|| !isset($_POST['news_cate_id'])
|| !isset($_POST['date']) 
|| !isset($_POST['subject']) 
|| !isset($_POST['author']) 
|| !isset($_POST['abstract']) 
|| !isset($_POST['news_content']) 
|| ( $static_uint_news_id = (int)$_POST['news_id'] ) <= 0 
|| ( $static_str_date = trim($_POST['date']) ) == '' 
|| ( $static_str_subject = trim(strip_tags($_POST['subject'])) ) == '' 
|| ( $static_str_author = trim(strip_tags($_POST['author'])) ) == '' 
|| ( $static_str_abstract = trim(strip_tags($_POST['abstract'])) ) == '' 
|| ( $str_content = trim($_POST['news_content']) ) == '' )
	exit(json_encode(array('ErrCode'=>-1, 'ErrMessage'=>'輸入資料不足', 'RedirectURL'=>'')));
else
	$static_arr_news_cate_ids = $_POST['news_cate_id'];

if( get_magic_quotes_gpc() )
	$str_content = stripslashes($str_content);

//$str_content = strip_tags($str_content, '<span><div><br><p><font><strong><b><i><u><h1><h2><h3><h2><h5><h6><hr><center><blockquote><ol><ul><li><dd><dt><img><a><object><embed><table><th><tr><td>');

$str_content = preg_replace('/src="\.\.\/images\/uploads\//i', 'src="images/uploads/', $str_content);
$str_content = preg_replace('/<a href=/i', '<a target="_blank" href=', $str_content);

require_once('includes/common.php');

require_once('includes/author.php');
$obj_class_author = new class_author();

if( !$obj_class_author->get_news_by_news_id($static_uint_news_id) )
	exit(json_encode(array('ErrCode'=>0, 'ErrMessage'=>'所選擇的新聞不存在！', 'RedirectURL'=>'')));
else if( !$obj_class_author->update_news($static_uint_news_id, $static_str_date, $static_str_subject, $static_str_author, $static_str_abstract) || !( $file_handle = @fopen('../documents/news/' . $static_uint_news_id . '.html', 'w') ) )
	exit(json_encode(array('ErrCode'=>0, 'ErrMessage'=>'編輯新聞失敗！', 'RedirectURL'=>'')));

fwrite($file_handle, $str_content);
fclose($file_handle);

@$obj_class_author->clear_news_cates_by_news_id($static_uint_news_id);

if( $static_arr_news_cate_ids && is_array($static_arr_news_cate_ids) && ( $arr_news_cates = $obj_class_author->get_news_cates() ) )
{
	foreach( $arr_news_cates as $arr_row )
	{
		if( in_array($arr_row['NewsCateId'], $static_arr_news_cate_ids) )
			@$obj_class_author->add_news_cates_news($arr_row['NewsCateId'], $static_uint_news_id);
	}
}

exit(json_encode(array('ErrCode'=>1, 'ErrMessage'=>'編輯新聞成功！', 'RedirectURL'=>'')));
?>