<?
session_start();
if(empty($login)){
print "<script>top.location.href='index.php';</script>";
}
?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1251">
<meta http-equiv="Content-Language" content="ru">
</head>
<body onLoad="parent.msg.window.scroll(0, 9999999)" topMArgin=0>
<?
print "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"10\"; URL=\"refresh.php\">\n";
include "conf.php";
include "on.php";
include "functions.php";
if (ereg("[<>\\/-]",$log)) {print "?!"; exit();}
$log=htmlspecialchars($log);
if($break == 1){
print "<script>top.location.href='index.php'</script>";
}

$data = mysql_connect($base_name, $base_user, $base_pass);
mysql_select_db($db_name,$data);
$sql = "SELECT * FROM users WHERE login='$login'";
$result = mysql_query($sql);
$db = mysql_fetch_array($result);



$base_room=$db['room'];
$city=$db['city_game'];

testZayavka($login);

if($db['battle']!=0){
print "<script>parent.msg.innerHTML+='".$_SESSION['battle_ref']."<BR>'</script>";
        if($db['battle_opponent'] != "" && $_SESSION['battle_ref'] == 0){
         //print "<script>top.main.location.reload()</script>";
     $_SESSION['battle_ref'] = 1;
     }
}

/*================ГЕНЕРАЦИЯ СПИСКА ЧАТОВЦЕВ=================*/
$online = 0;

        $l_sm       = "SELECT * FROM users WHERE login='Смотритель'";
        $ll_sm      = mysql_query($l_sm);
        $res_sm     = mysql_fetch_array($ll_sm);
        $level_sm     = $res_sm['level'];
        $orden_sm = $res_sm['orden'];
        $clan_s_sm  = $res_sm['clan_short'];
        $clan_f_sm  = $res_sm['clan'];
        $rang_sm    = $res_sm["rang"];

                if($orden_sm==1){$orden_dis_sm="Белое братство";}
                else if($orden_sm==2){$orden_dis_sm="Темное братство";}
                else if($orden_sm==3){$orden_dis_sm="Нейтральное братство";}
                else if($orden_sm==4){$orden_dis_sm="Алхимик";}
                else if($orden_sm==5){$orden_dis_sm="Тюремный заключеный.";}
                if(empty($clan_s_sm)){$clan_sm="";}
                else{$clan_sm="<img src='img/clan/$clan_s_sm.gif' border=0 alt='$clan_f_sm'>";}
                if(empty($orden_sm)){$orden_sm="";}
	        else{
if ($orden_sm==2) {$orden_sm="<img src='img/orden/arm/$rang_sm.gif' width=12 height=15 border=0 alt='$orden_dis_sm'>";} 
else{$orden_sm="<img src='img/orden/$orden_sm.gif' border=0 alt='$orden_dis_sm'>";}
if ($orden_sm==1) {$orden_sm="<img src='img/orden/pal/$rang_sm.gif' width=12 height=15 border=0 alt='$orden_dis_sm'>";} }



$R_ONLINE = mysql_query("SELECT * FROM online WHERE room ='$room'");
$room_online = 1;
        while(mysql_fetch_array($R_ONLINE)){
        $room_online++;
        }

$user_list = "<center><INPUT TYPE='submit' style='border: solid 1pt #B0B0B0; font-family: MS Sans Serif; font-size: 10px; color: #191970; MARGIN-BOTTOM: 2px; MARGIN-TOP: 1px;' value='Обновить' onclick=javascript:top.frames['ref'].window.location='refresh.php';></center><font style='COLOR:#8f0000;FONT-SIZE:10pt'><B>".$db['room']." ($room_online)</B></font><br>";


$user_list .= "&nbsp<a href=javascript:private('Смотритель')><img border=0 src=img/lock.gif alt='Приватное сообщение'></a>&nbsp$orden_sm$clan_sm<a href=javascript:AddTo(\'Смотритель\'); class=nick><b>Смотритель</b></a>[$level_sm]<a href=info.php?log=Смотритель target=_blank><img border=0 src=img/inf.gif alt='Информация о Смотритель'></A><BR>";

