<h1 style="text-align: left;">
<img  src="style/img/mesage.png" align="center">Сообщения</h1>
<a href="/?p=mesages">Мои сообщения</a> / <a href="/?p=mesages&t=new">Написать</a><br /><br />
<?php
function mesagesSend($recipient, $text){
global $_SESSION;
	$err = NULL;
		if(db_rows(db_query("SELECT `id` FROM `".DBpref."users` WHERE `login` = '".db_string($recipient)."'")) == 0)
			$err .= "<li>Не верно введен логин получателя</li>";
		if($text == NULL)
			$err .= "<li>Введите текст сообщения</li>";
			
		if($err == NULL){
			db_query("INSERT INTO `".DBpref."mesages` (`sender`, `recipient`, `text`, `dat`, `status`) VALUES ('".db_string($_SESSION["login"])."', '".db_string($recipient)."', '".db_string($text)."', '".date("Y-m-d H:i:s")."', 0)");
			
			echo "
				<script type='text/javascript'>
					location.replace('/?p=mesages&h=".htmlspecialchars($_POST["recipient"], ENT_NOQUOTES, "windows-1251")."');
				</script>
			";
		}else{
			echo "<ul>".$err."</ul>";
		}
}
if($_GET["t"] == "new"){
	
	if(isset($_POST["newok"])){
		db_connect();
		mesagesSend($_POST["recipient"], $_POST["text"]);
		db_close();
	}
	
	?>
	<form action="" method="post">
			<table class="table2">
			  <tr>
				<td>Кому </td>
				<td><input name="recipient" type="text" size="30" maxlength="30" value="<?php echo htmlspecialchars($_POST["recipient"], ENT_NOQUOTES, "windows-1251") ?>" /></td>
			  </tr>
			  <tr>
				<td>Текст</td>
				<td><textarea name="text" cols="50" rows="5"><?php echo htmlspecialchars($_POST["text"], ENT_NOQUOTES, "windows-1251") ?></textarea></td>
			  </tr>
			  <tr>
				<td></td>
				<td><input name="newok" type="submit" value="Отправить" /></td>
			  </tr>
			 </table>		
	</form>
	<?php
}else{
	
	if(isset($_GET["h"])){
		db_connect();
		
		$res = db_query("SELECT * FROM `".DBpref."mesages` WHERE (`sender` = '".db_string($_SESSION["login"])."' AND `recipient` = '".db_string($_GET["h"])."') OR (`sender` = '".db_string($_GET["h"])."' AND `recipient` = '".db_string($_SESSION["login"])."') ORDER BY `id` DESC");
		db_query("UPDATE `".DBpref."mesages` SET `status` = 1 WHERE `sender` = '".db_string($_GET["h"])."' AND `recipient` = '".db_string($_SESSION["login"])."'");
		
		db_close();
		
		if(db_rows($res) == 0){
			echo "<ul><li>История сообщений не найденна</li></ul>";
		}else{
			
			if(isset($_POST["newok"])){
				db_connect();
				mesagesSend($_POST["recipient"], $_POST["text"]);
				db_close();
			}
			
			echo '
				<form action="" method="post">
				<input name="recipient" type="hidden" size="30" maxlength="30" value="'.htmlspecialchars($_GET["h"], ENT_NOQUOTES, "windows-1251").'" /><br />
					Текст: <br />
					<textarea name="text" cols="30" rows="5">'.htmlspecialchars($_POST["text"],ENT_NOQUOTES, "windows-1251").'</textarea><br />
					<input name="newok" type="submit" value="Отправить" />
				</form>
			';
			while($row = db_assoc($res)){
				echo "<br />
					<a target='_blank' href='/?p=wprofile&login=".htmlspecialchars($row["sender"],ENT_NOQUOTES, "windows-1251")."'><strong>".htmlspecialchars($row["sender"], ENT_NOQUOTES, "windows-1251")."</strong></a>(".$row["dat"]."):<br />
					".htmlspecialchars($row["text"],ENT_NOQUOTES, "windows-1251")."<br />
				";
			}
		}
	}else{
		db_connect();
		$res = db_query("SELECT * FROM `".DBpref."mesages` WHERE `sender` = '".db_string($_SESSION["login"])."' OR `recipient` = '".db_string($_SESSION["login"])."' ORDER BY `id` DESC");
		db_close();
		
		
		echo '
			<table class="table3">
			  <tr class="title">
				<td>Кто -> кому</td>
				<td>Сообщение</td>
				<td>Когда</td>
			  </tr>
		';
		if(db_rows($res) == 0) echo "<tr><td colspan='3'><ul class='warning'><li>У Вас нет сообщений</li></ul></td></tr>";
		while($row = db_assoc($res)){
		
		
		//$row["text"] = htmlspecialchars($text,ENT_NOQUOTES, "windows-1251");
		
			if(!isset($NoMsgEcho[$row["sender"].$row["recipient"]]) AND !isset($NoMsgEcho[$row["recipient"].$row["sender"]])){
			echo '<tr><td>'.(($row["recipient"] == $_SESSION["login"] AND $row["status"] == 0)?"(new)":"")."<a target='_blank' href='/?p=wprofile&login=".htmlspecialchars($row["sender"],ENT_NOQUOTES, "windows-1251")."'><strong>".htmlspecialchars($row["sender"],ENT_NOQUOTES, "windows-1251"). "</strong></a> -> <a target='_blank' href='/?p=wprofile&login=".htmlspecialchars($row["recipient"],ENT_NOQUOTES, "windows-1251")."'><strong>".htmlspecialchars($row["recipient"],ENT_NOQUOTES, "windows-1251")."</a></strong> </td><td><a href='/?p=mesages&h=".htmlspecialchars($row["recipient"] == $_SESSION["login"]?$row["sender"]:($row["sender"] == $_SESSION["login"]?$row["recipient"]:""))."'>".substr(htmlspecialchars($row["text"],ENT_NOQUOTES, "windows-1251"),0,50)."...</a></td><td>".$row["dat"]."</td></tr>";
			
			$NoMsgEcho[$row["sender"].$row["recipient"]] = true;}
		}
		echo '</table>';
	}
}
?>