/var/www/(Del)hsihk.com/wp-content/plugins/backupbuddy/classes/_periodicCleanup.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
<?php
// LOADED FROM: core.php, periodic_cleanup().
// INCOMING VARIABLES: $backup_age_limit = 172800, $die_on_fail = true


$max_importbuddy_age 60*60*1// 1hr - Max age, in seconds, importbuddy files can be there before cleaning up (delay useful if just imported and testing out site).
$max_status_log_age 60*60*48// Max age in seconds of old logs. -- 48 hours
$max_site_log_size pb_backupbuddy::$options['max_site_log_size'] * 1024 1024// in bytes.

pb_backupbuddy::status'message''Starting cleanup procedure for BackupBuddy v' pb_backupbuddy::settings'version' ) . '.' );

if ( !isset( 
pb_backupbuddy::$options ) ) {
    
pb_backupbuddy::load();
}


// Clean up any old rollback undo files hanging around.
$files = (array)globABSPATH 'backupbuddy_rollback*' );
foreach( 
$files as $file ) {
    
$file_stats stat$file );
    if ( ( 
time() - $file_stats['mtime'] ) > $max_status_log_age ) {
        @
unlink$file );
    }
}


// Clean up any old cron file transfer locks.
$files = (array)globbackupbuddy_core::getLogDirectory() . 'cronSend-*' );
foreach( 
$files as $file ) {
    
$file_stats stat$file );
    if ( ( 
time() - $file_stats['mtime'] ) > $max_status_log_age ) {
        @
unlink$file );
    }
}


// Alert user if no new backups FINISHED within X number of days if enabled. Max 1 email notification per 24 hours period.
if ( pb_backupbuddy::$options['no_new_backups_error_days'] > ) {
    if ( 
pb_backupbuddy::$options['last_backup_finish'] > ) {
        
$time_since_last time() - pb_backupbuddy::$options['last_backup_finish'];
        
$days_since_last $time_since_last 60 60 24;
        if ( 
$days_since_last pb_backupbuddy::$options['no_new_backups_error_days'] ) {
            
            
$last_sent get_transient'pb_backupbuddy_no_new_backup_error' );
            if ( 
false === $last_sent ) {
                
$last_sent time();
                
set_transient'pb_backupbuddy_no_new_backup_error'$last_sent, (60*60*24) );
            }
            if ( ( 
time() - $last_sent ) > (60*60*24) ) { // 24hrs+ elapsed since last email sent.
                
$message 'Alert! BackupBuddy is configured to notify you if no new backups have completed in `' pb_backupbuddy::$options['no_new_backups_error_days'] . '` days. It has been `' $days_since_last '` days since your last completed backups.';
                
pb_backupbuddy::status'warning'$message );
                
backupbuddy_core::mail_error$message );
            }
            
        }
    }
}


// TODO: Check for orphaned .gz files in root from PCLZip.
// Cleanup backup itegrity portion of array (status logging info inside function).


/***** BEGIN CLEANUP LOGS *****/

// Purge old logs.
pb_backupbuddy::status'details''Cleaning up old logs.' );
$log_directory backupbuddy_core::getLogDirectory();
// Purge individual backup status logs unmodified in certain number of hours.
$files glob$log_directory 'status-*.txt' );
if ( 
is_array$files ) && !empty( $files ) ) { // For robustness. Without open_basedir the glob() function returns an empty array for no match. With open_basedir in effect the glob() function returns a boolean false for no match.
    
foreach( $files as $file ) {
        
$file_stats stat$file );
        if ( ( 
time() - $file_stats['mtime'] ) > $max_status_log_age ) {
            @
unlink$file );
        }
    }
}

// Purge site-wide log if over certain size.
$files glob$log_directory 'log-*.txt' );
if ( 
is_array$files ) && !empty( $files ) ) { // For robustness. Without open_basedir the glob() function returns an empty array for no match. With open_basedir in effect the glob() function returns a boolean false for no match.
    
foreach( $files as $file ) {
        
$file_stats stat$file );
        if ( 
$file_stats['size'] > ( $max_site_log_size ) ) {
            
backupbuddy_core::mail_error'NOTICE ONLY (not an error): A BackupBuddy log file has exceeded the size threshold of ' $max_site_log_size ' KB and has been deleted to maintain performance. This is only a notice. Deleted log file: ' $file '.' );
            @
unlink$file );
        }
    }
}

