From 9159880f124d9d9732dece97a0458309466a381b Mon Sep 17 00:00:00 2001 From: Chenx221 Date: Sat, 10 Feb 2024 17:41:07 +0800 Subject: [PATCH] =?UTF-8?q?update=20new=20feature:=20simple=20file=20manag?= =?UTF-8?q?e=20=E7=9B=AE=E5=89=8D=E5=8F=AA=E5=AE=9E=E7=8E=B0=E4=BA=86?= =?UTF-8?q?=E7=9B=AE=E5=BD=95=E5=88=87=E6=8D=A2=20=E8=B7=AF=E5=BE=84?= =?UTF-8?q?=E6=98=BE=E7=A4=BA=20=E6=96=87=E4=BB=B6=E4=B8=8B=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/params.php | 2 + config/web.php | 1 + controllers/HomeController.php | 88 ++++++++++++++++++++++++++++++++++ views/home/index.php | 48 +++++++++++++++++++ views/layouts/main.php | 8 ++-- views/site/login.php | 2 +- 6 files changed, 144 insertions(+), 5 deletions(-) create mode 100644 controllers/HomeController.php create mode 100644 views/home/index.php diff --git a/config/params.php b/config/params.php index 981c621..df49998 100644 --- a/config/params.php +++ b/config/params.php @@ -4,4 +4,6 @@ return [ 'adminEmail' => 'admin@example.com', 'senderEmail' => 'noreply@example.com', 'senderName' => 'Example.com mailer', + // data directory is used to store uploaded files by the user (e.g. root dir for every user's home) + 'dataDirectory' => '@app/data', ]; diff --git a/config/web.php b/config/web.php index ae5b4bc..d2de32d 100644 --- a/config/web.php +++ b/config/web.php @@ -22,6 +22,7 @@ $config = [ 'user' => [ 'identityClass' => 'app\models\User', 'enableAutoLogin' => true, + 'loginUrl' => ['user/login'], // 登录地址 ], 'errorHandler' => [ 'errorAction' => 'site/error', diff --git a/controllers/HomeController.php b/controllers/HomeController.php new file mode 100644 index 0000000..248683e --- /dev/null +++ b/controllers/HomeController.php @@ -0,0 +1,88 @@ +user->isGuest) { + return $this->redirect(Yii::$app->user->loginUrl); + } + $rootDataDirectory = Yii::getAlias(Yii::$app->params['dataDirectory']); + $userId = Yii::$app->user->id; + + if ($directory == null | $directory == '.') { + $parentDirectory = null; + } else { + $parentDirectory = dirname($directory); + } + $directoryContents = $this->getDirectoryContents(join(DIRECTORY_SEPARATOR, [$rootDataDirectory, $userId, $directory ?: '.'])); + return $this->render('index', [ + 'directoryContents' => $directoryContents, + 'parentDirectory' => $parentDirectory, + 'directory' => $directory, // 将$directory传递给视图 + ]); + } + + /** + * 获取指定路径下的文件和文件夹内容 + * @param string $path 路径 + * @return array 文件和文件夹内容数组 + * @throws NotFoundHttpException 如果路径不存在 + */ + protected function getDirectoryContents($path) + { + // 确定路径是否存在 + if (!is_dir($path)) { + throw new NotFoundHttpException('Directory not found.'); + } + + // 获取路径下的所有文件和文件夹 + $directoryContents = scandir($path); + + return array_diff($directoryContents, ['.', '..']); + } + + /** + * 下载指定路径下的文件 + * @param string $relativePath 文件的相对路径 + * @throws NotFoundHttpException 如果文件不存在 + */ + public function actionDownload($relativePath) + { + // 对相对路径进行解码 + $relativePath = rawurldecode($relativePath); + + // 检查相对路径是否只包含允许的字符 + if (!preg_match('/^[\w\-.\/]+$/u', $relativePath)) { + throw new NotFoundHttpException('Invalid file path.'); + } + + // 确定文件的绝对路径 + $absolutePath = Yii::getAlias(Yii::$app->params['dataDirectory']) . '/' . Yii::$app->user->id . '/' . $relativePath; + + // 使用realpath函数解析路径,并检查解析后的路径是否在预期的目录中 + $realPath = realpath($absolutePath); + $dataDirectory = str_replace('/', '\\', Yii::getAlias(Yii::$app->params['dataDirectory'])); + if (!$realPath || !str_starts_with($realPath, $dataDirectory)) { + throw new NotFoundHttpException('File not found.'); + } + + // 检查文件是否存在 + if (!file_exists($realPath)) { + throw new NotFoundHttpException('File not found.'); + } + + // 将文件发送给用户进行下载 + Yii::$app->response->sendFile($realPath)->send(); + } +} diff --git a/views/home/index.php b/views/home/index.php new file mode 100644 index 0000000..6e65d81 --- /dev/null +++ b/views/home/index.php @@ -0,0 +1,48 @@ +title = '文件管理'; +$this->params['breadcrumbs'][] = $this->title; +?> +
+

title) ?>

+ +

+ + + $path]); + $path .= '/'; + endforeach; + ?> + +

+ + +
\ No newline at end of file diff --git a/views/layouts/main.php b/views/layouts/main.php index 6bc7c7d..751d11d 100644 --- a/views/layouts/main.php +++ b/views/layouts/main.php @@ -41,12 +41,12 @@ $this->registerLinkTag(['rel' => 'icon', 'type' => 'image/x-icon', 'href' => Yii 'options' => ['class' => 'navbar-nav'], 'items' => [ ['label' => '首页', 'url' => ['/site/index']], - ['label' => '文件', 'url' => ['/site/about']], - ['label' => '分享', 'url' => ['/site/about']], + ['label' => '我的文件', 'url' => ['/home/index']], + ['label' => '分享管理', 'url' => ['/site/about']], ['label' => '个人设置', 'url' => ['/site/about']], ['label' => '系统设置', 'url' => ['/site/contact']], - ['label' => '客户端下载', 'url' => ['/site/contact']], - ['label' => 'API', 'url' => ['/site/about']], + ['label' => '应用下载', 'url' => ['/site/contact']], + ['label' => 'API文档', 'url' => ['/site/about']], Yii::$app->user->isGuest ? ['label' => '登录', 'url' => ['/user/login']] : '