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

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

if ($topic){
  $textl = 'Файлы темы: '.title($topicRes['topicname']);
}else{
  $textl = 'Файлы форума';
}
require_once('../incfiles/head.php');

$old = time() - (3 * 24 * 3600);

if ($topic){
  $sql = "AND `forum_files`.`topic` = '$topic'";
  $url = 'topic='.$topic.'&amp;';
  $go = 'topic='.$topic;
  $tree = array('<a href="index.php">Форум</a>', '<a href="index.php?topic='.$topic.'">'.functions::checkout($topicRes['topicname']).'</a>', 'Файлы темы');
}else{
  $sql = '';
  $url = '';
  $go = '';
  $tree = array('<a href="index.php">Форум</a>', 'Файлы форума');
}

echo '<div class="phdr">'.functions::display_menu($tree).'</div>';

$total = mysql_result(mysql_query("SELECT COUNT(*) FROM `forum_files` WHERE `tempid` = 0 $sql"), 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=files&amp;'.$url, $start, $total, $kmess).'</div>';
  
  $req = mysql_query("SELECT `forum_files`.*, `forum_files`.`user_id` AS `id`, `users`.`sex`, `users`.`name`, `users`.`rights`, `users`.`lastdate`, `users`.`datereg` FROM `forum_files` LEFT JOIN `users` ON `forum_files`.`user_id` = `users`.`id` WHERE `forum_files`.`tempid` = '0' $sql ORDER BY `forum_files`.`time` DESC ");
    
  while($res = mysql_fetch_array($req)){
    echo $i % 2 ? '<div class="list2">' : '<div class="list1">';
    
    $header = '<span class="gray">'.functions::display_date($res['time']).'</span> <a href="index.php?post='.$res['post'].'&amp;find" title="Перейти к сообщению">&gt;&gt;</a>';
    $text = ($res['time'] > $old ? '<span class="red">новый</span> ' : '').show_file($res);
    $sub = '';
    if (!$topic){
      $topicRes = mysql_fetch_array(mysql_query("SELECT * FROM `forum_topics` WHERE `topic` = '".$res['topic']."' LIMIT 1 "));
      $sub = 'Тема: <a href="index.php?topic='.$topicRes['topic'].'">'.functions::checkout($topicRes['topicname']).'</a>';
    }
    
    $array = array('header' => $header, 'body' => $text, 'sub' => $sub, 'iphide' => 1);
    core::$user_set['avatar'] = 0;
    echo functions::display_user($res, $array); 
    echo '</div>';
    ++$i;
  }
  
  
  echo '<div class="phdr">Файлов: '.$total.'</div>';
  if ($total > $kmess)
    echo '<div class="topmenu">'.functions::display_pagination('index.php?act=files&amp;'.$url, $start, $total, $kmess).'</div>';
}else{
  echo '<div class="list1">Пусто</div>';
}

require_once('../incfiles/end.php');