/***** END CLEANUP LOGS *****/



// Cleanup any temporary local destinations.
pb_backupbuddy::status'details''Cleaning up any temporary local destinations.' );
foreach( 
pb_backupbuddy::$options['remote_destinations'] as $destination_id => $destination ) {
    if ( ( 
$destination['type'] == 'local' ) && ( isset( $destination['temporary'] ) && ( $destination['temporary'] === true ) ) ) { // If local and temporary.
        
if ( ( time() - $destination['created'] ) > $backup_age_limit ) { // Older than 12 hours; clear out!
            
pb_backupbuddy::status'details''Cleaned up stale local destination `' $destination_id '`.' );
            unset( 
pb_backupbuddy::$options['remote_destinations'][$destination_id] );
            
pb_backupbuddy::save();
        }
    }
}


// Cleanup excess remote sending stats.
pb_backupbuddy::status'details''Cleaning up remote send stats.' );
backupbuddy_core::trim_remote_send_stats();


// Verify directory existance and anti-directory browsing is in place everywhere.
backupbuddy_core::verify_directories$skipTempGeneration true );


require_once( 
pb_backupbuddy::plugin_path() . '/classes/fileoptions.php' );

// Mark any backups noted as in progress to timed out if taking too long. Send error email is scheduled and failed or timed out.
// Also, Purge fileoptions files without matching backup file in existance that are older than 30 days.
pb_backupbuddy::status'details''Cleaning up old backup fileoptions option files.' );
$fileoptions_directory backupbuddy_core::getLogDirectory() . 'fileoptions/';
$files glob$fileoptions_directory '*.txt' );
if ( ! 
is_array$files ) ) {
    
$files = array();
}
foreach( 
$files as $file ) {
    
pb_backupbuddy::status'details''Fileoptions instance #43.' );
    
$backup_options = new pb_backupbuddy_fileoptions$file$read_only true );
    if ( 
true !== ( $result $backup_options->is_ok() ) ) {
        
pb_backupbuddy::status'error''Error retrieving fileoptions file `' $file '`. Err 335353266.' );
    } else {
        if ( isset( 
$backup_options->options['archive_file'] ) ) {
            
//error_log( print_r( $backup_options->options, true ) );
            
            
            
if ( ( $backup_options->options['finish_time'] >= $backup_options->options['start_time'] ) && ( != $backup_options->options['start_time'] ) ) {
                
// Completed
            
} elseif ( $backup_options->options['finish_time'] == -) {
                
// Cancelled manually
            
} elseif ( FALSE === $backup_options->options['finish_time']  ) {
                
// Failed.
            
} else {
                
// Timed out or in progress.
                
$status '<span class="pb_label pb_label-warning">In progress or timed out</span>';
                if ( ( 
$backup_options->options['updated_time'] - time() ) > 60*60*24 ) { // If 24hrs passed since last update to backup then mark this timeout as failed.
                    
$backup_options->options['finish_time'] = FALSE;
                    
                    if ( 
'scheduled' == $backup_options->options['trigger'] ) {
                        
                        
// Determine the first step to not finish.
                        
$timeoutStep '';
                        foreach( 
$backup_options->options['steps'] as $step ) {
                            if ( 
== $step['finish_time'] ) {
                                
$timeoutStep $step['function'];
                                break;
                            }
                        }
                        
                        
$timeoutMessage '';
                        if ( 
'' != $timeoutStep ) {
                            if ( 
'backup_create_database_dump' == $timeoutStep ) {
                                
$timeoutMessage 'The database dump step appears to have timed out. Make sure your database is not full of unwanted logs or clutter.';
                            } elseif ( 
'backup_zip_files' == $timeoutStep ) {
                                
$timeoutMessage 'The zip archive creation step appears to have timed out. Try turning off zip compression to significantly speed up the process or exclude large files.';
                            } elseif ( 
'send_remote_destination' == $timeoutStep ) {
                                
$timeoutMessage 'The remote transfer step appears to have timed out. Try turning on chunking in the destination settings to break up the file transfer into multiple steps.';
                            } else {
                                
$timeoutMessage 'The step function `' $timeoutStep '` appears to have timed out.';
                            }
                        }
                        
                        
$error_message 'BackupBuddy backup failed. ' $timeoutMessage ' Check the error log for further details and/or manually create a backup to test for problems.';
                        
pb_backupbuddy::status'error''Scheduled backup failed, possible due to timeout.' );
                        
backupbuddy_core::mail_error$error_message );
                    }
                    
                    
$backup_options->save();
                }
            }
            
            
            if ( ! 
file_exists$backup_options->options['archive_file'] ) ) { // No corresponding backup ZIP file.
                
$modified filemtime$file );
                if ( ( 
time() - $modified ) > backupbuddy_core::MAX_SECONDS_TO_KEEP_ORPHANED_FILEOPTIONS_FILES ) { // Too many days old so delete.
                    
if ( false === unlink$file ) ) {
                        
pb_backupbuddy::status'error''Unable to delete orphaned fileoptions file `' $file '`.' );
                    }
                    if ( 
file_exists$file '.lock' ) ) {
                        @
unlink$file '.lock' );
                    }
                } else {
                    
// Do not delete orphaned fileoptions because it is not old enough. Recent backups page needs these to list the backup.
                
}
            } else { 
// Backup ZIP file exists.
                
            
}
        }
    }
