<?php

defined('_IN_JOHNADM') or die('Error: restricted access');

// Проверяем права доступа
if ($rights < 9) {
    header('Location: /?err');
    exit;
}
// Нашел в интернете функцию скругления размера файла
function formatSize($size) {
	$filesizename = array("Bytes", "Kb", "Mb", "Gb", "Tb");
	return $size ? round($size/pow(1024, ($i = floor(log($size, 1024)))), 2) . $filesizename[$i]: '0' . $filesizename[0];
}
// Функция подсветки результатов запроса
function ReplaceKeywords($search, $text)
{
    $search = str_replace('*', '', $search);
    return mb_strlen($search) < 3 ? $text : preg_replace('|(' . preg_quote($search, '/') . ')|siu', '<span style="background-color: #FFFF33">$1</span>', $text);
}

echo '<div class="phdr"><a href="index.php"><b>' . $lng['admin_panel'] . '</b></a> | Pochta kuzatuvi</div>';

if (isset($_GET['delete'])) {
	mysql_query('delete from `cms_mail` where `id` = '.$id.'');
	echo '<div class="gmenu">Xabar o\'chirildi!</div>';
}

switch ($mod) {
	case 'track':
	$total = mysql_result(mysql_query('select count(*) from `cms_mail` where `sys` = 0 and `user_id` = '.$id.';'), 0);
	if ($total) {
		$req = mysql_query('select * from `cms_mail` where `sys` = 0 and `user_id` = '.$id.' order by `time` desc;');
		$i = 0;
	    while ($res = mysql_fetch_assoc($req)) {
		    $User = mysql_fetch_assoc(mysql_query('select * from `users` where `id` = '.$res['user_id'].''));
		    $from = mysql_fetch_assoc(mysql_query('select * from `users` where `id` = '.$res['from_id'].''));
		    echo $i % 2 ? '<div class="list1">' : '<div class="list2">';
		    echo '<b>Yuboruvchi:</b> <a href="' . $home . '/users/profile.php?user=' . $User['id'] . '">' . $User['name'] . '</a><br />
		        <b>Kimga:</b> <a href="' . $home . '/users/profile.php?user=' . $from['id'] . '">' . $from['name'] . '</a><br />
				<b>Vaqt:</b> ' . functions::display_date($res['time']) . '<br />';
		    $message = functions::checkout($res['text'], 1, 1);
            if ($set_user['smileys'])
                $message = functions::smileys($message, $user['rights'] >= 1 ? 1 : 0);
		    echo '<b>Xabar:</b> ' . $message . '<br />';
			if ($res['file_name']) {
				$size = $res['size'];
                echo '<div class="func">Fayl: <a href="../mail/index.php?act=load&amp;id=' . $res['id'] . '">' . $res['file_name'] . '</a> (' . formatSize($size) . ')(' . $res['count'] . ')</div>';
			}	
		    echo '<a href="?act=spy_mail&amp;delete&amp;id='.$res['id'].'">O\'chirish</a>';
		    echo '</div>';
		    $i++;
		}
		if ($total > $kmess) {
            echo '<div class="topmenu">' . functions::display_pagination('index.php?act=spy_mail&amp;mod=track&amp;id='.$id.'&amp;', $start, $total, $kmess) . '</div>';
            echo '<p><form action="index.php?act=spy_mail&amp;mod=track&amp;id='.$id.'" method="post"><input type="text" name="page" size="2"/><input type="submit" value="' . $lng['to_page'] . ' &gt;&gt;"/></form></p>';
        }
	} else {
		echo '<div class="menu">Bo\'sh</div>';
	}
	if ($total)
	echo '<div class="rmenu"><a href="?act=spy_mail&amp;mod=clean_usr&amp;id='.$id.'">Foydalanuvchi pochtasini o\'chirish</a></div>';
	echo '<div class="menu"><a href="?act=spy_mail">Asosiyga</a></div>';
	break;
	
	case 'search':
	if (isset($_POST['submit'])) {
	    $search = htmlspecialchars(mysql_real_escape_string(trim($_POST['search'])));
		$array = explode(' ', $search);
		
		$error = array();
		if (mb_strlen($search) < 3)
			$error[] = 'Juda kalta so\'rov!';
		if (mb_strlen($search) > 40)
			$error[] = 'Juda uzun so\'rov!';
		if (mb_strlen($search) == 0)
			$error[] = 'Bo\'sh so\'rov!';
		if (!$error) {
			$req = mysql_query("select * from `cms_mail` where `text` like '%$search%' order by `time` desc");
			//$a = mysql_fetch_assoc($req);
			//print_r($a);
			$total = mysql_num_rows($req);
			if (mysql_num_rows($req) > 0) {
				echo '<div class="gmenu">Qidirish natijalari <b>'.$search.'</b></div>';
				$i = 0;
				while ($res = mysql_fetch_assoc($req)) {
					$User = mysql_fetch_assoc(mysql_query('select * from `users` where `id` = '.$res['user_id'].''));
					$from = mysql_fetch_assoc(mysql_query('select * from `users` where `id` = '.$res['from_id'].''));
					echo $i % 2 ? '<div class="list1">' : '<div class="list2">';
					echo '<b>Yuboruvchi:</b> <a href="' . $home . '/users/profile.php?user=' . $User['id'] . '">' . $User['name'] . '</a> <a href="?act=spy_mail&amp;mod=track&amp;id=' . $User['id'] . '">[#]</a><br />
						<b>Kimga:</b> <a href="' . $home . '/users/profile.php?user=' . $from['id'] . '">' . $from['name'] . '</a> <a href="?act=spy_mail&amp;mod=track&amp;id=' . $from['id'] . '">[#]</a><br />
						<b>Vaqti:</b> ' . functions::display_date($res['time']) . '<br />';
					$message = functions::checkout($res['text'], 1, 1);
					if ($set_user['smileys'])
						$message = functions::smileys($message, $user['rights'] >= 1 ? 1 : 0);
					foreach ($array as $val) {
                        $message = ReplaceKeywords($val, $message);
                    }
					echo '<b>Xabar:</b> ' . $message . '<br />';
					if ($res['file_name']) {
						$size = $res['size'];
						echo '<div class="func">Fayl: <a href="../mail/index.php?act=load&amp;id=' . $res['id'] . '">' . $res['file_name'] . '</a> (' . formatSize($size) . ')(' . $res['count'] . ')</div>';
					}	
					echo '<a href="?act=spy_mail&amp;delete&amp;id='.$res['id'].'">O\'chirish</a>';
					echo '</div>';
					$i++;
				}
				echo '<div class="phdr">Hammasi: '.$total.'</div>';
			} else {
				echo '<div class="menu">Sizning so\'rov bo\'yicha hech nima topilmadi</div>';
			}
		} else {
			echo functions::display_error($error, '<a href="?act=spy_mail&amp;mod=search">Orqaga</a>');
		}
	} else {
		echo '<div class="gmenu"><form name="search" method="post" action="?act=spy_mail&amp;mod=search">
            <input type="search" name="search" placeholder="Поиск">
            <input type="submit" name="submit" value="Поиск" />
            </form></div>';
	}
	
	echo '<div class="menu"><a href="?act=spy_mail">Pochta kuzatuviga</a></div>';
	break;
	
	case 'clean':
	if (isset($_GET['yes'])) {
		mysql_query('truncate table `cms_mail`');
		header('location: ?act=spy_mail');
	} else {
		echo '<div class="rmenu">Siz rostdan ham barcha pochta xabarlarini o\'chirmoqchimisiz?<br />
		        <a href="?act=spy_mail&amp;mod=clean&amp;yes">O\'chirish</a> | <a href="?act=spy_mail">Bekorlash</a></div>';
	}
	break;
	
	case 'clean_usr':
	$usr = mysql_fetch_assoc(mysql_query('select * from `users` where `id` = '.$id.';'));
	if (isset($_GET['yes'])) {
		mysql_query('delete from `cms_mail` where `sys` = 0 and `user_id` = '.$id.';');
		header('location: ?act=spy_mail');
	} else {
		echo '<div class="rmenu">Siz rostdan ham '.$usr['name'].' ning barcha pochta xabarlarini o\'chirmoqchimisiz?<br />
		        <a href="?act=spy_mail&amp;mod=clean_usr&amp;id='.$id.'&amp;yes">O\'chirish</a> | <a href="?act=spy_mail">Bekorlash</a></div>';
	}
	break;
	
	default:
	echo '<div class="gmenu"><a href="?act=spy_mail&amp;mod=search"><small>Qidirish</small></a></div>';
	$total = mysql_result(mysql_query('select count(*) from `cms_mail` where `sys` = 0'), 0);
    if ($total){
		$req = mysql_query('select * from `cms_mail` where `sys` = 0 order by `time` desc limit ' . $start . ', ' . $kmess . ';');
	    $i = 0;
	    while ($res = mysql_fetch_assoc($req)) {
		    $User = mysql_fetch_assoc(mysql_query('select * from `users` where `id` = '.$res['user_id'].''));
		    $from = mysql_fetch_assoc(mysql_query('select * from `users` where `id` = '.$res['from_id'].''));
		    echo $i % 2 ? '<div class="list1">' : '<div class="list2">';
		    echo '<b>Yuboruvchi:</b> <a href="' . $home . '/users/profile.php?user=' . $User['id'] . '">' . $User['name'] . '</a> <a href="?act=spy_mail&amp;mod=track&amp;id=' . $User['id'] . '">[#]</a><br />
		        <b>Kimga:</b> <a href="' . $home . '/users/profile.php?user=' . $from['id'] . '">' . $from['name'] . '</a> <a href="?act=spy_mail&amp;mod=track&amp;id=' . $from['id'] . '">[#]</a><br />
				<b>Vaqti:</b> ' . functions::display_date($res['time']) . '<br />';
		    $message = functions::checkout($res['text'], 1, 1);
            if ($set_user['smileys'])
                $message = functions::smileys($message, $user['rights'] >= 1 ? 1 : 0);
		    echo '<b>Xabar:</b> ' . $message . '<br />';
			if ($res['file_name']) {
				$size = $res['size'];
                echo '<div class="func">Fayl: <a href="../mail/index.php?act=load&amp;id=' . $res['id'] . '">' . $res['file_name'] . '</a> (' . formatSize($size) . ')(' . $res['count'] . ')</div>';
			}	
		    echo '<a href="?act=spy_mail&amp;delete&amp;id='.$res['id'].'">O\'chirish</a>';
		    echo '</div>';
		    $i++;
	    }
		echo '<div class="rmenu"><a href="?act=spy_mail&amp;mod=clean">Pochtani tozalash</a></div>';
    } else {
	    echo '<div class="menu">Bo\'sh</div>';
    }
	if ($total > $kmess) {
        echo '<div class="topmenu">' . functions::display_pagination('index.php?act=spy_mail&amp;', $start, $total, $kmess) . '</div>';
        echo '<p><form action="index.php?act=spy_mail" method="post"><input type="text" name="page" size="2"/><input type="submit" value="' . $lng['to_page'] . ' &gt;&gt;"/></form></p>';
    }
	break;
}
echo '<div class="phdr"><a href="index.php">' . $lng['admin_panel'] . '</a></div>';