美化工作

This commit is contained in:
Chenx221 2024-02-11 12:39:05 +08:00
parent 9815159490
commit fab67a308a
Signed by: chenx221
GPG Key ID: D7A9EC07024C3021
2 changed files with 60 additions and 27 deletions

View File

@ -2,6 +2,7 @@
namespace app\controllers; namespace app\controllers;
use app\utils\FileTypeDetector;
use Yii; use Yii;
use yii\web\NotFoundHttpException; use yii\web\NotFoundHttpException;
use yii\web\Response; use yii\web\Response;
@ -9,11 +10,15 @@ use yii\web\Response;
class HomeController extends \yii\web\Controller class HomeController extends \yii\web\Controller
{ {
/** /**
* diplay the page of the file manager (accepts a directory parameter like cd command)
* visit it via https://devs.chenx221.cyou:8081/index.php?r=home
*
* @return string|Response * @return string|Response
* @throws NotFoundHttpException * @throws NotFoundHttpException
*/ */
public function actionIndex($directory = null) public function actionIndex($directory = null)
{ {
//Warning: Security Vulnerability: access via $directory parameter = ../ will display the internal files of the server
if (Yii::$app->user->isGuest) { if (Yii::$app->user->isGuest) {
return $this->redirect(Yii::$app->user->loginUrl); return $this->redirect(Yii::$app->user->loginUrl);
} }
@ -26,6 +31,12 @@ class HomeController extends \yii\web\Controller
$parentDirectory = dirname($directory); $parentDirectory = dirname($directory);
} }
$directoryContents = $this->getDirectoryContents(join(DIRECTORY_SEPARATOR, [$rootDataDirectory, $userId, $directory ?: '.'])); $directoryContents = $this->getDirectoryContents(join(DIRECTORY_SEPARATOR, [$rootDataDirectory, $userId, $directory ?: '.']));
foreach ($directoryContents as $key => $item) {
$relativePath = $directory ? $directory . '/' . $item : $item;
$absolutePath = Yii::getAlias('@app') . '/data/' . Yii::$app->user->id . '/' . $relativePath;
$type = FileTypeDetector::detect($absolutePath);
$directoryContents[$key] = ['name' => $item, 'type' => $type];
}
return $this->render('index', [ return $this->render('index', [
'directoryContents' => $directoryContents, 'directoryContents' => $directoryContents,
'parentDirectory' => $parentDirectory, 'parentDirectory' => $parentDirectory,
@ -54,6 +65,9 @@ class HomeController extends \yii\web\Controller
/** /**
* 下载指定路径下的文件 * 下载指定路径下的文件
* must be provided with a relative path of the file
* download link:https://devs.chenx221.cyou:8081/index.php?r=home%2Fdownload&relativePath={the relative path of the file}
*
* @param string $relativePath 文件的相对路径 * @param string $relativePath 文件的相对路径
* @throws NotFoundHttpException 如果文件不存在 * @throws NotFoundHttpException 如果文件不存在
*/ */

View File

@ -3,46 +3,65 @@
/* @var $this yii\web\View */ /* @var $this yii\web\View */
/* @var $directoryContents array 文件和文件夹内容数组 */ /* @var $directoryContents array 文件和文件夹内容数组 */
/* @var $parentDirectory string 父目录 */ /* @var $parentDirectory string 父目录 */
/* @var $directory string 当前路径 */ /* @var $directory string 当前路径 */
use yii\bootstrap5\Html; use yii\bootstrap5\Html;
use app\assets\FontAwesomeAsset;
$this->title = '文件管理'; $this->title = '文件管理';
$this->params['breadcrumbs'][] = $this->title; $this->params['breadcrumbs'][] = $this->title;
FontAwesomeAsset::register($this);
?> ?>
<div class="home-directory"> <div class="home-directory">
<h1><?= Html::encode($this->title) ?></h1> <h1><?= Html::encode($this->title) ?></h1>
<p> <nav aria-label="breadcrumb">
<?= Html::a('HOME', ['home/index']) ?> <ol class="breadcrumb">
<?php if ($directory !== null): ?> <?= Html::a('HOME', ['home/index'], ['class' => 'breadcrumb-item']) ?>
<?php <?php if ($directory !== null): ?>
$parts = explode('/', $directory); <?php
$path = ''; $parts = explode('/', $directory);
foreach ($parts as $part): $path = '';
$path .= $part; foreach ($parts as $part):
echo ' / ' . Html::a($part, ['home/index', 'directory' => $path]); $path .= $part;
$path .= '/'; echo Html::a($part, ['home/index', 'directory' => $path], ['class' => 'breadcrumb-item']);
endforeach; $path .= '/';
?> endforeach;
<?php endif; ?> ?>
</p> <?php endif; ?>
</ol>
</nav>
<ul> <table class="table table-hover">
<thead class="table-light">
<tr>
<th scope="col">名称</th>
<th scope="col">操作</th>
</tr>
</thead>
<tbody>
<?php foreach ($directoryContents as $item): ?> <?php foreach ($directoryContents as $item): ?>
<?php $relativePath = $directory ? $directory . '/' . $item : $item; ?> <?php $relativePath = $directory ? $directory . '/' . $item['name'] : $item['name']; ?>
<!-- 修复错误的绝对路径问题--> <!-- 修复错误的绝对路径问题-->
<?php $absolutePath = Yii::getAlias('@app') . '/data/' . Yii::$app->user->id . '/' . $relativePath; ?> <?php $absolutePath = Yii::getAlias('@app') . '/data/' . Yii::$app->user->id . '/' . $relativePath; ?>
<?php if (is_dir($absolutePath)): ?> <tr>
<!-- 文件夹 --> <?php if (is_dir($absolutePath)): ?>
<li><?= Html::a($item, ['home/index', 'directory' => $relativePath]) ?></li> <!-- 文件夹 -->
<?php else: ?> <td>
<!-- 文件 --> <?= Html::tag('i', '', ['class' => $item['type']]) ?>
<li><?= Html::a($item, ['home/download', 'relativePath' => $relativePath]) ?> <?= Html::a($item['name'], ['home/index', 'directory' => $relativePath]) ?>
(<?= Html::a('下载', ['home/download', 'relativePath' => $relativePath]) ?>) </td>
</li> <td></td>
<?php endif; ?> <?php else: ?>
<!-- 文件 -->
<td>
<?= Html::tag('i', '', ['class' => $item['type']]) ?>
<?= Html::a($item['name'], ['home/download', 'relativePath' => $relativePath]) ?>
</td>
<td><?= Html::a(Html::tag('i', '', ['class' => 'fa-regular fa-circle-down']), ['home/download', 'relativePath' => $relativePath]) ?></td>
<?php endif; ?>
</tr>
<?php endforeach; ?> <?php endforeach; ?>
</ul> </tbody>
</table>
</div> </div>