// end foreach.


// Handle high security mode archives directory .htaccess system. If high security backup directory mode: Make sure backup archives are NOT downloadable by default publicly. This is only lifted for ~8 seconds during a backup download for security. Overwrites any existing .htaccess in this location.
if ( pb_backupbuddy::$options['lock_archives_directory'] == '0' ) { // Normal security mode. Put normal .htaccess.
    
pb_backupbuddy::status'details''Removing .htaccess high security mode for backups directory. Normal mode .htaccess to be added next.' );
    
// Remove high security .htaccess.
    
if ( file_existsbackupbuddy_core::getBackupDirectory() . '.htaccess' ) ) {
        
$unlink_status = @unlinkbackupbuddy_core::getBackupDirectory() . '.htaccess' );
        if ( 
$unlink_status === false ) {
            
pb_backupbuddy::alert'Error #844594. Unable to temporarily remove .htaccess security protection on archives directory to allow downloading. Please verify permissions of the BackupBuddy archives directory or manually download via FTP.' );
        }
    }
    
    
// Place normal .htaccess.
    
pb_backupbuddy::anti_directory_browsingbackupbuddy_core::getBackupDirectory(), $die_on_fail );

} else { 
// High security mode. Make sure high security .htaccess in place.
    
pb_backupbuddy::status'details''Adding .htaccess high security mode for backups directory.' );
    
$htaccess_creation_status = @file_put_contentsbackupbuddy_core::getBackupDirectory() . '.htaccess''deny from all' );
    if ( 
$htaccess_creation_status === false ) {
        
pb_backupbuddy::alert'Error #344894545. Security Warning! Unable to create security file (.htaccess) in backups archive directory. This file prevents unauthorized downloading of backups should someone be able to guess the backup location and filenames. This is unlikely but for best security should be in place. Please verify permissions on the backups directory.' );
    }
    
}

$temp_dir get_temp_dir();


// Remove any copy of importbuddy.php in root.
pb_backupbuddy::status'details''Cleaning up any importbuddy scripts in site root if it exists & is not very recent.' );
$importbuddyFiles glob$temp_dir 'importbuddy*.php' );
if ( ! 
is_array$importbuddyFiles ) ) {
    
$importbuddyFiles = array();
}
foreach( 
$importbuddyFiles as $importbuddyFile ) {
    
$modified filemtime$importbuddyFile );
    if ( ( 
FALSE === $modified ) || ( time() > ( $modified $max_importbuddy_age ) ) ) { // If time modified unknown OR was modified long enough ago.
        
pb_backupbuddy::status'details''Unlinked `' basename$importbuddyFile ) . '` in root of site.' );
        
