<?
define('_IN_JOHNCMS', 1);

$headmod ="chat";
$textl = 'Menejer Chat';
$rootpath = '../';
require_once ("../incfiles/core.php");
require_once ("../incfiles/head.php");

if ($rights >= 7) {

if ($id)
$us = mysql_fetch_assoc(mysql_query('select * from `ban_chat` where `id` = ' . $id . ';'));

switch ($act) {
	// Добавляем сообщение
	case 'add':
	if (isset($_POST['submit'])) {
		$message = isset($_POST['message']) ? functions::checkin(mb_substr(trim($_POST['message']), 0, 500)) : '';
		    if (empty($message))
            $error[] = 'Пустое сообщение!';
include ('../incfiles/bantotal.php');
    $flood = functions::antiflood();
    if ($flood)
        $error[] = $lng['error_flood'] . ' ' . $flood . $lng['sec'];
            if ($ban['1'] || $ban['13'])
            $error[] = 'Вы забанены!';
		    if (!$error) {
				mysql_query('INSERT INTO `ban_chat` SET
		        `user_id` = "' . $user_id . '",
		        `message` = "' . mysql_real_escape_string($message) . '",
		        `time` = "' . time() . '";');
				mysql_query('update `users` set `postchat` = `postchat`+1 where `id` = '.$user_id.'');
		        header('location: ?');
			} else {
				echo functions::display_error($error, '<a href="banchat.php">Orqaga</a>');
			}	
	}
	break;
	//Удаляем сообщение
	case 'del':
    if ($us['user_id'] == $user_id || $rights >= 7) {
	    if (isset($_POST['submit'])) {
		    mysql_query('delete from `ban_chat` where `id` = ' . $id . ';');
		    header('location: ?');
	    } else {
echo '<div class="menu"><img src="/ico/home.png"> &raquo; Banchat</div>';
echo '<div class="phdr">Xabar sozlamasi</div>';		  
  echo '<div class="rmenu"><form action="?act=del&amp;id=' . $id . '" method="post">Ushbu xabarni rostan o\'chirasizmi?<br />
            <input type="submit" name="submit" value="O`chirish" /><br /><a href="?">Orqaga</a></form></div>';			
	}
}	
	break;
	// Чистим мини-чат
	case 'clean':
	if ($rights >= 7) {
		if (isset($_POST['submit'])) {
			mysql_query('truncate `ban_chat`');
		    header('location: ?');
		} else {
echo '<div class="menu"><img src="/ico/home.png"> &raquo; Banchat</div>'; 
 echo '<div class="phdr">Xabar sozlamasi</div>';	echo '<div class="rmenu"><form action="?act=clean" method="post">Chatni tozalash<br />
            <input type="submit" name="submit" value="Tozalash" /><br /><a href="?">Orqaga</a></form></div>';	
		}
	}
	break;
    // Редактируем сообщение
	case 'edit':
		if ($us['user_id'] == $user_id || $rights >= 7) {
		$post = mysql_fetch_assoc(mysql_query('select * from `ban_chat` where `id` = ' . $id . ';'));
		if (isset($_POST['submit'])) {
			$message = isset($_POST['message']) ? functions::checkin(mb_substr(trim($_POST['message']), 0, 500)) : '';
            if (empty($message))
            $error[] = 'Пустое сообщение!';
            if ($ban['1'] || $ban['13'])
            $error[] = 'Вы забанены!';
		    if (!$error) {
				mysql_query('update `ban_chat` set `message` = "' . mysql_real_escape_string($message) . '" where `id` = ' . $id . ';');
		        header('location: ?');
			} else {
				echo functions::display_error($error, '<a href="banchat.php">Назад</a>');
			}
		} else {
echo '<div class="menu"><img src="/ico/home.png"> &raquo; Banchat</div>';			
echo '<div class="phdr"><b>Xabarni taxrirlash</b></div>';
			echo '<div class="gmenu"><form action="?act=edit&amp;id=' . $id . '" name="edit" method="post">';
	        echo 'Сообщение (max 500):<br />';
	        echo bbcode::auto_bb('edit', 'message');
	        echo '<textarea rows="' . $set_user['field_h'] . '" name="message">' . $post['message'] . '</textarea><br />' .
'<input type="submit" name="submit" value="Saqlash" /></form></div>';
echo '<div class="menu"><a href="?">Orqaga</a></div>';
		}
	}
	break;
	// Отвечаем юзеру
	case 'otv':
	$user_posti = mysql_fetch_assoc(mysql_query('select * from `ban_chat` where `id` = '.$id.''));
	$user_otv = mysql_fetch_assoc(mysql_query('select * from `users` where `id` = '.$id.''));
echo '<div class="menu"><img src="/ico/home.png"> &raquo; Banchat</div>';	
echo '<div class="phdr"><b>Javob berish '.$user_otv['name'].'\'</b></div>';
	if (isset($_POST['submit'])) {
		$message = isset($_POST['message']) ? functions::checkin(mb_substr(trim($_POST['message']), 0, 500)) : '';
		if (empty($message))
            $error[] = 'Пустое сообщение!';
include ('../incfiles/bantotal.php');
    $flood = functions::antiflood();
    if ($flood)
        $error[] = $lng['error_flood'] . ' ' . $flood . $lng['sec'];
        if ($ban['1'] || $ban['13'])
            $error[] = 'Вы забанены!';
		if (!$error) {
$lo = '[b]'.$user_otv['name'].'[/b], ';
			mysql_query('INSERT INTO `ban_chat` SET
		        `user_id` = "' . $user_id . '",
		        `message` = "' . mysql_real_escape_string($lo.$message) . '",
		        `time` = "' . time() . '";');
				mysql_query('update `users` set `postchat` = `postchat`+1 where `id` = '.$user_id.'');
$oldinga=$id+1;
$theme = 'Menejer Chat';
$text_mail = 'Sizning Chatga qoldirgan izohga javob berishdi. [url='.core::$system_set['homeurl'].'/chat/banchat.php][red][b]Xabarni ko`rish[/b][/red][/url] ';
            mysql_query("INSERT INTO `cms_mail` SET
                    `user_id` = '0',
                    `from_id` = '".$user_otv['id']."',
                    `text` = '" . mysql_real_escape_string($text_mail) . "',
                    `time` = '" . time() . "',
`sys` = '1',
`them` = '" . mysql_real_escape_string($theme) . "'");

		        header('location: ?');
		} else {
			echo functions::display_error($error, '<a href="?act=otv&amp;id='.$id.'">Orqaga</a>');
}
} else {  
echo '<div class="menu"><form action="?act=otv&amp;id='.$user_otv['id'].'" name="form" method="post">' . 'Xabar (max 500):<br />';
 echo bbcode::auto_bb('form', 'message');  
echo '<textarea rows="' . $set_user['field_h'] . '" name="message"></textarea><br />' .
 '<input type="submit" name="submit" value="Yozish" /></form></div>';  
 echo '<div class="menu"><a href="?">Orqaga</a></div>'; 
 }
	break;
	//Цитируем юзера
	case 'cyt':
	$user_post = mysql_fetch_assoc(mysql_query('select * from `ban_chat` where `id` = '.$id.''));
	$user_cyt = mysql_fetch_assoc(mysql_query('select * from `users` where `id` = '.$user_post['user_id'].''));
echo '<div class="menu"><img src="/ico/home.png"> &raquo; Banchat</div>';
	echo '<div class="phdr"><b>Sitata '.$user_cyt['name'].'\'</b></div>';
	if (isset($_POST['submit'])) {
		$message = isset($_POST['message']) ? functions::checkin(mb_substr(trim($_POST['message']), 0, 500)) : '';
		if (empty($message))
            $error[] = 'Пустое сообщение!';
include ('../incfiles/bantotal.php');
    $flood = functions::antiflood();
    if ($flood)
        $error[] = $lng['error_flood'] . ' ' . $flood . $lng['sec'];
        if ($ban['1'] || $ban['13'])
            $error[] = 'Вы забанены!';
		if (!$error) {
			mysql_query('INSERT INTO `ban_chat` SET
		        `user_id` = "' . $user_id . '",
		        `message` = "' . mysql_real_escape_string($message) . '",
				`cid` = "' . $user_post['id'] . '",
		        `time` = "' . time() . '";');
				mysql_query('update `users` set `postchat` = `postchat`+1 where `id` = '.$user_id.'');
$oldinga=$id+1;
$theme = 'Menejer Chat';
$text_mail = 'Sizning Chatga qoldirgan izohga javob berishdi. [url='.core::$system_set['homeurl'].'/chat/banchat.php][red][b]Xabarni ko`rish[/b][/red][/url] ';
            mysql_query("INSERT INTO `cms_mail` SET
                    `user_id` = '0',
                    `from_id` = '".$user_cyt['id']."',
                    `text` = '" . mysql_real_escape_string($text_mail) . "',
                    `time` = '" . time() . "',
`sys` = '1',
`them` = '" . mysql_real_escape_string($theme) . "'");

		        header('location: ?');
		} else {
			echo functions::display_error($error, '<a href="?act=cyt&amp;id='.$id.'">Orqaga</a>');
		}
} else {  
 echo '<div class="menu"><form action="?act=otv&amp;id='.$user_otv['id'].'" name="form" method="post">' . 'Xabar (max 500):<br />';
 echo bbcode::auto_bb('form', 'message'); 
echo '<textarea rows="' . $set_user['field_h'] . '" name="message">[b]'.$user_otv['name'].',[/b]</textarea><br />' . '<input type="submit" name="submit" value="Yozish" /></form></div>';
echo '<div class="menu"><a href="?">Orqaga</a></div>'; 
}
	break;
	// По умолчанию (главная мини-чата)
	default:
echo '<div class="menu"><img src="/ico/home.png"> &raquo; Banchat</div>';
echo '<div class="phdr"><b>Ban Chat</b></div>';
	$who = mysql_result(mysql_query('select count(*) from `users` where `place` = "mchat" and `lastdate`+300 > '.time().''), 0);
echo '<div class="menu"><img src="/ico/user.png" alt="*"/> <a href="online.php">Chatda kimlar bor ??? </a>+'.mysql_result(mysql_query("SELECT COUNT(*) FROM `users` WHERE `place` = 'chat' AND `lastdate` > '".(time() - 300)."'"), 0).'</div>';
echo '<div class="menu"><form action="?act=add" name="add" method="post">';
	    echo 'Xabar (max 500):<br />';
echo bbcode::auto_bb('form', 'message');
	    echo '<textarea rows="' . $set_user['field_h'] . '" name="message"></textarea><br />';
	    echo '<input type="submit" name="submit" value="Yozish" /> | <a href="#"><b>Yangilash</b></a></form></div>';
	
	echo '<div class="phdr"><b>Xabarlar</b></div>';
	$total = mysql_result(mysql_query('select count(*) from `ban_chat`'), 0);
	if ($total) {
		$req = mysql_query('select * from `ban_chat` order by `time` desc limit ' . $start . ', ' . $kmess . ';');
		$i = 0;
		while ($res = mysql_fetch_assoc($req)) {

echo $i % 2 ? '<div class="gmenu">' : '<div class="rmenu">';
			$user = mysql_fetch_assoc(mysql_query('select * from `users` where `id` = ' . $res['user_id'] . ';'));

echo '<table cellpadding="0" cellspacing="0"><tr><td>';

if (file_exists('../files/users/photo/' . $user['id'] . '_small.jpg'))
echo '<div style="float: left; width: 38px;"><img src="'.$home.'/files/users/photo/' . $user['id'] . '_small.jpg" alt="#" style="width: 48px; height: 48px; float: left;"/></div>&#160;';
                else if ($user['sex']) {
echo '<div style="float: left; width: 38px;"><img src="' . $home . '/images/' . ($user['sex'] == 'm' ? 'empty-m' : 'empty-w') . '.png" alt="#" style="width: 48px; height: 48px; float: left;"/></div>&#160;';
                }

echo '</td><td>';

		        if ($user['dayb'] == date('j', time()) && $user['monthb'] == date('n', time()))
                echo functions::image('birthday.png');

$bani = mysql_num_rows(mysql_query("SELECT * FROM `cms_ban_users` WHERE `user_id` = '" . $user['id'] . "' AND `ban_time` > '" . time() . "'"));
if (!empty($bani)) {
echo ' <img src="/images/iconka/ban.png"> ';
} else {
 				if ($user['sex'])
echo (time() > $user['lastdate'] + 300 ? '<img src="../images/iconka/' . ($user['sex'] == 'm' ? 'man' : 'woman') . ($user['rights'] > 7 && $user['rights'] < 10 ? '_sv' : '') . ($user['rights'] > 6 && $user['rights'] < 9 ? '_adm' : '') . ($user['rights'] > 5 && $user['rights'] < 7 ? '_smod' : '') . ($user['rights'] > 4 && $user['rights'] < 6 ? '_lmod' : '') . ($user['rights'] > 3 && $user['rights'] < 5 ? '_dmod' : '') . ($user['rights'] > 2 && $user['rights'] < 4 ? '_fmod' : '') . '_off.gif" width="14" height="14">' : '<img src="../images/iconka/' . ($user['sex'] == 'm' ? 'man' : 'woman') . ($user['rights'] > 7 && $user['rights'] < 10 ? '_sv' : '') . ($user['rights'] > 6 && $user['rights'] < 9 ? '_adm' : '') . ($user['rights'] > 5 && $user['rights'] < 7 ? '_smod' : '') . ($user['rights'] > 4 && $user['rights'] < 6 ? '_lmod' : '') . ($user['rights'] > 3 && $user['rights'] < 5 ? '_dmod' : '') . ($user['rights'] > 2 && $user['rights'] < 4 ? '_fmod' : '') . '_on.gif" width="14" height="14">');
                else
                   echo functions::image('del.png');
}

$orginal= mysql_fetch_array(mysql_query("SELECT `id`, `oplata`, `user_id`, `status`, `img` FROM `forum_group` WHERE `user_id` = ".$user['id'].";"));
if($orginal){ 
echo ' <img src="/medal/group/'.$orginal['img'].'.png" width="13" height="13" style="margin-bottom: -2px;" alt="*"> ';
}

$colornick = mysql_fetch_assoc(mysql_query("select `colornick`,`colornick2` from `users` where `id`='".$user['id']."';"));
$colornickk = mysql_fetch_assoc(mysql_query("select `colornick`,`colornick2` from `users` where `id`='".$user['id']."';"));
echo '<a href="/users/profile.php?user=' . $user['id'] . '"><b>'.functions::gradient('' . $user['name'] . '', '' . $colornick['colornick'] . '',''.$colornick['colornick2'].'').'</b></a>';

$banla = mysql_num_rows(mysql_query("SELECT * FROM `cms_ban_users` WHERE `user_id` = '" . $user['id'] . "' AND `ban_time` > '" . time() . "'"));
if (!empty($banla)) {
echo '<font color="red">[ban]</font>';
} else {
$sek = time() - $user['lastdate']; if ($sek == 0) { $bbb = '<font color="green">[on]</font> '; } elseif ($sek < 60) {  $bbb = '<font color="green">[on]</font>'; } elseif ($sek > 60 && $sek < 120) { $bbb = '<font color="green">[1m]</font>'; } elseif ($sek > 120 && $sek < 180) { $bbb = '<font color="green">[2m]</font>'; } elseif ($sek > 180 && $sek < 240) { $bbb = '<font color="green">[3m]</font>'; } elseif ($sek > 240 && $sek < 300) { $bbb = '<font color="green">[4m]</font>'; } elseif($sek > 300 && $sek < 360) { $bbb = '<font color="green">[5m]</font>'; } elseif ($sek > 360 && $sek < 420) { $bbb = '<font color="green">[6m]</font>'; } elseif ($sek > 420 && $sek < 480) { $bbb = '<font color="green">[7m]</font>'; } elseif ($sek > 480 && $sek < 540) { $bbb = '<font color="green">[8m]</font>'; } elseif ($sek > 540 && $sek < 600) { $bbb = '<font color="green">[9m]</font>'; } else { $bbb = '<font color="red">[off]</font>'; } 
}

echo ' ' . $bbb . ' ';

echo '#<a href="view_mess.php?id='.$res['id'].'"><b>'.$res['id'].'</b></a> ';

			    echo '<br><font color="gray">(' . functions::display_date($res['time']) . ')</font>&#160;<br />';			
				if ($res['cid']) {
					$view = mysql_fetch_assoc(mysql_query('select * from `ban_chat` join `users` on `ban_chat`.`user_id` = `users`.`id` and `ban_chat`.`id` = '.$res['cid'].';'));
					$view_post = functions::checkout($view['message'], 1, 1);
                if ($set_user['smileys'])
                    $view_post = functions::smileys($view_post, $user['rights'] >= 1 ? 1 : 0);
echo '<div class="menu"><b>' . $view['name'] . '</b> &nbsp;(' . functions::display_date($view['time']) . ')<br /> <hr>' . $view_post . '</div>';
				}	
			    $post = functions::checkout($res['message'], 1, 1);
                if ($set_user['smileys'])
                    $post = functions::smileys($post, $user['rights'] >= 1 ? 1 : 0);
				    echo $post;
					if ($user_id != $res['user_id']) {
						echo '<br /><hr />[<a href="?act=otv&amp;id='.$res['user_id'].'">Javob</a>]&#160;[<a href="?act=cyt&amp;id='.$res['id'].'">Sitata</a>]';
					}		
if ($rights == 1 || $rights >= 9) {
						echo '[<a href="?act=del&amp;id=' . $res['id'] . '"><font color="red">O\'chirish</font></a>]';
					}		

$mening_id='1';
if($user_id==$mening_id){
if ($rights == 1 || $rights >= 9) {echo '[<a href="?act=edit&amp;id=' . $res['id'] . '">O\'zgartirish</a>]';}}
			
echo '</td>';	
echo '</td></tr></table></div></div>';
			echo '</div>';
			$i++;
		}
	} else {
		echo '<div class="menu">Xabarlar yo\'q. Birinchi bo\'ling!</div>';
	}
	echo '<div class="phdr">Barchasi: ' . $total . '</div>';
	if ($total > $kmess) {
         echo '<div class="topmenu">' . functions::display_pagination('banchat.php?', $start, $total, $kmess) . '</div>' .
            '<p><form action="banchat.php" method="get"><input type="text" name="page" size="2"/>' .
            '<input type="submit" value="' . $lng['to_page'] . ' &gt;&gt;"/></form></p>';
        }
		
	if ($rights >= 7) {
		echo '<div class="rmenu"><a href="?act=clean">Chatni tozalash</a></div>';
	}
	break;
}

	
}
require_once ("../incfiles/end.php");