Confirm buffer is not empty before erasing

Confirm file_exists before opening. For PHP 5.3 or earlier, check if
safe_mode is active to avoid errors.
This commit is contained in:
toddlahman 2014-05-25 14:10:23 -07:00
parent 42f9eab1bc
commit 969fd22849
1 changed files with 28 additions and 14 deletions

View File

@ -233,16 +233,19 @@ class WC_Download_Handler {
/** /**
* Prevents errors, for example: transfer closed with 3 bytes remaining to read * Prevents errors, for example: transfer closed with 3 bytes remaining to read
*/ */
@ob_end_clean(); // Clear the output buffer if ( ob_get_length() ) {
if ( ob_get_level() ) { if ( ob_get_level() ) {
$levels = ob_get_level(); $levels = ob_get_level();
for ( $i = 0; $i < $levels; $i++ ) { for ( $i = 0; $i < $levels; $i++ ) {
@ob_end_clean(); // Zip corruption fix ob_end_clean(); // Zip corruption fix
} }
} else {
ob_end_clean(); // Clear the output buffer
}
} }
if ( $is_IE && is_ssl() ) { if ( $is_IE && is_ssl() ) {
@ -299,9 +302,9 @@ class WC_Download_Handler {
} }
if ( $remote_file ) { if ( $remote_file ) {
$this->readfile_chunked( $file_path ) or header( 'Location: ' . $file_path ); $this->readfile_chunked( $file_path ) || header( 'Location: ' . $file_path );
} else { } else {
$this->readfile_chunked( $file_path ) or wp_die( __( 'File not found', 'woocommerce' ) . ' <a href="' . esc_url( home_url() ) . '" class="wc-forward">' . __( 'Go to homepage', 'woocommerce' ) . '</a>' ); $this->readfile_chunked( $file_path ) || wp_die( __( 'File not found', 'woocommerce' ) . ' <a href="' . esc_url( home_url() ) . '" class="wc-forward">' . __( 'Go to homepage', 'woocommerce' ) . '</a>' );
} }
exit; exit;
@ -321,16 +324,27 @@ class WC_Download_Handler {
$buffer = ''; $buffer = '';
$cnt = 0; $cnt = 0;
if ( file_exists( $file ) ) {
$handle = fopen( $file, 'r' );
if ( $handle === FALSE ) {
return FALSE;
}
} elseif ( version_compare( PHP_VERSION, '5.4.0', '<' ) && ini_get( 'safe_mode' ) ) {
$handle = @fopen( $file, 'r' ); $handle = @fopen( $file, 'r' );
if ( $handle === FALSE ) { if ( $handle === FALSE ) {
return FALSE; return FALSE;
} }
} else {
return FALSE;
}
while ( ! feof( $handle ) ) { while ( ! feof( $handle ) ) {
$buffer = fread( $handle, $chunksize ); $buffer = fread( $handle, $chunksize );
echo $buffer; echo $buffer;
@ob_flush(); if ( ob_get_length() ) {
@flush(); ob_flush();
flush();
}
if ( $retbytes ) { if ( $retbytes ) {
$cnt += strlen( $buffer ); $cnt += strlen( $buffer );