<?

header("Cache-Control: no-cache");
if ($ver=="wml")header ("Content-type:text/vnd.wap.wml; charset=utf-8");
else header("Content-Type:text/html; charset=UTF-8");

require("inc.php");
$link = connect_db();     
list($row, $id, $ps, $fsize1, $fsize2) = check_login($link);
require("version.php");

if ($rm!=23){

if(isset($HTTP_GET_VARS['rm'])) {$rm = $HTTP_GET_VARS['rm'];}
if (!ctype_digit($rm)) { header("Location: index.php"); die; }
$rm = mysql_escape_string($rm);

mysql_query ("Select rm from rooms where rm='".$rm."';");
if (mysql_affected_rows() == 0){
if ($ver=="wml"){
echo $xml;
echo $dtd;
echo "<wml>";
echo "<card id=\"error\" title=\"Ошибка\" ontimer=\"enter.php?$ses&amp;ref=$ref\"><timer value=\"15\"/>";
echo "<p align=\"center\">";
}else{
echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">";
echo "<html xmlns=\"http://www.w3.org/1999/xhtml\">";
echo "<head><link rel=\"stylesheet\" type=\"text/css\" href=\"css/css.css\"/>";
echo "<title>Ошибка</title>";
echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/></head><body>";
echo "<div align=\"center\">";
}
echo $fsize1;
echo "Такой комнаты не существует!";
echo $fsize2;
if ($ver=="wml")echo "</p></card></wml>";
else echo "</div></body></html>";
mysql_close($link);
exit;
}

$smset = $row["smiles"]; 
$us=$row["user"];
$room="room".$rm;

if($rm==10) $takep="&amp;pwd=$pwd&amp;ref=$ref";
else if($mod=="privat") $takep="&amp;mod=$mod&amp;ref=$ref";
else $takep="&amp;ref=$ref";

$max = $row["max"];    

if ($ver=="wml"){
echo $xml;
echo $dtd;
echo "<wml>\n";
echo "<head><meta http-equiv=\"Cache-Control\" content=\"no-cache\" forua=\"true\"/></head>\n";
echo "<card id=\"history\" title=\"История\">\n";
echo "<do type=\"options\" name=\"refresh\" label=\"Обновить\"><go href=\"chat.php?$ses&amp;rm=$rm$takep\"/></do>\n";
echo "<p mode=\"wrap\">\n"; 
}else{
echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">";
echo "<html xmlns=\"http://www.w3.org/1999/xhtml\">";
echo "<head><link rel=\"stylesheet\" type=\"text/css\" href=\"css/css.css\"/>";
echo "<title>История</title>";
echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/></head><body>";
echo "<div align=\"left\">";
}
echo $fsize1; 
if ($ver=="wml"){
echo "<a href=\"chat.php?$ses&amp;rm=$rm$takep\">Сказать</a><br/>\n";
echo "<a href=\"chat.php?$ses&amp;rm=$rm$takep\">Обновить</a><br/>---\n";
}else{ 
echo "<div class=\"d1\"><a href=\"add.php?$ses&amp;mod=chat&amp;rm=$rm$takep\">Сказать</a></div>\n";
echo "<div class=\"d1\"><a href=\"chat.php?$ses&amp;rm=$rm$takep\">Обновить</a></div>\n";
}
if (!isset($num)) $num=0; 
$bmax = $max*2; 
$bnum = $num+1;

if (empty($pwd)) $pwd=pub;
$pwd=htmlspecialchars(stripslashes(trim($pwd)));

if ($rm == "10")$res = mysql_query ("Select klu4,time,who,message,messagewosm,messagewoasm,id,towhom,hid,usid,pwd,komu from room10 WHERE ((pwd = '".$pwd."')OR(pwd = '')) and ((usid = '".$id."')OR(towhom = '".$id."')OR(towhom = '')) order by id desc LIMIT $bnum,$bmax");
elseif($mod=="privat") $res = mysql_query ("Select klu4,time,who,message,messagewosm,messagewoasm,id,towhom,hid,usid,komu from $room WHERE (usid = '".$id."')OR(towhom = '".$id."') order by id desc LIMIT $bnum,$bmax");
else $res = mysql_query ("Select klu4,time,who,message,messagewosm,messagewoasm,id,towhom,hid,usid,komu from $room WHERE (usid = '".$id."')OR(towhom = '".$id."')OR(towhom = '') order by id desc LIMIT $bnum,$bmax");

$kol = mysql_affected_rows();   
@$total=$kol;
$mread =0; 
while ($mread < $max){ 
$data = mysql_fetch_array ($res);
if($data===false)break;
$komu = $data["komu"];
$date = $data["time"];
$klu4 = $data["klu4"];
$name = $data["who"];
$usid = $data["usid"];
$msg = $data["message"];
if ($smset==0&&$data["messagewosm"]!="") $msg = $data["messagewosm"]; 
if ($smset==2&&$data["messagewoasm"]!="") $msg = $data["messagewoasm"];
$time = $data["id"];
$th = $data["towhom"]; 
$hid = $data["hid"];   
@mysql_query ("Select * from ignor where usid='".$usid."' and id='".$id."'");
if ((mysql_affected_rows() == false)&&(($hid != 2)||($id == $usid))){
$pols = @mysql_query ("SELECT sex FROM users where user='".$name."'");
$zs1 = @mysql_fetch_array ($pols);
$sex = $zs1["sex"];
if ($th == "") {
if (!empty($komu)) {
if ($us==$komu) $komu = "-<b>".$komu."</b>";
else $komu = "-".$komu."";
}
if ($row["level"]<5){
if ($ver=="wml"){
echo "<br/><b><a href=\"inside.php?$ses&amp;rm=$rm&amp;rm=$rm&amp;nk=$usid$takep\">".$name."</a></b>".$komu."(".$date.")\n".$msg."";$mread++;
}else{
if ($sex=="М") {echo "<br/><img src=\"smile/pol_m.gif\" alt=\"М\"/><b><a href=\"inside.php?$ses&amp;rm=$rm&amp;nk=$usid$takep\">".$name."</a></b>".$komu."(".$date.")\n".$msg.""; $mread++;}
else {echo "<br/><img src=\"smile/pol_j.gif\" alt=\"Ж\"/><b><a href=\"inside.php?$ses&amp;rm=$rm&amp;nk=$usid$takep\">".$name."</a></b>".$komu."(".$date.")\n".$msg.""; $mread++;}
}
}else{
if ($ver=="wml"){
echo "<br/><b><a href=\"inside.php?$ses&amp;rm=$rm&amp;nk=$usid$takep\">".$name."</a></b>".$komu."(".$date.")";
}else{
if ($sex=="М") {echo "<br/><img src=\"smile/pol_m.gif\" alt=\"М\"/><b><a href=\"inside.php?$ses&amp;rm=$rm&amp;nk=$usid$takep\">".$name."</a></b>".$komu."(".$date.")";}
else {echo "<br/><img src=\"smile/pol_j.gif\" alt=\"Ж\"/><b><a href=\"inside.php?$ses&amp;rm=$rm&amp;nk=$usid$takep\">".$name."</a></b>".$komu."(".$date.")";}
}
echo "<a href=\"delmess.php?$ses&amp;rm=$rm&amp;klu4=$klu4$takep\">[X]</a>\n".$msg."";$mread++;
}
}
else if (($th == $id)||($id == $usid)){ 
if (!empty($komu)) {
if ($us==$komu) $komu = "-<b>".$komu."</b>";
else $komu = "-".$komu."";
}
if ($row["level"]<5){
if ($ver=="wml"){
echo "<br/><b><a href=\"inside.php?$ses&amp;rm=$rm&amp;nk=$usid$takep\">".$name."</a></b>".$komu."(".$date.")<b>[P!]</b>\n".$msg.""; $mread++;
}else{
if ($sex=="М") {echo "<br/><img src=\"smile/pol_m.gif\" alt=\"М\"/><b><a href=\"inside.php?$ses&amp;rm=$rm&amp;nk=$usid$takep\">".$name."</a></b>".$komu."(".$date.")<b>[P!]</b>\n".$msg.""; $mread++;}
else {echo "<br/><img src=\"smile/pol_j.gif\" alt=\"Ж\"/><b><a href=\"inside.php?$ses&amp;rm=$rm&amp;nk=$usid$takep\">".$name."</a></b>".$komu."(".$date.")<b>[P!]</b>\n".$msg.""; $mread++;}
}
}else{
if ($ver=="wml"){
echo "<br/><b><a href=\"inside.php?$ses&amp;rm=$rm&amp;nk=$usid$takep\">".$name."</a>".$komu."</b>(".$date.")<b>[P!]</b>";
}else{
if ($sex=="М") {echo "<br/><img src=\"smile/pol_m.gif\" alt=\"М\"/><b><a href=\"inside.php?$ses&amp;rm=$rm&amp;nk=$usid$takep\">".$name."</a>".$komu."</b>(".$date.")<b>[P!]</b>";}
else {echo "<br/><img src=\"smile/pol_j.gif\" alt=\"Ж\"/><b><a href=\"inside.php?$ses&amp;rm=$rm&amp;nk=$usid$takep\">".$name."</a>".$komu."</b>(".$date.")<b>[P!]</b>";}
}
echo "<a href=\"delmess.php?$ses&amp;rm=$rm&amp;klu4=$klu4$takep\">[X]</a>\n".$msg.""; $mread++;
}
}
}    
}
mysql_close ($link);
if ($ver=="wml") echo "<br/>---";
else echo "<br/><br/>";
$page_next = $num + $max;
$page_prev = $num - $max;
if($num==0)$total+1;
if ($max < $total) {
if ($ver=="wml") echo "<br/><a href=\"history.php?$ses&amp;rm=$rm&amp;num=$page_next$takep\">След. $max &#8594;</a>";
else echo "<div class=\"d1\"><a href=\"history.php?$ses&amp;rm=$rm&amp;num=$page_next$takep\">След. $max &#8594;</a></div>\n";
}
if ($num >= $max) {
if ($ver=="wml") echo "<br/><a href=\"history.php?$ses&amp;rm=$rm&amp;num=$page_prev$takep\">&#8592; Пред. $max</a>\n";
else echo "<div class=\"d1\"><a href=\"history.php?$ses&amp;rm=$rm&amp;num=$page_prev$takep\">&#8592; Пред. $max</a></div>\n";
}
if ($ver=="wml") echo "<br/><a href=\"enter.php?$ses&amp;ref=$ref\">&#8592; Прихожая</a>\n";
else echo "<div class=\"d1\"><a href=\"enter.php?$ses&amp;ref=$ref\">&#8592; Прихожая</a></div>\n";
echo $fsize2; 
if ($ver=="wml")echo "</p></card></wml>";
else echo "</div></body></html>";
}else{
if(isset($HTTP_GET_VARS['rm'])) {$rm = $HTTP_GET_VARS['rm'];}
if (!ctype_digit($rm)) { header("Location: index.php"); die; }
$rm = mysql_escape_string($rm);

$rmaf = "room".$rm;
$ringame = "ingame".$rm;    
         
$us=$row["user"]; 
$smset = $row["smiles"]; 


mysql_query ("select * from $ringame WHERE gamer_id = '".$id."'");
if (mysql_affected_rows()!=0) $gmes = 1; else $gmes = 0;

$max = $row["max"];    

if ($ver=="wml"){
echo $xml;
echo $dtd;
echo "<wml>\n";
echo "<head><meta http-equiv=\"Cache-Control\" content=\"no-cache\" forua=\"true\"/></head>\n";
echo "<card id=\"history\" title=\"История\">\n";
echo "<do type=\"options\" name=\"refresh\" label=\"Обновить\"><go href=\"chat.php?$ses&amp;rm=$rm$takep\"/></do>\n";
echo "<p mode=\"wrap\">\n"; 
}else{
echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">";
echo "<html xmlns=\"http://www.w3.org/1999/xhtml\">";
echo "<head><link rel=\"stylesheet\" type=\"text/css\" href=\"css/css.css\"/>";
echo "<title>История</title>";
echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/></head><body>";
echo "<div align=\"left\">";
}
echo $fsize1; 
if ($ver=="wml"){
echo "<a href=\"chat.php?$ses&amp;rm=$rm$takep\">Сказать</a><br/>\n";
echo "<a href=\"chat.php?$ses&amp;rm=$rm$takep\">Обновить</a><br/>---\n";
}else{ 
echo "<div class=\"d1\"><a href=\"add.php?$ses&amp;mod=maf&amp;rm=$rm$takep\">Сказать</a></div>\n";
echo "<div class=\"d1\"><a href=\"chat.php?$ses&amp;rm=$rm$takep\">Обновить</a></div>\n";
}
$res = @mysql_query ("Select * from $rmaf order by id desc LIMIT 400");
$kol = mysql_affected_rows();   
@$total=$kol;
if (!isset($num)) $num=0;
$mread = $num; 
for ($i = 1; $i<=$num; $i++) $data = mysql_fetch_array ($res);
$i = $num;
while (($mread < $num+$max)&&($i<=$total)){ 
$data = mysql_fetch_array ($res);
$komu = $data["komu"];
$date = $data["time"];
$name = $data["who"];
$usid = $data["usid"];
$msg = $data["message"];
$gm = $data["gamemes"];
if ($smset==0&&$data["messagewosm"]!="") $msg = $data["messagewosm"]; 
if ($smset==2&&$data["messagewoasm"]!="") $msg = $data["messagewoasm"];
$time = $data["id"];
$th = $data["towhom"]; 
$hid = $data["hid"];
$i++;    
@mysql_query ("Select * from ignor where usid='".$usid."' and id='".$id."'");
if ((mysql_affected_rows() == false)&&(($hid != 2)||($id == $usid))&&($gmes==0||($gmes==1&&$gm==1))){
if (!empty($komu)) {
if ($us==$komu) $komu = "-<b>".$komu."</b>";
else $komu = "-".$komu."";
}
if ($th == "") {
echo "<br/><b><a href=\"inside.php?$ses&amp;nk=$usid&amp;rm=$rm&amp;ref=$ref\">".$name."</a></b>".$komu."(".$date.")\n".$msg.""; $mread++;}
else if (($th == $id)||($id == $usid)){ 
echo "<br/><b><a href=\"inside.php?$ses&amp;nk=$usid&amp;rm=$rm&amp;ref=$ref\">".$name."</a></b>".$komu."(".$date.")<b>[P!]</b>\n".$msg."";$mread++;}
}    
}
mysql_close ($link);
if ($ver=="wml") echo "<br/>---";
else echo "<br/><br/>";
$page_next = $num + $max-1;
$page_prev = $num - $max+1;
if ($num + $max < $total) {
if ($ver=="wml") echo "<br/><a href=\"history.php?$ses&amp;rm=$rm&amp;num=$page_next\">След. $max &#8594;</a>";
else echo "<div class=\"d1\"><a href=\"history.php?$ses&amp;rm=$rm&amp;num=$page_next\">След. $max &#8594;</a></div>\n";
}
if ($num >= $max) {
if ($ver=="wml") echo "<br/><a href=\"history.php?$ses&amp;rm=$rm&amp;num=$page_prev\">&#8592; Пред. $max</a>\n";
else echo "<div class=\"d1\"><a href=\"history.php?$ses&amp;rm=$rm&amp;num=$page_prev\">&#8592; Пред. $max</a></div>\n";
}
if ($ver=="wml") echo "<br/><a href=\"enter.php?$ses&amp;ref=$ref\">&#8592; Выйти из игры</a>\n";
else echo "<div class=\"d1\"><a href=\"enter.php?$ses&amp;ref=$ref\">&#8592; Выйти из игры</a></div>\n";
echo $fsize2; 
if ($ver=="wml")echo "</p></card></wml>";
else echo "</div></body></html>";
}
?>