<?php
if (!defined('webid')) {
    exit();
}

if ($user_status != 'admin') {
    exit();
}

$sql = mysql_fetch_array(mysql_query("SELECT Count(id) as count FROM tb_users WHERE id != 1"));

if (isset($_POST['massmail'])) {
    $sql2 = mysql_query("SELECT id FROM tb_users WHERE id != 1");
    $echo = "<font color='red'>Идет отправка сообщений ...</color><br />";
    while ($row2 = mysql_fetch_array($sql2)) {
        $sql3 = mysql_query("SELECT id FROM seo_mails WHERE text = '".substr($_POST['usermail'], 0, 2000)."' and toid = '$row2[id]'");
        if (mysql_num_rows($sql3) > 0) {
            
        } else {
            mysql_query("INSERT INTO seo_mails (text, subject, fromid, toid, timemessage) VALUES ('".substr($_POST['usermail'], 0, 2000)."', '".$_POST['usersubject']."', 1, '$row2[id]', '".time()."')");
        }
    }
    $echo = "<font color='green'>Сообщения успешно отправлены</font><br />";
}

echo $echo;
?>
<script type="text/javascript" src="js/nicEdit.js"></script>
<script type="text/javascript">
bkLib.onDomLoaded(function() {
	new nicEditor({fullPanel : true}).panelInstance('usermail');
});
</script>
<form action="" method="post">
<table width="50%" border="0" cellpadding="0" cellspacing="1" class="tab">
<thead style="background: #ebebeb;">
    <th colspan="2" align="right" style="padding: 5px;"><input type="submit" id="save" name="massmail" value="Разослать (<?=$sql['count']?>)" /></th>
</thead>
<tr>
    <td width="100" align="center" style="padding: 3px;" class="rt">Тема</td>
    <td align="center" style="padding: 3px;" align="center" class="rt"><input type="text" maxlength="55" class="val" style="width: 98%;" id="usersubject" name="usersubject" /></td>
</tr>
<tr>
    <td colspan="2" class="rt"><textarea id="usermail" name="usermail" style="width: 100%; height: 250px; padding: 5px;"></textarea></td>
</tr>
</table>
</form>