Skip to content

Commit 13c0534

Browse files
committed
fix: issue with fread, when filesize is false/null.
1 parent 276f753 commit 13c0534

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

php-error-log-viewer.php

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,23 @@ public function ajax_handle_errors() {
9595
}
9696
}
9797

98+
/**
99+
* Read the log-file.
100+
*
101+
* @return string|false The read string or false on failure.
102+
*/
98103
public function get_file() {
99-
100-
$myfile = fopen( $this->settings['file_path'], 'r' ) or die( 'Unable to open file!' );
101-
return fread( $myfile, $this->get_filesize() );
104+
$my_file = fopen( $this->settings['file_path'], 'r' );
105+
$size = $this->get_size();
106+
return ( $my_file && $size ) ? fread( $my_file, $size ) : false;
102107
}
103108

104-
public function get_filesize() {
109+
/**
110+
* Get the size of the log-file.
111+
*
112+
* @return int|false The size of the log file in bytes or false.
113+
*/
114+
public function get_size() {
105115
if ( empty( $this->filesize ) ) {
106116
$this->filesize = filesize( $this->settings['file_path'] );
107117
}
@@ -117,10 +127,10 @@ public function is_file_valid() {
117127
if ( ! file_exists( $this->settings['file_path'] ) ) {
118128
return 'The file you specified does not exist (' . $this->settings['file_path'] . ')';
119129
}
120-
if ( 0 == $this->get_filesize() ) {
130+
if ( 0 == $this->get_size() ) {
121131
return 'Your log file is empty.';
122132
}
123-
$mbs = $this->get_filesize() / 1024 / 1024; // in MB.
133+
$mbs = $this->get_size() / 1024 / 1024; // in MB.
124134
if ( $mbs > 100 ) {
125135
if ( ! isset( $_GET['ignore'] ) ) {
126136
return( "Aborting. debug.log is larger than 100 MB ($mbs).
@@ -220,6 +230,9 @@ public function ajax_header() {
220230
public function ajax_json_log() {
221231
$this->ajax_header();
222232
$file = $this->get_file();
233+
if ( ! $file ) {
234+
die( "File is empty or can't be opened." );
235+
}
223236
$this->parse( $file ); // writes to $this->content. preg_replace_callback is odd.
224237
echo( json_encode( array_values( $this->content ) ) );
225238
die();
@@ -233,7 +246,7 @@ public function ajax_delete() {
233246

234247
public function ajax_filesize() {
235248
$this->ajax_header();
236-
echo json_encode( $this->get_filesize() );
249+
echo json_encode( $this->get_size() );
237250
die();
238251
}
239252
}

0 commit comments

Comments
 (0)