From 3aaa397361c5b176911e8a575c5298f98cea28c0 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Fri, 24 Oct 2014 17:06:30 +0100 Subject: [PATCH] WC_Download_Handler - readfile_chunked refactor --- includes/class-wc-download-handler.php | 56 +++++++++----------------- includes/wc-deprecated-functions.php | 2 +- 2 files changed, 20 insertions(+), 38 deletions(-) diff --git a/includes/class-wc-download-handler.php b/includes/class-wc-download-handler.php index 9019964007a..fe0199331eb 100644 --- a/includes/class-wc-download-handler.php +++ b/includes/class-wc-download-handler.php @@ -321,51 +321,33 @@ class WC_Download_Handler { /** * readfile_chunked + * * Reads file in chunks so big downloads are possible without changing PHP.INI - http://codeigniter.com/wiki/Download_helper_for_large_files/ - * @param string $file - * @param bool $retbytes return bytes of file - * @return bool|int - * @todo Meaning of the return value? Last return is status of fclose? + * + * @param string $file + * @return bool Success or fail */ - public static function readfile_chunked( $file, $retbytes = true ) { + public static function readfile_chunked( $file ) { $chunksize = 1 * ( 1024 * 1024 ); - $buffer = ''; - $cnt = 0; - if ( file_exists( $file ) ) { - $handle = fopen( $file, 'r' ); - if ( $handle === FALSE ) { - return FALSE; + if ( file_exists( $file ) || ( version_compare( PHP_VERSION, '5.4.0', '<' ) && ini_get( 'safe_mode' ) ) ) { + if ( ( $handle = @fopen( $file, 'r' ) ) === false ) { + return false; } - } elseif ( version_compare( PHP_VERSION, '5.4.0', '<' ) && ini_get( 'safe_mode' ) ) { - $handle = @fopen( $file, 'r' ); - if ( $handle === FALSE ) { - return FALSE; + + while ( ! feof( $handle ) ) { + $buffer = fread( $handle, $chunksize ); + echo $buffer; + if ( ob_get_length() ) { + ob_flush(); + flush(); + } } - } else { - return FALSE; + + return fclose( $handle ); } - while ( ! feof( $handle ) ) { - $buffer = fread( $handle, $chunksize ); - echo $buffer; - if ( ob_get_length() ) { - ob_flush(); - flush(); - } - - if ( $retbytes ) { - $cnt += strlen( $buffer ); - } - } - - $status = fclose( $handle ); - - if ( $retbytes && $status ) { - return $cnt; - } - - return $status; + return false; } } diff --git a/includes/wc-deprecated-functions.php b/includes/wc-deprecated-functions.php index 1e34dbbdda5..d0f1c25e3be 100644 --- a/includes/wc-deprecated-functions.php +++ b/includes/wc-deprecated-functions.php @@ -57,7 +57,7 @@ function woocommerce_create_page( $slug, $option = '', $page_title = '', $page_c */ function woocommerce_readfile_chunked( $file, $retbytes = true ) { _deprecated_function( 'woocommerce_readfile_chunked', '2.1', 'WC_Download_Handler::readfile_chunked()' ); - return WC_Download_Handler::readfile_chunked( $file, $retbytes ); + return WC_Download_Handler::readfile_chunked( $file ); } /**