【discuzx2】forum_index.php文件的分析

<?php

/**
 *      [Discuz!] (C)2001-2099 Comsenz Inc.
 *      This is NOT a freeware, use is subject to license terms
 *
 *      $Id: forum_index.php 29580 2012-04-20 02:53:59Z svn_project_zhangjie $
 */

//mod文件只能被入口文件引用,不能直接访问
if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}

/**
* 返回库文件的全路径
*
* @param string $libname 库文件分类及名称
* @param string $folder 模块目录'module','include','class'
* @return string
*
* @example require DISCUZ_ROOT.'./source/function/function_cache.php'
* @example 我们可以利用此函数简写为:require libfile('function/cache');
* 再如:require_once libfile('space/'.$do, 'include');//包含 "/source/include/space/space_profile.php" 文件
* 1、libfile函数返回库文件的全路径
* 2、包含文件路径格式:"/source/include/space/space_$do.php"
* 3、require_once "返回的库文件的全路径",这样就可以将库文件包含进来
*/
require_once libfile('function/forumlist');//引入"/source/function/function_forumlist.php"函数库文件

$gid = intval(getgpc('gid'));//默认值为0:分区id;何为分区?简单说,就是凌驾与版块之上的一个分类
$showoldetails = get_index_online_details();//默认情况下为空值,不知道怎么回事

//论坛首页缓存更细时间,0-不开启
//uid-管理员id   gid-分区id   cacheindexlife-首页缓存时间
//gid为空的情况
if(!$_G['uid'] && !$gid && $_G['setting']['cacheindexlife'] && !defined('IN_ARCHIVER') && !defined('IN_MOBILE')) {
	get_index_page_guest_cache();//此代码只针对游客
}

$newthreads = round((TIMESTAMP - $_G['member']['lastvisit'] + 600) / 1000) * 1000;
//初始化数组变量
$catlist = $forumlist = $sublist = $forumname = $collapse = $favforumlist = array();
//初始化普通变量:主题、帖子、今日帖子、短消息
$threads = $posts = $todayposts = $announcepm = 0;
//定义发帖数量
$postdata = $_G['cache']['historyposts'] ? explode("\t", $_G['cache']['historyposts']) : array(0,0);
$postdata[0] = intval($postdata[0]);//昨日发帖
$postdata[1] = intval($postdata[1]);//历史最高发帖

//创建站点栏目名称、描述、关键字
list($navtitle, $metadescription, $metakeywords) = get_seosetting('forum');
if(!$navtitle) {//$navtitle-默认值为“论坛”
	$navtitle = $_G['setting']['navs'][2]['navname'];
	$nobbname = false;
} else {
        //默认执行
	$nobbname = true;
}
//描述
if(!$metadescription) {
	$metadescription = $navtitle;
}
//关键字
if(!$metakeywords) {
	$metakeywords = $navtitle;
}

//heatthread:热帖
if($_G['setting']['indexhot']['status'] && $_G['cache']['heats']['expiration'] < TIMESTAMP) {
	require_once libfile('function/cache');//引入"/source/function/function_cache.php"函数库文件
	updatecache('heats');
}

if($_G['uid'] && empty($_G['cookie']['nofavfid'])) {
	$favfids = array();
	$forum_favlist = C::t('home_favorite')->fetch_all_by_uid_idtype($_G['uid'], 'fid');
	if(!$forum_favlist) {
		dsetcookie('nofavfid', 1, 31536000);
	}
	foreach($forum_favlist as $key => $favorite) {
		if(defined('IN_MOBILE')) {
			$forum_favlist[$key]['title'] = strip_tags($favorite['title']);
		}
		$favfids[] = $favorite['id'];
	}
	if($favfids) {
		$favforumlist = C::t('forum_forum')->fetch_all($favfids);
		foreach($favforumlist as $id => $forum) {
			forum($favforumlist[$id]);
		}
	}
}


//默认情况下不执行,即:直接跳过这段if代码段
if(empty($gid) && empty($_G['member']['accessmasks']) && empty($showoldetails)) {
	extract(get_index_memory_by_groupid($_G['member']['groupid']));
	if(defined('FORUM_INDEX_PAGE_MEMORY') && FORUM_INDEX_PAGE_MEMORY) {
		categorycollapse();
		if(!defined('IN_ARCHIVER')) {
			include template('diy:forum/discuz');//引入模板文件的方式
		} else {
			include loadarchiver('forum/discuz');
		}
		dexit();
	}
}

