From 1551514abf0315ea17c8f0d5ffde0d58e48ffa39 Mon Sep 17 00:00:00 2001 From: Chenx221 Date: Fri, 16 Feb 2024 12:10:27 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=89=B9=E9=87=8F=E5=88=A0?= =?UTF-8?q?=E9=99=A4=E5=8A=9F=E8=83=BD,=E4=BF=AE=E5=A4=8D=E6=95=85?= =?UTF-8?q?=E9=9A=9C=E7=9A=84=E5=8D=95=E6=96=87=E4=BB=B6=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controllers/HomeController.php | 54 +++++++++++++++++++--------------- web/js/home_script.js | 19 ++++++++++-- 2 files changed, 47 insertions(+), 26 deletions(-) diff --git a/controllers/HomeController.php b/controllers/HomeController.php index 1dba1f8..0cd40ac 100644 --- a/controllers/HomeController.php +++ b/controllers/HomeController.php @@ -210,36 +210,42 @@ class HomeController extends Controller */ public function actionDelete() { - $relativePath = Yii::$app->request->post('relativePath'); - $relativePath = rawurldecode($relativePath); - if (!preg_match($this->pattern, $relativePath) || str_contains($relativePath, '..')) { - throw new NotFoundHttpException('Invalid file path.'); - } - $absolutePath = Yii::getAlias(Yii::$app->params['dataDirectory']) . '/' . Yii::$app->user->id . '/' . $relativePath; - if (!file_exists($absolutePath)) { - throw new NotFoundHttpException('File or directory not found.'); - } else { - $realPath = realpath($absolutePath); - $expectedPathPrefix = realpath(Yii::getAlias(Yii::$app->params['dataDirectory']) . '/' . Yii::$app->user->id); - if (!str_starts_with($realPath, $expectedPathPrefix)) { - throw new NotFoundHttpException('File or directory not found.'); - } + $relativePaths = Yii::$app->request->post('relativePath'); + if (!is_array($relativePaths)) { + $relativePaths = [$relativePaths]; } - if (is_dir($absolutePath)) { - if (!$this->deleteDirectory($absolutePath)) { - Yii::$app->session->setFlash('error', 'Failed to delete directory.'); - } else { - Yii::$app->session->setFlash('success', 'Directory deleted successfully.'); + foreach ($relativePaths as $relativePath) { + $relativePath = rawurldecode($relativePath); + if (!preg_match($this->pattern, $relativePath) || str_contains($relativePath, '..')) { + throw new NotFoundHttpException('Invalid file path.'); } - } else { - if (!unlink($absolutePath)) { - Yii::$app->session->setFlash('error', 'Failed to delete file.'); + $absolutePath = Yii::getAlias(Yii::$app->params['dataDirectory']) . '/' . Yii::$app->user->id . '/' . $relativePath; + if (!file_exists($absolutePath)) { + throw new NotFoundHttpException('File or directory not found.'); } else { - Yii::$app->session->setFlash('success', 'File deleted successfully.'); + $realPath = realpath($absolutePath); + $expectedPathPrefix = realpath(Yii::getAlias(Yii::$app->params['dataDirectory']) . '/' . Yii::$app->user->id); + if (!str_starts_with($realPath, $expectedPathPrefix)) { + throw new NotFoundHttpException('File or directory not found.'); + } + } + + if (is_dir($absolutePath)) { + if (!$this->deleteDirectory($absolutePath)) { + Yii::$app->session->setFlash('error', 'Failed to delete directory.'); + } else { + Yii::$app->session->setFlash('success', 'Directory deleted successfully.'); + } + } else { + if (!unlink($absolutePath)) { + Yii::$app->session->setFlash('error', 'Failed to delete file.'); + } else { + Yii::$app->session->setFlash('success', 'File deleted successfully.'); + } } } - return $this->redirect(['index', 'directory' => dirname($relativePath)]); + return $this->redirect(['index', 'directory' => dirname($relativePaths[0])]); } /** diff --git a/web/js/home_script.js b/web/js/home_script.js index f93d052..efc0a77 100644 --- a/web/js/home_script.js +++ b/web/js/home_script.js @@ -150,8 +150,23 @@ $(document).on('click', '.single-share-btn', function () { }); $(document).on('click', '.batch-delete-btn', function () { - console.log('删除按钮被点击'); - // 在这里添加你的代码 + var relativePaths = $('.select-item:checked').map(function () { + return $(this).data('relativePath'); + }).get(); + $.ajax({ + type: "POST", + url: "index.php?r=home%2Fdelete", + data: { relativePath: relativePaths }, + success: function(response) { + // 处理响应 + location.reload(); + }, + error: function() { + // 处理错误 + console.error('AJAX request failed.'); + location.reload(); + } + }); }); //下面的代码实现了各种按钮/样式功能,建议别看了(