code.php 3.21 KB
<?php
/**
 * Created by PhpStorm.
 * User: zhaojie
 * Date: 2018/8/28
 * Time: 上午11:44
 */

$log = new Log();
$param = '';
if (isset($_GET['param'])) {
    $param = $_GET['param'];
}
$log->Index($param);

/**
 * 项目日志管理接口
 * Class LogController
 * @package Apimini\Controller\Home
 */
class Log
{
    public function Index($param, $del = null)
    {
        $serverPath = str_replace('\\','/',realpath(dirname(__FILE__).'/'));
        $path = rtrim($serverPath, 'phpapi') . $param;
        // 是否需要删除文件操作
        if (!empty($del) || strpos(strtolower($param), '.log')) {
            $delPath = $path . '/' . $del;
            $this->delFile($delPath);
        }

        // 判断访问的是文件还是文件夹
        if(strpos($param, '.')) {
            $this->ReadFile($path);
        } else {
            echo '目录: ' . '  <a href="?param=">party</a> / ';
            $this->Banner($param);
            $dir = $this->ReadPath($path, $param);
            echo $dir;
        }
        return true;
    }

    // 删除文件
    // private function delFile($file_path)
    // {
    //     if(file_exists($file_path)){
    //         unlink($file_path);
    //     }
    // }

    /**
     * 读取文件
     * @param $file_path
     */
    private function ReadFile($file_path)
    {
        if(strpos($file_path,'config.') !== false || strpos($file_path,'upload') !== false || strpos($file_path,'img') !== false){
            return;
        }
        if(file_exists($file_path)){
            $file_arr = file($file_path);
            for($i=0;$i<count($file_arr);$i++){//逐行读取文件内容
                echo $file_arr[$i] . "<br />";
            }
        }
    }

    /**
     * 面包屑
     * @param $param
     */
    private function Banner($param)
    {
        $timestamp = '&t=' . time();
        $url = '?param=';
        $str = '';
        foreach (explode('/',$param) as $item) {
            if (empty($item))
                continue;
            $url .= '/' . $item;
            $str .= '<a href="'. $url . $timestamp .'">'. $item .'</a> / ';
        }
        echo $str . '<hr />';
    }

    /**
     * 读取文件夹
     * @param $path
     * @param $param
     * @return string
     */
    private function ReadPath($path, $param)
    {
        $timestamp = '&t=' . time();

        $fileArr = [];
        if(is_dir($path)){
            if($dh = @opendir($path)){
                while(($file = readdir($dh)) !== false){
                    if($file != '.' && $file != '..'){
                        $fileArr[] = $file;
                    }
                }
                closedir($dh); // 关闭
            }
        }
        // 给目录文件增加点击链接
        $url = '';
        foreach ($fileArr as $file) {
            if(strpos($file,'config.') !== false || strpos($file,'upload') !== false || strpos($file,'img') !== false){
                continue;
            }
            $target = '';
            $del = '';
            if (strpos($file, '.')) {
                $target = ' target = "_blank"';
            }
            $url .= '<a href="?param='. $param . '/' . $file . $timestamp . '" '. $target .'>' . $file . '</a> ' . $del . ' <br />';
        }
        return $url;
    }

}