<?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';
	$out = '';
	require_once ('../incfiles/class_pclzip.php');
	$req = mysql_query("SELECT * FROM `users` WHERE `id`='" . $id . "';");
	if(mysql_num_rows($req) == 0) {
		$out .= '<div class="rmenu">Пользователя не существует!</div>';
	} else {
		$row = mysql_fetch_assoc($req);
		$out .= '<div class="phdr"><h3>История переписки с ' . $row['name'] . '</h3></div>';
		if ($ban['1']) {
			$out .= '<div class="rmenu">Ваша учетная запись заблокированна. Отправка почты невозможна</div>';
		} else {
			$out .= '<div class="menu"><form enctype="multipart/form-data" method="post" action="' . $home . '/pochta/index.php?act=add&amp;id='.$id.'"><div>';
			$out .= '<strong>Сообщение:</strong> (4-'.$pochta['MAX_MESSAGE'].')<br />
			<textarea rows="4" cols="25" name="text"></textarea><br/>
			Прикрепить файл:<br/>';
			$out .= '<input type="file" name="fail"/><br/>';
			$out .= '<input type="submit" name="submit" value="Написать"/>
			</div></form></div>';
		}
		$total = mysql_result(mysql_query("SELECT COUNT(*) FROM `mail_msg` WHERE `id_user`='" . $user_id . "' AND `id_kont`='" . $id . "' AND `delete`!='" . $user_id . "' AND `sys`='1' AND `spam`='1' OR `id_user`='" . $id . "' AND `id_kont`='" . $user_id . "' AND `delete`!='" . $user_id . "' AND `sys`='1' AND `spam`='1'"), 0);
		if($total > 0) {
			$req = mysql_query("SELECT `mail_msg`.`id` as `mid`, `mail_msg`.`from` as `name`, `mail_msg`.`time` as `tm`, `mail_msg`.*, `users`.* FROM `mail_msg` LEFT JOIN `users` ON `mail_msg`.`id_user`=`users`.`id` WHERE (`id_user`='" . $user_id . "' AND `id_kont`='" . $id . "' OR `id_user`='" . $id . "' AND `id_kont`='" . $user_id . "') AND `delete`!='" . $user_id . "' AND `sys`='1' AND `spam`='1' ORDER BY `mail_msg`.`time` DESC LIMIT $start, $kmess");
			while($row = mysql_fetch_assoc($req)) {
				$out .= ($i % 2) ? '<div class="list2">':'<div class="list1">';
					$text  = checkout($row['msg'], 1, 1);
					if ($set_user['smileys'])
						$text = smileys($text, $res['rights'] >= 1 ? 1 : 0);
					$subtext = '<a href="'.$home.'/pochta/index.php?act=del&amp;id='.$row['mid'].'">Удалить</a>'.($row['id_user']==$user_id || $rights > 7 ? '':' | <a href="index.php?act=isspam&amp;id='.$row['mid'].'">Спам</a>');
					
					if(!empty($row['file']) && file_exists('../files/pochta/'.$row['file'])) {
						$clas = '';
						$exp = end(explode('.', $row['file']));
						switch($exp) {
							case 'jar':
							case 'jad':
							
							$jar = new PclZip('../files/pochta/'.$row['file']);
							$content = $jar->extract(PCLZIP_OPT_BY_NAME, 'META-INF/MANIFEST.MF', PCLZIP_OPT_EXTRACT_AS_STRING);
							if($content) {
								if(preg_match('/(messaging|sms|send|connector\.sms)/i',strtolower($content[0]['content'])))	{
									$clas .= '<br /><span style="color: #FF0000;"><b>Внимание:</b> файл содержит функцию отправки SMS.</span><br/>';
								}
							}
							break;
						}
						$text .= '<div class="func">Файл: <a href="'.$home.'/files/pochta/' . $row['file'] . '">' . $row['file'] . '</a> [' . formatsize(@filesize('../files/pochta/'.$row['file'])) . ']'.$clas.'</div>';
					}
					$out .= show_user($row, 1, ($rights >= 6 && $rights >= $row['rights'] ? 1 : 0), '[' . date('d.m.o в H:i',$row['tm']) . ']', $text, $subtext);
					if($row['id_user']!=$user_id && $row['read']==1)
						mysql_query("UPDATE `mail_msg` SET `read`='2' WHERE `id`='".$row['mid']."' AND `id_user`!='".$user_id."' LIMIT 1");
					$out .= '</div>';
					++$i;
			}
			$out .= '<div class="phdr">Всего: ' . $total . '</div>';
			if ($total > $kmess) {
				$out .= '<div class="menu">' . pagenav($home.'/pochta/index.php?act=history&amp;id='.$id.'&amp;', $start, $total, $kmess);
				$out .= '<form action="" method="post"><p><input type="text" name="page" size="2"/><input type="submit" value="Перейти"/></p></form></div>';
			}
		} else {
			$out .= '<div class="rmenu">Сообщений нет!</div>';
		}
		require_once('../incfiles/head.php');
		echo $out;
		$file_count = mysql_result(mysql_query("SELECT COUNT(*) FROM `mail_msg` WHERE ((`id_user`='" . $user_id . "' AND `id_kont`='".$id."') OR (`id_user`='" . $id . "' AND `id_kont`='" . $user_id . "')) AND `file`!='' AND `delete`!='".$user_id."';"), 0);
		echo '<div class="bmenu"><a href="index.php?act=mfiles&amp;id='.$id.'">Файлы</a> ['.$file_count.']</div>';
		echo '<div class="bmenu"><a href="'.$home.'/index.php?act=cab">Кабинет</a></div>';
	}
	
?>