$SEARCH = mysql_query("SELECT * FROM online WHERE login = '$login'");

        if(mysql_fetch_array($SEARCH)){$online = 1;}

     if(empty($ip))
     {
               if (getenv('HTTP_X_FORWARDED_FOR'))
                {
                        $ip=getenv('HTTP_X_FORWARDED_FOR');
                }
                       else
                {
                        $ip=getenv('REMOTE_ADDR');
                }
     }

        if($online == 0){
        $time = time();
        $INSERT = mysql_query("INSERT INTO online(ip,city,room,login,last_time) VALUES('$ip','$city','$room','$login','$time')");
        }
        else if($online == 1){
        $time = time();
        $UPDATE = mysql_query("UPDATE online SET last_time = '$time',room='$room',ip='$ip' WHERE login = '$login'");
        }

$CLEAR = mysql_query("SELECT * FROM online");

        while($DATA = mysql_fetch_array($CLEAR)){
        $min_time = time() - $DATA['last_time'];
                if($min_time>20){
                $CLEAR_LOGIN = $DATA['login'];
                $DELETE = mysql_query("DELETE FROM online WHERE login='$CLEAR_LOGIN'");
                }
        }


$LIST = mysql_query("SELECT * FROM online WHERE city='$city' AND room='$room' ORDER by login ASC");
        while($DATA = mysql_fetch_array($LIST)){
        $log1 = $DATA['login'];
        $l       = "SELECT * FROM users WHERE login='$log1'";
        $ll      = mysql_query($l);
        $res     = mysql_fetch_array($ll);
        $lev     = $res['level'];
        $orden_d = $res['orden'];
        $clan_s  = $res['clan_short'];
        $clan_f  = $res['clan'];
        $travm   = $res['travm'];
        $log     = $res['login_sec'];
        $bid     = $res['battle'];
        $rang    = $res["rang"];



        if($res['shut']!=0){
        $mol = "<img src='img/sleep2.gif' alt='Наложено заклятие молчания'>";
        }
        else{$mol="&nbsp";}

        if($res['travm']!=0){
        $travm_i = "<img src='img/travma2.gif' alt='У персонажа травма'>";
        }
        else{$travm_i="&nbsp";}

                if($orden_d==1){$orden_dis="Белое братство";}
                else if($orden_d==2){$orden_dis="Темное братство";}
                else if($orden_d==3){$orden_dis="Нейтральное братство";}
                else if($orden_d==4){$orden_dis="Алхимик";}
                else if($orden_d==5){$orden_dis="Тюремный заключеный.";}
                if(empty($clan_s)){$clan="";}
                else{$clan="<img src='img/clan/$clan_s.gif' border=0 alt='$clan_f'>";}
                if(empty($orden_d)){$orden="";}
	        else{
if ($orden_d==2) {$orden="<img src='img/orden/arm/$rang.gif' width=12 height=15 border=0 alt='$orden_dis'>";} 
else{$orden="<img src='img/orden/$orden_d.gif' border=0 alt='$orden_dis'>";}
if ($orden_d==1) {$orden="<img src='img/orden/pal/$rang.gif' width=12 height=15 border=0 alt='$orden_dis'>";} }

        $login_hr = str_replace(" ","%20",$log);
        $log2 = str_replace(" ","%20",$log);
        $logn = str_replace(" ","%20",$log1);

        $user_list .= "&nbsp<a href=javascript:private('$login_hr')><img border=0 src=img/lock.gif alt='Приватное сообщение'></a>&nbsp$orden$clan<a href=javascript:AddTo('$log2'); class=nick><b>$log</b></a>[$lev]<a href=info.php?log=$logn target=_blank><img border=0 src=img/inf.gif alt='Информация о $log'></A> $travm_i$mol<br>";
        }

?>
<script language="JavaScript">
if (parent.user.user_list){
parent.user.user_list.innerHTML = "<?echo $user_list?>";
}
</script>
<?
/*==========================================================*/
$chat = "";

