<?php
/*
 BMForum Datium! Bulletin Board Systems
 Version : Datium!
 
 This is a freeware, but don't change the copyright information.
 A SourceForge Project.
 Web Site: http://www.bmforum.com
 Copyright (C) Bluview Technology
*/
require_once ("datafile/config.php");
include_once ("include/template.php");
require_once ("getskin.php");
require_once ("include/forums.func.php");
$listfilename = "list.php";

// ===================================
// Get Forum Info
// ===================================
get_forum_info($sxfourmrow);


$up_id = $forum_upid;
for($i = 0;$i < $forumscount;$i++) {
    if ($up_id == $sxfourmrow[$i]['id']) {
        $up_name = $sxfourmrow[$i]['bbsname'];
        break;
    } 
} 

$amount = $thisamount;

// ===================================
// Language Pack
// ===================================

require("lang/$language/forums.php");
// ===================================
// Safe Filter
// ===================================
if ($listby != "hit" && $listby != "reply" && $listby != "posttime" && $listby != "author" && $listby != "title") $listby = "";
if ($jinhua != "jinhua" && !empty($jinhua) && !is_numeric($jinhua)) $jinhua = "";
if (isset($page) && !is_numeric($page)) $page = 1;
// ===================================
// Script to read post
// ===================================

if ($aviewpost == "openit") {
    $filetopn = "article.php";
} else {
    $filetopn = "topic.php";
} 
// ===================================
// Login for forum
// ===================================

if ($login == "ok") {
    echo '<meta http-equiv="Refresh" content="' . $refresh_allowed . '; URL=forums.php?forumid=' . $forumid . '">Checking...';
    exit;
} 
// ===================================
// Load Template
// ===================================

$lang_zone = array("infoan"=>$infoan, "listbyyy"=>$listbyyy, "forum_picie"=>$forum_picie, "forum_pos"=>$forum_pos, "jsinfo"=>$jsinfo, "pageinfo"=>$pageinfo, "forum_pos"=>$forum_pos, "forum_line"=>$forum_line, "bm_skin"=>$bm_skin, "otherimages"=>$otherimages, "online_info_show"=>$online_info_show, "listbyyy"=>$listbyyy, "coninfo"=> $coninfo, "maninfo" => $maninfo);
$template_name = newtemplate("forums", $temfilename, $styleidcode, $lang_zone);

$readed_t = explode("|", $_SESSION['readpost']);

if ($trash == "trash") $listfilename = "ttrash";

// ===================================
// Upload file daily
// ===================================

if ($login_status != 2 && $login_status != 0) {
    $lastuploadtime = gmdate("zY", $clastupload + $bbsdetime * 60 * 60);
    $lastuploadtime_a = gmdate("zY", $timestamp + $bbsdetime * 60 * 60);
    if ($lastuploadtime != $lastuploadtime_a) {
        $uploadfiletoday = 0;
    } 
} 

// ===================================
// Process data
// ===================================

if ((empty($forumid) || empty($forum_type) || $forum_type == "category") && $trash != "trash") {
	if ($ajax_online == 1) ajax_browse_error($error[2]);
	error_page($error[0], $error[1], $error[0], $error[2]);
} 
if (empty($page)) $page = 1;

check_forum_permission(0,0,"",1);

if ($trash == "trash" && ($view_recybin != "1" || $userpoint < $recy_allow_ww)) {
	if ($ajax_online == 1) ajax_browse_error($gl[438]);
	error_page($error[3],
        "<a href=\"forums.php?forumid=$forumid\">$forum_name</a>", $gl[192], $gl[438], $gl[192]);
} 
// ===================================
// Password required
// ===================================

