From e6e7e77e8ec8058260874df5408c638165cb872f Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Fri, 10 Feb 2023 21:22:02 +0800 Subject: [PATCH] fix(problem/manage/data): whitespace in dir name --- web/app/controllers/problem_data_manage.php | 45 ++++++++++++++------- 1 file changed, 30 insertions(+), 15 deletions(-) diff --git a/web/app/controllers/problem_data_manage.php b/web/app/controllers/problem_data_manage.php index 56a2632..381c768 100644 --- a/web/app/controllers/problem_data_manage.php +++ b/web/app/controllers/problem_data_manage.php @@ -65,26 +65,41 @@ function echoFilePre($file_name) { if ($_POST['problem_data_file_submit'] == 'submit') { if ($_FILES["problem_data_file"]["error"] > 0) { $errmsg = "Error: " . $_FILES["problem_data_file"]["error"]; - becomeMsgPage('
' . $errmsg . '
返回'); + UOJResponse::message('
' . HTML::escape($errmsg) . '
返回'); } else { - $zip_mime_types = array('application/zip', 'application/x-zip', 'application/x-zip-compressed'); + $zip_mime_types = ['application/zip', 'application/x-zip', 'application/x-zip-compressed']; + if (in_array($_FILES["problem_data_file"]["type"], $zip_mime_types) || $_FILES["problem_data_file"]["type"] == 'application/octet-stream' && substr($_FILES["problem_data_file"]["name"], -4) == '.zip') { - $up_filename = "/tmp/" . rand(0, 100000000) . "data.zip"; - move_uploaded_file($_FILES["problem_data_file"]["tmp_name"], $up_filename); $zip = new ZipArchive; - if ($zip->open($up_filename) === TRUE) { - $zip->extractTo("/var/uoj_data/upload/{$problem['id']}"); - $zip->close(); - exec("cd /var/uoj_data/upload/{$problem['id']}; if [ -z \"`find . -maxdepth 1 -type f`\" ]; then for sub_dir in `find -maxdepth 1 -type d ! -name .`; do mv -f \$sub_dir/* . && rm -rf \$sub_dir; done; fi"); - echo ""; - } else { - $errmsg = "解压失败!"; - becomeMsgPage('
' . $errmsg . '
返回'); + + try { + if ($zip->open($_FILES["problem_data_file"]["tmp_name"]) !== true) { + throw new UOJUploadFailedException('压缩文件打开失败'); + } + + if (!$zip->extractTo(UOJProblem::cur()->getUploadFolderPath())) { + throw new UOJUploadFailedException('压缩文件解压失败'); + } + + if (!$zip->close()) { + throw new UOJUploadFailedException('压缩文件关闭失败'); + } + } catch (Exception $e) { + becomeMsgPage('
' . $e->getMessage() . '
返回'); } - unlink($up_filename); + + UOJLocalRun::execAnd([ + ['cd', UOJProblem::cur()->getUploadFolderPath()], + <<<'EOD' + if [ "$(find . -maxdepth 1 -type f)File" = "File" ]; + then for sub_dir in "$(find -maxdepth 1 -type d ! -name .)"; + do mv -f "$sub_dir"/* . && rm -rf "$sub_dir"; done; fi + EOD + ]); + + echo ""; } else { - $errmsg = "请上传zip格式!"; - becomeMsgPage('
' . $errmsg . '
返回'); + becomeMsgPage('
请上传 zip 格式的文件!
返回'); } } }