unlink$importbuddyFile );
    } else {
        
pb_backupbuddy::status'details''SKIPPED unlinking `' basename$importbuddyFile ) . '` in root of site as it is fresh and may still be in use.' );
    }
}


// Remove any copy of importbuddy directory in root.
pb_backupbuddy::status'details''Cleaning up importbuddy directory in site root if it exists & is not very recent.' );
if ( 
file_existsABSPATH 'importbuddy/' ) ) {
    
$modified filemtimeABSPATH 'importbuddy/' );
    if ( ( 
FALSE === $modified ) || ( time() > ( $modified $max_importbuddy_age ) ) ) { // If time modified unknown OR was modified long enough ago.
        
pb_backupbuddy::status'details''Unlinked importbuddy directory recursively in root of site.' );
        
pb_backupbuddy::$filesystem->unlink_recursiveABSPATH 'importbuddy/' );
    } else {
        
pb_backupbuddy::status'details''SKIPPED unlinked importbuddy directory recursively in root of site as it is fresh and may still be in use.' );
    }
}



backupbuddy_core::cleanTempDir$backup_age_limit );



// Cleanup any temp files from a failed restore within WordPress. (extract file feature).
pb_backupbuddy::status'details''Cleaning up temporary files from individual file / directory restores in directory `' $temp_dir '`...' );
$possibly_temp_restore_dirs glob$temp_dir 'backupbuddy-*' );
if ( ! 
is_array$possibly_temp_restore_dirs ) ) { $possibly_temp_restore_dirs = array(); }
foreach( 
$possibly_temp_restore_dirs as $possibly_temp_restore_dir ) {
    if ( 
false === pb_backupbuddy::$filesystem->unlink_recursive$possibly_temp_restore_dir ) ) { // Delete.
        
pb_backupbuddy::status'details''Unable to delete temporary holding directory `' $possibly_temp_restore_dir '`.' );
    } else {
        
pb_backupbuddy::status'details''Cleaned up temporary files.' );
    }
}
pb_backupbuddy::status'details''Individual file / directory restore cleanup complete.' );


// Remove any old temporary zip directories: wp-content/uploads/backupbuddy_backups/temp_zip_XXXX/. Logs any directories it cannot delete.
pb_backupbuddy::status'details''Cleaning up any old temporary zip directories in backup directory temp location `' backupbuddy_core::getBackupDirectory() . 'temp_zip_XXXX/`.' );
$temp_directory backupbuddy_core::getBackupDirectory() . 'temp_zip_*';
$files glob$temp_directory '*' );
if ( 
is_array$files ) && !empty( $files ) ) { // For robustness. Without open_basedir the glob() function returns an empty array for no match. With open_basedir in effect the glob() function returns a boolean false for no match.
    
foreach( $files as $file ) {
        if ( ( 
strpos$file'index.' ) !== false ) || ( strpos$file'.htaccess' ) !== false ) ) { // Index file or htaccess dont get deleted so go to next file.
            
continue;
        }
        
$file_stats stat$file );
        if ( ( 
time() - $file_stats['mtime'] ) > $backup_age_limit ) { // If older than 12 hours, delete the log.
            
if ( @pb_backupbuddy::$filesystem->unlink_recursive$file ) === false ) {
                
$message 'BackupBuddy was unable to clean up (delete) temporary directory/file: `' $file '`. You should manually delete it and/or verify proper file permissions to allow BackupBuddy to clean up for you.';
                
pb_backupbuddy::status'error'$message );
                
backupbuddy_core::mail_error$message );
            }
        }
    }
}


