<?php
//case 26 文件管理器
//设置配置文件中，只能访问本目录
ini_set('open_basedir', __DIR__);
//路径
$path = isset($_GET['path']) ? $_GET['path'] : '.';
//文件名
$file = '';
//判断，如果是文件类型
if (is_file($path)) {
  //获得文件名
  $file = basename($path);
  //获得路径
  $path = dirname($path);
}
//判断，不是目录
elseif (!is_dir($path)) {
  die('无效的文件路径参数');
}

//获得文件列表
function getFileList($path)
{
  //打开目录，获得句柄
  $handle = opendir($path);
  //空数组
  $list = array('dir' => array(), 'file' => array());
  //从目录总读取文件
  while (false !== ($file_name = readdir($handle))) {
    //除去上级目录和本级目录
    if ($file_name != '.' && $file_name != '..') {
      //文件全路径
      $file_path = "$path/$file_name";
      //文件类型
      $file_type = filetype($file_path);
      //判断，文件类型是文件或者目录
      if (!in_array($file_type, array('file', 'dir'))) {
        continue;
      }
      //数组填入值
      $list[$file_type][] = array(
        'file_name' => $file_name,
        'file_path' => $file_path,
        'file_size' => round(filesize($file_path)),
        'file_time' => date('Y/m/d  H:i:s', filemtime($file_path)),
      );
    }
  }
  //释放句柄
  closedir($handle);
  return $list;
}
//处理操作
$action = isset($_GET['a']) ? $_GET['a'] : '';
//根据操作动作，执行相应程序
switch ($action) {
    //返回上一级
  case 'prev':
    $path = dirname($path);
    break;
}

$file_list = getFileList($path);

?>

<html lang='zh-CN'>

<head>
  <meta charset="UTF-8">
  <title>文件浏览器</title>
  <link rel="icon" href="https://cdn.plyr.io/static/icons/favicon.ico" />
  <link rel="icon" type="image/png" href="https://cdn.plyr.io/static/icons/32x32.png" sizes="32x32" />
  <link rel="icon" type="image/png" href="https://cdn.plyr.io/static/icons/16x16.png" sizes="16x16" />
  <style>
    body {
      font-size: 20px;
    }

    img {
      width: 20px;
    }

    a:link,
    a:visited {
      color: #2E8B57;
      text-decoration: none;
    }

    a:hover,
    a:active {
      color: #00BFFF;
      text-decoration: underline;
    }

    .tbl {
      border-collapse: collapse;
      width: 98%;
      margin-top: 10px;
      font-size: 10px;
    }

    .tbl th {
      text-align: left;
      background: #4682B4;
    }

    .tbl th,td {
      border: 0.5px solid #4682B4;
    }

    span {
      color: #00BFFF;
    }

    .help {
      color: cornflowerblue;
      font-size: 15px;
      margin: 0;
      padding: 0;
    }

    .prev {
      width: 30px;
      height: 30px;
      margin-top: 10px;
      margin-left: 20px;
    }

    .tupian {
      width: 100%;
    }

    .tupian img {
      width: 300px;
      margin: auto;
      display: block;
    }
    table tr:last-child td:first-child {
            border-bottom-left-radius: 12px;
        }
 
        table tr:last-child td:last-child {
            border-bottom-right-radius: 12px;
        }
 
		table tr:first-child td:first-child {
            border-top-left-radius: 12px;
        }
 
        table tr:first-child td:last-child {
            border-top-right-radius: 12px;
        }
  </style>
</head>

