Changeset 547958
- Timestamp:
- 05/23/2012 10:57:07 AM (13 years ago)
- Location:
- backup/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
backup/trunk/backup.php
r546966 r547958 2 2 /* 3 3 Plugin Name: Backup 4 Version: 1.1. 14 Version: 1.1.2 5 5 Plugin URI: http://hel.io/wordpress/backup/ 6 6 Description: Backup your WordPress website to Google Drive. … … 68 68 'client_secret' => '', 69 69 'last_backup' => '', 70 'number_of_backups' => 10, 70 'local_number' => 10, 71 'drive_number' => 10, 71 72 'local_files' => array(), 72 73 'drive_files' => array(), … … 77 78 78 79 } 80 81 /** 82 * Backup Deactivation 83 * 84 * This function is called whenever the plugin is being deactivated and removes 85 * all files and directories it created as well as the options stored in the database. 86 * It also revokes access to the Google Account associated with it and removes all scheduled events created. 87 */ 88 function backup_deactivation() { 89 $options = get_option( 'backup_options' ); 90 if ( ! empty( $options['token'] ) ) 91 @file_get_contents( 'https://accounts.google.com/o/oauth2/revoke?token=' . $options['token'] ); 92 delete_option( 'backup_options' ); 93 if ( wp_next_scheduled( 'backup_schedule' ) ) { 94 wp_clear_scheduled_hook('backup_schedule'); 95 } 96 rrmdir( WP_CONTENT_DIR . '/backup' ); 97 if ( file_exists( WP_CONTENT_DIR . '/backup.sql' ) ) 98 unlink( WP_CONTENT_DIR . '/backup.sql' ); 99 } 100 register_deactivation_hook( __FILE__, 'backup_deactivation' ); 79 101 80 102 // Add custom schedule intervals … … 198 220 <th scope="row"><?php _e( 'Store a maximum of', 'backup' ); ?></th> 199 221 <td> 200 <input name='number' type='text' class='small-text' value='<?php echo $options['number_of_backups']; ?>' /> <?php _e( 'backups.', 'backup' ); ?> 201 <span class="description"><?php _e( 'Oldest backups will be deleted first.', 'backup' ) ?></span> 202 222 <input name='local_number' type='text' class='small-text' value='<?php echo $options['local_number']; ?>' /> <?php _e( 'backups locally and ', 'backup' ); ?> 223 <input name='drive_number' type='text' class='small-text' value='<?php echo $options['drive_number']; ?>' /> <?php _e( 'backups on Google Drive.', 'backup' ); ?> 203 224 </td> 204 225 </tr> … … 206 227 <th scope="row"><?php _e( 'When to backup', 'backup' ); ?></th> 207 228 <td> 208 <select name='frequency'> 229 <select name='frequency'> 209 230 <option value='never' <?php selected( 'never', $options['frequency'] ); ?> ><?php _e( 'Never', 'backup' ); ?></option> 210 231 <option value='daily' <?php selected( 'daily', $options['frequency'] ); ?> ><?php _e( 'Daily', 'backup' ); ?></option> … … 236 257 237 258 // If number isn't an integer stop function execution 238 if ( ! (int) $_POST['number'] ) 259 $local_number = intval( $_POST['local_number'] ); 260 $drive_number = intval( $_POST['drive_number'] ); 261 if ( $local_number < 0 || $drive_number < 0 ) 239 262 return false; 240 263 241 264 $options['folder'] = $_POST['folder']; 242 265 $options['frequency'] = $_POST['frequency']; 243 $options['number_of_backups'] = $_POST['number']; 266 $options['local_number'] = $local_number; 267 $options['drive_number'] = $drive_number; 244 268 245 269 update_option( 'backup_options', $options ); 270 271 // If we have already scheduled a backup before, clear it first 272 if ( wp_next_scheduled( 'backup_schedule' ) ) { 273 wp_clear_scheduled_hook('backup_schedule'); 274 } 246 275 247 276 // Schedule backup if frequency is something else than never 248 277 if ( $options['frequency'] != 'never' ) { 249 250 // If we have already scheduled a backup before, clear it first251 if ( wp_next_scheduled( 'backup_schedule' ) ) {252 wp_clear_scheduled_hook('backup_schedule');253 }254 255 // Schedule backups256 278 wp_schedule_event( current_time( 'timestamp' ), $options['frequency'], 'backup_schedule'); 257 279 } … … 294 316 $options['last_backup'] = $backup_time; 295 317 $options['local_files'][] = $file_path; 296 if ( count( $options['local_files'] ) > $options[' number_of_backups'] )318 if ( count( $options['local_files'] ) > $options['local_number'] ) 297 319 if ( unlink( $f = array_shift( $options['local_files'] ) ) ) 298 320 backup_log( 'NOTICE', 'Deleted old backup file ' . $f ); … … 300 322 backup_log( 'WARNING', 'Could not delete file ' . $f ); 301 323 if ( $options['token'] && $options['client_id'] && $options['client_secret'] ) { 302 $access = access_token( $options['token'], $options['client_id'], $options['client_secret'] ); 303 backup_log( 'NOTICE', 'Attempting to upload archive to Google Drive' ); 304 $timer_start = microtime( true ); 305 if ( ! $id = upload_file( $file_path, $file_name, $options['folder'], $access ) ) 306 backup_log( 'ERROR', 'Failed to upload archive to Google Drive!' ); 324 if ( $access = access_token( $options['token'], $options['client_id'], $options['client_secret'] ) ) { 325 backup_log( 'NOTICE', 'Attempting to upload archive to Google Drive' ); 326 $timer_start = microtime( true ); 327 if ( ! $id = upload_file( $file_path, $file_name, $options['folder'], $access ) ) 328 backup_log( 'ERROR', 'Failed to upload archive to Google Drive!' ); 329 else { 330 backup_log( 'NOTICE', 'Archive ' . $file_name . ' uploaded to Google Drive in ' . ( microtime( true ) - $timer_start ) . ' seconds' ); 331 $options['drive_files'][] = $id; 332 if ( count( $options['drive_files'] ) > $options['drive_number'] ) 333 if ( delete_file( $r = array_shift( $options['drive_files'] ), $access ) ) 334 backup_log( 'NOTICE', 'Deleted Google Drive file ' . $r ); 335 else 336 backup_log( 'WARNING', 'Could not delete Google Drive file ' . $r ); 337 } 338 } 307 339 else { 308 backup_log( 'NOTICE', 'Archive ' . $file_name . ' uploaded to Google Drive in ' . ( microtime( true ) - $timer_start ) . ' seconds' ); 309 $options['drive_files'][] = $id; 310 if ( count( $options['drive_files'] ) > $options['number_of_backups'] ) 311 if ( delete_file( $r = array_shift( $options['drive_files'] ), $access ) ) 312 backup_log( 'NOTICE', 'Deleted Google Drive file ' . $r ); 313 else 314 backup_log( 'WARNING', 'Could not delete Google Drive file ' . $r ); 315 } 340 backup_log( 'ERROR', 'Did not receive an access token from Google!', __FILE__, __LINE__ ); 341 } 316 342 } 317 343 update_option( 'backup_options', $options ); … … 362 388 363 389 $result = @file_get_contents( $url, false, stream_context_create( $context ) ); 364 if ( strpos( array_shift( $http_response_header ), '200' ) ) { 365 $response_header = array(); 366 foreach ( $http_response_header as $header_line ) { 367 list( $key, $value ) = explode( ':', $header_line, 2 ); 368 $response_header[trim( $key )] = trim( $value ); 369 } 370 if ( isset( $response_header['Location'] ) ) { 371 $next_location = $response_header['Location']; 372 $pointer = 0; 373 $max_chunk_size = 524288; 374 while ( $pointer < $size - 1 ) { 375 $chunk = file_get_contents( $file, false, NULL, $pointer, $max_chunk_size ); 376 $next_location = upload_chunk( $next_location, $chunk, $pointer, $size, $token ); 377 if( $next_location === false ) { 378 return false; 379 } 380 // if object it means we have our simpleXMLElement response 381 if ( is_object( $next_location ) ) { 382 // return resource Id 383 return substr( $next_location->children( "http://schemas.google.com/g/2005" )->resourceId, 5 ); 390 if ( $result ) { 391 if ( strpos( $response = array_shift( $http_response_header ), '200' ) ) { 392 $response_header = array(); 393 foreach ( $http_response_header as $header_line ) { 394 list( $key, $value ) = explode( ':', $header_line, 2 ); 395 $response_header[trim( $key )] = trim( $value ); 396 } 397 if ( isset( $response_header['Location'] ) ) { 398 $next_location = $response_header['Location']; 399 $pointer = 0; 400 $max_chunk_size = 524288; 401 while ( $pointer < $size - 1 ) { 402 $chunk = file_get_contents( $file, false, NULL, $pointer, $max_chunk_size ); 403 $next_location = upload_chunk( $next_location, $chunk, $pointer, $size, $token ); 404 if( $next_location === false ) { 405 return false; 406 } 407 // if object it means we have our simpleXMLElement response 408 if ( is_object( $next_location ) ) { 409 // return resource Id 410 return substr( $next_location->children( "http://schemas.google.com/g/2005" )->resourceId, 5 ); 411 } 412 $pointer += strlen( $chunk ); 413 384 414 } 385 $pointer += strlen( $chunk ); 386 387 } 388 } 415 } 416 } 417 else { 418 backup_log( 'ERROR', 'Bad response: ' . $response, __FILE__, __LINE__ ); 419 return false; 420 } 421 } 422 else { 423 backup_log( 'ERROR', 'Unable to request file from ' . $url, __FILE__, __LINE__ ); 389 424 } 390 return false;391 425 } 392 426 /** … … 440 474 } 441 475 else { 442 backup_log( 'ERROR', 'Bad Response: ' . $response);476 backup_log( 'ERROR', 'Bad response: ' . $response, __FILE__, __LINE__ ); 443 477 return false; 444 478 } … … 768 802 769 803 /** 804 * Recursively remove a directory 805 * 806 * @param string $dir The path to the directory to be removed 807 */ 808 function rrmdir( $dir ) { 809 foreach ( glob( $dir . '/*' ) as $file ) { 810 if ( is_dir( $file ) ) 811 rrmdir( $file ); 812 else 813 unlink( $file ); 814 } 815 rmdir( $dir ); 816 } 817 818 /** 770 819 * Custom logging function for the backup plugin 771 820 * -
backup/trunk/readme.txt
r546967 r547958 5 5 Requires at least: 3.0 6 6 Tested up to: 3.3.2 7 Stable tag: 1.1. 17 Stable tag: 1.1.2 8 8 9 9 Make backups of your Wordpress site to Google Drive. … … 27 27 == Changelog == 28 28 29 = 1.1.2 = 30 * Added the ability to store a different number of backups locally then on Google Drive 31 * On deactivation the plugin deletes all traces of itself (backups stored locally, options) and revokes access to the Google Account 32 * Fixed some more frequency issues 33 29 34 = 1.1.1 = 30 35 * Fixed mothly backup frequency
Note: See TracChangeset
for help on using the changeset viewer.