<?

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 (isset($rm)) $takep2="&amp;rm=$rm&amp;ref=$ref";
else $takep2="&amp;ref=$ref";

settype($im, 'integer');
$r = mysql_query ("Select who,topic,date,message,idtowhom,readd,idwho from zapiski WHERE klu4 = '".$im."'");
if (mysql_affected_rows() == 0){ 
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=\"error\" title=\"Ошибка\" ontimer=\"chatmail.php?$ses$takep2\"><timer value=\"10\"/>\n";
echo "<p align=\"center\">\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=\"Refresh\" CONTENT=\"2; URL=chatmail.php?$ses$takep2\">";
echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/></head><body>";
echo "<div align=\"center\">";
}
echo $fsize1;
echo "Сообщение не найдено\n";
echo $fsize2;
if ($ver=="wml")echo "</p></card></wml>";
else echo "</div></body></html>";
mysql_close ($link);  
exit;
}
$a = mysql_fetch_array($r);
if ($a["idtowhom"] != $id){
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=\"error\" title=\"Ошибка\" ontimer=\"chatmail.php?$ses$takep2\"><timer value=\"15\"/>\n";
echo "<p align=\"center\">\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=\"Refresh\" CONTENT=\"2; URL=chatmail.php?$ses$takep2\">";
echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/></head><body>";
echo "<div align=\"center\">";
}
echo $fsize1;
echo "Это сообщение не ваше\n";
echo $fsize2;
if ($ver=="wml")echo "</p></card></wml>";
else echo "</div></body></html>";
mysql_close ($link); 
exit;
}
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=\"read\" title=\"Письмо\">\n";
echo "<do type=\"accept\" name=\"send\" label=\"Прихожая\"><go href=\"enter.php?$ses&amp;ref=$ref\"/></do>\n";
echo "<p>\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\">";
}
$who = $a ["who"];
$idwho = $a ["idwho"];
$topic = $a ["topic"];
$date = $a ["date"];
$message = $a ["message"]; 
$read = $a ["readd"];
$select = mysql_query ("Select id,user from users where id = '".$idwho."'"); 
if (mysql_affected_rows() == 0) {
echo $fsize1;
echo "Юзер не найден<br/>\n"; 
echo $fsize2;
}else{
$inf= mysql_fetch_array ($select);
$usid = $inf["id"];
$user = $inf["user"];
if ($read == 0)mysql_query ("Update zapiski set readd = '1' WHERE klu4 ='".$im."'");  
echo $fsize1;
echo "Вам записка от $user:<br/>\n";
echo "Тема: $topic<br/>\n";
echo "Дата: $date<br/>\n";
echo "Сообщение: $message<br/>\n";
echo $divide;
echo "<a href=\"send.php?$ses&amp;who=$im$takep2\">Ответить</a><br/>\n";
echo $divide;
echo "<a href=\"delmsg.php?s=$s&amp;$ses&amp;im=$im&amp;mod=delall&amp;ininc=1$takep2\">Удалить</a><br/>\n"; 
echo "<a href=\"delmsg.php?s=$s&amp;$ses&amp;usid=$usid&amp;mod=delusermsg&amp;ininc=1$takep2\">Удалить всё от</a> <b>$user</b><br/>\n";
echo $divide;
echo "<a href=\"inbox.php?s=$s&amp;$ses&amp;ref=$ref\">Входящие</a><br/>\n";    
echo $divide;
if (isset($rm)) echo "<a href=\"chat.php?$ses&amp;rm=$rm$takep\">В чат</a><br/>";
echo "<a href=\"enter.php?$ses&amp;ref=$ref\">Прихожая</a><br/>\n"; 
echo $fsize2;
}
if ($ver=="wml")echo "</p></card></wml>";
else echo "</div></body></html>";
mysql_close($link);
?>