- <?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';
- }
- }
- ?>
【discuzx2】forum_index.php文件的分析
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.