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

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

class Module extends CommonClass
{
    function execute()
    {
        switch($this->input['action']) {
			//
            case 'userStat':
        	    $this->userStat();
                break;
            case 'refreshMember':
                $this->refreshMember();
                break;
            case 'refreshList':
        	    $this->refreshList();
                break;
			//
        }
    } // end func

//-----------------------------------------------------------------------------
//---1、显示用户统计
//---2、更新member.php文件
//---3、更新list.php文件
//-----------------------------------------------------------------------------


//=============================================================================
//更新member.php文件
    function refreshMember(){
        $this->file = '../'.$this->set[dataPath]."/users/list.php";
        $listInfo = $this->openfile();
        $listUserName = count($listInfo);

        //更新用户统计文件
        $this->file = '../'.$this->set[dataPath]."/member.php";
        $user = $this->get();
        $user[2] = $listUserName;      //用户总数量增加
        if ($user[3] < $user[2])  //用户总数量增加
            $user[3] = $user[2];

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

        $this->showMsg('更新已完成!');
    }


//=============================================================================
//更新list.php文件
    function refreshList(){
        if ($this->input['mode'] == 'file') {
	        //========================================================
	        //            以用户文件为准更新list.php文件
	        //========================================================

            //登记用户文件
	        if ($dirHandle = dir('../'.$this->set[dataPath].'/users')) {
	            while ($fileName = $dirHandle->read()) {
	                if (ereg('^[0-9]+.php$',$fileName)) {
	                    $topicNum = split('.php',$fileName, 2);
                        if ($usersArray[$topicNum[0]] == 0) {
							$usersArray[$topicNum[0]] = $topicNum[0];
                        }
	                }
	            }
	            $dirHandle->close;
	        }
            //接id排序
            asort($usersArray);

	        $this->file = '../'.$this->set[dataPath]."/users/list.php";
	        $listInfo = $this->openfile();

            foreach($listInfo as $k=>$v) {
            	$usersListArray[$v[1]] = $v;
            }

            //生成list1.php文件
	        $this->file = '../'.$this->set[dataPath]."/users/list1.php";
            foreach($usersArray as $k=>$v) {
                if ($usersListArray[$k]<>0) {
                  	$this->add_line($usersListArray[$k],0);
                } else {
			        $this->file = '../'.$this->set[dataPath]."/users/".$k.".php";
                    $userInfo = $this->get();
	                $userList = array(stripslashes($this->code),$userInfo[1],$userInfo[2], md5(ctb),$userInfo[3],"1","");
			        $this->file = '../'.$this->set[dataPath]."/users/list1.php";
	                $this->add_line($userList);
                }
//            	echo $k.'=>'.$v.'<br>';
            }
            //替换list.php文件
            @unlink('../'.$this->set[dataPath].'/users/list.php');
            @rename('../'.$this->set[dataPath].'/users/list1.php','../'.$this->set[dataPath].'/users/list.php');

            $this->showMsg('更新已完成!');

            //echo $this->GetPassword(8);
        } else if ($this->input['mode'] == 'list') {
	        //========================================================
	        //            以list.php文件为准更新list.php文件
	        //========================================================
            $this->file = '../'.$this->set[dataPath]."/users/list.php";
	        $listInfo = $this->openfile();

	        $this->file = '../'.$this->set[dataPath]."/users/list1.php";
	        foreach($listInfo as $k=>$v) {
	            if (file_exists('../'.$this->set[dataPath].'/users/'.$v[1].'.php')) {
	                $this->add_line($v,0);
	                $usersArray[$v[1]] = 1;
	            }
	        }

	        if ($dirHandle = dir('../'.$this->set[dataPath].'/users')) {
	            while ($fileName = $dirHandle->read()) {
	                if (ereg('^[0-9]+.php$',$fileName)) {
	                    $topicNum = split('.php',$fileName, 2);

	                    if ($usersArray[$topicNum[0]] == 0) {
	                        @unlink('../'.$this->set[dataPath].'/users/'.$topicNum[0].'.php');
	                        @unlink('../'.$this->set[dataPath].'/users/'.$topicNum[0].'new.php');
	                    }
	                }
	            }

	            $dirHandle->close;

	            @unlink('../'.$this->set[dataPath].'/users/list.php');
	            @rename('../'.$this->set[dataPath].'/users/list1.php','../'.$this->set[dataPath].'/users/list.php');
	        }
            $this->showMsg('更新已完成!');
    	}
    }


//=============================================================================
//显示用户统计
    function userStat(){

    	//获的用户文件统计
        $fileUserNum= 0;
        $listUserNum = 0;
        $memberUserNum = 0;

	    if ($dirHandle = dir('../'.$this->set[dataPath]."/users")) {
	        while ($fileName = $dirHandle->read()) {
	        if (ereg('^[0-9]+.php$',$fileName))
	                $fileUserNum++;
	        }
	        $dirHandle->close;
	    }

        $this->file = '../'.$this->set[dataPath]."/users/list.php";
        $listUserNum = $this->getlines();
		$getListMaxId = "最大ID：".$this->get_max_value("1");

        $this->file = '../'.$this->set[dataPath]."/member.php";
        $status = $this->get();
        $memberUserNum = $status[2];
		$getMemMaxId = "最大ID：".$status[3];
    	eval("\$this->outPut(\"".$this->getTemplate("userStat")."\");");
    } // end func


//=============================================================================
//生成随机密码  (备用)
    function GetPassword ($pSize=8) {
	    mt_srand ((double) microtime() * 1000000);
	    for ($i=1; $i<=$pSize; $i++) {
	        $nRandom = mt_rand(1,30);
	        if ($nRandom <= 10) {
	            $password .= chr(mt_rand(65,90));
	        } else if ($nRandom <= 20) {
	            $password .= chr(mt_rand(97,122));
	        } else {
                $password .= chr(mt_rand(48,57));
            }
	    }
	    return $password;
	}

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