//默认情况下执行此if代码段;FORUM_INDEX_PAGE_MEMORY是首页所有缓存数据的一个常量
//gid为空的情况             FORUM_INDEX_PAGE_MEMORY常量
if(!$gid && (!defined('FORUM_INDEX_PAGE_MEMORY') || !FORUM_INDEX_PAGE_MEMORY)) {
	/*格式:
         * <li><span><a href="">公告1</a></span></li>
         * <li><span><a href="">公告2</a></span></li>
         */
        $announcements = get_index_announcements();//首页公告,此函数的定义在当前文件底部

        //通过状态值查询所有开启的版块或分区相关信息,可查看桌面$forums = Ct('forum_forum')-fetch_all_by_status(1).txt文件
	//对应pre_forum_forum数据表中的记录
        $forums = C::t('forum_forum')->fetch_all_by_status(1);
	//初始化一个数组变量
        $fids = array();
	foreach($forums as $forum) {//$forum-代表每一个开启的分区或版块相关的信息数组,一般为一维数组
            //分区或版块id数组,格式:$fid[分区或版块id]=分区或版块id;$fids[1]=1
            $fids[$forum['fid']] = $forum['fid'];//启用的分区id或版块id
	}

        //初始化一个版块访问权限数组变量
	$forum_access = array();
	if(!empty($_G['member']['accessmasks'])) {//用户访问权限标志,默认值为1
		$forum_access = C::t('forum_access')->fetch_all_by_fid_uid($fids, $_G['uid']);
	}
        //获取版块扩展字段信息,对应数据表:pre_forum_forumfield,查看桌面 $forum_fields.txt文件
	$forum_fields = C::t('forum_forumfield')->fetch_all($fids);
	foreach($forums as $forum) {
		if($forum_fields[$forum['fid']]['fid']) {//分区或版块id
                        //将分区或版块的通用信息字段或扩展字段信息合并在一起
                        //即将pre_forum_forum表的字段与pre_forum_fields表的字段合并在一起
			$forum = array_merge($forum, $forum_fields[$forum['fid']]);//一维数组
		}
		if($forum_access['fid']) {
			$forum = array_merge($forum, $forum_access[$forum['fid']]);
		}
                //版块或分区的名称
		$forumname[$forum['fid']] = strip_tags($forum['name']);
                //反序列化的pre_forum_forumfields表中extra字段的值
		$forum['extra'] = empty($forum['extra']) ? array() : dunserialize($forum['extra']);
		if(!is_array($forum['extra'])) {
			$forum['extra'] = array();//数组
		}
                
                //版块的情况,不包括分区的情况
		if($forum['type'] != 'group') {//版块数组信息的情况   group-分区  forum-版块
                    
			$threads += $forum['threads'];//主题数量
			$posts += $forum['posts'];//帖子数量
			$todayposts += $forum['todayposts'];//今日发帖数量

                        //版块的情况
			if($forum['type'] == 'forum' && isset($catlist[$forum['fup']])) {//版块存在上级分区的情况
                                //forum函数的定义位置:"/source/function/function_forumlist.php"函数库文件
				if(forum($forum)) {
					$catlist[$forum['fup']]['forums'][] = $forum['fid'];//分区下的所有的版块id
                                        /*如下形式:
                                         * [forums] => Array         //当前分区下版块id数组,即是fid数组
                                            (
                                                [0] => 2         //娱乐
                                                [1] => 36        //文化
                                                [2] => 37        //体育
                                                [3] => 38        //艺术
                                                [4] => 39        //商界
                                            )
                                         */
                                        //排序id
					$forum['orderid'] = $catlist[$forum['fup']]['forumscount']++;
					//子版块
                                        $forum['subforums'] = '';
					$forumlist[$forum['fid']] = $forum;//仅版块数组 $forumlist,可查看桌面$forumlist.txt文件
				}

			} elseif(isset($forumlist[$forum['fup']])) {//$forum['fup']=64 的情况,即:女装版块
                                //女装版块下的子版块数组信息
				$forumlist[$forum['fup']]['threads'] += $forum['threads'];//主题数量
				$forumlist[$forum['fup']]['posts'] += $forum['posts'];//帖子数量
				$forumlist[$forum['fup']]['todayposts'] += $forum['todayposts'];//今日发帖数
				if($_G['setting']['subforumsindex'] && $forumlist[$forum['fup']]['permission'] == 2 && !($forumlist[$forum['fup']]['simple'] & 16) || ($forumlist[$forum['fup']]['simple'] & 8)) {
					//绑定的域名
                                        $forumurl = !empty($forum['domain']) && !empty($_G['setting']['domain']['root']['forum']) ? 'http://'.$forum['domain'].'.'.$_G['setting']['domain']['root']['forum'] : 'forum.php?mod=forumdisplay&fid='.$forum['fid'];
					$forumlist[$forum['fup']]['subforums'] .= (empty($forumlist[$forum['fup']]['subforums']) ? '' : ', ').'<a href="'.$forumurl.'" '.(!empty($forum['extra']['namecolor']) ? ' style="color: ' . $forum['extra']['namecolor'].';"' : '') . '>'.$forum['name'].'</a>';
				}
			}

		} else {//分区的情况,不包含版块的情况

			if($forum['moderators']) {//版主
			 	$forum['moderators'] = moddisplay($forum['moderators'], 'flat');
			}
			$forum['forumscount'] 	= 0;
			$catlist[$forum['fid']] = $forum;//分区数组信息

		}
	}
        //销毁不用的数组变量
	unset($forum_access, $forum_fields);

        //循环分区二维数组信息   分区id  一维数组分区信息
	foreach($catlist as $catid => $category) {
		$catlist[$catid]['collapseimg'] = 'collapsed_no.gif';//伸张或收缩图片
		if($catlist[$catid]['forumscount'] && $category['forumcolumns']) {
			$catlist[$catid]['forumcolwidth'] = (floor(100 / $category['forumcolumns']) - 0.1).'%';
			$catlist[$catid]['endrows'] = '';
			if($colspan = $category['forumscount'] % $category['forumcolumns']) {
				while(($category['forumcolumns'] - $colspan) > 0) {
					$catlist[$catid]['endrows'] .= '<td width="'.$catlist[$catid]['forumcolwidth'].'"> </td>';
					$colspan ++;
				}
				$catlist[$catid]['endrows'] .= '</tr>';
			}
		} elseif(empty($category['forumscount'])) {
			unset($catlist[$catid]);
		}
	}
	unset($catid, $category);

	if(isset($catlist[0]) && $catlist[0]['forumscount']) {
		$catlist[0]['fid'] = 0;
		$catlist[0]['type'] = 'group';
		$catlist[0]['name'] = $_G['setting']['bbname'];
		$catlist[0]['collapseimg'] = 'collapsed_no.gif';
	} else {
		unset($catlist[0]);
	}

	if(!IS_ROBOT && ($_G['setting']['whosonlinestatus'] == 1 || $_G['setting']['whosonlinestatus'] == 3)) {
		$_G['setting']['whosonlinestatus'] = 1;

		$onlineinfo = explode("\t", $_G['cache']['onlinerecord']);//在线访问记录.Array ( [0] => 2 [1] => 1378968576 )
            
		if(empty($_G['cookie']['onlineusernum'])) {//在线用户数
			$onlinenum = C::app()->session->count();
			if($onlinenum > $onlineinfo[0]) {
				$onlinerecord = "$onlinenum\t".TIMESTAMP;
				C::t('common_setting')->update('onlinerecord', $onlinerecord);
				savecache('onlinerecord', $onlinerecord);
				$onlineinfo = array($onlinenum, TIMESTAMP);
			}
			dsetcookie('onlineusernum', intval($onlinenum), 300);
		} else {
			$onlinenum = intval($_G['cookie']['onlineusernum']);
		}
		$onlineinfo[1] = dgmdate($onlineinfo[1], 'd');

		$detailstatus = $showoldetails == 'yes' || (((!isset($_G['cookie']['onlineindex']) && !$_G['setting']['whosonline_contract']) || $_G['cookie']['onlineindex']) && $onlinenum < 500 && !$showoldetails);

		$guestcount = $membercount = 0;
		if(!empty($_G['setting']['sessionclose'])) {
			$detailstatus = false;
			$membercount = C::app()->session->count(1);
			$guestcount = $onlinenum - $membercount;
		}

		if($detailstatus) {
			$actioncode = lang('action');

			$_G['uid'] && updatesession();
			$whosonline = array();

			$_G['setting']['maxonlinelist'] = $_G['setting']['maxonlinelist'] ? $_G['setting']['maxonlinelist'] : 500;
			foreach(C::app()->session->fetch_member(1, 0, $_G['setting']['maxonlinelist']) as $online){
				$membercount ++;
				if($online['invisible']) {
					$invisiblecount++;
					continue;
				} else {
					$online['icon'] = !empty($_G['cache']['onlinelist'][$online['groupid']]) ? $_G['cache']['onlinelist'][$online['groupid']] : $_G['cache']['onlinelist'][0];
				}
				$online['lastactivity'] = dgmdate($online['lastactivity'], 't');
				$whosonline[] = $online;
			}
			if(isset($_G['cache']['onlinelist'][7]) && $_G['setting']['maxonlinelist'] > $membercount) {
				foreach(C::app()->session->fetch_member(2, 0, $_G['setting']['maxonlinelist'] - $membercount) as $online){
					$online['icon'] = $_G['cache']['onlinelist'][7];
					$online['username'] = $_G['cache']['onlinelist']['guest'];
					$online['lastactivity'] = dgmdate($online['lastactivity'], 't');
					$whosonline[] = $online;
				}
			}
			unset($actioncode, $online);

			if($onlinenum > $_G['setting']['maxonlinelist']) {
				$membercount = C::app()->session->count(1);
				$invisiblecount = C::app()->session->count_invisible();
			}

			if($onlinenum < $membercount) {
				$onlinenum = C::app()->session->count();
				dsetcookie('onlineusernum', intval($onlinenum), 300);
			}

			$invisiblecount = intval($invisiblecount);
			$guestcount = $onlinenum - $membercount;

			unset($online);
		}

	} else {
		$_G['setting']['whosonlinestatus'] = 0;
	}

	if(defined('FORUM_INDEX_PAGE_MEMORY') && !FORUM_INDEX_PAGE_MEMORY) {
		$key = !IS_ROBOT ? $_G['member']['groupid'] : 'for_robot';
		memory('set', 'forum_index_page_'.$key, array(
			'catlist' => $catlist,//分区数组列表,不包含版块,此数组对应版块扩展表及版块表-pre_forum_forumfield、pre_forum_forum的字段
			'forumlist' => $forumlist,//无下级版块的版块信息数组
			'sublist' => $sublist,//子版块列表
			'whosonline' => $whosonline,//谁在线
			'onlinenum' => $onlinenum,//在线人数
			'membercount' => $membercount,//会员数
			'guestcount' => $guestcount,//游客数
			'announcements' => $announcements,//公告
			'threads' => $threads,//主题数
			'posts' => $posts,//帖子数
			'todayposts' => $todayposts,//今天发表帖子数
			'onlineinfo' => $onlineinfo,//在线信息
			'announcepm' => $announcepm), getglobal('setting/memory/forumindex'));
	}

} else {
	require_once DISCUZ_ROOT.'./source/include/misc/misc_category.php';
}


