<?php 
	define('_CONSTANT_', 1);
	require_once '../../core/start.php';

	if(trim($_POST['text']) == '' ) {
	 	echo "<b>Введите текст!</b>";
	}else{
        $profile['id'] = $_POST['usid'];

		$_POST['text'] = $Filter->clearString($_POST['text']);

		# Select/update contact;
		$kontakt = DB::$dbs->queryFetch("SELECT * FROM ".MAIL_KONT." WHERE `id_user` = ? and `id_kont` = ? LIMIT 1", [$user['id'], $profile['id']]); 
        if ($kontakt['id_kont'] != $profile['id']) 
        {
            DB::$dbs->query("INSERT INTO ".MAIL_KONT." (`id_user`, `id_kont`, `time`) VALUES (?, ?, ?)", [$user['id'], $profile['id'], time()]);
            DB::$dbs->query("INSERT INTO ".MAIL_KONT." (`id_user`, `id_kont`, `time`) VALUES (?, ?, ?)", [$profile['id'], $user['id'], time()]);
        } 
        else 
        {
            DB::$dbs->query("UPDATE ".MAIL_KONT." SET `time` = ? WHERE `id_user` = ? and `id_kont` = ?", [time(), $user['id'], $profile['id']]);
            DB::$dbs->query("UPDATE ".MAIL_KONT." SET `time` = ? WHERE `id_user` = ? and `id_kont` = ?", [time(), $profile['id'], $user['id']]);
        }

		DB::$dbs->query("INSERT INTO ".MAIL." (`sender`, `given`, `text`, `time`) VALUES (?, ?, ?, ?)", [$user['id'], $profile['id'], $_POST['text'], time()]);

		echo '<em>Сообщение отправлено, пожалуйста, обновите страницу!</em>';
	}
?>