<?php

/*
  * @name        JMY CMS
  * @link        https://jmy.su/
  * @copyright   Copyright (C) 2012-2025 JMY LTD
  * @license     LICENSE.txt (see attached file)
  * @version     VERSION.txt (see attached file)
  * @author      Orlov Alexsey
  */

if (!defined('ACCESS')) {
    header('Location: /');
    exit;
}
loadConfigBLOCK('lastcom');
global $db, $url, $core, $lang, $lastcom_conf;
$core->loadblockLang('lastcom');
$l_online = $lang['lastcom_online'];
$l_offline = $lang['lastcom_offline'];
$width = '';
$height = '';
if ($lastcom_conf['on']) {
    if ($url[0] == 'news' || $url[0] == '') {
        $module = 'news';
    } elseif ($url[0] == 'content') {
        $module = 'content';
    } else {
        $module = '';
    }
    $query = $core->select_data('comments', 'module = "' . $module . '" AND c.status = 1', 'date DESC LIMIT 0,' . $lastcom_conf['num'], 'c.*, u.nick, u.group, u.last_visit', 'as c LEFT JOIN `' . DB_PREFIX . '_users` as u on (c.uid=u.id)');
    if ($db->numRows($query) > 0) {
        while ($commment = $db->getRow($query)) {
            $rows = $core->row_data('users', 'nick = "' . $commment['nick'] . '"');
            preg_match('/<img.*width="(.*)".*height="(.*)".*>/is', $commment['text'], $matches);
            preg_match('/<img.*height="(.*)".*>/is', $commment['text'], $matchess);
            if ($matches && $matchess) {
                $width = $matches[1];
                $height = $matchess[1];
            }
            if ($lastcom_conf['lastcom_img_height'] && $lastcom_conf['lastcom_img_widtht']) {
                $lastcom = str_replace(array($width, $height), array($lastcom_conf['lastcom_img_widtht'], $lastcom_conf['lastcom_img_height']), $commment['text']);
            } elseif ($lastcom_conf['lastcom_img_height'] == 0 && $lastcom_conf['lastcom_img_widtht']) {
                $lastcom = str_replace(array($width, $height), array($lastcom_conf['lastcom_img_widtht'], $height), $commment['text']);
            } elseif ($lastcom_conf['lastcom_img_height'] && $lastcom_conf['lastcom_img_widtht'] == 0) {
                $lastcom = str_replace(array($width, $height), array($width, $lastcom_conf['lastcom_img_height']), $commment['text']);
            } else {
                $lastcom = str_replace(array($width, $height), array($width, $height), $commment['text']);
            }
            if (!empty($rows['nick'])) $onn[] = '<a href="profile/' . $rows['nick'] . '" >' . $rows['nick'] . '</a>';
            if ($lastcom_conf['old']) {
                echo '<div class="main-comments">
   <div class="main-comments-top">
      <div class="main-comments-image">
         <img src="' . avatar($commment['uid']) . '" alt="">
      </div>
      <div class="main-comments-author">
         ' . $commment['nick'] . '
      </div>
      <div class="main-comments-date">
         ' . formatDate($commment['date']) . '
      </div>
      <a href="' . commentLink($commment['module'], $commment['post_id'], false) . '" title= "перейти на страницу" class="main-comments-link"><i>.</i><u>.</u>.</a>
   </div>
   <div class="main-comments-text">
      <div id="comm-' . $commment['id'] . '">' . $core->bbDecode(str(($lastcom), 300), true) . '</div>
   </div>
</div>';
            } else {
                $core->tpl->loadFile('blocks/lastcom');
                $core->tpl->setVar('ID', $commment['uid']);
                $core->tpl->setVar('AVATAR', avatar($commment['uid']));
                $core->tpl->setVar('NICK', $commment['nick']);
                $core->tpl->setVar('DATE', formatDate($commment['date']));
                $core->tpl->setVar('ONLINE', isset($rows['id']) ? ($core->isOnline($rows['id']) ? $l_online : formatDate($rows['last_visit'], true)) : '');
                $core->tpl->setVar('OFFLINE', isset($rows['id']) ? ($core->isOffline($rows['id']) ? $l_offline : formatDate($rows['last_visit'], true)) : '');
                $core->tpl->sources = preg_replace_callback("#\\[online\\](.*?)\\[/online\\]#is", function ($matches) use ($rows) {
                    if ($rows['last_visit'] + 500 >= time()) return $matches[1];
                }, $core->tpl->sources);
                $core->tpl->sources = preg_replace_callback("#\\[offline\\](.*?)\\[/offline\\]#is", function ($matches) use ($core, $rows) {
                    if ($core->isOffline($rows['id'])) return $matches[1];
                }, $core->tpl->sources);
                $core->tpl->setVar('ONLINE_USER', (empty($onn) ? (isset($rows['id']) ? ($core->auth->isUser ? '<a href="profile/' . $rows['nick'] . '" title="' . $rows['nick'] . '">' . $rows['nick'] . '</a>' : '...') : '') : implode(', ', $onn)));
                $core->tpl->setVar('COM_URL', '' . commentLink($commment['module'], $commment['post_id'], false) . '"');
                $core->tpl->setVar('EMAIL', isset($rows['id']) ? $rows['email'] : '');
                $news_title = stripslashes($core->bbDecode(parseBB($commment['text'])));
                $core->tpl->sources = preg_replace_callback("#\\{%COMMENT:(.*?)%\\}#is",
                    function ($match) use ($news_title) {
                        return short($match[1], $news_title);
                    }, $core->tpl->sources);
                $core->tpl->end();
            }
        }
    } else {
        $core->tpl->info($lang['lastcom_list_empty']);
    }
} else {
    if ($lastcom_conf['info_off']) $core->tpl->info($lang['lastcom_off']);
}