if ($forum_pwd <> "" && $forum_pwd <> "d41d8cd98f00b204e9800998ecf8427e" && $job <> "login" && $_COOKIE['b' . $forumid . 'mb'] <> $forum_pwd) {
	if ($ajax_online == 1) ajax_browse_error($error[8]);
    @header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
    @header("Cache-Control: no-cache, must-revalidate");
    @header("Pragma: no-cache");
    require("header.php");
    error_page($forum_name, $error[7], $error[8], "<form action='forums.php?forumid=$forumid&amp;job=login' method='post'>&nbsp;{$error[9]}
<input type='password' name='loginforumpwd' size='10' />
<input type='submit' value=\"GO\" /></form>
");

} 

if ($job == "login") {
	if ($ajax_online == 1) ajax_browse_error($error[0]);
    $loginforumpwd = md5($loginforumpwd);

    if ($loginforumpwd == $forum_pwd) {
        $_SESSION["b" . $forumid . "mb"] = $loginforumpwd;
        setcookie("b" . $forumid . "mb", $loginforumpwd, 0, $cookie_p, $cookie_d);
        jump_page('forums.php?login=ok&amp;forumid=' . $forumid, $tip[0], "<strong>$tip[0]</strong><br /><br />$tip[1] <a href=forums.php?login=ok&amp;forumid=$forumid>$tip[2]</a>", 3);
    } else {
        @header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
        @header("Cache-Control: no-cache, must-revalidate");
        @header("Pragma: no-cache");
        error_page($forum_name, $error[0], $error[0], "$tip[3]<form action='forums.php?forumid=$forumid&amp;job=login' method='post'>&nbsp;$tip[4] 
<input type='password' name='loginforumpwd' size='10' />
<input type='submit' value=\"$error[10]\" />
");

    } 
    exit;
} 
$load_ajax_script = 0;
if ($view_forum_online && $allow_ajax_browse && $ajax_online) {
	$load_ajax_script = 1;
    whosonline();
	require($template_name);
    exit;
}


// ===================================
// END / Title Started
// ===================================
$oldbbs_title = $bbs_title;
$bbs_title = strip_tags($forum_name) . " &lt; " . strip_tags($bbs_title);
require("header.php");
$bbs_title = $oldbbs_title;

// ===================================
// Nav bar
// ===================================

$prefix_file = ($bmfopt['rewrite']) ? "forums_" : "forums.php?forumid=";

if ($listfilename == "ttrash") {
    $navimode = newmode;


    if (empty($up_name)) {
        $snavi_bar[0] = "<a href='{$prefix_file}$forumid'>$forum_name</a>";
        $snavi_bar[1] = $tip[9];
        $des = $tip[8];
        navi_bar();
    } else {
        $snavi_bar[0] = "<a href='{$prefix_file}$up_id'>$up_name</a>";
        $snavi_bar[1] = "<a href='{$prefix_file}$forumid'>$forum_name</a>";
        $snavi_bar[2] = $tip[9];
        $des = $tip[8];
        navi_bar();
    } 
} else
if ($jinhua == "jinhua") {
    $navimode = newmode;
    if (empty($up_name)) {
        $snavi_bar[0] = "<a href='{$prefix_file}$forumid'>$forum_name</a>";
        $snavi_bar[1] = $tip[5];
        $des = $tip[6];
        navi_bar();
    } else {
        $snavi_bar[0] = "<a href='{$prefix_file}$up_id'>$up_name</a>";
        $snavi_bar[1] = "<a href='{$prefix_file}$forumid'>$forum_name</a>";
        $snavi_bar[2] = $tip[5];
        $des = $tip[6];
        navi_bar();
    } 
} else {
	$fix_file = ($bmfopt['rewrite']) ? "_1_{$jinhua}__" : "&amp;jinhua=$jinhua";
	
    if (empty($up_name)) {
        $navimode = newmode;
        $des = $tip[7];
        $snavi_bar[0] = "<a href='{$prefix_file}$forumid{$fix_file}'>$forum_name</a>";
        navi_bar();
    } else {
        $navimode = newmode;
        $des = $tip[7];
        $snavi_bar[0] = "<a href='{$prefix_file}$up_id{$fix_file}'>$up_name</a>";
        $snavi_bar[1] = "<a href='{$prefix_file}$forumid{$fix_file}'>$forum_name</a>";
        navi_bar();
    } 
} 
// ===================================
// Call Online Function
// ===================================

if ($view_forum_online) whosonline();

// ===================================
// Forum Rules
// ===================================

@include("datafile/rules/" . basename($forumid) . ".php");

// ===================================
// Sub forum display and call the function
// ===================================
$forum_admin_1 = "";

if ($fcaterows) {
	$current_width	=	floor(100/$fcaterows);
}

for($i = 0;$i < $forumscount;$i++) {
    $fourmrow = $sxfourmrow[$i];
    if ($fourmrow['id'] == $forumid || $fourmrow['id'] == $forum_cid || $fourmrow['id'] == $forum_upid) {
        $forum_admin_1 .= $fourmrow['adminlist'];
    } 
    if ($fourmrow['blad'] == $forumid) {
		if ($bmfopt['hidebyug'] && !check_forum_permission(0, 1, $fourmrow)) {
			continue;
		}
        if ($fourmrow['type'] == 'subforum' || $fourmrow['type'] == 'subjump' || $fourmrow['type'] == 'subselection' || $fourmrow['type'] == 'subformer' || $fourmrow['type'] == 'sublocked' || $fourmrow['type'] == 'subclosed') {
            if (!$oplined) {
                if ($fcaterows) {
                    $cate_rows_type = 1;
                }
                $oplined = 1;
            } 
            if ($fcaterows) forum_line_row($fourmrow['type'], $fourmrow, $nresult);
            else forum_line($fourmrow['type'], $fourmrow);
        } 
        if (check_permission($username, $fourmrow['type']) && ($fourmrow['type'] == 'subhidden' ||$fourmrow['type'] == 'subforumhid')) {
            if (!$oplined) {
                if ($fcaterows) {
                    $cate_rows_type = 1;
                }
                $oplined = 1;
            } 
            if ($fcaterows) forum_line_row($fourmrow['type'], $fourmrow, $nresult);
            else forum_line($fourmrow['type'], $fourmrow);
        } 
    } 
} 

// ===================================
// Admin
// ===================================

$forum_admin = explode("|", $forum_admin_1);

// ===================================
// Anouncement
// ===================================

if ($bbs_news && $page == 1) {
    if (file_exists("datafile/announcement{$forumid}.php")) {
        $announcelist = file("datafile/announcement{$forumid}.php");
    } 
    $count = count($announcelist);
    for ($i = 0;$i < $count;$i++) {
        if ($i >= $set_forinfo) break;
        $ic = $i + 1;
        list($aauthor, $atitle, $atime, $acontent, $amember) = explode("|", $announcelist[$i]);
        $almdtime = get_date($atime) . ' ' . get_time($atime);
        $salmdtime = get_date($atime);
        $size_gg = utf8_strlen($acontent);
        $ag = $timestamp - $atime;
        $announceurl = 'announcesys.php?forumid=' . $forumid . '&amp;job=read&amp;msg=' . $i;
        $bbs_news_loop[] = array("announceurl" => $announceurl, "topic_name" => $atitle, "title" => $atitle, "almdtime" => $almdtime, "topic_author" => $aauthor, "salmdtime" => $salmdtime);
        $hereis_top = 1;
    } 
} 

// ===================================
// Forum Admin list
// ===================================

if (isset($forum_admin)) {
    $count = count($forum_admin);
    for ($i = 0; $i < $count; $i++) {
        if ($forum_admin[$i] != "" && !@in_array($forum_admin[$i], $loaded_admin)) {
        	$loaded_admin[]=$forum_admin[$i];
            $echoinfoa .= "<option value=\"" . urlencode($forum_admin[$i]) . "\">$forum_admin[$i]</option>";
        }
    } 
} else $echoinfoa .= "<option value=\"#\">$infoan[1]</option>";

$maxpageno = 1;

// ===================================
// Process Data
// ===================================

$orderby = "ORDER BY `toptype` DESC,`changetime` DESC";
$jinhuasql = $forumsql = $forum_cid_sql = $chooseby = $hasacount = "";
if ($trash == "trash") {
    $trashq = "AND ordertrash>0";
    $hasacount = $trashcount;
} else {
    $trashq = "AND ttrash=0";
    $jinhuasql = "islock > 1 AND";
    $hasacount = $digestcount;
} 

// ===================================
// List by ...
// ===================================

if ($listby) {
    if ($listby == "hit") $orderby = "ORDER BY `hits` DESC";
    if ($listby == "reply") $orderby = "ORDER BY `replys` DESC";
    if ($listby == "posttime") $orderby = "ORDER BY `time` DESC";
    if ($listby == "author") $orderby = "ORDER BY `author` DESC";
    if ($listby == "title") $orderby = "ORDER BY `title` DESC";
} 
if ($jinhua != "jinhua" && !empty($jinhua)) {
    $maxchange = $timestamp-86400 * $jinhua;
    $chooseby = " AND changetime>$maxchange";
    $jinhuasql = $hasacount = "";
} 

// ===================================
// Mode Choose
// ===================================

if ($jinhua != "" || $trash == "trash") {
    $thislimit = $perpage * $page;
    $lastlimit = $perpage * ($page-1);
    
    // ===================================
    // SQL Query
    // ===================================
   	if ($trash == "trash" && !$forumid && !$cateid) {
   		$forumsql = "forumid!='-1'";
   	} elseif($trash == "trash" && $cateid){
		for($n = 0;$n < $forumscount;$n++) {
		    if ($cateid == $sxfourmrow[$n]['forum_cid']) {
		        $forum_cid_sql .= $forum_cid_sql ? ",".$sxfourmrow[$n]['id'] : $sxfourmrow[$n]['id'];
		    } 
		} 
   		$forumsql = "forumid in($forum_cid_sql)";
   	} else $forumsql = "forumid='$forumid'";
   	
    if (!$hasacount) {
    	if (($trash == "trash" && !$forumid) || is_numeric($jinhua)) {
            $result = bmbdb_fetch_array(bmbdb_query("SELECT COUNT(tid) FROM {$database_up}threads WHERE $jinhuasql $forumsql $chooseby $trashq"));
            $count = $result['COUNT(tid)'];
    	} else $count = $ttopicnum;
    } else {
        $count = $hasacount;
    }
    if ($count > 0) {
        $query = "SELECT * FROM {$database_up}threads WHERE $jinhuasql $forumsql $trashq $chooseby $orderby LIMIT $lastlimit,$thislimit";
        $result = bmbdb_query($query);
    } else $perpage = 0;
    
    // ===================================
    // Pre-Process
    // ===================================

    for ($i = 0; $i < $perpage; $i++) {
        $row = bmbdb_fetch_array($result);
        if ($row['tid'] != "") $showto[] = $row;
    } 


    // ===================================
    // Display
    // ===================================

    if ($showto) {
        if ($count % $perpage == 0) $maxpageno = max(1, $count / $perpage);
        else $maxpageno = max(1, floor($count / $perpage) + 1);
        if ($page > $maxpageno) $page = $maxpageno;
        $pagemin = min(($page-1) * $perpage , $count-1);
        $pagemax = min($pagemin + $perpage-1, $count-1);
        $coutop = count($filenametop);
        for ($i = 0; $i < $perpage; $i++) {
            if ($showto[$i]['tid'] != "") {
            	$snputed = 1;
                article_line($showto[$i]);
            }
        } 
        if ($page == 1 && $snputed == 1) $outputing = str_replace("{showits}", $coninfo[14], $importtopic);
    } else {
        $maxpageno = 1;
    } 

} else {
	// ===================================
    // Normal Mode;
    // SQL Query
    // ===================================
    
    @include_once("datafile/cache/pin_thread.php");

    $query_pagea = "";
    if (empty($forum_cid)) $forum_cid = "XXXXXXXXXXXX";
    
    $intopthread = $count_pint["$forum_cid"] ? $topthread['ALL'] : substr($topthread['ALL'] ,0 ,-1);
    $intopthread .=  substr($topthread["$forum_cid"] ,0 ,-1);

    $total_pin = $count_pint['ALL'] + $count_pint["$forum_cid"];
    
    $counttopsnum = 0;
    
    if ($page == 1 && $total_pin > 0) {
        $query = "SELECT * FROM {$database_up}threads WHERE tid in ($intopthread) and toptype in (8,9) $trashq $chooseby $orderby";
        $xresult = bmbdb_query($query);
        $topsnum = bmbdb_num_rows($xresult);
    } elseif ($page != 1 && $total_pin > 0) {
        $query = "SELECT COUNT(tid) FROM {$database_up}threads WHERE tid in ($intopthread) and forumid=$forumid and toptype in (8,9) $trashq $chooseby $orderby";
        $xresult = bmbdb_query($query);
        $xresultrow = bmbdb_fetch_array($xresult);
        $topsnum = $xresultrow['COUNT(tid)'];
        $counttopsnum = $topsnum;
    } else {
        $counttopsnum = $topsnum;
    }
    
    
    $thislimit = $perpage * $page;
    $lastlimit = $perpage * ($page-1);
    
    if ($ttopicnum < $perpage) $perpage = $ttopicnum;

    $query = "SELECT * FROM {$database_up}threads WHERE forumid='$forumid' and toptype in (0,1) $trashq $chooseby $orderby LIMIT $lastlimit,$perpage";
    $result = bmbdb_query($query);
    $topicsn = bmbdb_num_rows($result) + $topsnum;
    $x = 0;
    $xxi = 0;
    // ===================================
    // pre-process
    // ===================================

    for ($x = 0;$x < $topicsn;$x++) {
        if ($x < $topsnum) {
        	$row = bmbdb_fetch_array($xresult);
        	if ($row['forumid'] == $forumid) $counttopsnum++;
        }else $row = bmbdb_fetch_array($result);
        
        if ($row['toptype'] == '9' || $row['toptype'] == 8) {
            $xtopshows[$xxi] = $row;
            $xxi++;
        } else {
            $showto[$x] = $row;
        } 
    } 
    
    $count = max(0, $ttopicnum - $counttopsnum);
    
    // ===================================
    // Stat posts
    // ===================================

    if ($count == 0) {
        $pagemin = $pagemax = $page = $maxpageno = 1;
    }else{
        if ($count % $perpage == 0) $maxpageno = max(1, $count / $perpage);
        else $maxpageno = max(1, floor($count / $perpage) + 1);
        if ($page == "last" || $page > $maxpageno) $page = $maxpageno;
        $pagemin = max(1, min(($page-1) * $perpage , $count-1));
        $pagemax = max(1, min($pagemin + $perpage-1, $count-1) + 1);
    }
    
    
    // ===================================
    // Display and check permission
    // ===================================

	if ($page == 1) {
		for ($xti = 0; $xti < $xxi; $xti++) {
			article_line($xtopshows[$xti]);
		} 
	} 
	for ($i = 0; $i < $topicsn; $i++) {
		if ($showto[$i]['tid'] != "") {
			article_line($showto[$i]);
		}
	} 
    // NEW CODE BY 15:14 2005-8-23
} 

if ($login_status == 1 && (($forum_admin && in_array($username, $forum_admin)) || $usertype[22] == "1" || $usertype[21] == "1")) {
	$admin_allow = 1;
} else $admin_allow = 0;


$bmf_threads = array_merge((array)$topinfooutput, (array)$quinfooutput, (array)$allinfooutput);


// ===================================
// Forum jumping
// ===================================

$count = $forumscount;
for ($i = 0; $i <= $count; $i++) {
	if ($bmfopt['hidebyug'] && !check_forum_permission(0, 1, $sxfourmrow[$i])) {
		continue;
	}
    if ($sxfourmrow[$i]['type'] == "category") $listsinfo .= "<option value='index.php?cateid={$sxfourmrow[$i]['id']}'>{$sxfourmrow[$i]['bbsname']}</option>";
    if ($sxfourmrow[$i]['type'] == "forum" || $sxfourmrow[$i]['type'] == "former" || $sxfourmrow[$i]['type'] == "jump" || $sxfourmrow[$i]['type'] == "selection" || $sxfourmrow[$i]['type'] == "locked" || $sxfourmrow[$i]['type'] == "closed") $listsinfo .= "<option value='forums.php?forumid={$sxfourmrow[$i]['id']}'>&nbsp;|- {$sxfourmrow[$i]['bbsname']}</option>";
    if (check_permission($username, $sxfourmrow[$i]['type']) && ($sxfourmrow[$i]['type'] == 'hidden' || $sxfourmrow[$i]['type'] == 'forumhid')) $listsinfo .= "<option value='forums.php?forumid={$sxfourmrow[$i]['id']}'>&nbsp;|- {$sxfourmrow[$i]['bbsname']}</option>";
    if ($sxfourmrow[$i]['type'] == "subforum" || $sxfourmrow[$i]['type'] == "subformer" || $sxfourmrow[$i]['type'] == "subjump"  || $sxfourmrow[$i]['type'] == "subselection" || $sxfourmrow[$i]['type'] == "sublocked" || $sxfourmrow[$i]['type'] == "subclosed") $listsinfo .= "<option value='forums.php?forumid={$sxfourmrow[$i]['id']}'>&nbsp;|-|- {$sxfourmrow[$i]['bbsname']}</option>";
    if (check_permission($username, $sxfourmrow[$i]['type']) && ($sxfourmrow[$i]['type'] == 'subhidden' || $sxfourmrow[$i]['type'] == 'subforumhid')) $listsinfo .= "<option value='forums.php?forumid={$sxfourmrow[$i]['id']}'>&nbsp;|-|- {$sxfourmrow[$i]['bbsname']}</option>";
} 

// ===================================
// Pages
// ===================================
$nextpage = $page + 1;
$previouspage = $page-1;
$maxpagenum = $page + 4;
$minpagenum = $page - 4;

$current_page_url = ($bmfopt['rewrite'] ? "forums_{$forumid}_1_{$jinhua}_{$listby}_{$trash}" : "forums.php?forumid=$forumid&amp;jinhua=$jinhua&amp;page=&amp;listby=$listby&amp;trash=$trash");
$pageinfos = "<td class=\"pagenumber\"><strong>&nbsp;&nbsp;{$page}/$maxpageno&nbsp;&nbsp;</strong></td><td class=\"pagenumber_2\" onmouseover=\"javascript:this.className='pagenumber_2 pagenumber_2_onmouseover';\" onmouseout=\"javascript:this.className='pagenumber_2';\" onclick=\"window.location='$current_page_url';\"><a href=\"$current_page_url\"><strong>«</strong></a></td>";
for ($i = $minpagenum; $i <= $maxpagenum; $i++) {
    if ($i > 0 && $i <= $maxpageno) {
        if ($i == $page) {
                $pageinfos .= "<td class=\"pagenumber_1\"><strong><u>$i</u></strong></td>\n";
        } else {
        	$current_page_url = ($bmfopt['rewrite'] ? "forums_{$forumid}_{$i}_{$jinhua}_{$listby}_{$trash}" : "forums.php?forumid=$forumid&amp;jinhua=$jinhua&amp;page=$i&amp;listby=$listby&amp;trash=$trash");
            $pageinfos .= "<td class=\"pagenumber_2\" onmouseover=\"javascript:this.className='pagenumber_2 pagenumber_2_onmouseover';\" onmouseout=\"javascript:this.className='pagenumber_2';\" onclick=\"window.location='$current_page_url';\"><a href=\"$current_page_url\">$i</a></td>\n";
        } 
    } 
} 
$current_page_url = ($bmfopt['rewrite'] ? "forums_{$forumid}_{$maxpageno}_{$jinhua}_{$listby}_{$trash}" : "forums.php?forumid=$forumid&amp;jinhua=$jinhua&amp;page=$maxpageno&amp;listby=$listby&amp;trash=$trash");
$pageinfos .= "<td class=\"pagenumber_2\" onmouseover=\"javascript:this.className='pagenumber_2 pagenumber_2_onmouseover';\" onmouseout=\"javascript:this.className='pagenumber_2';\" onclick=\"window.location='$current_page_url';\"><a href=\"$current_page_url\"><strong>»</strong></a></td>\n";



$nextpage = $page + 1;
$previouspage = $page-1;



// ===================================
// Fast New thread
// ===================================
$count_rows = $allow_fast_post = 0;

if ($fnew_select == 1) { // 开关 
	if (!($login_status == 1 && ($canpost != 1 || $userpoint < $post_allow_ww)) && !($login_status == 1 && !check_permission($username, $forum_type))) {
		$allow_fast_post = 1;
    	fast_new();
    }
} //开关


require($template_name);
require("footer.php");
exit;
