<?php
/*
\* ===========================================
\* 项目: CTB文本论坛
\* 版本:1.4
\* 团队: 实易数码
\* 小组: CTB文本论坛小组
\* 作者: 泡泡龙 (Felixsun)
\* 修改: 险情
\* 功能: 后台版块处理程式
\* ===========================================
\* Copyright (c) 2002
\* 团队主页: http://www.11cn.org
\* 团队信箱: ------
\* 创建日期: 2002/7/19
\* 修改日期: 2002-12-27
\* 修改说明: ------
\* 版权声明: 这个程序是免费软件.
\* ===========================================
*/

//=============================================================================
class Module extends CommonClass
{
    //构造函数
    function execute()
    {
      global $action;
      switch($this->input['action']) {
        //
        case 'forumStat':
            $this->forumStat();
            break;
        case 'refreshList':
        	$this->refreshList();
            break;
        case 'refreshStatus':
        	$this->refreshStatus();
            break;

        //
       default:
            $this->forumStat();
            break;
        //
      }
    } // end func

//-----------------------------------------------------------------------------
//---1、显示论坛帖子统计
//---2、更新list.php文件
//---3、更新Status.php文件
//-----------------------------------------------------------------------------

//=============================================================================
//更新Status文件
	function refreshStatus()
    {

        if ( !is_dir('../'.$this->set[dataPath]."/".$this->input['forumId'])) {
            $this->adminShowWrong("./main.php?mod=forumstat","请您选择子论坛");
        } else {

            $this->file = '../'.$this->set[dataPath]."/".$this->input['forumId'].'/list.php';
			$getMaxId = $this->get_max_value("1");
        	if ($listArray = $this->openFile()) {
                //计算主题及主题总数
	        	foreach($listArray as $k=>$v) {

                    $this->file = '../'.$this->set[dataPath]."/".$this->input['forumId'].'/'.$v[1].'_.php';
                    if ($this->topicInfo = $this->get()) {
                    	$postCount += $this->topicInfo[3];
                        $topicCount ++;
                    }
                }
            }
            //更新Status.php文件
	        $this->file = '../'.$this->set[dataPath]."/".$this->input['forumId']."/status.php";
	        $line = $this->get();

            $line[1] = $topicCount;
            $line[2] = $postCount;
			$line[3] = $getMaxId;

	        $this->overwrite($line);
	        chmod($this->file,0777);

            $this->adminShowMsg("./main.php?mod=forumstat&forumId={$this->input[forumId]}","您已经成功完成更新");
        }
    }

//=============================================================================
//更新List文件
	function refreshList()
    {

        if ( !is_dir('../'.$this->set[dataPath]."/".$this->input['forumId'])) {
            $this->adminShowWrong("./main.php?mod=forumstat","请您选择一个子论坛");
        } else {
	        if ($this->input['mode'] == 'file') {
	        //========================================================
	        //            以主题文件为准更新list.php文件
	        //========================================================
	            //跟据时间对 主题文件排序
	            //获的主题文件统计
	            if ($dirHandle = dir('../'.$this->set[dataPath]."/".$this->input['forumId'])) {
	                //获得所有主题的时间
	                while ($fileName = $dirHandle->read()) {
	                    if (ereg('[0-9]+_.php',$fileName)) {
	                        $this->file = '../'.$this->set[dataPath]."/".$this->input['forumId'].'/'.$fileName;
	                        $topicLine = $this->get();
	                        $topicArray[$topicLine[1]] = $topicLine[6];
	                    }
	                }
	                $dirHandle->close;
	                //按时间排序
	                arsort($topicArray);
	                //写list文件
	                $this->file = '../'.$this->set[dataPath]."/".$this->input['forumId'].'/list.php';
	                if ($listArray = $this->openFile()) {
	                    foreach($listArray as $k=>$v) {
	                        $newListArray[$v[1]] = $v;
	                    }
	                }

                    $this->file = '../'.$this->set[dataPath]."/".$this->input['forumId'].'/list1.php';
	                foreach($topicArray as $id=>$time) {
	                    if (isset($newListArray[$id])) {
	                        $this->add_line($newListArray[$id],0);
	                    } else {
	                        $this->file = '../'.$this->set[dataPath]."/".$this->input['forumId'].'/'.$id.'.php';
	                        $topicAuthor = $this->get();
							$topicAuthorName = $this->getNameFromId($topicAuthor[5]);

	                        $this->file = '../'.$this->set[dataPath]."/".$this->input['forumId'].'/'.$id.'_.php';
	                        $topicLine = $this->get();

	                        $postList = array($this->code,$id,$topicAuthorName,$time,$topicLine[17],"0","0","0",$topicLine[13],"0","0","\n");

	                        $this->file = '../'.$this->set[dataPath]."/".$this->input['forumId'].'/list1.php';
	                        $this->add_line($postList,0);
	                    }
	                }
	            }
                unlink('../'.$this->set[dataPath]."/".$this->input['forumId'].'/list.php');
                rename('../'.$this->set[dataPath]."/".$this->input['forumId'].'/list1.php','../'.$this->set[dataPath]."/".$this->input['forumId'].'/list.php');
            $this->adminShowMsg("./main.php?mod=forumstat&forumId={$this->input[forumId]}","您已经成功完成更新");
	        } else if ($this->input['mode'] == 'list')  {
	        //========================================================
	        //            以List文件为准更新list.php文件
	        //========================================================
	            //读list文件
	            $this->file = '../'.$this->set[dataPath]."/".$this->input['forumId'].'/list.php';

	            $listArray = $this->openFile();

	            $this->file = '../'.$this->set[dataPath]."/".$this->input['forumId'].'/list1.php';

	            foreach($listArray as $k=>$v) {
	                if (file_exists('../'.$this->set[dataPath]."/".$this->input['forumId'].'/'.$v[1].'.php')) {
	                    $this->add_line($v,0);
	                    $topicArray[$v[1]] = 1;
	                }
	            }

	            if ($dirHandle = dir('../'.$this->set[dataPath]."/".$this->input['forumId'])) {
	                //获得所有主题的时间
	                while ($fileName = $dirHandle->read()) {
	                    if (ereg('^[0-9]+.php$',$fileName)) {
	                        $topicNum = split('.php',$fileName, 2);

	                        if ($topicArray[$topicNum[0]] == 0) {
	                            @unlink('../'.$this->set[dataPath]."/".$this->input['forumId'].'/'.$topicNum[0].'_.php');
	                            @unlink('../'.$this->set[dataPath]."/".$this->input['forumId'].'/'.$topicNum[0].'.php');
	                            @unlink('../'.$this->set[dataPath]."/".$this->input['forumId'].'/'.$topicNum[0].'.php4');
	                        }
	                    }
	                }

	                $dirHandle->close;
	            }
                unlink('../'.$this->set[dataPath]."/".$this->input['forumId'].'/list.php');
                rename('../'.$this->set[dataPath]."/".$this->input['forumId'].'/list1.php','../'.$this->set[dataPath]."/".$this->input['forumId'].'/list.php');
            $this->adminShowMsg("./main.php?mod=forumstat&forumId={$this->input[forumId]}","您已经成功完成更新");
	        }
    	}
    }



//=============================================================================
//统计主题
    function forumStat()
    {

        //$statusTopicNum;$listTopicName;$fileTopicName;
        if(!$this->input[forumId] or $this->input[forumId]=="all" or $this->input[forumId] > 1000){
			$this->file = '../'.$this->set[dataPath]."/forums.php";
			$forums = $this->openfile();
			foreach ($forums as $this->forum) {
				if ($this->forum[1] == "forum") {
					//获得status列表主题统计
					$this->file = '../'.$this->set[dataPath]."/".$this->forum[2]."/status.php";
					$this->status = $this->get();

					$statusTopicNum += $this->status[1];
					//获的主题文件统计
					if ($dirHandle = dir('../'.$this->set[dataPath]."/".$this->forum[2])) {
						while ($fileName = $dirHandle->read()) {
						if (ereg('^[0-9]+.php$',$fileName))
								$fileTopicName++;
						}
						$dirHandle->close;
					}
					//获的list.php文件主题统计
					$this->file = '../'.$this->set[dataPath]."/".$this->forum[2]."/list.php";
					$this->listInfo = $this->openfile();

					$listTopicName+= count($this->listInfo);
				}
			}
		}//
		else{
			if(!file_exists('../'.$this->set[dataPath]."/".$this->input[forumId]."/list.php")){
			    $this->adminShowWrong("./main.php?mod=forumstat","您选择的版块还没有文章");
			}
					
					//获得status列表主题统计
					$this->file = '../'.$this->set[dataPath]."/".$this->input[forumId]."/status.php";
					$this->status = $this->get();
                    $getStatusMaxId = "最大ID：".$this->status[3];
					$statusTopicNum = $this->status[1];
					//获的主题文件统计
					if ($dirHandle = dir('../'.$this->set[dataPath]."/".$this->input[forumId])) {
						while ($fileName = $dirHandle->read()) {
						if (ereg('^[0-9]+.php$',$fileName))
								$fileTopicName++;
						}
						$dirHandle->close;
					}
					//获的list.php文件主题统计
					$this->file = '../'.$this->set[dataPath]."/".$this->input[forumId]."/list.php";
					$this->listInfo = $this->openfile();
                    $getListMaxId = "最大ID：".$this->get_max_value("1");
					$listTopicName = count($this->listInfo);

		}
        //获取论坛分类列表
        $optionLine = $this->getForumLine($this->input[forumId]);

        eval("\$this->outPut(\"".$this->getTemplate("forumStat")."\");");
    } // end func

//<<
//=============================================================================
} // end class