<?php
session_start();

include('includes/ajax_check_login.php');

if( !isset($_POST['news_cate_id'])
|| !isset($_POST['date'])
|| !isset($_POST['subject']) 
|| !isset($_POST['author']) 
|| !isset($_POST['is_top']) 
|| !isset($_POST['is_local_top']) 
|| !isset($_POST['active']) 
|| !isset($_POST['abstract']) 
|| !isset($_POST['news_content']) 
|| ( $static_str_date = trim($_POST['date']) ) == '' 
|| ( $static_str_subject = trim(strip_tags($_POST['subject'])) ) == '' 
|| ( $static_str_author = trim(strip_tags($_POST['author'])) ) == '' 
|| ( $static_uint_is_top = (int)$_POST['is_top'] ) < 0 
|| $static_uint_is_top > 1 
|| ( $static_uint_is_local_top = (int)$_POST['is_local_top'] ) < 0 
|| $static_uint_is_local_top > 1 
|| ( $static_uint_active = (int)$_POST['active'] ) < 0 
|| $static_uint_active > 1 
|| ( $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/admin.php');
$obj_class_admin = new class_admin();

mysqli_query($obj_class_admin->sql_connect, 'BEGIN;');

if( !( $uint_news_id = $obj_class_admin->add_news($static_str_date, $static_str_subject, $static_str_author, $static_str_abstract, $static_uint_is_top, $static_uint_is_local_top, $static_uint_active) ) )
{
	mysqli_query($obj_class_admin->sql_connect, 'COMMIT;');

	exit(json_encode(array('ErrCode'=>0, 'ErrMessage'=>'新增新聞失敗！', 'RedirectURL'=>'')));
}
else if( !( $file_handle = @fopen('../documents/news/' . $uint_news_id . '.html', 'w') ) )
{
	mysqli_query($obj_class_admin->sql_connect, 'ROLLBACK;');
	mysqli_query($obj_class_admin->sql_connect, 'COMMIT;');

	exit(json_encode(array('ErrCode'=>0, 'ErrMessage'=>'新增新聞內容失敗！', 'RedirectURL'=>'')));
}
else if( !@fwrite($file_handle, $str_content) )
{
	mysqli_query($obj_class_admin->sql_connect, 'ROLLBACK;');
	mysqli_query($obj_class_admin->sql_connect, 'COMMIT;');

	exit(json_encode(array('ErrCode'=>0, 'ErrMessage'=>'寫入新聞內容失敗！', 'RedirectURL'=>'')));
}
else
	fclose($file_handle);

mysqli_query($obj_class_admin->sql_connect, 'COMMIT;');

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

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