<?php


defined('_IN_JOHNADM') or die('Error: restricted access');

// Проверка на права
if ($rights < 7) {
    header('Location: /?err');
    exit;
}

echo '<div class="phdr"><a href="index.php"><b>Admin panel</b></a>&nbsp;|&nbsp;Sistema xabari</div>';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $msg = isset($_POST['msg']) ? functions::checkin(mb_substr(trim($_POST['msg']), 0, 5000)) : '';
    $me = isset($_POST['me']) ? abs(intval($_POST['me'])) : 0;
    $author = isset($_POST['author']) ? abs(intval($_POST['author'])) : 0;
    $who = isset($_POST['who']) ? $_POST['who'] : '';

    $error = array();
    if (empty($msg))
        $error[] = 'Xabar matnini kiriting!';
    if (empty($who))
        $error[] = 'Pochta qabul qiluvchilarni tanlamadinigz!';
    if (in_array('all', $who) && (in_array('only_adm', $who) || in_array('only_usr', $who) || in_array('only_mans', $who) || in_array('only_girls', $who)))
        $error[] = 'Pochta qabul qiluvchilar xato tanlandi! Bir vaqtda hamma foydalanuvchilar bilan boshqa foydalanuvchi turlarini tanlay olmaysiz!';
    if ((in_array('only_adm', $who) && in_array('only_usr', $who)) || (in_array('only_mans', $who) && in_array('only_girls', $who)))
        $error[] = 'Pochta qabul qiluvchilar xato tanlandi! Bir vaqtda oddiy foydalanuvchilar va adminlar yoki yigitlar va qizlarni tanlay olmaysiz!';

    if (!$error) {
        if (in_array('all', $who)) // Все пользователи
            $sql = "SELECT id FROM users WHERE id != $user_id";
        elseif (in_array('only_adm', $who)) // Только администарации
            $sql = "SELECT id FROM users WHERE rights > 0 AND id != $user_id";
        elseif (in_array('only_adm', $who) && in_array('only_mans', $who)) //Только администрации с уточнением только парням
            $sql = "SELECT id FROM users WHERE rights > 0 AND sex = 'm' AND id != $user_id";
        elseif (in_array('only_adm', $who) && in_array('only_girls', $who)) //Только администрации с уточнением только девушкам
            $sql = "SELECT id FROM users WHERE rights > 0 AND sex = 'zh' AND id != $user_id";
        elseif (in_array('only_usr', $who)) // Только простым юзерам
            $sql = "SELECT id FROM users WHERE rights = 0 AND id != $user_id";
        elseif (in_array('only_usr', $who) && in_array('only_mans', $who)) // Только простым юзерам с уточнением только парням
            $sql = "SELECT id FROM users WHERE rights = 0 AND sex = 'm' AND id != $user_id";
        elseif (in_array('only_usr', $who) && in_array('only_girls', $who)) // Только простым юзерам с уточнением только девушкам
            $sql = "SELECT id FROM users WHERE rights = 0 AND sex = 'zh' AND id != $user_id";
        elseif (in_array('only_mans', $who)) // Только парням
            $sql = "SELECT id FROM users WHERE sex = 'm' AND id != $user_id";
        elseif (in_array('only_girls', $who)) // Только девушкам
            $sql = "SELECT id FROM users WHERE sex = 'zh' AND id != $user_id";
        else
            echo functions::display_error('Yana biror xato topildi...', '<a href="?act=send_mail">Yana urinib ko\'ring</a>');

        $req = mysql_query($sql) or die(mysql_error());
        while ($res = mysql_fetch_assoc($req))
            $arr_des[] = $res['id'];
        if ($me == 1)
            $arr_des[] = $user_id;

        if (!empty($arr_des)) {
            if ($author == 1)
                $msg .= "\n\nHurmat bilan [b]$login [/b]";
            foreach ($arr_des as $des) {
                $sql = "INSERT INTO `cms_mail` SET
                    `user_id` =  0,
                    `from_id` = $des,
                    `text` = '" . mysql_real_escape_string($msg) . "',
                    `time` = " . time() . ",
                    `sys` = 1,
                    `them` = 'Sistema xabari'";
                mysql_query($sql) or die(mysql_error());
            }
            echo '<div class="gmenu">Xabar ' . count($arr_des) . ' foydalanuvchiga yetkazildi!</div>';
            header('Refresh:3; url=?act=send_mail');
        }
        else {
            echo functions::display_error('Bunaqa toifadagi foydalanuvchilar yo\'q...', '<a href="?act=send_mail">Yana urinib ko\'ring</a>');
        }
    }
    else {
        echo functions::display_error($error, '<a href="?act=send_mail">Yana urinib ko\'ring</a>');
    }
}
else {
    echo '<div class="gmenu"><form name="form" action="?act=send_mail" method="post">' .
        '<p><b>Xabar matni (5000):</b></p>' . bbcode::auto_bb('form', 'msg') .
        '<textarea name="msg" rows="' . $set_user['field_h'] . '"></textarea>' .
        '<p><b>Kimga yuborasiz (xat yuboruvchi tanlanmasa xabar yuborilmaydi):</b></p>' .
        '<input type="checkbox" name="me" value="1">&nbsp;Sistema xabari avtoriga yuborish<br>' .
        '<input type="checkbox" name="who[]" value="only_adm">&nbsp;Administratorlarga yuborish<br>' .
        '<input type="checkbox" name="who[]" value="only_usr">&nbsp;Faqat sayt foydalanuvchilariga<br>' .
        '<input type="checkbox" name="who[]" value="all">&nbsp;Hamma sayt foydalanuvchilariga<br>' .
        '<input type="checkbox" name="who[]" value="only_mans">&nbsp;Faqat er bolalarga<br>' .
        '<input type="checkbox" name="who[]" value="only_girls">&nbsp;Faqat ayol qizlarga' .
        '<p><i>* "Faqat er bolalarga" yoki "Faqatt ayol qizlarga" punkti bilan birga "Administratorlarga yuborish" va "Faqat sayt foydalanuvchilariga" punktlarini birga ishlata olasiz</i></p>' .
        '<p><b>Sozlamalar (hech narsa tanlanmaganda xat yuboruvchi belgilanib turadi):</b></p>' .
        '<input type="checkbox" name="author" value="1" checked>&nbsp;Xabar avtorini ko\'rsatish' .
        '<p><input type="submit" value="Yuborish"></p>' .
        '</form></div>';
}

echo '<div class="phdr"><a href="index.php">Orqaga</a></div>';