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


//=============================================================================
class Module extends CommonClass
{
    //构造函数
    function execute()
    {
        switch($this->input['action']) {
            case 'dataUser':
                $this->dataUser();
                break;
            case 'createUserBak':
                $this->doAdminOrder("你确定要将论坛所有用户打包备份吗？");
                break;
            case 'createUserBak2':
                $this->createUserBak();
                break;
            case 'delUserBak':
                $this->doAdminOrder("您确定要删除此用户备份数据吗？");
                break;
            case 'delUserBak2':
                $this->delUserBak();
                break;
            case 'getUserBak':
                $this->getUserBak();
                break;
            case 'delUserData':
                $this->doAdminOrder("您确定要删除所有的用户数据吗？");
                break;
            case 'delUserData2':
                $this->delUserData();
                break;
            case 'coverUserData':
                $this->doAdminOrder("您确定要将所有用户数据还原吗？");
                break;
            case 'coverUserData2':
                $this->coverUserData();
                break;
            case 'uploadUserBak':
                $this->uploadUserBak();
                break;
            default:
                break;
        }
    } // end func

//-----------------------------------------------------------------------------
//1---用户资料管理首页:dataUser()/2002.11.20/险情
//2---将所有用户进行打包:createUserBak()/2002.11.20/险情
//3---
//4---
//-----------------------------------------------------------------------------

//=============================================================================
//用户资料管理首页
    function dataUser()
    {
        $infomation="欢迎来到CTB论坛后台管理中心，以下是本论坛<font color=red>[用户数据]</font>管理程式。";

        //获取用户数据基本信息
        $this->file = "../".$this->set[dataPath]."/member.php";
        $userInfo = $this->get();
        $userNum = $userInfo[2];
        $userMaxId = $userInfo[3];
        $userMaxName = $userInfo[4];
        $lastBakTime = date("Y-m-d H:i:s",$userInfo[5]);
        $lastOverTime = date("Y-m-d H:i:s",$userInfo[6]);

        //循环获取备份数据包
        $openUserBak=opendir("../".$this->set[dataPath]."/bak");
        while ($userBak=readdir($openUserBak)) {
            if (($userBak!="") and ($userBak!=".") and ($userBak!="..")) {
                $fileInfo = split("_", $userBak);
                if ($fileInfo[0] == "user") {
                    $userBakName[] = $userBak ;
                }
             }
        }
        closedir($openUserBak);
        $userBakNum = count($userBakName);
        for ($i=$userBakNum-1; $i>=0; $i--) {
            $fSize = filesize("../".$this->set[dataPath]."/bak/$userBakName[$i]");
            $fSize = $fSize/1024;
            $fSize = substr($fSize, 0,4); 
            //获取日期
            $this->file = "../".$this->set[dataPath]."/bak/$userBakName[$i]";
            $user1Array = $this->get();
            $userBakTime = date("Y-m-d H:i:s",$user1Array[4]);
            if ($lastBakTime == $userBakTime){
                $userBakTime = "<font color=red>".$userBakTime."</font>";
            }else {
                $userBakTime = $userBakTime;
            }
            

            $systemuser_htm .= "<tr> 
                <td bgcolor=\"white\" align=\"center\">".($i+1)."</td>
                <td bgcolor=\"f5f5f5\" align=\"center\">$userBakName[$i]</td>
                <td bgcolor=\"white\" align=\"center\">$userBakTime</td>
                <td bgcolor=\"f5f5f5\" align=\"center\">$fSize k</td>
                <td bgcolor=\"white\" align=\"center\"><a href=\"./main.php?mod=datauser&action=coverUserData&id=$userBakName[$i]\">执行</a></td>
                <td bgcolor=\"f5f5f5\" align=\"center\"><a href=\"./main.php?mod=datauser&action=coverUserData&id=$userBakName[$i]&startSet=1\">执行</a></td>
                <td bgcolor=\"white\" align=\"center\"><a href=\"./main.php?mod=datauser&action=getUserBak&id=$userBakName[$i]\">下载</a></td>
                <td bgcolor=\"f5f5f5\" align=\"center\">
                  <a href=\"./main.php?mod=datauser&action=delUserBak&id=$userBakName[$i]\">删除</a>
                </td>
            </tr>";
        }
        eval("\$this->outPut(\"".$this->getTemplate("dataUser")."\");");
 
    } // end func

//=============================================================================
//将所有用户进行打包:
    function createUserBak()
    {
        //创建文件
        $getBakTime = time();
        $this->file = "../".$this->set[dataPath]."/bak/user_".$getBakTime.".ctb";
        $userBakVer = "V1";
        $userBakName = "CTB文本论坛-用户数据备份";
        $userBakAuthor = "程式制作：险情";
        $userList =  array(stripslashes($this->code),$userBakName,$userBakVer,$userBakAuthor,$getBakTime,"\n");
        $this->add_line($userList,$check_n=0);
        //写入所有文件
        $this->file = "../".$this->set[dataPath]."/users/list.php";
        $list2Array = $this->openfile();
        $listLineTotal = count($list2Array);
        for ($i=0; $i<$listLineTotal; $i++) {
            $this->file = "../".$this->set[dataPath]."/users/".$list2Array[$i][1].".php";
            if (file_exists($this->file)) {
                $userLine = $this->read_file();
                $userLine[0] = ereg_replace("[\n\t\r\f]"," ",$userLine[0]);
                $userLine = $list2Array[$i][3]."|".$userLine[0]."|\n";
                $this->file = "../".$this->set[dataPath]."/bak/user_".$getBakTime.".ctb";
                $this->add_write($userLine);
            }
        }
        //写入最后打包日期
        $this->file = "../".$this->set[dataPath]."/member.php";
        $line = $this->get();
        $line[5] = $getBakTime;
        $this->overwrite($line);

        $this->adminShowMsg("./main.php?mod=datauser&action=dataUser","您已经成功将所有会员数据打包备份");
    }

//=============================================================================
//将所有用户进行打包:
    function delUserBak()
    {
        //global $id;

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

//=============================================================================
//将用户打包下载:
    function getUserBak()
    {
        //global $id;

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

//=============================================================================
//将所有用户清除:
    function delUserData()
    {
        $delMsg = $this->delFileValue("../".$this->set[dataPath]."/users");
        mkdir("../".$this->set[dataPath]."/users");
        $this-> file = "../".$this->set[dataPath]."/users/list.php";
        $this->null_write("");
        $this->file = "../".$this->set[dataPath]."/member.php";
        $memberInfo = $this->get();
        $memberInfo[2] = 0;
        $memberInfo[3] = 0;
        $memberInfo[4] = 0;
        $memberInfo[6] = 0;
        $this->overwrite($memberInfo);
        $this->adminShowMsg("./main.php?mod=datauser&action=dataUser","已经成功将所有用户资料删除");
    }

//=============================================================================
//将用户打包数据还原:
    function coverUserData()
    {
        global $id,$startSet;
        $startSet = $this->input['startSet'];
        $this->file = "../".$this->set[dataPath]."/bak/".$this->input['id'];
        $userBakLine = $this->read_file();
        $bakLineTotal = count($userBakLine);

        //判断数据是否为备份数据
        $checkUserBak = $this->make_array($userBakLine[0]);
        if ($checkUserBak[1] != "CTB文本论坛-用户数据备份" or $checkUserBak[2] != "V1"){
            $this->adminShowWrong("./main.php?mod=datauser&action=dataUser","还原失败，错误原因：备份数据版本不对");
        }
        if (filesize("../".$this->set[dataPath]."/users/list.php")>0 and $startSet!="1"){

            $this->adminShowWrong("./main.php?mod=datauser&action=dataUser","还原失败，错误原因：请先清空本版块数据，再运行此程序。");

        }

        //判断是否为覆盖
        if ($startSet == "1"){
            //获取用户数据基本信息
            $this->file = "../".$this->set[dataPath]."/member.php";
            $userStatus = $this->get();
            $userMaxId = $userStatus[3]+1;
            $startNum = $userMaxId;
        }
        else {
            $startNum = "1";
        }
        for ($i=1; $i<$bakLineTotal; $i++) {
             
             $userLine1Array = explode("|",$userBakLine[$i]);
             $userPassWord = $userLine1Array[0];
             $userInfo = $userLine1Array[1];
             $userInfo1Array = explode("\x0E",$userInfo);
                 //判断是否覆盖
            if ($startSet == "1") {
                 $userId = $startNum;
                 $startNum++;
        
                 $userInfo1Array[1] = $userId;
                 $userInfo = implode("\x0E",$userInfo1Array);
                 //
                 $this->file = "../".$this->set[dataPath]."/users/list.php";
                 $checkLoginName = $this->select_line(2, $userInfo1Array[2]);
                 $checkShowName = $this->select_line(4, $userInfo1Array[3]);
            }else {
                $userId = $userInfo1Array[1];
            }
                 

            if ((!$checkLoginName and !$checkShowName) or $startSet != "1"){
                if (ereg("[0-9]",$userInfo1Array[1])){             
                    //写入用户id
                    $this->file = "../".$this->set[dataPath]."/users/".$userId.".php";

                    //本来是addLineToEnd($s)
                    $f=fopen($this->file,"w");    
                    flock($f,LOCK_EX);
                    fputs($f,$userInfo);       
                    fclose($f); 

                    //写入list
                    $userList = array(stripslashes($this->code),$userId,$userInfo1Array[2],$userPassWord,$userInfo1Array[3],"1","");
                    $this->file = "../".$this->set[dataPath]."/users/list.php";

                    //本来是add_line($array,$check_n=1)
                    $s=implode("\x0E",$userList);    
                    $f=fopen($this->file,"a");    
                    flock($f,LOCK_EX);
                    fputs($f,$s);    
                    fputs($f,"\n");    
                    fclose($f); 
                }
             }//结束总if
        } //结束总循环

        //从新统计会员了列表
        $this->file = "../".$this->set[dataPath]."/users/list.php";
        $memberNum = $this->getlines();

        $this->file = "../".$this->set[dataPath]."/member.php";
        $memberInfo = $this->get();
        $memberInfo[2] = $memberNum;
        $memberInfo[3] = $userId;
        $memberInfo[4] = $userInfo1Array[3];
        $memberInfo[6] = time();
        $this->overwrite($memberInfo);

        $this->adminShowMsg("./main.php?mod=datauser&action=dataUser","已经成功将所有用户资料进行还原");

    }

//=============================================================================
//将用户打包下载:
    function uploadUserBak()
    {
        $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=datauser&action=dataUser","已经成功本地备份数据上传");
            }
            else $this->adminShowWrong("./main.php?mod=datauser&action=dataUser","上传失败,文件已经存在");
        }
        else {
            $this->adminShowWrong("./main.php?mod=datauser&action=dataUser","上传失败,文件名非法");
        }
    }

} //end class
