From be257eeb03daaa29d6d12b8317331da34f3f2891 Mon Sep 17 00:00:00 2001 From: FusionPBX Date: Mon, 12 Sep 2022 09:19:21 -0600 Subject: [PATCH] Use is_array before foreach --- app/log_viewer/log_viewer.php | 131 ++++++++++++++++++++-------------- 1 file changed, 77 insertions(+), 54 deletions(-) diff --git a/app/log_viewer/log_viewer.php b/app/log_viewer/log_viewer.php index 25acb819e5..6a4038e892 100644 --- a/app/log_viewer/log_viewer.php +++ b/app/log_viewer/log_viewer.php @@ -66,9 +66,11 @@ //set default default log file if (isset($_POST['log_file'])) { $approved_files = glob($_SESSION['switch']['log']['dir'].'/freeswitch.log*'); - foreach($approved_files as $approved_file) { - if ($approved_file == $_SESSION['switch']['log']['dir'].'/'.$_POST['log_file']) { - $log_file = $approved_file; + if (is_array($approved_files)) { + foreach($approved_files as $approved_file) { + if ($approved_file == $_SESSION['switch']['log']['dir'].'/'.$_POST['log_file']) { + $log_file = $approved_file; + } } } } @@ -81,9 +83,11 @@ if (isset($_GET['n'])) { if (isset($filename)) { unset($filename); } $approved_files = glob($_SESSION['switch']['log']['dir'].'/freeswitch.log*'); - foreach($approved_files as $approved_file) { - if ($approved_file == $_SESSION['switch']['log']['dir'].'/'.$_GET['n']) { - $filename = $approved_file; + if (is_array($approved_files)) { + foreach($approved_files as $approved_file) { + if ($approved_file == $_SESSION['switch']['log']['dir'].'/'.$_GET['n']) { + $filename = $approved_file; + } } } if (isset($filename) && file_exists($filename)) { @@ -98,6 +102,16 @@ } } +//get the file size + if (file_exists($log_file)) { + $file_size = filesize($log_file); + } + +//read the log + if (file_exists($log_file)) { + $file_handle = fopen($log_file, "r"); + } + //include the header $document['title'] = $text['title-log_viewer']; require_once "resources/header.php"; @@ -109,9 +123,11 @@ echo "
\n"; echo " ".$text['label-log_file']." \n"; echo $text['label-filter']." "; @@ -181,8 +197,6 @@ $array_filter[6]['type'] = 'bold'; $array_filter[6]['font'] = 'monospace'; - $file_size = filesize($log_file); - /* // removed: duplicate of above if (isset($_POST['submit'])) { @@ -208,8 +222,6 @@ echo " ".$text['label-displaying']." ".number_format($user_file_size,0,'.',',')." of ".number_format($file_size,0,'.',',')." ".$text['label-bytes']."."; echo ""; - $file = fopen($log_file, "r") or exit($text['error-open_file']); - //set pointer in file if ($user_file_size >= '0') { if ($user_file_size == '0') { @@ -217,21 +229,23 @@ } if ($file_size >= $user_file_size) { //set an offset on fopen - $byte_count=$file_size-$user_file_size; + $byte_count = $file_size-$user_file_size; fseek($file, $byte_count); //echo "opening at " . $byte_count . " bytes
"; } else { if ($file_size >= $default_file_size) { //set an offset on fopen - $byte_count=$file_size-$default_file_size; + $byte_count = $file_size-$default_file_size; fseek($file, $byte_count); echo $text['label-open_at']." " . $byte_count . " ".$text['label-bytes']."
"; } else { //open the file - $byte_count='0'; - fseek($file, 0); + $byte_count ='0'; + if ($file_handle) { + fseek($file_handle, 0); + } echo "
".$text['label-open_file']."
"; } } @@ -253,40 +267,42 @@ //start processing $byte_count = 0; - while(!feof($file)) { - $log_line = escape(fgets($file)); - $byte_count++; - $noprint = false; + if ($file_handle) { + while(!feof($file_handle)) { + $log_line = escape(fgets($file_handle)); + $byte_count++; + $noprint = false; - $skip_line = false; - if (!empty($filter) ) { - $uuid_match = strpos($log_line, $filter); - if ($uuid_match === false) { - $skip_line = true; - } - else { - $skip_line = false; - } - } - - if ($skip_line === false) { - foreach ($array_filter as $v1) { - $pos = strpos($log_line, escape($v1['pattern'])); - //echo "
POS is: '$pos'
"; - if ($pos !== false) { - //color adjustments on words in log line - for ($i=2; $i<=$MAXEL; $i++) { - if (isset($v1["pattern".$i])) { - $log_line = str_replace(escape($v1["pattern".$i]), "".$v1["pattern".$i]."", $log_line); - } - } - $array_output[] = "".$log_line."
"; - $noprint = true; + $skip_line = false; + if (!empty($filter) ) { + $uuid_match = strpos($log_line, $filter); + if ($uuid_match === false) { + $skip_line = true; + } + else { + $skip_line = false; } } - if ($noprint !== true) { - $array_output[] = "".$log_line."
"; + if ($skip_line === false) { + foreach ($array_filter as $v1) { + $pos = strpos($log_line, escape($v1['pattern'])); + //echo "
POS is: '$pos'
"; + if ($pos !== false) { + //color adjustments on words in log line + for ($i=2; $i<=$MAXEL; $i++) { + if (isset($v1["pattern".$i])) { + $log_line = str_replace(escape($v1["pattern".$i]), "".$v1["pattern".$i]."", $log_line); + } + } + $array_output[] = "".$log_line."
"; + $noprint = true; + } + } + + if ($noprint !== true) { + $array_output[] = "".$log_line."
"; + } } } } @@ -299,16 +315,18 @@ else { $adj_index = 1; } - foreach ($array_output as $index => $line) { - $line_num = ""; - if ($line != "
") { - if ($_POST['line_number']) { - $line_num = "".($index + $adj_index)."   "; + if (is_array($array_output)) { + foreach ($array_output as $index => $line) { + $line_num = ""; + if ($line != "
") { + if ($_POST['line_number']) { + $line_num = "".($index + $adj_index)."   "; + } + echo $line_num." ".$line; } - echo $line_num." ".$line; } } - fclose($file); + echo " "; } echo " "; @@ -318,4 +336,9 @@ //include the footer require_once "resources/footer.php"; +//close the file + if ($file_handle) { + fclose($file_handle); + } + ?>