<?php

if($_G['pa_count'] < 1) {

    odz_error('parameters_error', -112);

}



require_once libfile('function/forumlist');

require_once './includes/functions/forumlist.php';

odz_loadforum();

//版块密码验证

if($_G['forum']['password']) {

        if(!$_GET['pw']){

                odz_error('need_password',$code=400);

        }

        if($_GET['pw'] != $_G['forum']['password']) {

                showmessage('forum_passwd_incorrect');

        }

}

$subexists = 0;



if(!$_G['cache']['forums']){

	loadcache('forums');

}



foreach($_G['cache']['forums'] as $sub) {

	if($sub['type'] == 'sub' && $sub['fup'] == $_G['fid'] && (!$_G['setting']['hideprivate'] || !$sub['viewperm'] || forumperm($sub['viewperm']) || strstr($sub['users'], "\t$_G[uid]\t"))) {

		if(!$sub['status']) {

			continue;

		}

		$subexists = 1;

		$sublist = array();

		$sql = !empty($_G['member']['accessmasks']) ? "SELECT f.fid, f.fup, f.type, f.name, f.threads, f.posts, f.todayposts, f.lastpost, f.domain, ff.description, ff.moderators, ff.icon, ff.viewperm, ff.extra, ff.redirect, a.allowview FROM ".DB::table('forum_forum')." f

						LEFT JOIN ".DB::table('forum_forumfield')." ff ON ff.fid=f.fid

						LEFT JOIN ".DB::table('forum_access')." a ON a.uid='$_G[uid]' AND a.fid=f.fid

						WHERE fup='$_G[fid]' AND status>'0' AND type='sub' ORDER BY f.displayorder"

					: "SELECT f.fid, f.fup, f.type, f.name, f.threads, f.posts, f.todayposts, f.lastpost, f.domain, ff.description, ff.moderators, ff.icon, ff.viewperm, ff.extra, ff.redirect FROM ".DB::table('forum_forum')." f

						LEFT JOIN ".DB::table('forum_forumfield')." ff USING(fid)

						WHERE f.fup='$_G[fid]' AND f.status>'0' AND f.type='sub' ORDER BY f.displayorder";

		$query = DB::query($sql);

		while($sub = DB::fetch($query)) {

			$sub['extra'] = unserialize($sub['extra']);

			if(!is_array($sub['extra'])) {

				$sub['extra'] = array();

			}

			if(odz_forum($sub)) {

				$sub['orderid'] = count($sublist);

				$sublist[] = $sub;

			}

		}

		break;

	}

}





// 组织返回数据

$tmp_sublist = array();

foreach($sublist as $sub) {

    if (empty($sub['icon'])) {

        $sub['icon'] = $_G['siteurl'].'assets/img/forum'.(empty($sub['folder']) ? '' : '_new').'.png';

    }

    $msg = odz_lang('forum_desc_empty');

    $tmp_sublist[] = array(

        'fid' => $sub['fid'],

        'name' => strip_tags($sub['name']),

        'icon' => $sub['icon'],

        'description' => empty($sub['description']) ? $msg : strip_tags($sub['description']),

        'threads' => $sub['threads'],

        'posts' => $sub['posts'],

        'todayposts' => $sub['todayposts'],

        'lastpost' => array(

            'author' => $sub['lastpost']['authorusername'],

            'dateline' => strip_tags($sub['lastpost']['dateline']),

            'subject' => $sub['lastpost']['subject'],

            'tid' => (int) $sub['lastpost']['tid']

        )

    );

}

unset($sublist);



// 广告信息

$advert = array();

if ($_GET['adverttype']) {

	$advert = odz_getadvertlist('threadlist');

	if(!empty($advert) && isset($advert['typedata'])) {

		$advert['typedata'] = json_decode($advert['typedata']);

	}

	$resultdata['advert']['result'][] = $advert;

}else{

	$resultdata['advert']['result'] = array();

}



$resultdata['sublist'] = $tmp_sublist;





odz_result($resultdata);

?>