From 5d87e913a17ebfae71705004c695c2b572e12f40 Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Fri, 24 Mar 2017 16:08:19 +0100 Subject: Improve error handling in json reformater Signed-off-by: Florian Pritz --- application/controllers/file/file_default.php | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) (limited to 'application/controllers/file/file_default.php') diff --git a/application/controllers/file/file_default.php b/application/controllers/file/file_default.php index 15fb54832..265bff0d8 100644 --- a/application/controllers/file/file_default.php +++ b/application/controllers/file/file_default.php @@ -242,7 +242,7 @@ class File_default extends MY_Controller { echo $this->load->view('file/html_footer', $this->data, true); } - private function _colorify($file, $lexer, $anchor_id = false) + private function _colorify($output_cache, $file, $lexer, $anchor_id = false) { $output = ""; $lines_to_remove = 0; @@ -254,7 +254,20 @@ class File_default extends MY_Controller { $linecount = count(explode("\n", $content)); if ($lexer === "json" && $linecount === 1) { - $content = json_encode(json_decode($content), JSON_PRETTY_PRINT); + $decoded_json = json_decode($content); + if ($decoded_json !== null && $decoded_json !== false) { + $pretty_json = json_encode($decoded_json, JSON_PRETTY_PRINT); + if ($pretty_json !== false) { + $content = $pretty_json; + $output_cache->render_now( + array( + "error_type" => "alert-info", + "error_message" => "

The file below has been reformated for readability. It may differ from the original.

" + ), + "file/fragments/alert-wide" + ); + } + } } if ($lexer == "ascii") { @@ -320,7 +333,7 @@ class File_default extends MY_Controller { // highlight the file and cache the result, fall back to plain text if $lexer fails foreach (array($lexer, "text") as $lexer) { $highlit = cache_function($filedata['data_id'].'_'.$lexer, 100, - function() use ($filedata, $lexer, $is_multipaste) { + function() use ($output_cache, $filedata, $lexer, $is_multipaste) { $file = $this->mfile->file($filedata['data_id']); if ($lexer == "rmd") { ob_start(); @@ -340,7 +353,7 @@ class File_default extends MY_Controller { "return_value" => 0, ); } else { - return get_instance()->_colorify($file, $lexer, $is_multipaste ? $filedata["id"] : false); + return get_instance()->_colorify($output_cache, $file, $lexer, $is_multipaste ? $filedata["id"] : false); } }); -- cgit v1.2.3-24-g4f1b