Skip to content

Commit 2d41f0f

Browse files
committed
Adding logging to backup downloads
1 parent 4127a63 commit 2d41f0f

File tree

2 files changed

+57
-13
lines changed

2 files changed

+57
-13
lines changed

admin/class-boldgrid-backup-admin-core.php

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2670,28 +2670,39 @@ public function boldgrid_backup_now_callback() {
26702670
* @see Boldgrid_Backup_File::send_file()
26712671
*/
26722672
public function download_archive_file_callback() {
2673+
$log = new Boldgrid_Backup_Admin_Log( $this );
2674+
$log->init( 'backup-download.log' );
2675+
$log->add_separator();
2676+
$log->add( 'Attempting ajax download...' );
2677+
26732678
// Verify nonce, or die.
26742679
check_ajax_referer( 'archive_auth', 'wpnonce' );
26752680

26762681
// Check user capabilities.
26772682
if ( ! current_user_can( 'update_plugins' ) ) {
2678-
esc_html_e( 'Security violation (not authorized).', 'boldgrid-backup' );
2683+
$error = __( 'Security violation (not authorized).', 'boldgrid-backup' );
2684+
echo esc_html( $error );
2685+
$log->add( $error );
26792686
wp_die();
26802687
}
26812688

26822689
// Validate download_key.
26832690
if ( isset( $_POST['download_key'] ) && is_numeric( $_POST['download_key'] ) ) {
26842691
$download_key = (int) $_POST['download_key'];
26852692
} else {
2686-
esc_html_e( 'INVALID DOWNLOAD KEY', 'boldgrid-backup' );
2693+
$error = __( 'INVALID DOWNLOAD KEY', 'boldgrid-backup' );
2694+
echo esc_html( $error );
2695+
$log->add( $error );
26872696
wp_die();
26882697
}
26892698

26902699
// Validate download_filename.
26912700
if ( ! empty( $_POST['download_filename'] ) ) {
26922701
$download_filename = sanitize_file_name( $_POST['download_filename'] );
26932702
} else {
2694-
esc_html_e( 'INVALID DOWNLOAD FILENAME', 'boldgrid-backup' );
2703+
$error = __( 'INVALID DOWNLOAD FILENAME', 'boldgrid-backup' );
2704+
echo esc_html( $error );
2705+
$log->add( $error );
26952706
wp_die();
26962707
}
26972708

@@ -2700,7 +2711,9 @@ public function download_archive_file_callback() {
27002711

27012712
// Check WP_Filesystem method; ensure it is "direct".
27022713
if ( 'direct' !== $access_type ) {
2703-
esc_html_e( 'WP_Filesystem method is not "direct"', 'boldgrid-backup' );
2714+
$error = __( 'WP_Filesystem method is not "direct"', 'boldgrid-backup' );
2715+
echo esc_html( $error );
2716+
$log->add( $error );
27042717
wp_die();
27052718
}
27062719

@@ -2709,7 +2722,9 @@ public function download_archive_file_callback() {
27092722

27102723
// If no files were found, then abort.
27112724
if ( empty( $archives ) ) {
2712-
esc_html_e( 'NO BACKUP ARCHIVES FOUND', 'boldgrid-backup' );
2725+
$error = __( 'NO BACKUP ARCHIVES FOUND', 'boldgrid-backup' );
2726+
echo esc_html( $error );
2727+
$log->add( $error );
27132728
wp_die();
27142729
}
27152730

@@ -2721,7 +2736,9 @@ public function download_archive_file_callback() {
27212736

27222737
// Verify filename.
27232738
if ( $download_filename !== $filename ) {
2724-
esc_html_e( 'FILE NOT FOUND', 'boldgrid-backup' );
2739+
$error = __( 'FILE NOT FOUND', 'boldgrid-backup' );
2740+
echo esc_html( $error );
2741+
$log->add( $error );
27252742
wp_die();
27262743
}
27272744

@@ -2734,6 +2751,7 @@ public function download_archive_file_callback() {
27342751
}
27352752

27362753
// Send the file and die nicely.
2754+
$log->add( 'Request validated successfully. Now on to sending the file...' );
27372755
Boldgrid_Backup_File::send_file( $filepath, $filesize );
27382756
}
27392757

includes/class-boldgrid-backup-file.php

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,34 @@ class Boldgrid_Backup_File {
3030
* @param int $filesize File size (optional).
3131
*/
3232
public static function send_file( $filepath, $filesize = null ) {
33-
WP_Filesystem();
34-
global $wp_filesystem;
33+
$core = apply_filters( 'boldgrid_backup_get_core', null );
34+
$log = new Boldgrid_Backup_Admin_Log( $core );
35+
$log->init( 'backup-download.log' );
36+
$log->add( 'Initializing send_file() method...' );
3537

36-
if ( empty( $filepath ) || ! $wp_filesystem->exists( $filepath ) ) {
38+
if ( empty( $filepath ) || ! $core->wp_filesystem->exists( $filepath ) ) {
39+
$log->add( 'Invalid filepath.' );
3740
wp_redirect( get_site_url(), 404 );
3841
}
3942

4043
$filename = basename( $filepath );
4144

4245
if ( empty( $filesize ) ) {
43-
$filesize = $wp_filesystem->size( $filepath );
46+
$filesize = $core->wp_filesystem->size( $filepath );
4447
}
4548

4649
// Send header.
50+
$log->add( 'Sending headers...' );
4751
header( 'Content-Disposition: attachment; filename="' . $filename . '"' );
4852
header( 'Content-Transfer-Encoding: binary' );
4953
header( 'Content-Type: binary/octet-stream' );
5054
header( 'Content-Length: ' . $filesize );
5155

5256
// Check and flush output buffer if needed.
53-
if ( 0 !== ob_get_level() ) {
57+
$ob_level = ob_get_level();
58+
$log->add( 'Output buffering level: ' . $ob_level );
59+
if ( 0 !== $ob_level ) {
60+
$log->add( 'Calling ob_end_flush().' );
5461
ob_end_flush();
5562
}
5663

@@ -78,17 +85,36 @@ public static function send_file( $filepath, $filesize = null ) {
7885
// If we can't open the file, abort.
7986
$handle = fopen( $filepath, 'rb' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_fopen
8087
if ( false === $handle ) {
88+
$log->add( 'Invalid handle. fopen failed.' );
8189
wp_die();
8290
}
8391

8492
// Loop through the file and send it 1MB at a time.
93+
$buffer_size = 1024 * 1024;
94+
$log->add( 'Beginnig to send file... Buffer size: ' . size_format( $buffer_size, 2 ) );
8595
while ( ! feof( $handle ) ) {
86-
$buffer = fread( $handle, 1024 * 1024 ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_fread
96+
$log->add( 'Reading buffer...' );
97+
$time_start = microtime( true );
98+
$buffer = fread( $handle, $buffer_size ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_fread
99+
$duration = microtime( true ) - $time_start;
100+
$log->add( 'Buffer read in ' . round( $duration, 4 ) . ' seconds.' );
101+
102+
$log->add( 'Sending buffer...' );
103+
$time_start = microtime( true );
87104
echo $buffer; // phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped
105+
$duration = microtime( true ) - $time_start;
106+
$log->add( 'Buffer sent in ' . round( $duration, 4 ) . ' seconds.' );
88107
}
108+
$log->add( 'Finished sending file.' );
89109

90-
fclose( $handle ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_fclose
110+
$log->add( 'Closing file...' );
111+
if ( fclose( $handle ) ) { // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_fclose
112+
$log->add( 'File closed successfully.' );
113+
} else {
114+
$log->add( 'Error closing file.' );
115+
}
91116

117+
$log->add( 'send_file() method complete. Ending with wp_die().' );
92118
wp_die();
93119
}
94120
}

0 commit comments

Comments
 (0)