Tidy download function. nocache headers

This commit is contained in:
Mike Jolley 2013-02-02 16:48:38 +00:00
parent 9f529ff25a
commit 8b06ab44f1
1 changed files with 92 additions and 101 deletions

View File

@ -898,6 +898,7 @@ function woocommerce_download_product() {
$email = sanitize_email( str_replace( ' ', '+', urldecode( $_GET['email'] ) ) ); $email = sanitize_email( str_replace( ' ', '+', urldecode( $_GET['email'] ) ) );
$download_id = isset( $_GET['key'] ) ? urldecode( $_GET['key'] ) : ''; // backwards compatibility for existing download URLs $download_id = isset( $_GET['key'] ) ? urldecode( $_GET['key'] ) : ''; // backwards compatibility for existing download URLs
$_product = get_product( $product_id ); $_product = get_product( $product_id );
$file_download_method = apply_filters( 'woocommerce_file_download_method', get_option( 'woocommerce_file_download_method' ), $product_id );
if ( ! is_email( $email) ) if ( ! is_email( $email) )
wp_die( __( 'Invalid email address.', 'woocommerce' ) . ' <a href="' . home_url() . '">' . __( 'Go to homepage &rarr;', 'woocommerce' ) . '</a>' ); wp_die( __( 'Invalid email address.', 'woocommerce' ) . ' <a href="' . home_url() . '">' . __( 'Go to homepage &rarr;', 'woocommerce' ) . '</a>' );
@ -985,27 +986,24 @@ function woocommerce_download_product() {
$file_path = $_product->get_file_download_path( $download_id ); $file_path = $_product->get_file_download_path( $download_id );
if ( ! $file_path ) if ( ! $file_path )
exit; wp_die( __( 'No file defined', 'woocommerce' ) . ' <a href="'.home_url().'">' . __( 'Go to homepage &rarr;', 'woocommerce' ) . '</a>' );
$file_download_method = apply_filters( 'woocommerce_file_download_method', get_option( 'woocommerce_file_download_method' ), $product_id ); // Redirect to the file...
if ( $file_download_method == "redirect" ) {
// Redirect to download location
if ( $file_download_method == 'redirect' ) {
header( 'Location: ' . $file_path ); header( 'Location: ' . $file_path );
exit; exit;
} }
// Get URLS with https // ...or serve it
$site_url = site_url();
$network_url = network_admin_url();
if ( is_ssl() ) {
$site_url = str_replace( 'https:', 'http:', $site_url );
$network_url = str_replace( 'https:', 'http:', $network_url );
}
if ( ! is_multisite() ) { if ( ! is_multisite() ) {
$site_url = is_ssl() ? str_replace( 'https:', 'http:', site_url() ) : site_url();
$file_path = str_replace( trailingslashit( $site_url ), ABSPATH, $file_path ); $file_path = str_replace( trailingslashit( $site_url ), ABSPATH, $file_path );
} else { } else {
$network_url = is_ssl() ? str_replace( 'https:', 'http:', network_admin_url() ) : network_admin_url();
$upload_dir = wp_upload_dir(); $upload_dir = wp_upload_dir();
// Try to replace network url // Try to replace network url
@ -1023,9 +1021,7 @@ function woocommerce_download_product() {
$file_path = realpath( $file_path ); $file_path = realpath( $file_path );
} }
// Download the file
$file_extension = strtolower( substr( strrchr( $file_path, "." ), 1 ) ); $file_extension = strtolower( substr( strrchr( $file_path, "." ), 1 ) );
$ctype = "application/force-download"; $ctype = "application/force-download";
foreach ( get_allowed_mime_types() as $mime => $type ) { foreach ( get_allowed_mime_types() as $mime => $type ) {
@ -1036,6 +1032,34 @@ function woocommerce_download_product() {
} }
} }
// Start setting headers
if ( ! ini_get('safe_mode') )
@set_time_limit(0);
if ( function_exists( 'get_magic_quotes_runtime' ) && get_magic_quotes_runtime() )
@set_magic_quotes_runtime(0);
if( function_exists( 'apache_setenv' ) )
@apache_setenv( 'no-gzip', 1 );
@session_write_close();
@ini_set( 'zlib.output_compression', 'Off' );
@ob_end_clean();
if ( ob_get_level() )
@ob_end_clean(); // Zip corruption fix
nocache_headers();
header( "Robots: none" );
header( "Content-Type: " . $ctype );
header( "Content-Description: File Transfer" );
header( "Content-Disposition: attachment; filename=\"" . basename( $file_path ) . "\";" );
header( "Content-Transfer-Encoding: binary" );
if ( $size = @filesize( $file_path ) )
header( "Content-Length: " . $size );
if ( $file_download_method == 'xsendfile' ) { if ( $file_download_method == 'xsendfile' ) {
// Path fix - kudos to Jason Judge // Path fix - kudos to Jason Judge
@ -1062,9 +1086,16 @@ function woocommerce_download_product() {
} }
} }
if ( ! function_exists('readfile_chunked')) { if ( $remote_file )
@woocommerce_readfile_chunked( $file_path ) or header( 'Location: ' . $file_path );
else
@woocommerce_readfile_chunked( $file_path ) or wp_die( __( 'File not found', 'woocommerce' ) . ' <a href="' . home_url() . '">' . __( 'Go to homepage &rarr;', 'woocommerce' ) . '</a>' );
/** exit;
}
}
/**
* readfile_chunked * readfile_chunked
* *
* Reads file in chunks so big downloads are possible without changing PHP.INI - http://codeigniter.com/wiki/Download_helper_for_large_files/ * Reads file in chunks so big downloads are possible without changing PHP.INI - http://codeigniter.com/wiki/Download_helper_for_large_files/
@ -1074,7 +1105,7 @@ function woocommerce_download_product() {
* @param boolean return bytes of file * @param boolean return bytes of file
* @return void * @return void
*/ */
function readfile_chunked( $file, $retbytes = true ) { function woocommerce_readfile_chunked( $file, $retbytes = true ) {
$chunksize = 1 * ( 1024 * 1024 ); $chunksize = 1 * ( 1024 * 1024 );
$buffer = ''; $buffer = '';
@ -1100,48 +1131,8 @@ function woocommerce_download_product() {
return $cnt; return $cnt;
return $status; return $status;
}
}
@session_write_close();
if ( function_exists( 'apache_setenv' ) )
@apache_setenv( 'no-gzip', 1 );
@ini_set( 'zlib.output_compression', 'Off' );
@set_time_limit(0);
@set_magic_quotes_runtime(0);
@ob_end_clean();
if ( ob_get_level() )
@ob_end_clean(); // Zip corruption fix
header( "Pragma: no-cache" );
header( "Expires: 0" );
header( "Cache-Control: must-revalidate, post-check=0, pre-check=0" );
header( "Robots: none" );
header( "Content-Type: " . $ctype );
header( "Content-Description: File Transfer" );
header( "Content-Disposition: attachment; filename=\"" . basename( $file_path ) . "\";" );
header( "Content-Transfer-Encoding: binary" );
if ( $size = @filesize( $file_path ) )
header( "Content-Length: " . $size );
// Serve it
if ( $remote_file ) {
@readfile_chunked( "$file_path" ) or header( 'Location: ' . $file_path );
} else {
@readfile_chunked( "$file_path" ) or wp_die( __( 'File not found', 'woocommerce' ) . ' <a href="' . home_url() . '">' . __( 'Go to homepage &rarr;', 'woocommerce' ) . '</a>' );
}
exit;
}
} }
/** /**
* ecommerce tracking with piwik. * ecommerce tracking with piwik.
* *