From c1294121d3114a37275b83c73c71df6f071531af Mon Sep 17 00:00:00 2001 From: Masco Skray Date: Thu, 11 Oct 2018 23:01:50 +0800 Subject: [PATCH] fix(uoj/1/app/libs): fix no preview of text files in problem_data_manage When managing data, there will be a preview of test data or validator binary file. But in PHP7 the prefiew of test data text file or problem.conf text file is not shown. After my test it is caused by file_get_contents() when passing offset with -1. It is strange that the manual on php.net in Chinese the default of $offset is -1, but in English the default of $offset is 0. Use -1 will get nothing, 0 instead is fine. The commit also change uojFilePreview() that can also be used as viewing binary file. For backwards compatibility, we just set text as the default file type and no need to specify. --- uoj/1/app/controllers/problem_data_manage.php | 2 +- uoj/1/app/libs/uoj-html-lib.php | 9 +++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/uoj/1/app/controllers/problem_data_manage.php b/uoj/1/app/controllers/problem_data_manage.php index dcb1aea..a9c3d7d 100644 --- a/uoj/1/app/controllers/problem_data_manage.php +++ b/uoj/1/app/controllers/problem_data_manage.php @@ -65,7 +65,7 @@ EOD; if (strStartWith($mimetype, 'text/')) { echo htmlspecialchars(uojFilePreview($file_full_name, $output_limit)); } else { - echo htmlspecialchars(strOmit(shell_exec('xxd -g 4 -l 5000 ' . escapeshellarg($file_full_name) . ' | head -c ' . ($output_limit + 4)), $output_limit)); + echo htmlspecialchars(uojFilePreview($file_full_name, $output_limit, 'binary')); } echo "\n"; } diff --git a/uoj/1/app/libs/uoj-html-lib.php b/uoj/1/app/libs/uoj-html-lib.php index 20f5309..b0ae04e 100644 --- a/uoj/1/app/libs/uoj-html-lib.php +++ b/uoj/1/app/libs/uoj-html-lib.php @@ -24,8 +24,13 @@ function uojHandleAtSign($str, $uri) { return array($res, $referrers_list); } -function uojFilePreview($file_name, $output_limit) { - return strOmit(file_get_contents($file_name, false, null, -1, $output_limit + 4), $output_limit); +function uojFilePreview($file_name, $output_limit, $file_type = 'text') { + switch ($file_type) { + case 'text': + return strOmit(file_get_contents($file_name, false, null, 0, $output_limit + 4), $output_limit); + default: + return strOmit(shell_exec('xxd -g 4 -l 5000 ' . escapeshellarg($file_name) . ' | head -c ' . ($output_limit + 4)), $output_limit); + } } function uojIncludeView($name, $view_params = array()) {