if(defined('IN_ARCHIVER')) {
	include loadarchiver('forum/discuz');
	exit();
}

//版块伸张或收缩
categorycollapse();

/*
 * 1、gid-分区id不为空的情况,如果gid为空时,不会执行下面if...else...中的代码段
 * 2、$catlist:查看桌面
        echo "<pre>";
        print_r($catlist);
        exit;
 * 3、$_G['category']全局变量的生成
 * 4、$_G['fid']全局变量的生成
 */
//gid不为空的情况
if($gid && !empty($catlist)) {//当前分区版块
	$_G['category'] = $catlist[$gid];//gid-分区id        当前分区数组数据,包括所有的子版块fid
	$forumseoset = array(//当前分区的SEO设置
		'seotitle' => $catlist[$gid]['seotitle'],//分区SEO标题
		'seokeywords' => $catlist[$gid]['keywords'],//分区SEO关键字
		'seodescription' => $catlist[$gid]['seodescription']//分区SEO描述
	);
	$seodata = array('fgroup' => $catlist[$gid]['name']);//当前分区名称
        //生成当前分区的SEO信息
	list($navtitle, $metadescription, $metakeywords) = get_seosetting('threadlist', $seodata, $forumseoset);
	if(empty($navtitle)) {
		$navtitle = $navtitle_g;
		$nobbname = false;
	} else {//默认执行
		$nobbname = true;
	}
	$_G['fid'] = $gid;//当前分区id
}

