<?php
	/*
	* Модуль почты для JohnCMS 3.2.2 v2.6
	* Copyright © 2010-2011 UPcoder.RU Group All Rights Reserved.
	* Coding by Krite 2010-2011
	*/
	defined('_IN_JOHNCMS') or die('Error: restricted access');
	$textl = 'Удаление сообщений';
	$headmod = 'pochta';
	require_once('../incfiles/head.php');
	echo '<div class="phdr"><h3>Удаление сообщения</h3></div>';
		if (mysql_result(mysql_query("SELECT COUNT(*) FROM `mail_msg` WHERE " . ($rights>=7 ? '':'`id_user` = ' . $user_id . ' AND `id`=' . $id . ' or `id_kont`=' . $user_id . ' AND')." `id`='" . $id . "'"), 0)==0) {
			echo '<div class="rmenu">Сообщения не существует</div>';
			echo '<div class="menu"><a href="'.$home.'/pochta/">Назад</a></div>';
		}
		else {
			$row = mysql_fetch_assoc(mysql_query("SELECT * FROM `mail_msg` WHERE ".($rights>=7 ? '':'`id_user`=' . $user_id . ' AND `id`=' . $id . ' or `id_kont`=' . $user_id . ' AND')." `id`='" . $id . "';"));
			if(!isset($_POST['submit'])) {
				echo '<div class="rmenu">Подтвердите удаление</div>';
				echo '<form action="'.$home.'/pochta/index.php?act=del&amp;id=' . $id . '" method="post"><div class="menu">
				<input type="submit" name="submit" value="Удалить"/></div></form>';
				echo '<div class="menu"><a href="'.$home.'/pochta/">Почта</a></div>';
			}
			else {
				if($row['spam']==2 && $rights>=7) {
					mysql_query("DELETE FROM `mail_msg` WHERE `spam`='2' AND `id`='".$id."' LIMIT 1;");
					mysql_query("OPTIMIZE TABLE `mail_msg`;");
					if($row['file'])
						@unlink('../files/pochta/' . $row['file']);
				}
				else {
					if($row['sys']==2) {
						mysql_query("DELETE FROM `mail_msg` WHERE `id_kont`='" . $user_id . "' AND `sys`=2 AND `delete`!='" . $user_id . "' AND `id`='$id' LIMIT 1;");
						mysql_query("OPTIMIZE TABLE `mail_msg`;");
						if($row['file']) 
							@unlink('../files/pochta/' . $row['file']);
					}
					else if(!empty($row['delete']) && $row['delete'] != $user_id) {
						mysql_query("DELETE FROM `mail_msg` WHERE `id`='".$id."' AND `id_user`='$user_id' OR `id`='$id' AND `id_kont`='" . $user_id . "' LIMIT 1;");
						mysql_query("OPTIMIZE TABLE `mail_msg`;");
						if($row['file']) 
							@unlink('../files/pochta/' . $row['file']);
					}
					else {
						mysql_query("UPDATE `mail_msg` SET
						`delete`='".$user_id."',
						`read`='".($row['id_user']==$user_id ? 1:2)."'
						WHERE `id`='".$id."' AND `id_user`='$user_id' or `id`='".$id."' AND `id_kont`='" . $user_id . "';");
					}
				}
				echo '<div class="menu">Сообщение удалено</div>';
				echo '<div class="menu"><a href="'.$home.'/pochta/">Почта</a></div>';
			}
		}
?>