<?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');

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

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

/** @var Johncms\Config $config */
$config = $container->get(Johncms\Config::class);

/** @var Johncms\Environment $env */
$env = App::getContainer()->get('env');

/** @var Johncms\Bbcode $bbcode */
$bbcode = $container->get('bbcode');

/** @var Johncms\Counters $counters */
$counters = $container->get('counters');

$mp = new Johncms\NewsWidget();

echo '</div></div>';

// notify profile
if($systemUser->id){
if(!file_exists(($rootpath.'files/users/avatar/'.$systemUser->id.
'.png'))) {
echo '<div class="maintxt"><div class="rmenu">Thanks For Register ' .$systemUser->name. ' ! Hey ' .$systemUser->name. ' Please <a href="../profile/?act=images&amp;mod=avatar&amp;user='.$user_id.'">Click Here</a> to Complete Your Profile.Otherwise You Will Be Blocked Within 24 Hours.</div></div>';
   }
}
// quick login
if(!$systemUser->id){
echo '<div class="maintxt"><div class="mainblok"><div
class="phdr"><b> Quick Login </b></div><div class="sbox sundawap"><form action="login.php" method="post"><p><input type="text" name="n" value="' . htmlentities($user_login, ENT_QUOTES, 'UTF-8') . '" maxlength="20" size="5" class="name"/>' .
            '<input type="password" name="p" maxlength="20" class="pass" size="5"/> <input type="submit" value="' . _t('Login', 'system') . '"/></p>' .
            '</form>';
echo '<div class="list2" style="padding:3px"> ● <a href="/login.php"><span style="padding:4px;"> Alternative Login! </span></a><br> ● <a href="/profile/skl.php?continue"><span style="padding:4px"> Lost Password? </span></a><br><a href="/registration"><div align="center" style="background:#69A74E;color:white;border: 1px outset #69A74E;padding:2px;font-weight:bold"> Create a New Account </div></a></div>';

echo '</div></div></div>';
            }
            
            // my timeline
            if($systemUser->id){
    	
            include 'mytimeline.php';
            }
            // welcoming board

if(!$systemUser->id){
echo '<div class="maintxt"><div class="mainblok"><div class="phdr"><b>Welcome Guest</b></div><table><tbody><tr><td><img style="float" :="" left;="" margin:="" 5px="" 6px="" 2px="" 2px;="" border:="" 0px="" src="/images/rumah.png" width="50" height="50"></td><td class="menu"><font color="green">MasterKings.Ga is a community based Forum for discussion and sharing with peoples. To connect with peoples around the world on one place to share and get a new informations, technology,file sharing,thinking, ideas and more you are in the right place.People can read forum posts without registration. After registering users can create a user profile, add other users as friends,send text messages, post on forum, uploading files,sharing photos and receive notifications when someone reply or Quote thier Forum Post and album.So In Order To Acces All Features Of MasterKings,You Should Login or Register Immidiately.If You Think MasterKings Is Helpul You Can Write in<a href="#guestbook"> guestbook!</a></font></td></tr></tbody></table></div></div>';
}

// Notice
if($systemUser->id){
echo '<div class="maintxt"><div class="mainblok"><div class="phdr"><b>Notice Board</b></div>';
echo $mp->news;
echo '<div class="menu"><a href="/news">' . _t('News archive', 'system') . '</a> (' . $mp->newscount . ')</div>' .
    '<div class="menu"><a href="help/">' . _t('Information, FAQ', 'system') . '</a></div>';
echo '</div></div>';
}

// forum search
	
	echo '<div class="maintxt"><div class="mainblok"><div class="phdr"><b>Forum Search</b></div><div class="forumb"><form action="' . $set['homeurl'] . '/forum/search.php" method="post"><input name="t" type="checkbox" value="1"  />Forum Topic<br/><form action="' . $set['homeurl'] . '/forum/search.php" method="post"><div> <div style="text-align:left"><input type="text" value="' . ($search ? $tools->checkout($search) : '') . '" name="search" /><input class="btn" type="submit" value="' . _t('Search') . '" name="submit"></small></form>
</div></div></div></div></div></div>';

