<?php
header("Content-type:text/html;charset=utf-8");  
session_start();
$session_admin = isset($_SESSION['admin']) ? $_SESSION['admin'] : ''; 
if ($session_admin == '') {
	die("Can not access");
}
require_once("../common.php");
checkadminrights($session_admin, 'sysmsgs');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link type="text/css" rel="stylesheet" href="../css/base.css" />
<script type="text/javascript" src="../../ckeditor/ckeditor.js"></script>
<style type="text/css">
.td1 { padding:5px 0 0 0;width:70px;height:20px;vertical-align:top; }
.td2 { width:898px;vertical-align:top; }
</style>
<script type="text/javascript">
String.prototype.trim=function() {
     return this.replace(/(^\s*)(\s*$)/g, '');
}
function chkform() {
	var form = document.getElementById('form');
    if(form.title.value.trim() == "") { 
      alert('标题不能为空！'); 
      return false; 
    } 
	if(form.content.value.trim() == "") { 
      alert('内容不能为空！'); 
      return false; 
    } 
	form.sendtodetail.value = document.getElementById('childframe').contentWindow.getCheckedIds();

	if (form.htmlurl.value.trim() != '') {
		if ((form.htmlurl.value.trim().substring(0, 7) != "http://") && 
			(form.htmlurl.value.trim().substring(0, 8) != "https://")) { 
		  alert('链接地址必须以"http://"或"https://"开头'); 
		  return false; 
		} 
	}
    return   true; 
}//chkform

function onusertypechange() {
	var obj = document.getElementById('forusertype');
	if (obj.value == 1) {		
		document.getElementById('departs').style.display = "";
	} else {
		document.getElementById('departs').style.display = "none";
	}
}

function changelinktype(obj) {
	if (obj.value == 1) {
		document.getElementById('link1').style.display = "";
		document.getElementById('link2').style.display = "none";
	} else if (obj.value == 2) {
		document.getElementById('link1').style.display = "none";
		document.getElementById('link2').style.display = "";
	} else {
		document.getElementById('link1').style.display = "none";
		document.getElementById('link2').style.display = "none";
	}
}
</script>
</head>
<body>

<?php
$step = isset($_GET['step']) ? $_GET['step'] : '1'; 

function step1() {
	require_once("../common.php");
	require_once("../../ckeditor/ckeditor.php");
	
echo<<<EOT
<div class="rightbar">
	<div class="form_title">发布系统消息</div>
	<div class="form_panel">
		<form action="sysmsgs_add.php?step=2" method="post" id="form" onSubmit="return chkform();">
		<input type="hidden" name="sendtodetail" value=""/>		
		<table  style="border:0;">
		<tr>
			<td class="td1">标题:</td>
			<td class="td2"><input style="width:260px;" type="text" name="title" value=""/><span style="color:#ff0000;">&nbsp;*</span>&nbsp;<span style="color:#2fb900;">最大长度为50个字符</span></td>
		</tr>
		<tr>
			<td class="td1">分类:</td>
			<td class="td2"><input style="width:260px;" type="text" name="category" value="通知"/><span style="color:#ff0000;">&nbsp;&nbsp;</span>&nbsp;<span style="color:#2fb900;">可自定义输入“通知”
			、“公告”等</span></td>
		</tr>		
		<tr>
			<td class="td1">内容:</td>
			<td class="td2" style="height:150px;"><textarea style="width:264px;height:140px;" id="content" name="content"></textarea><span style="color:#ff0000;">&nbsp;*</span>&nbsp;<span style="color:#2fb900;">最大长度为1024个字符</span></td>
		</tr>
		<tr>
			<td class="td1">接收范围:</td>
			<td class="td2" style="height:30px;">
				<select style="width:266px;" id="forusertype" name="forusertype" onchange="onusertypechange();">
					<option value="1">内部帐号</option>
					<option value="0">外部帐号</option>
					<option value="-1">全部帐号</option>
				</select>
				<span style="color:#ff0000;">&nbsp;&nbsp;</span><span style="color:#2fb900;">帐号类型</span>
			</td>
		</tr>
		<tr id="departs">
			<td class="td1"></td>
			<td class="td2" style="height:225px;">
				<iframe id="childframe" style="margin:0;padding:0;width:264px;height:200px;border:0;" src="selectusers.php"></iframe>
			</td>
		</tr>
		<tr>
			<td class="td1">附加选项:</td>
			<td class="td2" style="height:40px;">
				<input type="checkbox" class="checkbox" name="foronlineusers" value="1" />只发送给在线用户&nbsp;
				<input type="checkbox" class="checkbox" name="needreaded" value="1" />允许已读计数&nbsp;
				<input type="checkbox" class="checkbox" name="needfeedback" value="1" />允许消息反馈
			</td>
		</tr>
		<tr>
			<td class="td1">网页链接:</td>
			<td class="td2">
				<input type="radio" class="radio" name="linktype" value="0" onclick="changelinktype(this);" checked />无网页链接
				<input type="radio" class="radio" name="linktype" value="1" onclick="changelinktype(this);" />外部网页链接
				<input type="radio" class="radio" name="linktype" value="2" onclick="changelinktype(this);" />编辑网页内容
			</td>
		</tr>	
		<tr id="link1" style="display:none;">
			<td class="td1"></td>
			<td class="td2"><input style="width:260px;" type="text" name="htmlurl" value=""/><span style="color:#2fb900;"><span style="color:#ff0000;">&nbsp;&nbsp;</span>http:&#x2F;&#x2F;...</span></td>
		</tr>
		<tr id="link2" style="display:none;">
			<td class="td1"></td>
			<td class="td2"><textarea class="ckeditor" cols="80" rows="10" id="htmlcode" name="htmlcode"></textarea></td>
		</tr>
		<tr>
			<td class="td1"></td>
			<td class="td2" style="height:30px;"></td>
		</tr>
		<tr>
			<td class="td1"></td>
			<td class="td2"><input class="cssbtn" type="submit" value="发送">&nbsp;<input class="cssbtn" type="button" value="取消" onclick="window.location='sysmsgs.php'"></td>
		</tr>
		</table>
		</form>
	</div>
</div>

<div class="whiteline"></div>
<div class="whiteline"></div>
<div class="whiteline"></div>
<div class="whiteline"></div>
<div class="whiteline"></div>
EOT;
}//step1

