<?php
/*
\* ========================================================
\* 项目: CTB文本论坛
\* 版本: 1.0
\* 团队: 实易数码
\* 小组: CTB文本论坛小组
\* 作者: 险情
\* 功能: 数据处理与备份
\* ========================================================
\* Copyright (c) 2002
\* 团队主页: http://www.11cn.org
\* 团队信箱: www@11cn.org,support@11cn.org
\* 创建日期: 2002/9
\* 修改日期: 2003-3-24
\* 修改说明: ------
\* 版权声明: 这个程序是免费软件.
\* ========================================================
*/

//=============================================================================

class Module extends CommonClass
{
    //构造函数
    function execute()
    {
        switch($this->input['action']) {
            case 'dataForum':
                $this->dataForum();
                break;
            case 'dataForumOne':
                $this->dataForumOne();
                break;
            case 'createForumBak':
                $this->doAdminOrder("你确定要将论坛本版块的所有数据打包备份吗？");
                break;
            case 'createForumBak2':
                $this->createForumBak();
                break;
            case 'delForumData':
                $this->doAdminOrder("你确定要将论坛本版块的所有数据删除吗？");
                break;
            case 'delForumData2':
                $this->delForumData();
                break;
            case 'getForumBak':
                $this->getForumBak();
                break;
            case 'delForumBak':
                $this->doAdminOrder("您确定要删除此备份数据吗？");
                break;
            case 'delForumBak2':
                $this->delForumBak();
                break;
            case 'uploadForumBak':
                $this->uploadForumBak();
                break;
            case 'coverForumData':
                $this->doAdminOrder("您确定要还原此数据吗？");
                break;
            case 'coverForumData2':
                $this->coverForumData();
                break;
        }
    }

//=============================================================================
//以下为显示版块备份首页:
    function dataForum()
    {
        $infomation = "欢迎来到CTB论坛后台管理中心，以下是本论坛<font color=red>[版块数据]</font>管理程式。";
        $forumId = $this->input['forumId'];

        $this->file = "../".$this->set[dataPath]."/forums.php";
        $forums = $this->read_file();
        foreach ($forums as $forumLine) {
            $forum = $this->make_array($forumLine);
            // 显示论坛的分类信息,版块统计信息
            if ($forum[1] == "forum") {
                $forumName = $forum[3];
                $forumId = $forum[2];
                //获取详细信息
                $this->file = "../".$this->set[dataPath]."/".$forumId."/status.php";
                @$forumStatus = $this->get();
                $topicNum = $forumStatus[1];
                $postnNum = $forumStatus[2]-$topicNum;

                $lastBakTime = date("Y-m-d H:i:s",$forumStatus[9]);
                $lastCoverTime = date("Y-m-d H:i:s",$forumStatus[10]);
                $dataBakNum = $forumStatus[11];
                //获取备份
                //----------------------来自用户列表-------------------------------------
                $openforumBak=opendir("../".$this->set[dataPath]."/bak");
                $forumBakName = array();
                while ($forumBak=readdir($openforumBak)) {
                    if (($forumBak!="") and ($forumBak!=".") and ($forumBak!="..")) {
                        $fileInfo = split("_", $forumBak);
                        if ($fileInfo[0] == "forum" and $fileInfo[1] == $forumId) {
                            $forumBakName[] = $forumBak ;
                        }
                     }
                }
                closedir($openforumBak);
                $forumBakNum = count($forumBakName);
                //
                eval ("\$main .= \"".$this->getTemplate("dataForumLine")."\";");
            }
        }

        eval("\$this->outPut(\"".$this->getTemplate("dataForum")."\");");
    }

//=============================================================================
//以下为显示一个版块的备份数据:
    function dataForumOne()
    {
        $forumId = $this->input['forumId'];
        //获取版块数据
        $this->file = "../".$this->set[dataPath]."/forums.php";
        $forums = $this->read_file();
        foreach ($forums as $line) {
            $forum = $this->make_array($line);
                if ($forum[2] == $forumId) {
                    $forumName = $forum[3];
            }
         }

         $infomation = "欢迎来到CTB论坛后台管理中心，以下是本论坛<font color=red>[版块数据->$forumName]</font>管理程式。";
        //获取版块详细信息
        $this->file = "../".$this->set[dataPath]."/".$forumId."/status.php";
        @$forumStatus = $this->get();
        $topicNum = $forumStatus[1];
        $postnNum = $forumStatus[2]-$topicNum;

        $lastBakTime = date("Y-m-d H:i:s",$forumStatus[9]);
        $lastCoverTime = date("Y-m-d H:i:s",$forumStatus[10]);
        $dataBakNum = $forumStatus[11];
        //显示本版块所有备份数据
        //----------------------来自用户列表-------------------------------------
        $openforumBak=opendir("../".$this->set[dataPath]."/bak");
        while ($forumBak=readdir($openforumBak)) {
            if (($forumBak!="") and ($forumBak!=".") and ($forumBak!="..")) {
                $fileInfo = split("_", $forumBak);
                if ($fileInfo[0] == "forum" and $fileInfo[1] == $forumId) {
                    $forumBakName[] = $forumBak ;
                }
             }
        }
        closedir($openforumBak);
        $forumBakNum = count($forumBakName);
        //
        for ($i=$forumBakNum-1; $i>=0; $i--) {
            $fSize = filesize("../".$this->set[dataPath]."/bak/$forumBakName[$i]");
            $fSize = $fSize/1024;
            $fSize = substr($fSize, 0,4); 
            //获取日期
            $this->file = "../".$this->set[dataPath]."/bak/$forumBakName[$i]";
            $forum1Array = $this->get();
            $forumBakTime = date("Y-m-d H:i:s",$forum1Array[4]);
            if ($lastBakTime == $forumBakTime){
                $forumBakTime = "<font color=red>".$forumBakTime."</font>";
            }else {
                $forumBakTime = $forumBakTime;
            }
            
            //
            $systemforum_htm .= "<tr> 
                <td bgcolor=\"white\" align=\"center\">".($i+1)."</td>
                <td bgcolor=\"f5f5f5\" align=\"center\">$forumBakName[$i]</td>
                <td bgcolor=\"white\" align=\"center\">$forumBakTime</td>
                <td bgcolor=\"f5f5f5\" align=\"center\">$fSize k</td>
                <td bgcolor=\"white\" align=\"center\"><a href=\"./main.php?mod=dataforum&action=coverForumData&forumId=$forumId&id=$forumBakName[$i]\">执行</a></td>
                <td bgcolor=\"f5f5f5\" align=\"center\"><a href=\"./main.php?mod=dataforum&action=coverForumData&forumId=$forumId&id=$forumBakName[$i]&startSet=1\">执行</a></td>
                <td bgcolor=\"white\" align=\"center\"><a href=\"./main.php?mod=dataforum&action=getForumBak&id=$forumBakName[$i]\">下载</a></td>
                <td bgcolor=\"f5f5f5\" align=\"center\">
                  <a href=\"./main.php?mod=dataforum&action=delForumBak&forumId=$forumId&id=$forumBakName[$i]\">删除</a>
                </td>
            </tr>";
        }

        eval("\$this->outPut(\"".$this->getTemplate("dataForumOne")."\");");
    }

//=============================================================================
//将一个论坛的所有数据进行打包:
    function createForumBak()
    {
        $forumId = $this->input['forumId'];

        //创建文件
        $getBakTime = time();
        $this->file = "../".$this->set[dataPath]."/bak/forum_".$forumId."_".$getBakTime.".ctb";
        $forumBakVer = "V1";
        $forumBakName = "CTB文本论坛-版块数据备份";
        $forumBakAuthor = "程式制作：险情";
        $forumList =  array(stripslashes($this->code),$forumBakName,$forumBakVer,$forumBakAuthor,$getBakTime,"\n");
        $this->overwrite($forumList);

        //写入所有文件
        $this->file = "../".$this->set[dataPath]."/".$forumId."/list.php";
        $list1Array = $this->read_file();
        $listLineTotal = count($list1Array);

        for ($i=0; $i<$listLineTotal; $i++) {
            $listLineCode = $this->make_array($list1Array[$i]);
            $this->file = "../".$this->set[dataPath]."/".$forumId."/".$listLineCode[1]."_.php";
            if (file_exists($this->file)) {
                $topicLine = $this->read_file();
                $this->file = "../".$this->set[dataPath]."/".$forumId."/".$listLineCode[1].".php";
                $postLine1Array = $this->read_file();

                //
                for ($j=0; $j<count($postLine1Array); $j++){
                    $postLine1Array[$j] = ereg_replace("[\n\t\r\f]","",$postLine1Array[$j]);
                }
                $postLine = implode("|",$postLine1Array);
                //将数据中的换行去掉
                $listLine = ereg_replace("\n","",$list1Array[$i]);
                $topicLine[0] = ereg_replace("\n"," ",$topicLine[0]);
                $postBakLine = $listLine."|".$topicLine[0]."|".$postLine."|\n";
                $this->file = "../".$this->set[dataPath]."/bak/forum_".$forumId."_".$getBakTime.".ctb";
                $this->add_write($postBakLine);
            }
        }
        //写入最后打包日期
        $this->file = "../".$this->set[dataPath]."/".$forumId."/status.php";
        $line = $this->get();
        $line[9] = $getBakTime;

        $this->overwrite($line);
        $this->adminShowMsg("./main.php?mod=dataforum&action=dataForumOne&forumId=$forumId","您已经成功将本版块所有数据打包备份");
    }

//=============================================================================
//将一个版块内的所有数据清除:
    function delForumData()
    {
        $forumId = $this->input['forumId'];

        $delMsg = $this->delFileValue("../".$this->set[dataPath]."/".$forumId."/");
        mkdir("../".$this->set[dataPath]."/".$forumId."/");
        $this->file =  "../".$this->set[dataPath]."/".$forumId."/list.php";
		$this->null_write("");
            
        $this->file =  "../".$this->set[dataPath]."/".$forumId."/status.php";
        $newforum_line=array(stripslashes($this->code),"0","0","0","","","","","","","0","0","0");
        $this->overwrite($newforum_line); 

        $this->adminShowMsg("./main.php?mod=dataforum&action=dataForumOne&forumId=$forumId","已经成功将本版块所有帖子资料删除");
    }

//=============================================================================
//将用户打包下载:
    function getForumBak()
    {

        $dataForumBakDir="../".$this->set[dataPath]."/bak/".$this->input['id'];
        header("location:$dataForumBakDir");
    }

//=============================================================================
//将删除打包的文件:
    function delForumBak()
    {
        //$forumId = $this->input['forumId'];
        $forumId = $this->input['id'];
        //创建文件
        if (@unlink("../".$this->set[dataPath]."/bak/".$this->input['id'])){
        //
            $this->adminShowMsg("./main.php?mod=dataforum&action=dataForumOne&forumId=$forumId","您已经成功删除此备份");
        }else {
            $this->adminShowWrong("./main.php?mod=dataforum&action=dataForumOne&forumId=$forumId","删除失败");
        }
        
    }

//=============================================================================
//将用户打包下载:
    function uploadForumBak()
    {
        
		
		$forumId = $this->input['forumId'];

        $newName = explode(".", $_FILES['uploadUserBak']['name']);
        if ($newName[ count($newName)-1 ] == "ctb"){
            if (!file_exists("../".$this->set[dataPath]."/bak/".$_FILES['uploadUserBak']['name']) and copy($_FILES['uploadUserBak']['tmp_name'],"../".$this->set[dataPath]."/bak/".$_FILES['uploadUserBak']['name'])) {
                $this->adminShowMsg("./main.php?mod=dataforum&action=dataForumOne&forumId=$forumId","已经成功本地备份数据上传");
            } else {
                $this->adminShowWrong("./main.php?mod=dataforum&action=dataForumOne&forumId=$forumId","上传失败,文件已经存在");
            }
        } else {
            $this->adminShowWrong("./main.php?mod=dataforum&action=dataForumOne&forumId=$forumId","上传失败,文件名非法");
        }
        
    }

//=============================================================================
//将用户打包数据还原:
    function coverForumData()
    {
        //global $id,$startSet,$forumId;
        $forumId  = $this->input['forumId'];
        $startSet = $this->input['startSet'];
        $path = substr($this->set[dataPath],2);
		$this->file = "../".$path."/bak/".$this->input['id'];
        $postBakLine = $this->read_file();
        $bakLineTotal = count($postBakLine);
        //判断数据是否为备份数据
        $checkPostBak = $this->make_array($postBakLine[0]);
        if ($checkPostBak[1] != "CTB文本论坛-版块数据备份" or $checkPostBak[2] != "V1"){
            $this->adminShowWrong("./main.php?mod=dataforum&action=dataForumOne&forumId=$forumId","还原失败，错误原因：备份数据版本不对");
        }
        if (filesize("../".$path."/".$forumId."/list.php")>0 and $startSet!="1"){
            $this->adminShowWrong("./main.php?mod=dataforum&action=dataForumOne&forumId=$forumId","还原失败，错误原因：请先清空本版块数据，再运行此程序。");

        }

        //判断是否为覆盖
        if ($startSet == "1"){
            //获得新主题的id
            if (file_exists("../".$path."/".$forumId."/list.php")) {
                $this->file = "../".$path."/".$forumId."/list.php";
                $max_id = $this->get_max_value("1");
                $startNum = $max_id+1;
            } else {
                $startNum = 1;
            }
        } else {
            $startNum = "1";
        }

        $totalPosts = 0;
        for ($i=1; $i<=$bakLineTotal; $i++) {
             $postBakLine1Array = explode("|",$postBakLine[$i]);
             $postBakLineNum = count($postBakLine1Array)-1;
             
             $postListLine = $postBakLine1Array[0];
             $postTopicLine = $postBakLine1Array[1];
             //$postPostsLine = $postBakLine1Array[2];
                 
             //判断是否覆盖
             if ($startSet == "1" and !empty($postBakLine1Array[0]) and !empty($postBakLine1Array[1])) {
                 $postId = $startNum;
                 $startNum++;
                 
                 //获取新的---3---列表//当累加的时候把所有的id更新
                 $postListLine1Array = explode("\x0E",$postBakLine1Array[0]);
                 $postListLine1Array[1] = $postId;
                 $postListLine = implode("\x0E",$postListLine1Array);
             
                 $postTopicLine1Array = explode("\x0E",$postBakLine1Array[1]);
                 $postTopicLine1Array[1] = $postId;
                 $postTopicLine = implode("\x0E",$postTopicLine1Array);

                 //$postPostsLine1Array = explode("\x0E",$postBakLine1Array[2]);
                 //$postPostsLine1Array[1] = $postId;
                 //$postPostsLine = implode("\x0E",$postPostsLine1Array);
                 //<<累加结束
             }
             else {
                  $postListLine1Array = explode("\x0E",$postBakLine1Array[0]);
                  $postId = $postListLine1Array[1];
             }

             if (ereg("[0-9]",$postId)){    
                 
             //写入主题
             $this->file = "../".$path."/".$forumId."/".$postId."_.php";
             //
             $f=fopen($this->file,"w");    
             flock($f,LOCK_EX);
             fputs($f,$postTopicLine);       
             fclose($f); 

             //写入帖子
             for ($j=2; $j<$postBakLineNum; $j++) {
             $this->file = "../".$path."/".$forumId."/".$postId.".php";
             //
             $f=fopen($this->file,"a");    
             flock($f,LOCK_EX);
             fputs($f,$postBakLine1Array[$j]."\n");       
             fclose($f); 
             $totalPosts++;
             }

             //写入list
             $this->file = "../".$path."/".$forumId."/list.php";
             $f=fopen($this->file,"a");    
             flock($f,LOCK_EX);
             fputs($f,$postListLine);    
             fputs($f,"\n");    
             fclose($f); 
             }
        }

        //从新统计所有帖子列表
        $this->file = "../".$path."/".$forumId."/list.php";
        $postLastId = $this->get();
        $this->file = "../".$path."/".$forumId."/list.php";
        $postListNum = $this->getlines();
                    
        $postBakLine1Array = explode("|",$postBakLine[1]);
        $postLastName = explode("\x0E",$postBakLine1Array[1]);

        $this->file =  "../".$path."/".$forumId."/status.php";
        $postInfo = $this->get();
        if ($startSet == "1") {
            $postInfo[2] = $totalPosts+$postInfo[2];       //帖子数量
        }else {
            $postInfo[2] = $totalPosts;       //帖子数量
        }
        $postInfo[1] = $postListNum;  //主题数量

        $postInfo[3] = $postLastId[1];     //最后帖子id
        $postInfo[4] = $postLastName[2];     //最后帖子标题
        $postInfo[5] = $postLastName[6];     //最后发帖时间

        $postInfo[6] = $postLastName[5];    //作者id 
        $postInfo[7] = $postLastId[2];    //作者
        $postInfo[10] = time();    //作者

        $this->file =  "../".$path."/".$forumId."/status.php";
        $this->overwrite($postInfo);
        $this->adminShowMsg("./main.php?mod=dataforum&action=dataForumOne&forumId=$forumId","已经成功将本版块所有资料进行还原");

    }

} //end class