// featured post
echo '<div class="maintxt"><div class="mainblok">';
echo '<div class="phdr"><a href="/forum/index.php"><b>Featured Post</b></a></div>';
    $req = $db->query("SELECT * FROM `forum` WHERE `type` = 't' AND `close` != '1' ORDER BY `vip` DESC LIMIT 3");

    if ($req->rowCount()) {
        for ($i = 0; $res = $req->fetch(); ++$i) {
            $razd = $db->query("SELECT `id`, `refid`, `text` FROM `forum` WHERE type='r' AND id='" . $res['refid'] . "' LIMIT 1")->fetch();
            $frm = $db->query("SELECT `id`, `refid`, `text` FROM `forum` WHERE type='f' AND id='" . $razd['refid'] . "' LIMIT 1")->fetch();
            $nikuser = $db->query("SELECT `from`, `time` FROM `forum` WHERE `type` = 'm' AND `close` != '1' AND `refid` = '" . $res['id'] . "'ORDER BY `time` DESC");
            $colmes1 = $nikuser->rowCount();
            $cpg = ceil($colmes1 / $kmess);
            $nam = $nikuser->fetch();
           
            echo $i % 2 ? '<div class="list2">' : '<div class="list1">';

// Значки
                    $icons = [
                        ($res['vip'] ? $tools->image('pt.gif') : ''),
                        ($res['realid'] ? $tools->image('rate.gif') : ''),
                        ($res['edit'] ? $tools->image('tz.gif') : ''),
                    ];
                    echo implode('', array_filter($icons));
                    echo '<div name="h_post"><a href="/forum/index.php?id=' . $res['id'] . ($cpg > 1 && $set_forum['upfp'] && $set_forum['postclip'] ? '&amp;clip' : '') . ($set_forum['upfp'] && $cpg > 1 ? '&amp;page=' . $cpg : '') . '">' . (empty($res['text']) ? '-----' : $res['text']) .
                        '(' . $colmes1 . ')</a></div>';

                    if ($cpg > 1) {
                echo '&#160;<a href="/forum/index.php?id=' . $res['id'] . '&amp;clip&amp;page=' . $cpg . '">&gt;&gt;</a>';
            }

            echo '<br><div class="sub"><a href="/forum/index.php?id=' . $razd['id'] . '">' . $frm['text'] . '&#160;/&#160;' . $razd['text'] . '</a><br />';
            echo $res['from'];

            if (!empty($nam['from'])) {
                echo '&#160;/&#160;' . $nam['from'];
            }

            echo ' <span class="gray">' . date("d.m.y / H:i", $nam['time']) . ' <hr/>Viewed: ' . $res['view'] . '</span>';
            echo '</div></div>';
        
}
            } else {
                echo '<div class="menu"><p>' . _t('The list is empty') . '</p></div>';
            }
 
echo '<script type="text/javascript" src="/thumbnail/th_forum1.js"></script><script type="text/javascript" src="/thumbnail/th_update.js"></script>';
            echo '</div></div>';


// latest post with paging & guest can see only 20 post without paging


$total = $db->query("SELECT COUNT(*) FROM `forum` WHERE `type`='t'")->fetchColumn();
            echo '<div class="maintxt"><div class="mainblok"><div class="phdr">Latest Forum Updates</div>';
            
            if ($total > 0) {
                $req = $db->query("SELECT * FROM `forum` WHERE `type` = 't' AND `close` != '1' ORDER BY `time` DESC LIMIT $start, $kmess");
                    
        for ($i = 0; $res = $req->fetch(); ++$i) {
                    if ($res['close']) {
                        echo '<div class="rmenu">';
                    } else {
                        echo $i % 2 ? '<div class="list2">' : '<div class="list1">';
                    }

                    $razd = $db->query("SELECT `id`, `refid`, `text` FROM `forum` WHERE type='r' AND id='" . $res['refid'] . "' LIMIT 1")->fetch();
            $frm = $db->query("SELECT `id`, `refid`, `text` FROM `forum` WHERE type='f' AND id='" . $razd['refid'] . "' LIMIT 1")->fetch();
            $nikuser = $db->query("SELECT `from`, `time` FROM `forum` WHERE `type` = 'm' AND `close` != '1' AND `refid` = '" . $res['id'] . "'ORDER BY `time` DESC");
            $colmes1 = $nikuser->rowCount();
            $cpg = ceil($colmes1 / $kmess);
            $nam = $nikuser->fetch();

                    // Значки
                    $icons = [
                        ($res['vip'] ? $tools->image('pt.gif') : ''),
                        ($res['realid'] ? $tools->image('rate.gif') : ''),
                        ($res['edit'] ? $tools->image('tz.gif') : ''),
                    ];
                    echo implode('', array_filter($icons));
                    echo '<div name="h_post"><a href="/forum/index.php?id=' . $res['id'] . ($cpg > 1 && $set_forum['upfp'] && $set_forum['postclip'] ? '&amp;clip' : '') . ($set_forum['upfp'] && $cpg > 1 ? '&amp;page=' . $cpg : '') . '">' . (empty($res['text']) ? '-----' : $res['text']) .
                        '(' . $colmes1 . ')</a></div>';

                    if ($cpg > 1) {
                echo '&#160;<a href="/forum/index.php?id=' . $res['id'] . '&amp;clip&amp;page=' . $cpg . '">&gt;&gt;</a>';
            }

            echo '<br><div class="sub"><a href="/forum/index.php?id=' . $razd['id'] . '">' . $frm['text'] . '&#160;/&#160;' . $razd['text'] . '</a><br />';
            echo $res['from'];

            if (!empty($nam['from'])) {
                echo '&#160;/&#160;' . $nam['from'];
            }

            echo ' <span class="gray">' . date("d.m.y / H:i", $nam['time']) . ' ' . $res['view'] . ' views</span>';
            echo '</div></div>';
                }
            } else {
                echo '<div class="menu"><p>' . _t('The list is empty') . '</p></div>';
            }
            if ($total > $kmess) {
                echo '<div class="topmenu">Pages: ' . $tools->displayPagination('index.php?', $start, $total, $kmess) . '</div>';
                    
            }
                    echo '<script type="text/javascript" src="/thumbnail/th_forum1.js"></script><script type="text/javascript" src="/thumbnail/th_update.js"></script>';
                    
                    
            echo '</div></div>';
            
