class-wc-download-handler.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. <?php
  2. /**
  3. * Download handler
  4. *
  5. * Handle digital downloads.
  6. *
  7. * @package WooCommerce/Classes
  8. * @version 2.2.0
  9. */
  10. defined( 'ABSPATH' ) || exit;
  11. /**
  12. * Download handler class.
  13. */
  14. class WC_Download_Handler {
  15. /**
  16. * Hook in methods.
  17. */
  18. public static function init() {
  19. if ( isset( $_GET['download_file'], $_GET['order'] ) && ( isset( $_GET['email'] ) || isset( $_GET['uid'] ) ) ) { // WPCS: input var ok, CSRF ok.
  20. add_action( 'init', array( __CLASS__, 'download_product' ) );
  21. }
  22. add_action( 'woocommerce_download_file_redirect', array( __CLASS__, 'download_file_redirect' ), 10, 2 );
  23. add_action( 'woocommerce_download_file_xsendfile', array( __CLASS__, 'download_file_xsendfile' ), 10, 2 );
  24. add_action( 'woocommerce_download_file_force', array( __CLASS__, 'download_file_force' ), 10, 2 );
  25. }
  26. /**
  27. * Check if we need to download a file and check validity.
  28. */
  29. public static function download_product() {
  30. $product_id = absint( $_GET['download_file'] ); // phpcs:ignore WordPress.VIP.SuperGlobalInputUsage.AccessDetected, WordPress.VIP.ValidatedSanitizedInput.InputNotValidated
  31. $product = wc_get_product( $product_id );
  32. $data_store = WC_Data_Store::load( 'customer-download' );
  33. if ( ! $product || empty( $_GET['key'] ) || empty( $_GET['order'] ) ) { // WPCS: input var ok, CSRF ok.
  34. self::download_error( __( 'Invalid download link.', 'woocommerce' ) );
  35. }
  36. // Fallback, accept email address if it's passed.
  37. if ( empty( $_GET['email'] ) && empty( $_GET['uid'] ) ) { // WPCS: input var ok, CSRF ok.
  38. self::download_error( __( 'Invalid download link.', 'woocommerce' ) );
  39. }
  40. if ( isset( $_GET['email'] ) ) { // WPCS: input var ok, CSRF ok.
  41. $email_address = wp_unslash( $_GET['email'] ); // WPCS: input var ok, CSRF ok, sanitization ok.
  42. } else {
  43. // Get email address from order to verify hash.
  44. $order_id = wc_get_order_id_by_order_key( wc_clean( wp_unslash( $_GET['order'] ) ) ); // WPCS: input var ok, CSRF ok.
  45. $order = wc_get_order( $order_id );
  46. $email_address = is_a( $order, 'WC_Order' ) ? $order->get_billing_email() : null;
  47. // Prepare email address hash.
  48. $email_hash = function_exists( 'hash' ) ? hash( 'sha256', $email_address ) : sha1( $email_address );
  49. if ( is_null( $email_address ) || ! hash_equals( wp_unslash( $_GET['uid'] ), $email_hash ) ) { // WPCS: input var ok, CSRF ok, sanitization ok.
  50. self::download_error( __( 'Invalid download link.', 'woocommerce' ) );
  51. }
  52. }
  53. $download_ids = $data_store->get_downloads(
  54. array(
  55. 'user_email' => sanitize_email( str_replace( ' ', '+', $email_address ) ),
  56. 'order_key' => wc_clean( wp_unslash( $_GET['order'] ) ), // WPCS: input var ok, CSRF ok.
  57. 'product_id' => $product_id,
  58. 'download_id' => wc_clean( preg_replace( '/\s+/', ' ', wp_unslash( $_GET['key'] ) ) ), // WPCS: input var ok, CSRF ok, sanitization ok.
  59. 'orderby' => 'downloads_remaining',
  60. 'order' => 'DESC',
  61. 'limit' => 1,
  62. 'return' => 'ids',
  63. )
  64. );
  65. if ( empty( $download_ids ) ) {
  66. self::download_error( __( 'Invalid download link.', 'woocommerce' ) );
  67. }
  68. $download = new WC_Customer_Download( current( $download_ids ) );
  69. $file_path = $product->get_file_download_path( $download->get_download_id() );
  70. $parsed_file_path = self::parse_file_path( $file_path );
  71. $download_range = self::get_download_range( @filesize( $parsed_file_path['file_path'] ) ); // @codingStandardsIgnoreLine.
  72. self::check_order_is_valid( $download );
  73. if ( ! $download_range['is_range_request'] ) {
  74. // If the remaining download count goes to 0, allow range requests to be able to finish streaming from iOS devices.
  75. self::check_downloads_remaining( $download );
  76. }
  77. self::check_download_expiry( $download );
  78. self::check_download_login_required( $download );
  79. do_action(
  80. 'woocommerce_download_product',
  81. $download->get_user_email(),
  82. $download->get_order_key(),
  83. $download->get_product_id(),
  84. $download->get_user_id(),
  85. $download->get_download_id(),
  86. $download->get_order_id()
  87. );
  88. $download->save();
  89. // Track the download in logs and change remaining/counts.
  90. $current_user_id = get_current_user_id();
  91. $ip_address = WC_Geolocation::get_ip_address();
  92. if ( ! $download_range['is_range_request'] ) {
  93. $download->track_download( $current_user_id > 0 ? $current_user_id : null, ! empty( $ip_address ) ? $ip_address : null );
  94. }
  95. self::download( $file_path, $download->get_product_id() );
  96. }
  97. /**
  98. * Check if an order is valid for downloading from.
  99. *
  100. * @param WC_Customer_Download $download Download instance.
  101. */
  102. private static function check_order_is_valid( $download ) {
  103. if ( $download->get_order_id() ) {
  104. $order = wc_get_order( $download->get_order_id() );
  105. if ( $order && ! $order->is_download_permitted() ) {
  106. self::download_error( __( 'Invalid order.', 'woocommerce' ), '', 403 );
  107. }
  108. }
  109. }
  110. /**
  111. * Check if there are downloads remaining.
  112. *
  113. * @param WC_Customer_Download $download Download instance.
  114. */
  115. private static function check_downloads_remaining( $download ) {
  116. if ( '' !== $download->get_downloads_remaining() && 0 >= $download->get_downloads_remaining() ) {
  117. self::download_error( __( 'Sorry, you have reached your download limit for this file', 'woocommerce' ), '', 403 );
  118. }
  119. }
  120. /**
  121. * Check if the download has expired.
  122. *
  123. * @param WC_Customer_Download $download Download instance.
  124. */
  125. private static function check_download_expiry( $download ) {
  126. if ( ! is_null( $download->get_access_expires() ) && $download->get_access_expires()->getTimestamp() < strtotime( 'midnight', current_time( 'timestamp', true ) ) ) {
  127. self::download_error( __( 'Sorry, this download has expired', 'woocommerce' ), '', 403 );
  128. }
  129. }
  130. /**
  131. * Check if a download requires the user to login first.
  132. *
  133. * @param WC_Customer_Download $download Download instance.
  134. */
  135. private static function check_download_login_required( $download ) {
  136. if ( $download->get_user_id() && 'yes' === get_option( 'woocommerce_downloads_require_login' ) ) {
  137. if ( ! is_user_logged_in() ) {
  138. if ( wc_get_page_id( 'myaccount' ) ) {
  139. wp_safe_redirect( add_query_arg( 'wc_error', rawurlencode( __( 'You must be logged in to download files.', 'woocommerce' ) ), wc_get_page_permalink( 'myaccount' ) ) );
  140. exit;
  141. } else {
  142. self::download_error( __( 'You must be logged in to download files.', 'woocommerce' ) . ' <a href="' . esc_url( wp_login_url( wc_get_page_permalink( 'myaccount' ) ) ) . '" class="wc-forward">' . __( 'Login', 'woocommerce' ) . '</a>', __( 'Log in to Download Files', 'woocommerce' ), 403 );
  143. }
  144. } elseif ( ! current_user_can( 'download_file', $download ) ) {
  145. self::download_error( __( 'This is not your download link.', 'woocommerce' ), '', 403 );
  146. }
  147. }
  148. }
  149. /**
  150. * Count download.
  151. *
  152. * @deprecated unknown
  153. * @param array $download_data Download data.
  154. */
  155. public static function count_download( $download_data ) {}
  156. /**
  157. * Download a file - hook into init function.
  158. *
  159. * @param string $file_path URL to file.
  160. * @param integer $product_id Product ID of the product being downloaded.
  161. */
  162. public static function download( $file_path, $product_id ) {
  163. if ( ! $file_path ) {
  164. self::download_error( __( 'No file defined', 'woocommerce' ) );
  165. }
  166. $filename = basename( $file_path );
  167. if ( strstr( $filename, '?' ) ) {
  168. $filename = current( explode( '?', $filename ) );
  169. }
  170. $filename = apply_filters( 'woocommerce_file_download_filename', $filename, $product_id );
  171. $file_download_method = apply_filters( 'woocommerce_file_download_method', get_option( 'woocommerce_file_download_method', 'force' ), $product_id );
  172. // Add action to prevent issues in IE.
  173. add_action( 'nocache_headers', array( __CLASS__, 'ie_nocache_headers_fix' ) );
  174. // Trigger download via one of the methods.
  175. do_action( 'woocommerce_download_file_' . $file_download_method, $file_path, $filename );
  176. }
  177. /**
  178. * Redirect to a file to start the download.
  179. *
  180. * @param string $file_path File path.
  181. * @param string $filename File name.
  182. */
  183. public static function download_file_redirect( $file_path, $filename = '' ) {
  184. header( 'Location: ' . $file_path );
  185. exit;
  186. }
  187. /**
  188. * Parse file path and see if its remote or local.
  189. *
  190. * @param string $file_path File path.
  191. * @return array
  192. */
  193. public static function parse_file_path( $file_path ) {
  194. $wp_uploads = wp_upload_dir();
  195. $wp_uploads_dir = $wp_uploads['basedir'];
  196. $wp_uploads_url = $wp_uploads['baseurl'];
  197. /**
  198. * Replace uploads dir, site url etc with absolute counterparts if we can.
  199. * Note the str_replace on site_url is on purpose, so if https is forced
  200. * via filters we can still do the string replacement on a HTTP file.
  201. */
  202. $replacements = array(
  203. $wp_uploads_url => $wp_uploads_dir,
  204. network_site_url( '/', 'https' ) => ABSPATH,
  205. str_replace( 'https:', 'http:', network_site_url( '/', 'http' ) ) => ABSPATH,
  206. site_url( '/', 'https' ) => ABSPATH,
  207. str_replace( 'https:', 'http:', site_url( '/', 'http' ) ) => ABSPATH,
  208. );
  209. $file_path = str_replace( array_keys( $replacements ), array_values( $replacements ), $file_path );
  210. $parsed_file_path = wp_parse_url( $file_path );
  211. $remote_file = true;
  212. // See if path needs an abspath prepended to work.
  213. if ( file_exists( ABSPATH . $file_path ) ) {
  214. $remote_file = false;
  215. $file_path = ABSPATH . $file_path;
  216. } elseif ( '/wp-content' === substr( $file_path, 0, 11 ) ) {
  217. $remote_file = false;
  218. $file_path = realpath( WP_CONTENT_DIR . substr( $file_path, 11 ) );
  219. // Check if we have an absolute path.
  220. } elseif ( ( ! isset( $parsed_file_path['scheme'] ) || ! in_array( $parsed_file_path['scheme'], array( 'http', 'https', 'ftp' ), true ) ) && isset( $parsed_file_path['path'] ) && file_exists( $parsed_file_path['path'] ) ) {
  221. $remote_file = false;
  222. $file_path = $parsed_file_path['path'];
  223. }
  224. return array(
  225. 'remote_file' => $remote_file,
  226. 'file_path' => $file_path,
  227. );
  228. }
  229. /**
  230. * Download a file using X-Sendfile, X-Lighttpd-Sendfile, or X-Accel-Redirect if available.
  231. *
  232. * @param string $file_path File path.
  233. * @param string $filename File name.
  234. */
  235. public static function download_file_xsendfile( $file_path, $filename ) {
  236. $parsed_file_path = self::parse_file_path( $file_path );
  237. if ( function_exists( 'apache_get_modules' ) && in_array( 'mod_xsendfile', apache_get_modules(), true ) ) {
  238. self::download_headers( $parsed_file_path['file_path'], $filename );
  239. header( 'X-Sendfile: ' . $parsed_file_path['file_path'] );
  240. exit;
  241. } elseif ( stristr( getenv( 'SERVER_SOFTWARE' ), 'lighttpd' ) ) {
  242. self::download_headers( $parsed_file_path['file_path'], $filename );
  243. header( 'X-Lighttpd-Sendfile: ' . $parsed_file_path['file_path'] );
  244. exit;
  245. } elseif ( stristr( getenv( 'SERVER_SOFTWARE' ), 'nginx' ) || stristr( getenv( 'SERVER_SOFTWARE' ), 'cherokee' ) ) {
  246. self::download_headers( $parsed_file_path['file_path'], $filename );
  247. $xsendfile_path = trim( preg_replace( '`^' . str_replace( '\\', '/', getcwd() ) . '`', '', $parsed_file_path['file_path'] ), '/' );
  248. header( "X-Accel-Redirect: /$xsendfile_path" );
  249. exit;
  250. }
  251. // Fallback.
  252. self::download_file_force( $file_path, $filename );
  253. }
  254. /**
  255. * Parse the HTTP_RANGE request from iOS devices.
  256. * Does not support multi-range requests.
  257. *
  258. * @param int $file_size Size of file in bytes.
  259. * @return array {
  260. * Information about range download request: beginning and length of
  261. * file chunk, whether the range is valid/supported and whether the request is a range request.
  262. *
  263. * @type int $start Byte offset of the beginning of the range. Default 0.
  264. * @type int $length Length of the requested file chunk in bytes. Optional.
  265. * @type bool $is_range_valid Whether the requested range is a valid and supported range.
  266. * @type bool $is_range_request Whether the request is a range request.
  267. * }
  268. */
  269. protected static function get_download_range( $file_size ) {
  270. $start = 0;
  271. $download_range = array(
  272. 'start' => $start,
  273. 'is_range_valid' => false,
  274. 'is_range_request' => false,
  275. );
  276. if ( ! $file_size ) {
  277. return $download_range;
  278. }
  279. $end = $file_size - 1;
  280. $download_range['length'] = $file_size;
  281. if ( isset( $_SERVER['HTTP_RANGE'] ) ) { // @codingStandardsIgnoreLine.
  282. $http_range = sanitize_text_field( wp_unslash( $_SERVER['HTTP_RANGE'] ) ); // WPCS: input var ok.
  283. $download_range['is_range_request'] = true;
  284. $c_start = $start;
  285. $c_end = $end;
  286. // Extract the range string.
  287. list( , $range ) = explode( '=', $http_range, 2 );
  288. // Make sure the client hasn't sent us a multibyte range.
  289. if ( strpos( $range, ',' ) !== false ) {
  290. return $download_range;
  291. }
  292. /*
  293. * If the range starts with an '-' we start from the beginning.
  294. * If not, we forward the file pointer
  295. * and make sure to get the end byte if specified.
  296. */
  297. if ( '-' === $range[0] ) {
  298. // The n-number of the last bytes is requested.
  299. $c_start = $file_size - substr( $range, 1 );
  300. } else {
  301. $range = explode( '-', $range );
  302. $c_start = ( isset( $range[0] ) && is_numeric( $range[0] ) ) ? (int) $range[0] : 0;
  303. $c_end = ( isset( $range[1] ) && is_numeric( $range[1] ) ) ? (int) $range[1] : $file_size;
  304. }
  305. /*
  306. * Check the range and make sure it's treated according to the specs: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html.
  307. * End bytes can not be larger than $end.
  308. */
  309. $c_end = ( $c_end > $end ) ? $end : $c_end;
  310. // Validate the requested range and return an error if it's not correct.
  311. if ( $c_start > $c_end || $c_start > $file_size - 1 || $c_end >= $file_size ) {
  312. return $download_range;
  313. }
  314. $start = $c_start;
  315. $end = $c_end;
  316. $length = $end - $start + 1;
  317. $download_range['start'] = $start;
  318. $download_range['length'] = $length;
  319. $download_range['is_range_valid'] = true;
  320. }
  321. return $download_range;
  322. }
  323. /**
  324. * Force download - this is the default method.
  325. *
  326. * @param string $file_path File path.
  327. * @param string $filename File name.
  328. */
  329. public static function download_file_force( $file_path, $filename ) {
  330. $parsed_file_path = self::parse_file_path( $file_path );
  331. $download_range = self::get_download_range( @filesize( $parsed_file_path['file_path'] ) ); // @codingStandardsIgnoreLine.
  332. self::download_headers( $parsed_file_path['file_path'], $filename, $download_range );
  333. $start = isset( $download_range['start'] ) ? $download_range['start'] : 0;
  334. $length = isset( $download_range['length'] ) ? $download_range['length'] : 0;
  335. if ( ! self::readfile_chunked( $parsed_file_path['file_path'], $start, $length ) ) {
  336. if ( $parsed_file_path['remote_file'] ) {
  337. self::download_file_redirect( $file_path );
  338. } else {
  339. self::download_error( __( 'File not found', 'woocommerce' ) );
  340. }
  341. }
  342. exit;
  343. }
  344. /**
  345. * Get content type of a download.
  346. *
  347. * @param string $file_path File path.
  348. * @return string
  349. */
  350. private static function get_download_content_type( $file_path ) {
  351. $file_extension = strtolower( substr( strrchr( $file_path, '.' ), 1 ) );
  352. $ctype = 'application/force-download';
  353. foreach ( get_allowed_mime_types() as $mime => $type ) {
  354. $mimes = explode( '|', $mime );
  355. if ( in_array( $file_extension, $mimes, true ) ) {
  356. $ctype = $type;
  357. break;
  358. }
  359. }
  360. return $ctype;
  361. }
  362. /**
  363. * Set headers for the download.
  364. *
  365. * @param string $file_path File path.
  366. * @param string $filename File name.
  367. * @param array $download_range Array containing info about range download request (see {@see get_download_range} for structure).
  368. */
  369. private static function download_headers( $file_path, $filename, $download_range = array() ) {
  370. self::check_server_config();
  371. self::clean_buffers();
  372. wc_nocache_headers();
  373. header( 'X-Robots-Tag: noindex, nofollow', true );
  374. header( 'Content-Type: ' . self::get_download_content_type( $file_path ) );
  375. header( 'Content-Description: File Transfer' );
  376. header( 'Content-Disposition: attachment; filename="' . $filename . '";' );
  377. header( 'Content-Transfer-Encoding: binary' );
  378. $file_size = @filesize( $file_path ); // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
  379. if ( ! $file_size ) {
  380. return;
  381. }
  382. if ( isset( $download_range['is_range_request'] ) && true === $download_range['is_range_request'] ) {
  383. if ( false === $download_range['is_range_valid'] ) {
  384. header( 'HTTP/1.1 416 Requested Range Not Satisfiable' );
  385. header( 'Content-Range: bytes 0-' . ( $file_size - 1 ) . '/' . $file_size );
  386. exit;
  387. }
  388. $start = $download_range['start'];
  389. $end = $download_range['start'] + $download_range['length'] - 1;
  390. $length = $download_range['length'];
  391. header( 'HTTP/1.1 206 Partial Content' );
  392. header( "Accept-Ranges: 0-$file_size" );
  393. header( "Content-Range: bytes $start-$end/$file_size" );
  394. header( "Content-Length: $length" );
  395. } else {
  396. header( 'Content-Length: ' . $file_size );
  397. }
  398. }
  399. /**
  400. * Check and set certain server config variables to ensure downloads work as intended.
  401. */
  402. private static function check_server_config() {
  403. wc_set_time_limit( 0 );
  404. if ( function_exists( 'get_magic_quotes_runtime' ) && get_magic_quotes_runtime() && version_compare( phpversion(), '5.4', '<' ) ) {
  405. set_magic_quotes_runtime( 0 ); // @codingStandardsIgnoreLine
  406. }
  407. if ( function_exists( 'apache_setenv' ) ) {
  408. @apache_setenv( 'no-gzip', 1 ); // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged, WordPress.PHP.DiscouragedPHPFunctions.runtime_configuration_apache_setenv
  409. }
  410. @ini_set( 'zlib.output_compression', 'Off' ); // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged, WordPress.PHP.DiscouragedPHPFunctions.runtime_configuration_ini_set
  411. @session_write_close(); // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged, WordPress.VIP.SessionFunctionsUsage.session_session_write_close
  412. }
  413. /**
  414. * Clean all output buffers.
  415. *
  416. * Can prevent errors, for example: transfer closed with 3 bytes remaining to read.
  417. */
  418. private static function clean_buffers() {
  419. if ( ob_get_level() ) {
  420. $levels = ob_get_level();
  421. for ( $i = 0; $i < $levels; $i++ ) {
  422. @ob_end_clean(); // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
  423. }
  424. } else {
  425. @ob_end_clean(); // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
  426. }
  427. }
  428. /**
  429. * Read file chunked.
  430. *
  431. * Reads file in chunks so big downloads are possible without changing PHP.INI - http://codeigniter.com/wiki/Download_helper_for_large_files/.
  432. *
  433. * @param string $file File.
  434. * @param int $start Byte offset/position of the beginning from which to read from the file.
  435. * @param int $length Length of the chunk to be read from the file in bytes, 0 means full file.
  436. * @return bool Success or fail
  437. */
  438. public static function readfile_chunked( $file, $start = 0, $length = 0 ) {
  439. if ( ! defined( 'WC_CHUNK_SIZE' ) ) {
  440. define( 'WC_CHUNK_SIZE', 1024 * 1024 );
  441. }
  442. $handle = @fopen( $file, 'r' ); // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged, WordPress.WP.AlternativeFunctions.file_system_read_fopen
  443. if ( false === $handle ) {
  444. return false;
  445. }
  446. if ( ! $length ) {
  447. $length = @filesize( $file ); // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
  448. }
  449. $read_length = (int) WC_CHUNK_SIZE;
  450. if ( $length ) {
  451. $end = $start + $length - 1;
  452. @fseek( $handle, $start ); // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
  453. $p = @ftell( $handle ); // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
  454. while ( ! @feof( $handle ) && $p <= $end ) { // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
  455. // Don't run past the end of file.
  456. if ( $p + $read_length > $end ) {
  457. $read_length = $end - $p + 1;
  458. }
  459. echo @fread( $handle, $read_length ); // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged, WordPress.XSS.EscapeOutput.OutputNotEscaped, WordPress.WP.AlternativeFunctions.file_system_read_fread
  460. $p = @ftell( $handle ); // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
  461. if ( ob_get_length() ) {
  462. ob_flush();
  463. flush();
  464. }
  465. }
  466. } else {
  467. while ( ! @feof( $handle ) ) { // @codingStandardsIgnoreLine.
  468. echo @fread( $handle, $read_length ); // @codingStandardsIgnoreLine.
  469. if ( ob_get_length() ) {
  470. ob_flush();
  471. flush();
  472. }
  473. }
  474. }
  475. return @fclose( $handle ); // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged, WordPress.WP.AlternativeFunctions.file_system_read_fclose
  476. }
  477. /**
  478. * Filter headers for IE to fix issues over SSL.
  479. *
  480. * IE bug prevents download via SSL when Cache Control and Pragma no-cache headers set.
  481. *
  482. * @param array $headers HTTP headers.
  483. * @return array
  484. */
  485. public static function ie_nocache_headers_fix( $headers ) {
  486. if ( is_ssl() && ! empty( $GLOBALS['is_IE'] ) ) {
  487. $headers['Cache-Control'] = 'private';
  488. unset( $headers['Pragma'] );
  489. }
  490. return $headers;
  491. }
  492. /**
  493. * Die with an error message if the download fails.
  494. *
  495. * @param string $message Error message.
  496. * @param string $title Error title.
  497. * @param integer $status Error status.
  498. */
  499. private static function download_error( $message, $title = '', $status = 404 ) {
  500. if ( ! strstr( $message, '<a ' ) ) {
  501. $message .= ' <a href="' . esc_url( wc_get_page_permalink( 'shop' ) ) . '" class="wc-forward">' . esc_html__( 'Go to shop', 'woocommerce' ) . '</a>';
  502. }
  503. wp_die( $message, $title, array( 'response' => $status ) ); // WPCS: XSS ok.
  504. }
  505. }
  506. WC_Download_Handler::init();