<body>
  <div>
    <a href="?path=<?= $path; ?>&a=prev"><svg class="prev" xmlns="http://www.w3.org/2000/svg" fill="currentColor" viewBox="0 0 16 16">
        <path fill-rule="evenodd" d="M15 8a.5.5 0 0 0-.5-.5H2.707l3.147-3.146a.5.5 0 1 0-.708-.708l-4 4a.5.5 0 0 0 0 .708l4 4a.5.5 0 0 0 .708-.708L2.707 8.5H14.5A.5.5 0 0 0 15 8z" />
      </svg></a>
  </div>

  <table width="100%" style="font-size: 20px;text-align: center;">
    <tr>
      <th>图标</th>
      <th>名称</th>
      <th>修改日期</th>
      <th>大小</th>
      <th>操作</th>
    </tr>
    <?php foreach ($file_list['dir'] as $v) :
      if ($v['file_name'] == 'player' | $v['file_name'] == 'error') {
        continue;
      }
    ?>
      <tr>
        <td><svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" fill="currentColor" class="bi bi-folder" viewBox="0 0 16 16">
            <path d="M.54 3.87.5 3a2 2 0 0 1 2-2h3.672a2 2 0 0 1 1.414.586l.828.828A2 2 0 0 0 9.828 3h3.982a2 2 0 0 1 1.992 2.181l-.637 7A2 2 0 0 1 13.174 14H2.826a2 2 0 0 1-1.991-1.819l-.637-7a1.99 1.99 0 0 1 .342-1.31zM2.19 4a1 1 0 0 0-.996 1.09l.637 7a1 1 0 0 0 .995.91h10.348a1 1 0 0 0 .995-.91l.637-7A1 1 0 0 0 13.81 4H2.19zm4.69-1.707A1 1 0 0 0 6.172 2H2.5a1 1 0 0 0-1 .981l.006.139C1.72 3.042 1.95 3 2.19 3h5.396l-.707-.707z" />
          </svg></td>
        <td><a href="?path=<?= $v['file_path']; ?>"> <?= $v['file_name']; ?> </a></td>
        <td><?= $v['file_time'] ?></td>
        <td>-</td>
        <td><a href="?path=<?= $v['file_path']; ?>">打开</a></td>
      </tr>
    <?php endforeach; ?>
    <?php foreach ($file_list['file'] as $v) :
      if ($v['file_name'] == 'index.php' | $v['file_name'] == 'web.config') {
        continue;
      }
    ?>
      <tr>
        <?php
        $temp = explode('.', $v['file_name']);
        $ext = end($temp);
        if ($ext == 'mp4' | $ext == 'mkv') { ?>
          <td><svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" fill="currentColor" class="bi bi-play-btn" viewBox="0 0 16 16">
              <path d="M6.79 5.093A.5.5 0 0 0 6 5.5v5a.5.5 0 0 0 .79.407l3.5-2.5a.5.5 0 0 0 0-.814l-3.5-2.5z" />
              <path d="M0 4a2 2 0 0 1 2-2h12a2 2 0 0 1 2 2v8a2 2 0 0 1-2 2H2a2 2 0 0 1-2-2V4zm15 0a1 1 0 0 0-1-1H2a1 1 0 0 0-1 1v8a1 1 0 0 0 1 1h12a1 1 0 0 0 1-1V4z" />
            </svg></td>
        <?php
        } elseif ($ext == 'torrent') {
        ?>
          <td><svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" fill="currentColor" class="bi bi-magnet-fill" viewBox="0 0 16 16">
              <path d="M15 12h-4v3h4v-3ZM5 12H1v3h4v-3ZM0 8a8 8 0 1 1 16 0v8h-6V8a2 2 0 1 0-4 0v8H0V8Z" />
            </svg></td>
        <?php
        } elseif ($ext == 'jpg' | $ext == 'png') {
        ?>
          <td><svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" fill="currentColor" class="bi bi-image" viewBox="0 0 16 16">
              <path d="M6.002 5.5a1.5 1.5 0 1 1-3 0 1.5 1.5 0 0 1 3 0z" />
              <path d="M2.002 1a2 2 0 0 0-2 2v10a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V3a2 2 0 0 0-2-2h-12zm12 1a1 1 0 0 1 1 1v6.5l-3.777-1.947a.5.5 0 0 0-.577.093l-3.71 3.71-2.66-1.772a.5.5 0 0 0-.63.062L1.002 12V3a1 1 0 0 1 1-1h12z" />
            </svg></td>
        <?php
        } else {
        ?>
          <td><svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" fill="currentColor" class="bi bi-files" viewBox="0 0 16 16">
              <path d="M13 0H6a2 2 0 0 0-2 2 2 2 0 0 0-2 2v10a2 2 0 0 0 2 2h7a2 2 0 0 0 2-2 2 2 0 0 0 2-2V2a2 2 0 0 0-2-2zm0 13V4a2 2 0 0 0-2-2H5a1 1 0 0 1 1-1h7a1 1 0 0 1 1 1v10a1 1 0 0 1-1 1zM3 4a1 1 0 0 1 1-1h7a1 1 0 0 1 1 1v10a1 1 0 0 1-1 1H4a1 1 0 0 1-1-1V4z" />
            </svg></td>
        <?php
        }


        if ($ext == 'mp4' | $ext == 'mkv') { ?>
          <td><a href="/player/?path=.<?= $v['file_path']; ?>"> <?= $v['file_name']; ?> </a></td>
        <?php
        }
        elseif ($ext == 'jpg' | $ext == 'png') { ?>
          <td><a href="<?= $v['file_path']; ?>"> <?= $v['file_name']; ?> </a></td>
        <?php
        } else { ?>
          <td><?= $v['file_name']; ?></td>
        <?php
        }
        ?>

        <td><?= $v['file_time']; ?></td>
        <td><?php
            if ($v['file_size'] < 1024) { //1B
              $show = sprintf("%01.2f", $v['file_size'] . 'B');
            } elseif ($v['file_size'] <= 1024 * 1024) { //1KB
              $show = sprintf("%01.2f", $v['file_size'] / 1024) . 'KB';
            } elseif ($v['file_size'] <= 1024 * 1024 * 1024) { //1MB
              $show = sprintf("%01.2f", $v['file_size'] / 1024 / 1024) . 'MB';
            } elseif ($v['file_size'] <= 1024 * 1024 * 1024 * 1024) { //1GB
              $show = sprintf("%01.2f", $v['file_size'] / 1024 / 1024 / 1024) . 'GB';
            } elseif ($v['file_size'] <= 1024 * 1024 * 1024 * 1024 * 1024) { //1TB
              $show = sprintf("%01.2f", $v['file_size'] / 1024 / 1024 / 1024 / 1024) . 'TB';
            } else {
              $show = "太大啦";
            }
            ?>
          <?= $show; ?>
        </td>
        <?php

        if ($ext == 'mp4' | $ext == 'mkv') { ?>
          <td><a href="/doc/player/?path=.<?= $v['file_path']; ?>">播放</a></td>
        <?php
        }elseif($ext == 'jpg' | $ext == 'png'){?>
          <td><a href=".<?= $v['file_path']; ?>">浏览</a></td>
        <?php
        }else{?>
          <td>-</td>
        <?php
        }
        ?>
          <td><a href="/doc/<?= $v['file_path']; ?>">下载</a></td>
      </tr>
    <?php endforeach; ?>
  </table>
  <p align="center" class="help">如果播放不出来说明是H265编码</br></p>
  <p align="center" class="help">不支持网页解码</br></p>
  <p align="center" class="help">需要下载到本地观看</br></p>
  <div class="tupian">
    <img src="https://tool.lu/netcard/">
  </div>
</body>

</html>