// Showing Latest Files 5 Uploaded By Users
echo '<div class="maintxt"><div class="mainblok">';
echo '<div class="phdr"><b>Latest Files</b></div>';
$total = $db->query("SELECT COUNT(*) FROM `download__files` WHERE `type` = '2'")->fetchColumn();

// Навигация
$lim = 6;
// Выводим список
if ($total) {
    $i = 0;
    $req_down = $db->query("SELECT * FROM `download__files` WHERE `type` = '2' ORDER BY `time` DESC LIMIT 5");

    while ($res_down = $req_down->fetch()) {
    
    
echo '<div class="list1">› <a href="/downloads/?act=view&id='.$res_down['id'].'">'.$res_down['rus_name'].' ('.$res_down['field'].')</div>';
    }
    
} else {
    echo '<div class="rmenu"><p>' . _t('The list is empty') . '</p></div>';
}
            
          echo '</div></div>';

////////////////////////////////////////////////////////////
// Блок общения                                           //
////////////////////////////////////////////////////////////


echo '<div class="maintxt"><div class="mainblok">';
echo '<div class="phdr"><b>Main Menu</b></div>';
// screenshts
echo '<div class="list1">› <a href="/system/gabriel/upload_img.php">ScreenShots</a></div>';

// Ссылка на гостевую
if ($config->mod_guest || $systemUser->rights >= 7) {
    echo '<div class="list2" id="guestbook">› <a href="guestbook/index.php">' . _t('Guestbook', 'system') . '</a></div>';
}

// Ссылка на Форум
if ($config->mod_forum || $systemUser->rights >= 7) {
    echo '<div class="list1">› <a href="forum/">' . _t('Forum', 'system') . '</a>(' . $counters->forum() . ')</div>';
}

////////////////////////////////////////////////////////////
// Блок полезного                                         //
////////////////////////////////////////////////////////////


// Ссылка на загрузки
if ($config->mod_down || $systemUser->rights >= 7) {
    echo '<div class="list2">› <a href="downloads/">' . _t('Downloads', 'system') . '</a>(' . $counters->downloads() . ')</div>';
}

////////////////////////////////////////////////////////////
// Блок Сообщества                                        //
////////////////////////////////////////////////////////////
if ($systemUser->isValid() || $config->active) {
    
      echo '<div class="list1">› <a href="users/index.php">' . _t('Users', 'system') . '</a></div>' .
        '<div class="list2">› <a href="album/index.php">' . _t('Photo Albums', 'system') . '</a></div>';
}
echo '</div></div>';

//forum categories johncms 7.0

echo '<div class="maintxt"><div class="mainblok"><div class="phdr"><b>Forum Categories</b></div>';
$req = $db->query("SELECT `id`, `text`, `soft`, `edit` FROM `forum` WHERE `type`='r' AND `refid`='1' ORDER BY `realid`");
                $total = $req->rowCount();
                if ($total) {
                    $i = 0;

                    while ($res = $req->fetch()) {
                        echo $i % 2 ? '<div class="list2">' : '<div class="list1">';
                        $coltem = $db->query("SELECT COUNT(*) FROM `forum` WHERE `type` = 't' AND `refid` = '" . $res['id'] . "'")->fetchColumn();
                        echo '› <a href="/forum/?id=' . $res['id'] . '">' . $res['text'] . '</a>';

                        if ($coltem) {
                            echo " ($coltem)";
                        }

                        if (!empty($res['soft'])) {
                            echo '<div class="sub"><span class="gray">' . $res['soft'] . '</span></div>';
                        }

                        echo '</div>';
                        ++$i;
                    }

                    unset($_SESSION['fsort_id']);
                    unset($_SESSION['fsort_users']);
                } else {
                    echo '<div class="menu"><p>' . _t('There are no sections in this category') . '</p></div>';
                }

                echo '<div class="phdr">' . _t('Total') . ': ' . $total . '</div>';
               echo '</div></div>';
               
           

// latest users

echo '<div class="maintxt"><div class="mainblok">';
$total = $db->query("SELECT COUNT(*) FROM `users` WHERE `preg` = 1")->fetchColumn();
echo '<div class="phdr"><b>New Users</b></div>';
$req = $db->query("SELECT `id`, `name`, `sex`, `lastdate`, `datereg`, `status`, `rights`, `ip`, `browser`, `rights` FROM `users` WHERE `preg` = 1 ORDER BY `datereg` DESC LIMIT 5");

for ($i = 0; ($res = $req->fetch()) !== false; $i++) {
    echo $i % 2 ? '<div class="list2">' : '<div class="list1">';
    echo $tools->displayUser($res) . '</div>';
}

echo '</div></div>';

echo '<div class="menu" align="center"><a href="http://fb.com/dearmanbinrahman">Contact Us</a> | <a href="/news">About Us</a> | <a href="/help" align="center">FAQ</a></div>';