<?php
# Script by seg0ro http://mobilarts.ru
# Not for sale!!!

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

$textl .= ' | Онлайн';
require_once('../incfiles/head.php');

if (!$user_id){
  echo functions::display_error('Только для авторизованных!<br /><a href="index.php">Форум</a>');
  require_once('../incfiles/end.php');
  exit;
}

$onltime = time() - 300;
$intopic = $topic ? '&amp;topic='.$topic : '';

if (isset($_GET['guests'])){
  $guests =  1;
  $url = 'guests&amp;';
  $top = 'Гости ';
  $bottom = 'Гостей: ';
  $show = '<div class="topmenu"><a href="index.php?act=onforum'.$intopic.'">Показать пользователей</a></div>';
}else{
  $guests =  0;
  $url = '';
  $top = 'Пользователи ';
  $bottom = 'Пользователей: ';
  $show = '<div class="topmenu"><a href="index.php?act=onforum'.$intopic.'&amp;guests">Показать гостей</a></div>';
}

if ($topic){
  $tree = array('<a href="index.php">Форум</a>', '<a href="index.php?topic='.$topicRes['topic'].'">'.functions::checkout($topicRes['topicname']).'</a>', $top.'в теме');
  echo '<div class="phdr">'.functions::display_menu($tree).'</div>';

  $total = mysql_result(mysql_query("SELECT COUNT(*) FROM `".($guests ? "cms_sessions" : "users" )."` WHERE `lastdate` > $onltime AND `place` = 'forum,$topic' " ), 0);
  
  if ($total){
    if ($total > $kmess)
      echo '<div class="topmenu">'.functions::display_pagination('index.php?act=onforum&amp;topic='.$topic.'&amp;'.$url, $start, $total, $kmess).'</div>';

    $req = mysql_query("SELECT * FROM `".($guests ? "cms_sessions" : "users" )."` WHERE `lastdate` > $onltime AND `place` = 'forum,$topic' ".($guests ? "" : "ORDER BY `name` ASC" )." LIMIT $start, $kmess");
    while ($res = mysql_fetch_array($req)){
      if ($res['id'] == core::$user_id) echo '<div class="gmenu">';
      else echo $i % 2 ? '<div class="list2">' : '<div class="list1">';

      $array = array('header' => '', 'body' => '', 'sub' => '');
      echo functions::display_user($res, $array);
      echo '</div>';
      ++$i;
    }
    if (!$i) echo '<div class="list1">Неверные данные. Убедитесь в правильности ввода страницы!</div>';
    echo '<div class="phdr">'.$bottom.$total.'</div>';
    if ($total > $kmess){
      echo '<div class="topmenu">'.functions::display_pagination('index.php?act=onforum&amp;topic='.$topic.'&amp;'.$url, $start, $total, $kmess).'</div>';
    }
  }else{
    echo '<div class="list1">Пусто</div>';
  }
}else{
  $tree = array('<a href="index.php">Форум</a>', $top.'на форуме');
  echo '<div class="phdr">'.functions::display_menu($tree).'</div>';

  $total = mysql_result(mysql_query("SELECT COUNT(*) FROM `".($guests ? "cms_sessions" : "users" )."` WHERE `lastdate` > $onltime AND `place` LIKE 'forum%'" ), 0);
  if ($start >= $total){
    // Исправляем запрос на несуществующую страницу
    $start = max(0, $total - (($total % $kmess) == 0 ? $kmess : ($total % $kmess)));
  }
  
  if ($total){
    if ($total > $kmess)
      echo '<div class="topmenu">'.functions::display_pagination('index.php?act=onforum&amp;'.$url, $start, $total, $kmess).'</div>';

    $req = mysql_query("SELECT * FROM `".($guests ? "cms_sessions" : "users" )."` WHERE `lastdate` > $onltime AND `place` LIKE 'forum%' ".($guests ? "" : "ORDER BY `name` ASC" )." LIMIT $start, $kmess");
    while ($res = mysql_fetch_array($req)){
      if ($res['id'] == core::$user_id) echo '<div class="gmenu">';
      else echo $i % 2 ? '<div class="list2">' : '<div class="list1">';

      $place = explode(',', $res['place']);
      if ($place['0'] == 'forum' && intval($place['1'])){
        $where = mysql_fetch_assoc(mysql_query("SELECT `topicname`, `count` FROM `forum_topics` WHERE `topic` = '".$place['1']."' LIMIT 1 "));

        $text = 'В теме: <strong><a href="index.php?topic='.$place['1'].'">'.functions::checkout($where['topicname']).'</a>';
        if ($where['count'] > 10)
          $text .= ' <a href="index.php?topic='.$place['1'].'&amp;page='.ceil($where['count'] / $kmess).'" title="К последней странице">&gt;&gt;</a>';
        $text .= '</strong>';
      }else{
        $text = '<strong><a href="index.php">На форуме</a></strong>';
      }

      if ($guests)
        $res['name'] = preg_match('/bot|spider/', $res['browser']) ? 'Бот' : '';
      $array = array('header' => '', 'body' => '<img src="../images/info.png" width="16" height="16" align="middle" /> '.$text, 'sub' => '');
      echo functions::display_user($res, $array).'</div>';
      ++$i;
    }
    
    echo '<div class="phdr">'.$bottom.$total.'</div>';
    if ($total > $kmess){
      echo '<div class="topmenu">'.functions::display_pagination('index.php?act=onforum&amp;'.$url, $start, $total, $kmess).'</div>';
    }
      
  }else{
    echo '<div class="list1">Пусто</div>';
  }
}

echo '<p>'.$show.'</p>';