/*
 * 1、对应模板文件:/data/diy/forum/discuz.htm  或者 /template/default/forum/discuz.htm,两者选其一
 * 2、使用template()函数显示已经存在的模板
 * 3、格式 include template("模板文件夹/模板名称无后缀");的方式进行解析
 * 4、在 source/function/function_core.php文件中定义
 * 5、如:include template('forum/mytest'); //使用自定义模板套系中的forum目录的mytest.htm
 */
include template('diy:forum/discuz:'.$gid);//结果:template("diy:forum/discuz:0")

/*
 * 1、获取首页公告
 */
function get_index_announcements() {
	global $_G;//超级全局变量
	$announcements = '';//公告变量
	if($_G['cache']['announcements']) {//公告数组
		$readapmids = !empty($_G['cookie']['readapmid']) ? explode('D', $_G['cookie']['readapmid']) : array();
		foreach($_G['cache']['announcements'] as $announcement) {
                        //公告结束时间不为空,公告结束时间大于当前时间
			if(!$announcement['endtime'] || $announcement['endtime'] > TIMESTAMP && (empty($announcement['groups']) || in_array($_G['member']['groupid'], $announcement['groups']))) {
				if(empty($announcement['type'])) {//公告类型为空的情况
					$announcements .= '<li><span><a href="forum.php?mod=announcement&id='.$announcement['id'].'" target="_blank" class="xi2">'.$announcement['subject'].
						'</a></span><em>('.dgmdate($announcement['starttime'], 'd').')</em></li>';
				} elseif($announcement['type'] == 1) {//公告类型为1的情况
					$announcements .= '<li><span><a href="'.$announcement['message'].'" target="_blank" class="xi2">'.$announcement['subject'].
						'</a></span><em>('.dgmdate($announcement['starttime'], 'd').')</em></li>';
				}
			}
		}
	}
	return $announcements;
}