// Cleanup remote S3 multipart chunking.
foreach( pb_backupbuddy::$options['remote_destinations'] as $destination ) {
    if ( 
$destination['type'] != 's3' ) { continue; }
    if ( isset( 
$destination['max_chunk_size'] ) && ( $destination['max_chunk_size'] == '0' ) ) { continue; }
    
    
pb_backupbuddy::status'details''Found S3 Multipart Chunking Destinations to cleanup.' );
    require_once( 
pb_backupbuddy::plugin_path() . '/destinations/bootstrap.php' );
    
$cleanup_result pb_backupbuddy_destinations::multipart_cleanup$destination );
    
/*
    if ( true === $cleanup_result ) {
        pb_backupbuddy::status( 'details', 'S3 Multipart Chunking Cleanup Success.' );
    } else {
        pb_backupbuddy::status( 'error', 'S3 Multipart Chunking Cleanup FAILURE. Manually cleanup stalled multipart send via S3 or try again later.' );
    }
    */
}


// Clean up and temp rollback database tables.
foreach( pb_backupbuddy::$options['rollback_cleanups'] as $cleanup_serial => $start_time ) {
    if ( ( 
time() - $start_time ) > $max_status_log_age ) {
        if ( 
false !== ( $results mysql_query"SELECT table_name FROM information_schema.tables WHERE ( ( table_name LIKE 'BBnew-" substr$cleanup_serial0) . "\_%' ) OR ( table_name LIKE 'BBold-" substr$cleanup_serial0) . "\_%' ) ) AND table_schema = DATABASE()" ) ) ) {
                while( 
$result mysql_fetch_row$results ) ) {
                    if ( 
false === mysql_query"DROP TABLE `" mysql_real_escape_string$result[0] ) . "`") ) {
                        
pb_backupbuddy::status'details''Error #8493984: Unable to drop temp rollback table `' $result['table_name'] . '`.' );
                    }
                }
        }
        unset( 
pb_backupbuddy::$options['rollback_cleanups'][$cleanup_serial] );
        
pb_backupbuddy::save();
    }
}



// Cleanup any cron schedules pointing to non-existing schedules.
$cron get_option('cron');
// Loop through each cron time to create $crons array for displaying later.
$crons = array();
foreach ( (array) 
$cron as $time => $cron_item ) {
    if ( 
is_numeric$time ) ) {
        
// Loop through each schedule for this time
        
foreach ( (array) $cron_item as $hook_name => $event ) {
            foreach ( (array) 
$event as $item_name => $item ) {
                
                if ( 
'pb_backupbuddy-cron_scheduled_backup' == $hook_name ) { // scheduled backup
                    
if ( !empty( $item['args'] ) ) {
                        
                        if ( ! isset( 
pb_backupbuddy::$options['schedules'][ $item['args'][0] ] ) ) { // BB schedule does not exist so delete this cron item.
                            
if ( FALSE === backupbuddy_core::unschedule_event$time$hook_name$item['args'] ) ) { // Delete the scheduled cron.
                                
pb_backupbuddy::status'error''Error #5657667675b. Unable to delete CRON job. Please see your BackupBuddy error log for details.' );
                            } else {
                                
pb_backupbuddy::status'details''Removed stale cron scheduled backup.' );
                            }
                        }
                        
                    } else { 
// No args, something wrong so delete it.
                        
                        
if ( FALSE === backupbuddy_core::unschedule_event$time$hook_name$item['args'] ) ) { // Delete the scheduled cron.
                            
pb_backupbuddy::status'error''Error #5657667675c. Unable to delete CRON job. Please see your BackupBuddy error log for details.' );
                        } else {
                            
pb_backupbuddy::status'details''Removed stale cron scheduled backup which had no arguments.' );
                        }
                        
                    }
                }
                
            } 
// End foreach.
            
unset( $item );
            unset( 
$item_name );
        } 
// End foreach.
        
unset( $event );
        unset( 
$hook_name );
    } 
// End if is_numeric.
// End foreach.
unset( $cron_item );
unset( 
$time );



@
clearstatcache(); // Clears file info stat cache.

pb_backupbuddy::status'message''Finished cleanup procedure.' );