使用php編寫在線瀏覽日誌文件的小應用

由於公司內部開發文件權限限制,非主管無法隨意改動獲取正式服文件,所以寫了一個這樣的小應用來查看日誌文件

php文件

abstract class getDir
{
    protected $dir = null;
    protected $chmod = 0;
    protected $file = null;

    public function __construct($dir = null, $type = 0)
    {
        if (!$type) {
            if (empty($dir)) {
                $this->dir = dirname(__FILE__);
            } else {
                $this->dir = $dir;
            }
        } else {
            $this->dir = dirname(__FILE__);
            $this->file = dirname(__FILE__) . '/' . $dir;
        }
        $this->chmod = $this->getChmod($this->dir);
    }

    // 獲取權限
    protected function getChmod($filepath)
    {
        return substr(base_convert(@fileperms($filepath), 10, 8), -4);
    }

    public function get_chmod()
    {
        return $this->chmod;
    }

    public function get_dir()
    {
        return $this->dir;
    }

    abstract function readfile();

    abstract function printFiles();
}

class getDirectory extends getDir
{
    public function printFiles()
    {
        $files = scandir($this->dir);
        echo <<< EOD
dir:{$this->dir}<br>
EOD;
        echo <<< EOD
chmod:{$this->chmod}<br>  
EOD;
        foreach ($files as $item) {
            $d = mb_substr($item,0,1);
            if ($d == '.'){
                continue; 
            }
            echo <<< EOD
<a href="?file=$item">$item</a><br>
EOD;
        }
    }

    public function readfile()
    {
        if (is_file($this->file)) {
            $file_type = mb_substr($this->file, strrpos($this->file, '.') + 1);
            if ($file_type!='log'){
                exit(sprintf("The file type is %s not log file!",$file_type));
            }
            if (!is_writable($this->file)){
                echo "The file can't read!";
            }
        $chmod = $this->getChmod($this->file);
        echo <<< EOD 
dir:{$this->dir}<br> 
EOD;                 

        echo <<< EOD 
chmod:$chmod<br>     
EOD;                             
            $str = file_get_contents($this->file);
            $str = str_replace("\r\n","<br />",$str);
            echo $str;
        }else{
            echo "This is not a file!";
        }
    }
}

使用

if (isset($_REQUEST['file'])) {       
    $files = $_REQUEST['file'];       
    $gd = new getDirectory($files, 1);
    $gd->readfile();                  
} else {                              
    $gd = new getDirectory();         
    $gd->printFiles();                
}                                     

日誌目錄(例子)

日誌目錄

日誌內容

日誌內容

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章