/*
 * 1、获取游客数据缓存
 */
function get_index_page_guest_cache() {
	global $_G;
	$indexcache = getcacheinfo(0);
	if(TIMESTAMP - $indexcache['filemtime'] > $_G['setting']['cacheindexlife']) {
		@unlink($indexcache['filename']);
		define('CACHE_FILE', $indexcache['filename']);
	} elseif($indexcache['filename']) {
		@readfile($indexcache['filename']);
		$updatetime = dgmdate($indexcache['filemtime'], 'H:i:s');
		$gzip = $_G['gzipcompress'] ? ', Gzip enabled' : '';
		echo "<script type=\"text/javascript\">
			if($('debuginfo')) {
				$('debuginfo').innerHTML = '. This page is cached  at $updatetime $gzip .';
			}
			</script>";
		exit();
	}
}

/*
 * 1、获取论坛首页缓存
 */
function get_index_memory_by_groupid($key) {
	$enable = getglobal('setting/memory/forumindex');
	if($enable !== null && memory('check')) {
		if(IS_ROBOT) {
			$key = 'for_robot';
		}
		$ret = memory('get', 'forum_index_page_'.$key);
		define('FORUM_INDEX_PAGE_MEMORY', $ret ? 1 : 0);
		if($ret) {
			return $ret;
		}
	}
	return array('none' => null);
}

/*
 * 1、获取首页在线信息详情
 */
function get_index_online_details() {
	$showoldetails = getgpc('showoldetails');
	switch($showoldetails) {
		case 'no': dsetcookie('onlineindex', ''); break;
		case 'yes': dsetcookie('onlineindex', 1, 86400 * 365); break;
	}
	return $showoldetails;
}

/*
 * 1、版块绑定的域名
 */
function do_forum_bind_domains() {
	global $_G;
	if($_G['setting']['binddomains'] && $_G['setting']['forumdomains']) {
		$loadforum = isset($_G['setting']['binddomains'][$_SERVER['HTTP_HOST']]) ? max(0, intval($_G['setting']['binddomains'][$_SERVER['HTTP_HOST']])) : 0;
		if($loadforum) {
			dheader('Location: '.$_G['setting']['siteurl'].'/forum.php?mod=forumdisplay&fid='.$loadforum);
		}
	}
}

//版块伸张或收缩功能函数
function categorycollapse() {
	global $_G, $collapse, $catlist;
	if(!$_G['uid']) {
		return;
	}
        //此时 fid-分区id   forum-可以查看桌面
        //如果是首页,那么就会有多个分区
	foreach($catlist as $fid => $forum) {
		if(!isset($_G['cookie']['collapse']) || strpos($_G['cookie']['collapse'], '_category_'.$fid.'_') === FALSE) {
			$catlist[$fid]['collapseimg'] = 'collapsed_no.gif';
			$collapse['category_'.$fid] = '';
		} else {
			$catlist[$fid]['collapseimg'] = 'collapsed_yes.gif';//伸展或收缩图片
			$collapse['category_'.$fid] = 'display: none';//跟html标签的id属性值有关
		}
	}
	if(!isset($_G['cookie']['collapse']) || strpos($_G['cookie']['collapse'], '_category_0_') === FALSE) {
		$collapse['collapseimg_0'] = 'collapsed_no.gif';//伸展或收缩图片
		$collapse['category_0'] = '';
	} else {
		$collapse['collapseimg_0'] = 'collapsed_yes.gif';//伸展或收缩图片
		$collapse['category_0'] = 'display: none';
	}
}
?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值