<?
define('_IN_JOHNCMS', 1);
/*
 by Str@nnik
 http://johncms.com/users/profile.php?user=21326
 ICQ: 609745227
*/
$headmod ="chat";
$textl = 'Suhbathona';
$rootpath = '../';
require_once ("../incfiles/core.php");
require_once ("../incfiles/head.php");

// Выгоняем гостей
if (!$user_id) {
echo '<div class="menu"><b>XATO</b><br/>Faqat sayt a\'zolari uchun!</div>';


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

if ($id)
$us = mysql_fetch_assoc(mysql_query('select * from `m_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[] = 'Пустое сообщение!';
            if ($ban['1'] || $ban['13'])
            $error[] = 'Вы забанены!';
		    if (!$error) {
				mysql_query('INSERT INTO `m_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="index.php">Назад</a>');
			}	
	}
	break;
	//Удаляем сообщение
	case 'del':
    if ($us['user_id'] == $user_id || $rights >= 7) {
	    if (isset($_POST['submit'])) {
		    mysql_query('delete from `m_chat` where `id` = ' . $id . ';');
		    header('location: ?');
	    } else {
echo '<div class="menu"><img src="/ico/home.png"> &raquo; Yoqimli chat</div>'; 
echo '<div class="phdr">Xabar o\'chirish</div>';
echo '<div class="menu"><form action="?act=del&amp;id=' . $id . '" method="post">Ushbu xabarni o`chirmoqchimisiz?<br />

<input type="submit" name="submit" value="O`chirish" />      <a href="?">Qaytish</a></form></div>';
	}
}	
	break;
	// Чистим мини-чат
	case 'clean':
	if ($rights >= 7) {
		if (isset($_POST['submit'])) {
			mysql_query('truncate `m_chat`');
		    header('location: ?');
		} else {
echo '<div class="menu"><img src="/ico/home.png"> &raquo; Yoqimli chati</div>';
echo '<div class="phdr">Xabar sozlamasi</div>';	echo '<div class="rmenu"><form action="?act=clean" method="post">Вarcha xabarlarni 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 `m_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 `m_chat` set `message` = "' . mysql_real_escape_string($message) . '" where `id` = ' . $id . ';');
		        header('location: ?');
			} else {
				echo functions::display_error($error, '<a href="index.php">Назад</a>');
			}
		} else {
echo '<div class="menu"><img src="/ico/home.png"> &raquo; Yoqimli chati</div>';			
echo '<div class="phdr"><b>O\'zgartirish</b></div>';
			echo '<div class="menu"><form action="?act=edit&amp;id=' . $id . '" name="edit" method="post">';

echo 'Xabarlar (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_otv = mysql_fetch_assoc(mysql_query('select * from `users` where `id` = '.$id.''));
echo '<div class="menu"><img src="/ico/home.png"> &raquo; Yoqimli chati</div>';	
echo '<div class="phdr"><b> '.$user_otv['name'].'\'ga javob berish</b></div>';
	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('INSERT INTO `m_chat` SET
		        `user_id` = "' . $user_id . '",
		        `message` = "' . mysql_real_escape_string($message) . '",
		        `time` = "' . time() . '";');
						mysql_query("INSERT INTO `chat_javob` SET
					`kim_id` = '" . $user_id . "',
					`kimga_id`  = '" . $user_otv['id'] . "',
					`time` = '" . time() . "',
					`yangi` = '1'") or die (mysql_error());
				mysql_query('update `users` set `postchat` = `postchat`+1 where `id` = '.$user_id.'');
		        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">[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;
	//Цитируем юзера
	case 'cyt':
	$user_post = mysql_fetch_assoc(mysql_query('select * from `m_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; Yoqimli chati</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[] = 'Пустое сообщение!';
        if ($ban['1'] || $ban['13'])
            $error[] = 'Вы забанены!';
		if (!$error) {
			mysql_query('INSERT INTO `m_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.'');
				mysql_query('update `users` set `balans` = `balans`+1 where `id` = '.$user_id.'');
		        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=cyt&amp;id='.$user_post['id'].'" name="form" method="post">' .

'Xabarlar (max 500):<br />';
		echo bbcode::auto_bb('form', 'message');
		echo '<div class="menu"><b>' . $user_cyt['name'] . '</b>&nbsp;(' . functions::display_date($user_post['time']) . ')<br />' . $user_post['message'] . '</div>';

		echo '<textarea rows="' . $set_user['field_h'] . '" name="message"></textarea><br />' .
'<input type="submit" name="submit" value="Sitata javob" /></form></div>';
echo '<div class="menu"><a href="?">Orqaga</a></div>';
	}
	break;
	// По умолчанию (главная мини-чата)
	default:
echo '<div class="menu"><img src="/ico/home.png"> &raquo; Yoqimli chati</div>';
echo '<div class="phdr"><b>'.$textl.'</b>+'.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 bbcode::auto_bb('add', 'message');
echo '<textarea  rows="' . $set_user['field_h'] . '" name="message"></textarea><br />';

echo '<input type="submit" name="submit" value=" Yuborish"/> | <a href="/chat"> <b> Yangilash </b> </a>
<a href="/pages/faq.php?act=smileys"><span class="count float-right">|  Smayllar</span></a><a href="online.php"><span class="count float-right">| Online</span></a></form></div>';
echo '<div class="phdr"> <b> Xabarlar </b> </div>';

	$total = mysql_result(mysql_query('select count(*) from `m_chat`'), 0);
	if ($total) {
		$req = mysql_query('select * from `m_chat` order by `time` desc limit ' . $start . ', ' . $kmess . ';');
		$i = 0;
		while ($res = mysql_fetch_assoc($req)) {
			
			$user = mysql_fetch_assoc(mysql_query('select * from `users` where `id` = ' . $res['user_id'] . ';'));
			
			echo'<div class="menu">';
			  if ($user_id != $res['user_id']) {
echo'<div class="sening">';
                } else {
echo '<div class="mening">';
                }

  $nom = $user['name'];
 $javob = substr($nom, 0, 1);
 $massiv = array(
 '1' => '1.png',
 '2' => '2.png',
 '3' => '3.png',
 '4' => '4.png',
 '5' => '5.png',
 '6' => '6.png',
 '7' => '7.png',
 '8' => '8.png',
 '9' => '9.png',
 '0' => '0.png',
 '$' => '$.png',
 '!' => '!.png',
 '@' => '@.png',
 'A' => 'A.png',
 'a' => 'aa.png',
 'B' => 'B.png',
 'b' => 'bb.png',
 'C' => 'C.png',
 'c' => 'cc.png',
 'D' => 'D.png',
 'd' => 'dd.png',
 'E' => 'E.png',
 'e' => 'ee.png',
 'F' => 'F.png',
 'f' => 'ff.png',
 'G' => 'G.png',
 'g' => 'gg.png',
'H' => 'H.png',
 'h' => 'hh.png',
 'I' => 'I.png',
 'i' => 'ii.png',
 'J' => 'J.png',
 'j' => 'jj.png',
 'K' => 'K.png',
 'kk' => 'kk.png',
 'L' => 'L.png',
 'll' => 'll.png',
 'M' => 'M.png',
 'm' => 'mm.png',
 'N' => 'N.png',
 'n' => 'nn.png',
 'O' => 'O.png',
 'o' => 'oo.png',
 'P' => 'P.png',
 'p' => 'pp.png',
 'Q' => 'Q.png',
 'q' => 'qq.png',
 'R' => 'R.png',
 'r' => 'rr.png',
 'S' => 'S.png',
 's' => 'ss.png',
 'T' => 'T.png',
 't' => 'tt.png',
 'U' => 'U.png',
 'u' => 'uu.png',
 'V' => 'V.png',
 'v' => 'vv.png',
 'X' => 'X.png',
 'x' => 'xx.png',
 'Y' => 'Y.png',
 'y' => 'yy.png',
 'Z' => 'Z.png',
 'z' => 'zz.png',
 'W' => 'W.png',
 'w' => 'ww.png'
 );
 $nick = strtr($javob, $massiv);
	   if ($user_id != $res['user_id']) {
echo'<div class="dlmenu">';
                } else {
echo '<div class="drmenu">';
                }		
if (file_exists((ROOTPATH.'files/users/photo/'.$user['id'].'.jpg'))) {
echo '<a href="'.$home.'/files/users/photo/'.$user['id'].'.jpg"> <img src="'.$home.'/files/users/photo/'.$user['id'].'.jpg" width="70" height="70" alt="'.$user['id'].'" style="background: #ffffff; padding: 3px;border: 1px solid #CCCCE5; border-radius: 5px; float: left;" /></a>';
} else {
echo  '<img src="' .$home.'/images/harf/'.$nick.'" width="70" height="70" alt="'.$user['id'].'" style="background: #ffffff; padding: 3px;border: 1px solid #CCCCE5; border-radius: 5px; float: left;" />&#160;';
}
if ($user['sex'])
echo  (time() > $user['lastdate'] + 2000 ? '<img style="margin-bottom:-2px;" src="../images/iconka' . ($user['sex'] == 'm' ? 'man' : 'woman') . ($user['rights'] > 7 && $user['rights'] < 10 ? '_sv' : '') . ($user['rights'] > 6 && $user['rights'] < 9 ? '_adm' : '') . ($userres['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"  alt=""/>&#160;' : '<img style="margin-bottom:-2px;"  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"  alt=""/> ');
else
echo functions::image('del.png');
$colornick = mysql_fetch_assoc(mysql_query("select `colornick`,`colornick2`,`cfon` from `users` where `id`='".$user['id']."';"));
echo !$user_id || $user_id == $total['id'] ? '<span style="background:'.$cfon.';border-radius:8px 0px 8px 0px;padding:3px 5px 2px 5px;"><b>'.functions::gradient('' . $user['name'] . '', '' . $colornick['colornick'] . '',''.$colornick['colornick2'].'').'</b></span>&#160;' : '<span style="background:'.$colornick['cfon'].';border-radius:8px 0px 8px 0px;padding:3px 5px 2px 5px;"><a href="' . $home . '/users/profile.php?user=' . $user['id'] .'"><b>'.functions::gradient('' . $user['name'] . '', '' . $colornick['colornick'] . '',''.$colornick['colornick2'].'').'</b></span></a>&#160;';
$sek = time() - $user['lastdate'];
if ($sek == 0) {
$bbb = '<span class="sub"><img src="/images/sardor/online.gif" alt="9" /></span>  ';
 } elseif ($sek < 60) {
 $bbb = '<span class="green"> ['.$sek.'s]</span>  ';
 } elseif ($sek > 60 && $sek < 120) {
 $bbb = '<span class="green"> [1m]</span>  ';
 } elseif ($sek > 120 && $sek < 180) {
 $bbb = '<span class="green"> [2m]</span>  ';
 } elseif ($sek > 180 && $sek < 240) {
 $bbb = '<span class="green"> [3m]</span>  ';
 } elseif ($sek > 240 && $sek < 300) {
 $bbb = '<span class="green"> [4m]</span>  ';
 } elseif($sek > 300 && $sek < 360) {
 $bbb = '<span class="green"> [5m]</span>  ';
 } elseif ($sek > 360 && $sek < 420) {
 $bbb = '<span class="green"> [6m]</span>  ';
 } elseif ($sek > 420 && $sek < 480) {
 $bbb = '<span class="green"> [7m]</span>  ';
 } elseif ($sek > 480 && $sek < 540) {
 $bbb = '<span class="green"> [8m]</span>  ';
 } elseif ($sek > 540 && $sek < 600) {
 $bbb = '<span class="green"> [9m]</span>  ';
 }else{
$bbb = '<span><img src="../images/sardor/offline.gif" alt="9" /></span>  ';
 }
echo ' ' . $bbb . '';
echo'<b class="count">' . functions::display_date($res['time']) . '</b>';
echo'<br />';
if ($res['cid']) {
					$view = mysql_fetch_assoc(mysql_query('select * from `m_chat` join `users` on `m_chat`.`user_id` = `users`.`id` and `m_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/> ' . $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 ($res['user_id'] == $user_id){

}
echo'<br />';
if ($rights >= 7){
echo '<span style="font-size: small; float: right;"> <a href="?act=del&amp;id=' . $res['id'] . '"> <font color="red">   [x]   </font> </a></span>';
}
$mening_id='1';
 if($user_id==$mening_id){ 
if ($rights == 1 || $rights >= 9) {echo '<a href="?act=edit&amp;id=' . $res['id'] . '"><span style="font-size: small; float: right;"><font color="green">[O\'z]</font> | </a></span>';}}
if ($user_id != $res['user_id']) {
echo '<a href="?act=otv&amp;id='.$res['user_id'].'"> <b> <span style="font-size: small; float: right;"> <font color="red">JA</font><font color="green">VOB</font> &nbsp; </b></a>';
	
}else{
mysql_query("UPDATE `chat_javob` SET `yangi` = '2' WHERE `kimga_id` = '".$res['user_id']."'");
}

			echo '</div></div>
			       <div style="clear: both;"></div></div>';

			$i++;
		}
	} else {
		echo '<div class="menu">Hali hechkim yozmagan siz birinchi bo\'ling!</div>';
}
if ($total > $kmess) {  
 
 
echo '<center><div class="menu">' . functions::display_pagination('index.php?id=' . $id . '&', $start, $total, $kmess) . '</div></center>'; 
}
	if ($rights >= 7) {
echo '<div class="rmenu"><a href="?act=clean">Xabarlarni Tozalash</a></div>';
	}
	break;
}
require_once ("../incfiles/end.php");