<?php
/*
 * JohnCMS NEXT Mobile Content Management System (http://johncms.com)
 *
 * For copyright and license information, please see the LICENSE.md
 * Installing the system or redistributions of files must retain the above copyright notice.
 *
 * @link        http://johncms.com JohnCMS Project
 * @copyright   Copyright (C) JohnCMS Community
 * @license     GPL-3
 */

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

require('../system/head.php');

/** @var Psr\Container\ContainerInterface $container */
$container = App::getContainer();

/** @var PDO $db */
$db = $container->get(PDO::class);

/** @var Johncms\Api\UserInterface $systemUser */
$systemUser = $container->get(Johncms\Api\UserInterface::class);

/** @var Johncms\Api\BbcodeInterface $bbcode */
$bbcode = $container->get(Johncms\Api\BbcodeInterface::class);

/** @var Johncms\Api\ToolsInterface $tools */
$tools = $container->get(Johncms\Api\ToolsInterface::class);

if (empty($_GET['id'])) {
    echo $tools->displayError(_t('Wrong data'));
    require('../system/end.php');
    exit;
}

// Запрос сообщения
$res = $db->query("SELECT `forum`.*, `users`.`sex`, `users`.`balans`, `users`.`rights`, `users`.`lastdate`, `users`.`status`, `users`.`postforum`, `users`.`datereg`, `users`.`negara`, `users`.`podpis`, `users`.`www` , `users`.`ip` , `users`.`browser`
FROM `forum` LEFT JOIN `users` ON `forum`.`user_id` = `users`.`id`
WHERE `forum`.`type` = 'm' AND `forum`.`id` = '$id'" . ($systemUser->rights >= 7 ? "" : " AND `forum`.`close` != '1'") . " LIMIT 1")->fetch();

// Запрос темы
$them = $db->query("SELECT * FROM `forum` WHERE `type` = 't' AND `id` = '" . $res['refid'] . "'")->fetch();
echo '<div class="mainblok"><div class="phdr"><b>' . _t('Topic') . ':</b>' . $them['text'] . '</div></div>';
echo '<div class="mainblok">';
//Mod thanks alert 
$checkthankdau = $db->query('SELECT COUNT(*) FROM `forum_thank` WHERE `userthank` = "' . $systemUser->id . '" and `topic` = "' . $_GET['thanks'] . '" and `user` = "' . $_GET['user'] . '"');
if ($systemUser->isValid() && $systemUser->id != $_GET['user'] && ($checkthankdau->fetchColumn() < 1)) {
if (isset($_GET['thank']) && isset($_GET['user']) && isset($_GET['thanks'])) {
echo '<div class="orange" id="thankyou">Your thanks has been received..!!</div>';
$db->exec("INSERT INTO `forum_thank` SET
`user` = '".trim($_GET['user'])."',
`topic` = '".trim($_GET['thanks'])."' ,
`time` = '$realtime',
`userthank` = '$systemUser->id',
`chude` = '".$_GET['id']."'
");
$congcamon = $db->query('SELECT * FROM `users` WHERE `id` = "' . trim($_GET['user']) . '"')->fetch();
$db->exec("UPDATE `users` SET `thank_duoc`='" . ($congcamon['thank_duoc'] + 1) . "' WHERE `id` = '" . trim($_GET['user']) . "'");
$db->exec("UPDATE `users` SET `thank_duoc`='" . ($congcamon['thank_duoc'] + 1) . "' WHERE `id` = '" . trim($_GET['user']) . "'");
$db->exec("UPDATE `users` SET `thank_di`='" . ($systemUser->thank_di + 1) . "' WHERE `id` = '" . $systemUser->id . "'");
}
}


// Данные пользователя
$vrp = $res['time'] + $sdvig * 3600;
$vr = date("d.m.Y - g:i a", $vrp);
echo '<div class="phdr"><table width="100%" cellpadding="0" cellspacing="0"><tr><td width="auto"><img src="/images/file.png" align="top" alt="file"/>&#160;' . $vr . '</td><td width="auto" align="right">&#160;#<a href="../index.php?act=post&amp;id=' . $res['id']. '#' .( $start + $i ). '">' .( $start + $i ). '</a></td></tr></table></div>';
echo '<div class="newsx">';
echo '<table width="100%" cellpadding="0" cellspacing="0"><tr><td width="39" align="left" valign="top">';

if (file_exists(('../files/users/avatar/' . $res['user_id'] . '.png'))) {
    echo '<img src="../files/users/avatar/' . $res['user_id'] . '.png" width="36" height="auto" alt="' . $res['from'] . '" />';
} else {
    echo '<img src="../images/empty.png" width="36" height="auto" alt="' . $res['from'] . '" />';
}

echo '</td><td width="auto" align="left" valign="top">';

if ($res['sex']) {
    echo $tools->image(($res['sex'] == 'm' ? 'm' : 'w') . ($res['datereg'] > time() - 86400 ? '_new' : '') . '.png', ['class' => 'icon-inline']);
} else {
    echo $tools->image('del.png');
}
$rank = [
0 => '<font color="orange">',
3 => '<font color="#3399ff">',
6 => '<font color="#3399ff">',
7 => '<font color="green">',
9 => '<font color="#cc0000">',
];
// Ник юзера и ссылка на его анкету
if ($systemUser->isValid() && $systemUser->id != $res['user_id']) {
    echo '<a href="../profile/?user=' . $res['user_id'] . '">&#160;' . $rank[$res['rights']] . '<b>' . $res['from'] . '</b></a></font>';
} else {
    echo '&#160;' . $rank[$res['rights']] . '<b>' . $res['from'] . '</b></font>';
}

// Метка Онлайн / Офлайн
echo (time() > $res['lastdate'] + 300 ? '<span class="red">&nbsp;<img src="../images/offline.gif" width="8" height="10" alt="off"/></span>' : '<span class="green">&nbsp;<img src="../images/online.gif" width="8" height="10" alt="on"/></span>');
echo '<br/>';
$user_rights = [
0 => '<img src="/images/member.png" width="100" height="24" alt="" align="middle"/>',
3 => '<img src="/images/moderator.png" width="100" height="24" alt="" align="middle"/>',
6 => '<img src="/images/moderator.png" width="100" height="24" alt="" align="middle"/>',
7 => '<img src="/images/admin.png" width="100" height="24" alt="" align="middle"/>',
9 => '<img src="/images/owner.png" width="100" height="24" alt="" align="middle"/>',
];
echo '<div class="status">' . $user_rights[$res['rights']] . '</div>';
echo '<div  align="left">';
include ('rank.php');
echo '</div>';
echo '</td><td width="auto" align="right" valign="top">';
// TOTAL POST
if(!$systemUser->isValid()) {
echo '<a href="../login.php"><img src="/images/pm.png" width="16" height="16" alt="pm"/></a>';
} else {
echo '<a href="../mail/index.php?act=write&amp;id=' . $res['user_id'] . '"><img src="/images/pm.png" width="16" height="16" alt="pm"/></a>';
}
$kaka = $db->query("SELECT COUNT(*) FROM `forum_thank` WHERE `user` = " . $res['user_id'] . ";")->fetchColumn();
echo '<br/><img src="/images/thanx.gif" width="14" height="14" alt="thanx"/> ' . $kaka . '';
echo '<br/>Post: ';
echo $res['postforum'];

echo '</td></tr></table></div>';
echo '<div class="textx">';

// Вывод текста поста
$text = htmlentities($res['text'], ENT_QUOTES, 'UTF-8');
$text = nl2br($text);
$text = $bbcode->tags($text);
$text = $tools->checkout($res['text'], 1, 1);
$text = $tools->smilies($text, ($res['rights'] >= 1) ? 1 : 0);
$checkthank = $db->query('SELECT COUNT(*) FROM `forum_thank` WHERE `userthank` = "' . $systemUser->id . '" and `topic` = "' . $res['id'] . '" and `user` = "' . $res['user_id'] . '"');
$thankcheck = $checkthank->fetchColumn();
if (($thankcheck < 1) && $systemUser->id != $res['user_id']) {
$text = preg_replace('#\[thank\](.*?)\[/thank\]#si', '<div class="rmenu" style="text-align:center"><font color="cc0000"><b>POST LOCKED</b></font><br><font color="blue">Click ' . $tools->image('thanks.gif') . ' To Unlock Content(s)</font></div>', $text);
} else {
$text = preg_replace('#\[thank\](.*?)\[/thank\]#si', '\1', $text);
}
echo $text . '</div>';

echo '<div class="forumb"><table width="100%" cellspacing="0" cellpadding="0"><tr><td width="auto" align="left">';
//Mod. Thanks
$checkthank = $db->query('SELECT COUNT(*) FROM `forum_thank` WHERE `userthank` = "' . $systemUser->id . '" and `topic` = "' . $res['id'] . '" and `user` = "' . $res['user_id'] . '"');
if ($systemUser->isValid() && $systemUser->id != $res['user_id'] && ($checkthank->fetchColumn() < 1)) {
echo '<a href="index.php?id=' . $id . '&amp;thanks=' . $res['id'] . '&amp;user=' . $res['user_id'] . '&amp;start=' . $start . '&amp;thank#thankyou"><img src="/images/thanks.gif" width="55" height="16" alt="thanx"/></a>&#160;';
}
echo '&nbsp;';
// Hacklord quote
echo '</td><td width="auto" align="right">';
if ($systemUser->isValid() && $systemUser->id != $res['user_id']) {
echo '<a href="index.php?act=say&amp;id=' . $res['id'] . '&amp;start=' . $start . '&amp;cyt"><img src="/images/quote.gif" width="55" height="16" alt="' . _t('Quote') . '" /></a>';
}
echo '</td></tr></table>';
echo ''; 
// Если есть прикрепленный файл, выводим его описание
$freq = $db->query("SELECT * FROM `cms_forum_files` WHERE `post` = '" . $res['id'] . "'");

if ($freq->rowCount()) {
    $fres = $freq->fetch();
    $fls = round(@filesize('../files/forum/attach/' . $fres['filename']) / 1024, 2);
    echo '<div class="gray" style="font-size: x-small; background-color: rgba(128, 128, 128, 0.1); padding: 2px 4px; margin-top: 4px">' . _t('Attachment') . ':';
    // Предпросмотр изображений
    $att_ext = strtolower(pathinfo('./files/forum/attach/' . $fres['filename'], PATHINFO_EXTENSION));
    $pic_ext = [
        'gif',
        'jpg',
        'jpeg',
        'png',
    ];

    if (in_array($att_ext, $pic_ext)) {
        echo '<div><a href="index.php?act=file&amp;id=' . $fres['id'] . '">';
        echo '<img src="thumbinal.php?file=' . (urlencode($fres['filename'])) . '" alt="' . _t('Click to view image') . '" /></a></div>';
    } else {
        echo '<br /><img src="/images/attach.png" align="left" alt="attach"/>&#160;<a href="index.php?act=file&amp;id=' . $fres['id'] . '">' . $fres['filename'] . '</a>';
    }

    echo ' (' . $fls . ' кб.)<br>';
    echo _t('Downloads') . ': ' . $fres['dlcount'] . '</div>';
    $file_id = $fres['id'];
}

echo '</div>';
// Mod. Total Cash
echo '<div class="list1"><span>Total Cash:<font color="blue">&#160;' . $res['balans'] . '</font></span></div>';
// Mod. Signature
if (!empty($res['podpis']))
echo '<div class="list1"><img src="/images/podpis.png" alt="" align="left"/><font color="green">&#160;' . $tools->smilies($bbcode->tags($res['podpis'])) . '</font></div>';
// Mod. Negara
if (!empty($res['negara']))
echo '<div class="list1"><span>Location:<font color="blue">&#160;' . $res['negara'] . '</font></span></div>';
// Mod. Mysite
if (!empty($res['www']))
echo '<div class="list1"><span>My site:<font color="orange">&#160;' . $res['www'] . '</font></span></div>';
// Mod. Freaking IP
echo '<div class="list1"><span>' . $res['soft'] . ' - ' . long2ip($res['ip']) . '</a></span></div>';

// Вычисляем, на какой странице сообщение?
$page = ceil($db->query("SELECT COUNT(*) FROM `forum` WHERE `refid` = '" . $res['refid'] . "' AND `id` " . ($set_forum['upfp'] ? ">=" : "<=") . " '$id'")->fetchColumn() / $kmess);
echo '<div class="phdr"><a href="index.php?id=' . $res['refid'] . '&amp;page=' . $page . '">' . _t('Back to topic') . '</a></div>';
echo '<div class="phdr"><a href="index.php">' . _t('Forum') . '</a></div></div>';