if($db['chat_s']!=2){

        $chas = date("H");
        $minute = date("i");
        $mes = date("m");
        $dat = date("d");
        $year = date("Y");

        $dname=date("d.m.Y.H", mktime($chas-$GSM));

$S = mysql_query("SELECT * FROM chat");
$c = mysql_num_rows($S);

        if(empty($act)){$act="";}

$last = $_SESSION['last'];

        if(empty($last))
        {
        $_SESSION["last"] = time();
        }

$sm=array();
$sm[0]='face1';
$sm[1]='face2';
$sm[2]='face3';
$sm[3]='face4';
$sm[4]='face5';
$sm[5]='fingal';
$sm[6]='evil';
$sm[7]='batman';
$sm[8]='adolf';
$sm[9]='am';
$sm[10]='angel';
$sm[11]='cool';
$sm[12]='coolman';
$sm[13]='crazy';
$sm[14]='devil';
$sm[15]='aplause';
$sm[16]='ha';
$sm[17]='help';
$sm[18]='happy';
$sm[19]='hello';
$sm[20]='ill';
$sm[21]='hummer2';
$sm[22]='music';
$sm[23]='newyear';
$sm[24]='ogo';
$sm[25]='police';
$sm[26]='police2';
$sm[27]='prise';
$sm[28]='punk';
$sm[29]='ravvin';
$sm[30]='ravvin2';
$sm[31]='rupor';
$sm[32]='scare';
$sm[33]='king';
$sm[34]='sleep';
$sm[35]='song';
$sm[36]='strong';
$sm[37]='student';
$sm[38]='goodnigth';
$sm[39]='fuu';
$sm[40]='girl';
$sm[41]='inlove';
$sm[42]='kiss1';
$sm[43]='lick';
$sm[44]='lips';
$sm[45]='two';
$sm[46]='pare';
$sm[47]='fuck';
$sm[48]='dinner';
$sm[49]='friday';
$sm[50]='drink';
$sm[51]='beer';
$sm[52]='cola';
$sm[53]='killed';
$sm[54]='throwout';
$sm[55]='boxing';
$sm[56]='duel';
$sm[57]='gun1';
$sm[58]='gun2';
$sm[59]='gun_1';
$sm[60]='hummer';
$sm[61]='jack';
$sm[62]='kill';
$sm[63]='knut';
$sm[64]='matrix';
$sm[65]='med';
$sm[66]='ninja';
$sm[67]='nunchak';
$sm[68]='t2';
$sm[69]='terminator';
$sm[70]='training';
$sm[71]='trio';
$sm[72]='user';
$sm[73]='censored';
$sm[74]='compkill';
$sm[75]='helloween';
$sm[76]='lock';
$sm[77]='lol';
$sm[78]='loo';
$sm[79]='mol';
$sm[80]='nuclear';
$sm[81]='yo';
$sm[82]='dollar';
$sm[83]='heart';
$sm[84]='luck';
$sm[85]='mac';
$sm[86]='win';
$sm[87]='rip';
$sm[88]='student';
$sm[89]='bye';
$sm[90]='baby';
$sm[91]='man_hat';
$sm[92]='music';


// print "$last";
$GET = mysql_query("SELECT * FROM chat WHERE date_stamp>$last AND room='$base_room' AND city='$city'"); 
while($DATA = mysql_fetch_array($GET)){
$msg = $DATA['msg'];
$msgto = $DATA['msg'];
$date = $DATA['date'];
$sender = $DATA['name'];
$_SESSION['last'] = $DATA['date_stamp'];

$sender=str_replace("sys","<span class=sysm>",$sender);
$sender2=str_replace(" ","%20",$sender);
$date=str_replace("begSys","<span class=sysm>",$date);
$msg=str_replace("begSys2","</span><span class=sysm>",$msg);
$msg=str_replace("endSys2","</span>",$msg);
$msg=str_replace("private [","<a href=javascript:private('$sender2')><span class=private>private [",$msg);
$msg=str_replace("]","]</a></span>",$msg);
$msg=str_replace("endSys","</span>",$msg);
$msgto=str_replace("]","]</b></a><font class=msg>",$msgto);

        for($g=0;$g<count($sm);$g++){
        $msg=str_replace(":{$sm[$g]}:","<img src='img/smile/{$sm[$g]}.gif' border=0>",$msg);
        }
        for($g=0;$g<count($sm);$g++){
        $msgto=str_replace(":{$sm[$g]}:","<img src='img/smile/{$sm[$g]}.gif' border=0>",$msgto);
        }

$to    = "to [$login]";
$private = "private [$login]";
$sys = "SysMs";
$mystring = $msg;
$pos = strpos($mystring, $to);
$pos2 = strpos($mystring, $private);
$pos4 = strpos($mystring, $sys);
$comstring = "[$login] $msg";
$comstring2 = "to [$sender] $msg";
echo "результат поиска $sys в $mystring: pos4=$pos4";
	if($pos2!=false){
        $sender2=str_replace(" ","%20",$sender);
        $chat.= "<font class=date2>$date</font> [<a href=javascript:AddTo('$sender2')><span class=nick>$sender</span></a>] <font class=msg>$msg</font><BR>";
        }
        if ($pos != false) {
        if(ereg("to \[$login\]",$msgto))
        {
        $col = mysql_query("SELECT * FROM users WHERE login='$sender'");
        $res_col = mysql_fetch_array($col);
        $col_sen = $res_col['color'];
        $msgto = str_replace("to [$login]","<span style='color: $col_sen;'>to [$login]</span>",$msgto);
        }
        $sender2=str_replace(" ","%20",$sender);
        $chat.= "<font class=date2>$date</font> [<a href=javascript:AddTo('$sender2')><span class=nick>$sender</span></a>] <a href=javascript:AddTo('$sender2')><b>$msgto</font><BR>";
        }

        else if($pos4 != false){
                if($sender=="toall"){
                $msg=str_replace($sys,"<span class=sysm>&nbsp",$msg);
                $msg=str_replace("endSys","</span>",$msg);
                $chat.= "<font class=date>$date</font>$msg<BR>";
                }
                else if($login!=$sender){
                $chat.= "";
                }
               else if($login==$sender){
                $msg=str_replace($sys,"<span class=sysm>&nbsp",$msg);
                $msg=str_replace("endSys","</span>",$msg);
                $chat.= "<font class=date2>$date</font>$msg<BR>";
                }

        }
        else{
        $p = "private [";
        $pos3=strpos($mystring,$p);
                if($pos3!=false and $sender!=$login){
                print "";
                }
                else if($db['chat_s']==0){
                $sender2=str_replace(" ","%20",$sender);
                $msg=str_replace("private [","<a href=javascript:private('$sender2')><span class=private>private [",$msg);
                $msg=str_replace("]","]</span></a>",$msg);
                if (!empty($sender)){$name="[<a href=javascript:AddTo('$sender2')><span class=nick>$sender</span></a>]";} else {$name="";}
                $chat.= "<font class=date>$date</font> $name <font class=msg>$msg</font><BR>";
                }
        else if($db['chat_s']!=0 && $sender==$login){
                   $sender2=str_replace(" ","%20",$sender);
                $msg=str_replace("private [","<a href=javascript:private('$sender2')><span class=private>private [",$msg);
                $msg=str_replace("]","]</span></a>",$msg);
                $chat.= "<font class=date>$date</font> [<a href=javascript:AddTo('$sender2')><span class=nick>$sender</span></a>] <font class=msg>$msg</font><BR>";
                }

        }

}

// echo $chat;
?>

<script language="JavaScript">
if (parent.msg.mes){
tmp = parent.msg.mes.innerHTML;
parent.msg.mes.innerHTML += "<?echo $chat?>";
}
</script>
<?
}
else if($db['chat_s']==2){
 $_SESSION['last'] = time();
}
?>
