403 ) ); } $can = current_user_can( 'frkt_view_library' ) || current_user_can( 'frkt_view_library' ) || current_user_can( 'upload_files' ); if ( ! $can || ! FeatureRegistry::is_available( 'download_zip' ) ) { wp_die( esc_html__( 'You cannot download this zip.', 'foldrocket' ), '', array( 'response' => 403 ) ); } // The single-use, user-bound transient token is the request nonce for this streamed download. // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Token is consumed below and bound to the current user. $token = isset( $_GET['token'] ) ? sanitize_text_field( wp_unslash( $_GET['token'] ) ) : ''; $token = strtolower( preg_replace( '/[^a-f0-9]/', '', $token ) ?? '' ); if ( strlen( $token ) !== 32 ) { wp_die( esc_html__( 'This download link is not valid.', 'foldrocket' ), '', array( 'response' => 404 ) ); } $key = LibraryZipService::TRANSIENT_PREFIX . $token; $data = get_transient( $key ); delete_transient( $key ); if ( ! is_array( $data ) ) { wp_die( esc_html__( 'This download link has already been used or has expired.', 'foldrocket' ), '', array( 'response' => 404 ) ); } $file = isset( $data['file'] ) ? (string) $data['file'] : ''; $name = isset( $data['name'] ) ? (string) $data['name'] : 'media.zip'; $user = (int) ( $data['user'] ?? 0 ); $real = $file !== '' ? realpath( $file ) : false; $temp = realpath( get_temp_dir() ); $owns = $user > 0 && $user === get_current_user_id(); $safe = is_string( $real ) && is_string( $temp ) && is_file( $real ) && str_starts_with( $real, $temp . DIRECTORY_SEPARATOR ); if ( ! $owns || ! $safe || ! str_starts_with( basename( $real ), 'frkt-lib-' ) ) { if ( is_string( $real ) && is_file( $real ) ) { wp_delete_file( $real ); } wp_die( esc_html__( 'This download link is not valid.', 'foldrocket' ), '', array( 'response' => 404 ) ); } register_shutdown_function( static function () use ( $real ): void { if ( is_file( $real ) ) { wp_delete_file( $real ); } } ); $filename = sanitize_file_name( $name ); if ( $filename === '' ) { $filename = 'media.zip'; } nocache_headers(); header( 'Content-Type: application/zip' ); header( 'Content-Disposition: attachment; filename="' . $filename . '"' ); header( 'Content-Length: ' . (string) filesize( $real ) ); header( 'X-Robots-Tag: noindex' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_readfile -- Stream a temp zip once. readfile( $real ); exit; } }