function step2() {
	require_once("../common.php");

	$title = isset($_POST['title']) ? $_POST['title'] : ''; 
	$category = isset($_POST['category']) ? $_POST['category'] : ''; 
	$content = isset($_POST['content']) ? $_POST['content'] : ''; 
	$link = '';
	$attachfile = isset($_POST['attachfile']) ? $_POST['attachfile'] : '';
	$forusertype = isset($_POST['forusertype']) ? $_POST['forusertype'] : '0'; 
	$sendtodetail = isset($_POST['sendtodetail']) ? $_POST['sendtodetail'] : ''; 
	$foronlineusers = isset($_POST['foronlineusers']) ? $_POST['foronlineusers'] : '0'; 
	$needreaded = isset($_POST['needreaded']) ? $_POST['needreaded'] : '0'; 
	$needfeedback = isset($_POST['needfeedback']) ? $_POST['needfeedback'] : '0'; 

	$linktype = isset($_POST['linktype']) ? $_POST['linktype'] : '0'; 
	if ($linktype == 1) {
		$link = isset($_POST['htmlurl']) ? $_POST['htmlurl'] : ''; 
	} else if ($linktype == 2) {
		$link = strtolower(md5(uniqid(mt_rand(), true))) . '.html'; 
		$htmlfile = '../../sysmsgs/' . $link;
		$htmlcode = isset($_POST['htmlcode']) ? $_POST['htmlcode'] : ''; 
		$htmlcode = str_replace('../../sysmsgs/', '', $htmlcode);
		$htmlpage = chr(0xEF).chr(0xBB).chr(0xBF).
			'<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />'.
			'<title>'.$title.'</title></head><body style="font-size:13px;">'.
			'标题: '.$title.'<br>'.
			'发布时间: '.date('Y-m-d H:i:s').'<br>'.
			'<hr>'.$htmlcode.'</body></html>';
		file_put_contents($htmlfile, $htmlpage);
	}

	$methodname = 'Admin.SendSysMsg';
	$requestinfo = '<?xml version="1.0" encoding="utf-8"?><request>' . 
		'<ip>' . getip() . '</ip>' . 		
		'<title>' . strtoxml($title) . '</title>' . 
		'<category>' . strtoxml($category) . '</category>' . 
		'<content>' . strtoxml($content) . '</content>' . 
		'<link>' . strtoxml($link) . '</link>' . 
		'<attachfile>' . strtoxml($attachfile) . '</attachfile>' .  
		'<forusertype>' . $forusertype . '</forusertype>' .  
		'<sendtodetail>' . strtoxml($sendtodetail) . '</sendtodetail>' . 
		'<foronlineusers>' . $foronlineusers . '</foronlineusers>' . 
		'<needreaded>' . $needreaded . '</needreaded>' . 
		'<needfeedback>' . $needfeedback . '</needfeedback>' . 
		'</request> ';
	$responseinfo = '';
	$responsexml = null;
	$errorcode = ERRORCODE_UNKNOWN;

	$cmd = new COM("OpenCom.Command");
	$methodname = iconv('UTF-8', 'GBK//IGNORE', $methodname);
	$requestinfo = iconv('UTF-8', 'GBK//IGNORE', $requestinfo);
	if ($cmd->Execute($methodname, $requestinfo, $responseinfo, 5000)) {
		$responseinfo = iconv('GBK', 'UTF-8//IGNORE', $responseinfo);
		$responsexml = simplexml_load_string($responseinfo);
		$errorcode = $responsexml->errorcode;
	}
	$cmd = null;

	switch($errorcode) {
		case ERRORCODE_OK: 
echo<<<EOT
	<div class="msgbox">
		<div class="msgbox_text">
			<img width="40px" height="40px" align="absmiddle" src="../images/ok.gif" />&nbsp;系统消息发布成功！
		</div>
		<div class="msgbox_btns">
			<input class="cssbtn" type="button" value="返回" onclick="window.location='sysmsgs.php'" />
		</div>
	</div>
EOT;
			break;
		default:

	}//switch
}//step2

if ($step == 1) {
	step1();
}
else if ($step == 2) {
	step2();
}
?>

</body>
</html>
