-
Notifications
You must be signed in to change notification settings - Fork 4
/
backup.php
1698 lines (1530 loc) · 59.3 KB
/
backup.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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/*
Plugin Name: Backup
Version: 2.2
Plugin URI: http://hel.io/wordpress/backup/
Description: Back up your WordPress website to Google Drive.
Author: Sorin Iclanzan
Author URI: http://hel.io/
License: GPL3
Text Domain: backup
Domain Path: /languages
*/
/*
Copyright 2012 Sorin Iclanzan (email : sorin@hel.io)
This file is part of Backup.
Backup is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Backup is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Backup. If not, see http://www.gnu.org/licenses/gpl.html.
*/
// Only load the plugin if needed.
if ( is_admin() || defined( 'DOING_CRON' ) || isset( $_GET['doing_wp_cron'] ) || isset( $_GET['backup'] ) ) {
// Load required classes.
if ( ! class_exists( 'GOAuth' ) )
require_once( 'class-goauth.php' );
if ( ! class_exists( 'GDocs' ) )
require_once( 'class-gdocs.php' );
// Load helper functions
require_once( 'functions.php' );
/**
* Backup for WordPress class.
*
* Implements backup functionality in WordPress. Currently supports
* backing up on the local filesystem and on Google Drive.
*
* @uses WP_Error for storing error messages.
* @uses GOAuth for Google OAuth2 authorization.
* @uses GData to upload backups to Google Drive (Docs).
*/
class Backup {
/**
* Stores the plugin version.
*
* @var string
* @access private
*/
private $version;
/**
* Stores the plugin base filesystem directory
*
* @var string
* @access private
*/
private $plugin_dir;
/**
* Stores the unique text domain used for I18n
*
* @var string
* @access private
*/
private $text_domain;
/**
* Stores plugin options.
*
* Options are automatically updated in the database when the destructor is called.
*
* @var array
* @access private
*/
private $options;
/**
* Stores custom schedule intervals to use with WP_Cron.
*
* @var array
* @access private
*/
private $schedules;
/**
* Stores the redirect URI needed by GOAuth.
*
* @var string
* @access private
*/
private $redirect_uri;
/**
* Stores an instance of GDocs.
*
* @var GDocs
* @access private
*/
private $gdocs;
/**
* Stores an instance of GOAuth.
*
* @var GOAuth
* @access private
*/
private $goauth;
/**
* Stores messages that need to be displayed on the option page.
*
* @var array
* @access private
*/
private $messages = array();
/**
* Stores the absolute path to the directory this plugin will use to store files.
*
* @var string
* @access private
*/
private $local_folder;
/**
* Stores the absolute path and file name where database dumps are saved.
*
* @var string
* @access private
*/
private $dump_file;
/**
* Stores the absolute path to the log file.
*
* @var string
* @access private
*/
private $log_file;
/**
* Stores a list of paths to directories and files that are available for backup.
*
* @var array
* @access private
*/
private $sources;
/**
* Stores paths that are to be excluded when backing up.
*
* @var array
* @access private
*/
private $exclude = array();
/**
* Stores a list of URIs representing the scope required by GOAuth.
*
* @var array
* @access private
*/
private $scope;
/**
* Stores the timestamp at the time of the execution.
*
* @var integer
* @access private
*/
private $time;
/**
* Stores the identifier of the plugin options page.
*
* @var string
* @access private
*/
private $pagehook;
/**
* Stores the ID of the current user
*
* @var integer
* @access private
*/
private $user_id;
/**
* Stores the list of HTTP transports supported by WordPress
*
* @var array
* @access private
*/
private $http_transports;
/**
* Constructor
*
* This loads most of what's needed to run the plugin and sets actions and filters.
*
* @global float $timestart Seconds from when script started
*/
function __construct() {
global $timestart;
$this->version = '2.2';
$this->time = intval( $timestart );
$this->plugin_dir = dirname( plugin_basename( __FILE__ ) );
$this->text_domain = 'backup';
// Enable internationalization
load_plugin_textdomain( $this->text_domain, false, $this->plugin_dir . '/languages' );
$this->scope = array(
'https://www.googleapis.com/auth/drive.file',
'https://www.googleapis.com/auth/userinfo.profile',
'https://www.googleapis.com/auth/userinfo.email',
'https://docs.google.com/feeds/',
'https://docs.googleusercontent.com/',
'https://spreadsheets.google.com/feeds/'
);
$this->http_transports = array( 'curl', 'streams', 'fsockopen' );
$this->schedules = array(
'weekly' => array(
'interval' => 604800,
'display' => __( 'Weekly', $this->text_domain )
),
'monthly' => array(
'interval' => 2592000,
'display' => __( 'Monthly', $this->text_domain )
)
);
$this->redirect_uri = admin_url( 'options-general.php?page=backup&action=auth' );
// Get options if they exist, else set defaults
if ( ! $this->options = get_option( 'backup_options' ) ) {
$this->options = array(
'plugin_version' => $this->version,
'backup_token' => '',
'refresh_token' => '',
'backup_title' => get_bloginfo( 'name' ),
'local_folder' => '',
'drive_folder' => '',
'backup_frequency' => 'never',
'source_list' => array( 'database', 'content', 'uploads', 'plugins' ),
'exclude_list' => array( '.svn', '.git', '.DS_Store' ),
'include_list' => array(),
'backup_list' => array(),
'client_id' => '',
'client_secret' => '',
'local_number' => 1,
'drive_number' => 10,
'quota_total' => '',
'quota_used' => '',
'chunk_size' => 1, // MB
'time_limit' => 600, // seconds
'backup_attempts' => 3,
'request_timeout' => 60, // seconds
'enabled_transports' => $this->http_transports,
'ssl_verify' => true,
'email_notify' => false,
'user_info' => array()
);
}
else
if (
!isset( $this->options['plugin_version'] ) ||
version_compare( $this->version, $this->options['plugin_version'], '>' )
)
add_action( 'init', array( &$this, 'upgrade' ), 1 );
// Allow some options to be overwritten from the config file.
if ( defined( 'BACKUP_REFRESH_TOKEN' ) ) $this->options['refresh_token'] = BACKUP_REFRESH_TOKEN;
if ( defined( 'BACKUP_DRIVE_FOLDER' ) ) $this->options['drive_folder'] = BACKUP_DRIVE_FOLDER;
if ( defined( 'BACKUP_CLIENT_ID' ) ) $this->options['client_id'] = BACKUP_CLIENT_ID;
if ( defined( 'BACKUP_CLIENT_SECRET' ) ) $this->options['client_secret'] = BACKUP_CLIENT_SECRET;
if ( defined( 'BACKUP_LOCAL_FOLDER' ) ) $this->options['local_folder'] = BACKUP_LOCAL_FOLDER;
$this->local_folder = absolute_path( $this->options['local_folder'], ABSPATH );
if ( defined( 'BACKUP_LOCAL_FOLDER' ) )
$this->create_dir( $this->local_folder );
$this->dump_file = $this->local_folder . '/dump.sql';
$upload_dir = wp_upload_dir();
$this->sources = array(
'database' => array( 'title' => __( 'Database', $this->text_domain ), 'path' => $this->dump_file ),
'content' => array( 'title' => __( 'Content', $this->text_domain ), 'path' => WP_CONTENT_DIR ),
'uploads' => array( 'title' => __( 'Uploads', $this->text_domain ), 'path' => $upload_dir['basedir'] ),
'plugins' => array( 'title' => __( 'Plugins', $this->text_domain ), 'path' => WP_PLUGIN_DIR ),
'wordpress' => array( 'title' => __( 'WordPress', $this->text_domain ), 'path' => ABSPATH )
);
$this->exclude[] = $this->local_folder;
register_activation_hook( __FILE__, array( &$this, 'activate' ) );
// Add custom cron intervals
add_filter( 'cron_schedules', array( &$this, 'cron_add_intervals' ) );
// Link to the settings page from the plugins page
add_filter( 'plugin_action_links', array( &$this, 'action_links' ), 10, 2 );
// Disable unwanted HTTP transports.
if ( isset( $this->options['enabled_transports'] ) )
foreach ( $this->http_transports as $t )
if ( !in_array( $t, $this->options['enabled_transports'] ) )
add_filter( 'use_' . $t . '_transport', '__return_false' );
// Add 'Backup' to the Settings admin menu; save default metabox layout in the database.
add_action( 'admin_menu', array( &$this, 'backup_menu' ) );
// Handle Google OAuth2.
if ( $this->is_auth() )
add_action( 'init', array( &$this, 'auth' ) );
// Display persistent error notifications if we have any.
if ( isset( $this->options['messages']['error'] ) )
add_action( 'admin_notices', array( &$this, 'error_notice' ), 3 );
// Print admin notices.
add_action( 'admin_notices', array( &$this, 'print_notices' ), 2 );
// Do backup on schedule.
add_action( 'backup_schedule', array( &$this, 'do_backup' ) );
// Retry to backup after a failed attempt.
add_action( 'backup_retry', array( &$this, 'retry_backup' ) );
// Do stuff just before the end of script execution.
add_action( 'shutdown', array( &$this, 'shutdown' ) );
// Prepare GOAuth object.
$this->goauth = new GOAuth( array(
'client_id' => $this->options['client_id'],
'client_secret' => $this->options['client_secret'],
'redirect_uri' => $this->redirect_uri,
'refresh_token' => $this->options['refresh_token'],
'request_timeout' => $this->options['request_timeout'],
'ssl_verify' => $this->options['ssl_verify']
) );
// If we're doing backup work set the environment accordingly.
if ( $this->doing_backup() ) {
if ( get_transient( 'backup_lock' ) )
exit; // Exit if another backup process is running.
set_transient( 'backup_lock', $this->time, 60 * 60 * 24 );
add_action( 'shutdown', array( &$this, 'unlock' ) );
// Enable manual backup URI.
add_action( 'template_redirect', array( &$this, 'manual_backup' ) );
@ini_set( 'safe_mode', 0 ); // Try to disable safe mode.
set_time_limit( $this->options['time_limit'] ); // Set the time limit.
// We might need a lot of memory for backing up.
@ini_set( 'memory_limit', apply_filters( 'admin_memory_limit', WP_MAX_MEMORY_LIMIT ) );
ignore_user_abort( true ); // Allow the script to run after the user closes the window.
// All this is needed in order that every echo gets sent to the browser.
@ini_set( 'zlib.output_compression', 0 );
@ini_set( 'implicit_flush', 1 );
wp_ob_end_flush_all();
ob_implicit_flush();
}
}
/**
* This is run when you activate the plugin, checking for compatibility, adding the default options to the database.
*
* @global string $wp_version Used to check against the required WordPress version.
*/
public function activate() {
global $wp_version;
// Check for compatibility
try {
// check OpenSSL
if ( !function_exists( 'openssl_open' ) ) {
throw new Exception(
__( 'Please enable OpenSSL in PHP. Backup needs it to communicate with Google Drive.', $this->text_domain )
);
}
// check SimpleXMLElement
if ( !class_exists( 'SimpleXMLElement' ) ) {
throw new Exception(
__( 'Please enable SimpleXMLElement in PHP. Backup could not be activated.', $this->text_domain )
);
}
// check WordPress version
if ( version_compare( $wp_version, '3.4', '<' ) ) {
throw new Exception( __( 'Backup requires WordPress 3.4 or higher!', $this->text_domain ) );
}
}
catch ( Exception $e ) {
deactivate_plugins( $this->plugin_dir . '/backup.php', true );
echo '<div id="message" class="error">' . $e->getMessage() . '</div>';
trigger_error( 'Could not activate Backup.', E_USER_ERROR );
return;
}
// Set the backup token here, because in the constructor 'wp_generate_password()' is not defined.
$this->options['backup_token'] = wp_generate_password( 12, false );
if ( empty( $this->options['local_folder'] ) ) {
$this->local_folder = WP_CONTENT_DIR . '/backup-' . $this->options['backup_token'];
$this->options['local_folder'] = relative_path( ABSPATH, $this->local_folder );
}
if ( $this->goauth->is_authorized() )
$this->set_user_info();
// Add the default options to the database, without letting WP autoload them
add_option( 'backup_options', $this->options, '', 'no' );
$this->pagehook = get_plugin_page_hookname( 'backup', 'options-general.php' );
if ( ! $this->user_id )
$this->user_id = get_current_user_id();
// Set the default order of the metaboxes.
if ( ! get_user_meta( $this->user_id, "meta-box-order_".$this->pagehook, true ) ) {
$meta_value = array(
'side' => 'metabox-authorization,metabox-status',
'normal' => 'metabox-advanced',
'advanced' => 'metabox-logfile',
);
update_user_meta( $this->user_id, "meta-box-order_".$this->pagehook, $meta_value );
}
// Set the default closed metaboxes.
if ( ! get_user_meta( $this->user_id, "closedpostboxes_".$this->pagehook, true ) ) {
$meta_value = array( 'metabox-advanced' );
update_user_meta( $this->user_id, "closedpostboxes_".$this->pagehook, $meta_value );
}
// Set the default hidden metaboxes.
if ( ! get_user_meta( $this->user_id, "metaboxhidden_".$this->pagehook, true ) ) {
$meta_value = array( 'metabox-logfile' );
update_user_meta( $this->user_id, "metaboxhidden_".$this->pagehook, $meta_value );
}
// try to create the default backup folder files
$this->create_dir( $this->local_folder );
}
/**
* This function handles upgrading the plugin to a new version.
* It gets triggered when the plugin version is different than the one stored in the database.
*/
function upgrade() {
if (
!isset( $this->options['plugin_version'] ) ||
version_compare( $this->options['plugin_version'], '2.1', '<' )
) {
$this->options['backup_token'] = wp_generate_password( 12, false );
$this->options['backup_title'] = get_bloginfo( 'name' );
$this->options['include_list'] = array();
$this->options['request_timeout'] = 60;
$this->options['backup_attempts'] = 3;
$this->options['enabled_transports'] = $this->http_transports;
$this->options['ssl_verify'] = true;
$this->options['email_notify'] = false;
$this->options['user_info'] = array();
$this->options['backup_list'] = array();
if ( $this->goauth->is_authorized() )
$this->set_user_info();
// Delete the old backup folder if it's the default
if ( WP_CONTENT_DIR . '/backup' == $this->local_folder ) {
delete_path( $this->local_folder );
$this->options['local_files'] = array();
// and create a new one based on the random token.
$this->local_folder = WP_CONTENT_DIR . '/backup-' . $this->options['backup_token'];
$this->options['local_folder'] = relative_path( ABSPATH, $this->local_folder );
if ( wp_mkdir_p( $this->local_folder ) ) {
if ( !@is_file( $this->local_folder . "/.htaccess" ) )
file_put_contents( $this->local_folder . "/.htaccess", "Order allow,deny\nDeny from all" );
}
}
else {
file_put_contents( $this->local_folder . "/.htaccess", "Order allow,deny\nDeny from all" );
delete_path( $this->local_folder . '/backup.log' );
}
// Use the new backup_list array to store backup data.
if ( isset( $this->options['backup_list'] ) )
return;
$local_list = array_reverse( $this->options['local_files'] );
$drive_list = array_reverse( $this->options['drive_files'] );
$count = count( $drive_list );
if ( count( $local_list ) > $count )
$count = count( $local_list );
for ( $i = 0; $i < $count; $i++ ) {
$element = array();
$element['status'] = 1;
if ( isset( $local_list[$i] ) )
$element['file_path'] = $local_list[$i];
if ( isset( $drive_list[$i] ) )
$element['drive_id'] = $drive_list[$i];
if ( 0 == $i )
$element['timestamp'] = $this->options['last_backup'];
$this->options['backup_list'] = array_merge( array( (string) $i => $element ), $this->options['backup_list'] );
}
unset(
$this->options['local_files'],
$this->options['drive_files'],
$this->options['last_backup'],
$this->options['resume_attempts']
);
}
// Save the new plugin version
$this->options['plugin_version'] = $this->version;
}
/**
* Filter - Add custom schedule intervals.
*
* @param array $schedules The array of defined intervals.
* @return array Returns the array of defined intervals after adding the custom ones.
*/
function cron_add_intervals( $schedules ) {
return array_merge( $schedules, $this->schedules );
}
/**
* Filter - Adds a 'Settings' action link on the plugins page.
*
* @param array $links The list of links.
* @param string $file The plugin file to check.
* @return array Returns the list of links with the custom link added.
*/
function action_links( $links, $file ) {
if ( $file != plugin_basename( __FILE__ ) )
return $links;
$settings_link = sprintf( '<a href="options-general.php?page=backup">%s</a>',
__( 'Settings', $this->text_domain )
);
array_unshift( $links, $settings_link );
return $links;
}
/**
* Action - Adds options page in the admin menu.
*/
function backup_menu() {
$this->pagehook = add_options_page(
__( 'Backup Settings', $this->text_domain ),
__( 'Backup', $this->text_domain ),
'manage_options', 'backup',
array( &$this, 'options_page' )
);
// Hook to update options
add_action( 'load-'.$this->pagehook, array( &$this, 'options_update' ) );
// Hook to add metaboxes, context help and screen options
add_action( 'load-'.$this->pagehook, array( &$this, 'on_load_options_page' ) );
}
/**
* Action - Adds meta boxes and checks if the local folder is writable.
*/
function on_load_options_page() {
// These scripts are needed for metaboxes to function
wp_enqueue_script( 'common' );
wp_enqueue_script( 'wp-lists' );
wp_enqueue_script( 'postbox' );
add_action( 'admin_print_footer_scripts', array( &$this, 'print_footer_scripts' ) );
add_action( 'admin_print_styles', array( &$this, 'print_styles' ) );
// Add the metaboxes
add_meta_box( 'metabox-authorization',
__( 'Authorization', $this->text_domain ),
array( &$this, 'metabox_authorization_content' ),
$this->pagehook, 'side', 'core'
);
add_meta_box( 'metabox-status',
__( 'Status', $this->text_domain ),
array( &$this, 'metabox_status_content' ),
$this->pagehook, 'side', 'core'
);
add_meta_box( 'metabox-advanced',
__( 'Advanced', $this->text_domain ),
array( &$this, 'metabox_advanced_content' ),
$this->pagehook, 'normal', 'core'
);
add_meta_box( 'metabox-logfile',
__( 'Log File', $this->text_domain ),
array( &$this, 'metabox_logfile_content' ),
$this->pagehook, 'advanced', 'core'
);
// Add help tabs and help sidebar
include( 'context-help.php' );
add_screen_option( 'layout_columns', array( 'max' => 2, 'default' => 2 ) );
// Check if the local folder is writable
if ( ( !@is_dir( $this->local_folder ) && !$this->create_dir( $this->local_folder ) ) ||
!@is_writable( $this->local_folder ) )
$this->messages['error'][] = sprintf(
__( "The local path %s is not writable. Please change the permissions or choose another directory.",
$this->text_domain
), '<kbd>' . esc_html( $this->local_folder ) . '</kbd>'
);
}
/**
* Display the options page.
*
* @global integer $screen_layout_columns The number of of columns of the current screen
* @global object $wp_locale
*/
function options_page() {
global $screen_layout_columns, $wp_locale;
require_once( 'page-options.php' );
}
function print_styles() {
?><style>
#metabox-status > .inside {
padding: 0;
}
td > .feature-filter > .feature-group > li {
width: 120px;
}
</style><?php
}
function print_footer_scripts() {
?><script type="text/javascript">
//<![CDATA[
jQuery(document).ready( function($) {
$('.if-js-closed').removeClass('if-js-closed').addClass('closed');
postboxes.add_postbox_toggles('<?php echo $this->pagehook; ?>');
var val = "";
$('#backup_frequency').change(function(){
val = $("#backup_frequency option:selected").val();
switch(val) {
case "never":
$("#start_wrap").addClass("hide-if-js");
break;
default:
$("#start_wrap").removeClass("hide-if-js");
}
});
$('#need-help-link').click(function(e){e.preventDefault();$('#contextual-help-link').trigger('click')});
$('.show-para').click(function(e){e.preventDefault();$(this).hide();$(this).parent().siblings('.para').slideDown("fast");});
$('.click-select').click(function(){(this).select()});
});
//]]>
</script><?php
}
/**
* Render Authorization meta box.
*/
function metabox_authorization_content( $data ) {
if ( !$this->goauth->is_authorized() ) { ?>
<form action="<?php echo $this->redirect_uri; ?>" method="post">
<p><?php _e('Before backups can be uploaded to Google Drive, you need to authorize the plugin and give it permission to make changes on your behalf.', $this->text_domain ); ?></p>
<p>
<label for="client_id"><?php _e( 'Client ID', $this->text_domain ); ?></label>
<input id="client_id" name="client_id" type='text' <?php __checked_selected_helper( defined( 'BACKUP_CLIENT_ID' ), true, true, 'readonly' ); ?> class="large-text" value='<?php echo esc_html( $this->options['client_id'] ); ?>' />
</p>
<p>
<label for="client_secret"><?php _e( 'Client secret', $this->text_domain ); ?>
<input id="client_secret" name='client_secret' type='text' <?php __checked_selected_helper( defined( 'BACKUP_CLIENT_SECRET' ), true, true, 'readonly' ); ?> class="large-text" value='<?php echo esc_html( $this->options['client_secret'] ); ?>' />
</p>
<p class="para hide-if-js">
<label for="refresh_token"><?php _e( 'Refresh token', $this->text_domain ); ?>
<input id="refresh_token" name='refresh_token' type='text' <?php __checked_selected_helper( defined( 'BACKUP_REFRESH_TOKEN' ), true, true, 'readonly' ); ?> class="large-text" value='<?php echo esc_html( $this->options['refresh_token'] ); ?>' />
</p>
<p>
<input name="authorize" type="submit" class="button-secondary" value="<?php _e( 'Authorize', $this->text_domain ) ?>" /> <a href="#refresh_token" class="show-para hide-if-no-js"><?php _e( 'More', $this->text_domain ); ?></a>
</p>
</form>
<?php } else { ?>
<?php if ( !empty( $this->options['user_info'] ) ) { ?>
<div id="dashboard_right_now" class="column-username">
<?php if ( $this->options['user_info']['picture'] ) { ?>
<img src="<?php echo $this->options['user_info']['picture']; ?>" width="50" heigth="50" />
<?php } else {
$default = get_option( 'avatar_default' );
if ( empty( $default ) )
$default = 'mystery';
echo get_avatar( $this->options['user_info']['email'], 50, $default );
}?>
<strong><?php echo $this->options['user_info']['name']; ?></strong><br />
<?php echo $this->options['user_info']['email']; ?><br />
<span class="approved"><?php _e( "Authorized", $this->text_domain ); ?></span>
</div>
<?php }
?>
<p><?php _e( 'Authorization to use Google Drive has been granted.', $this->text_domain ); ?></p>
<p class="para hide-if-js"><input type="text" readonly="readonly" class="click-select large-text" value="<?php echo esc_html( $this->options['refresh_token'] ); ?>" /></p>
<p><?php
if ( defined( 'BACKUP_REFRESH_TOKEN' ) ) {
?><a class="button disabled"><?php
} else {
?><a href="<?php echo $this->redirect_uri; ?>&state=revoke" class="button-secondary"><?php
}
?><?php _e( 'Revoke access', $this->text_domain ); ?></a> <a href="#token-select" class="show-para hide-if-no-js"><?php _e( 'Show token', $this->text_domain ); ?></a></p><?php
}
}
/**
* Render Status meta box.
*/
function metabox_status_content( $data ) {
$datetime_format = get_option( 'date_format' ) . " " . get_option( 'time_format' );
echo '<div class="misc-pub-section">' . __( 'Current date and time:', $this->text_domain ) . '<br/><strong>' .
date_i18n( $datetime_format, $this->time ) .
'</strong></div>' .
'<div class="misc-pub-section">' . __( 'Most recent backup:', $this->text_domain ) . '<br/><strong>';
if ( $backup = $this->get_last_backup( true ) )
echo date_i18n( $datetime_format, $backup['timestamp'] );
else
_e( 'never', $this->text_domain );
echo '</strong></div>' .
'<div class="misc-pub-section">' . __( 'Next scheduled backup:', $this->text_domain ) . '<br/><strong>';
if ( $next = wp_next_scheduled( 'backup_schedule' ) )
echo date_i18n( $datetime_format, $next );
else
_e( 'never', $this->text_domain );
echo '</strong></div>';
if ( $this->options['quota_used'] ) {
echo '<div class="misc-pub-section">' . __( 'Google Drive quota:', $this->text_domain ) . '<br/><strong>';
printf( __( '%s of %s used', $this->text_domain ),
size_format( $this->options['quota_used'], 2 ),
size_format( $this->options['quota_total'], 2 )
);
echo '</strong></div>';
}
?>
<div class="misc-pub-section misc-pub-section-last"><?php _e( 'Manual backup:', $this->text_domain ) ?>
<p class="para hide-if-js">
<input type="text" readonly="readonly" class="click-select large-text" value="<?php echo home_url( '?backup=' . $this->options['backup_token'] ); ?>" />
</p>
<div id="waiting">
<a href="<?php echo home_url( '?backup=' . $this->options['backup_token'] ); ?>" class="button-secondary" target="_blank">
<?php _e( 'Back up now', $this->text_domain ) ?></a>
<a href="#waiting" class="show-para hide-if-no-js">
<?php _e( 'Show URI', $this->text_domain ) ?>
</a>
</div>
</div><?php
}
/**
* Render Advanced meta box.
*/
function metabox_advanced_content( $data ) {
$names = array_keys( $this->sources );
echo '<div id="the-comment-list">' .
'<div class="comment-item">' .
'<h4>' . __( 'Backup options', $this->text_domain ) . '</h4>' .
'<table class="form-table">' .
'<tbody>' .
'<tr valign="top">' .
'<th scope="row"><label for="exclude">' . __( 'Exclude list', $this->text_domain ) . '</label></th>' .
'<td><input id="exclude" name="exclude" type="text" class="regular-text code" placeholder="' .
__( 'Comma separated paths to exclude.', $this->text_domain ) . '" value="' .
esc_html( implode( ', ', $this->options['exclude_list'] ) ) . '" /></td>' .
'</tr>' .
'<tr valign="top">' .
'<th scope="row"><label for="include">' . __( 'Include list', $this->text_domain ) . '</label></th>' .
'<td><input id="include" name="include" type="text" class="regular-text code" placeholder="' .
__( 'Comma separated paths to include.', $this->text_domain ) . '" value="' .
esc_html( implode( ', ', $this->options['include_list'] ) ) . '" /></td>' .
'</tr>' .
'</tbody>' .
'</table>' .
'</div>' .
'<div class="comment-item">' .
'<h4>' . __( 'Notification options', $this->text_domain ) . '</h4>' .
'<table class="form-table">' .
'<tbody>' .
'<tr valign="top">' .
'<th scope="row">' . __( 'When Backup fails', $this->text_domain ) . '</th>' .
'<td><label for="email_notify"><input id="email_notify" name="email_notify" type="checkbox" value="" ' .
checked( true, $this->options['email_notify'], false ) . ' /> ' .
__( 'send me an email notification.', $this->text_domain ) . '</label></td>' .
'</tr>' .
'</tbody>' .
'</table>' .
'</div>' .
'<div class="comment-item">' .
'<h4>' . __( 'Upload options', $this->text_domain ) . '</h4>' .
'<table class="form-table">' .
'<tbody>' .
'<tr valign="top">' .
'<th scope="row"><label for="chunk_size">' . __( 'Chunk size', $this->text_domain ) . '</label></th>' .
'<td><input id="chunk_size" name="chunk_size" class="small-text" type="number" min="0.5" step="0.5" value="' .
floatval( $this->options['chunk_size'] ) . '" /> <span>' .
__( 'MB', $this->text_domain ) . '</span></td>' .
'</tr>' .
'<tr valign="top">' .
'<th scope="row"><label for="time_limit">' . __( 'Time limit', $this->text_domain ) . '</label></th>' .
'<td><input id="time_limit" name="time_limit" class="small-text" type="number" min="0" step="1" value="' .
intval( $this->options['time_limit'] ) . '" /> <span>' .
__( 'seconds', $this->text_domain ) . '</span></td>' .
'</tr>' .
'<tr valign="top">' .
'<th scope="row"><label for="backup_attempts">' .
__( 'Retry failed backups', $this->text_domain ) . '</label></th>' .
'<td><input id="backup_attempts" name="backup_attempts" class="small-text" type="number" min="0" step="1" value="' .
intval( $this->options['backup_attempts'] ) . '" /> <span>' .
__( 'times', $this->text_domain ) . '</span></td>' .
'</tr>' .
'</tbody>' .
'</table>' .
'</div>' .
'<div class="comment-item">' .
'<h4>' . __( 'HTTP options', $this->text_domain ) . '</h4>' .
'<table class="form-table">' .
'<tbody>' .
'<tr valign="top">' .
'<th scope="row"><label for="request_timeout">' .
__( 'Request timeout', $this->text_domain ) . '</label></th>' .
'<td><input id="request_timeout" name="request_timeout" class="small-text" type="number" min="0" step="1" value="' .
intval( $this->options['request_timeout'] ) . '" /> <span>' .
__( 'seconds', $this->text_domain ) . '</span></td>' .
'</tr>' .
'<tr valign="top">' .
'<th scope="row">' . __( 'SSL verification', $this->text_domain ) . '</th>' .
'<td><label for="ssl_verify"><input id="ssl_verify" name="ssl_verify" type="checkbox" value="" ' .
checked( true, $this->options['ssl_verify'], false ) . ' /> ' .
__( 'Enable SSL verification.', $this->text_domain ) . '</label></td>' .
'</tr>' .
'<tr valign="top">' .
'<th scope="row">' . __( 'Enabled transports', $this->text_domain ) . '</th>' .
'<td>' .
'<div class="feature-filter">' .
'<ol class="feature-group">';
foreach ( $this->http_transports as $transport )
echo '<li><label for="transport_' . $transport . '"><input id="transport_' .
$transport . '" name="transports[]" type="checkbox" value="' . $transport . '" ' .
checked( true, in_array( $transport, $this->options['enabled_transports'] ), false ) . ' /> ' .
$transport . '</label></li>';
echo '</ol>' .
'<div class="clear">' .
'</div>' .
'</td>' .
'</tr>' .
'</tbody>' .
'</table>' .
'</div>' .
'</div>';
}
/**
* Render Log file meta box.
*/
function metabox_logfile_content( $data ) {
$last_backup = $this->get_last_backup();
$lines = @file( $last_backup['log'], FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES );
if ( !empty( $lines ) ) {
$header = array_shift( $lines );
$header = substr( $header, 9 );
$header = explode( "\t", $header );
foreach ( $lines as $i => $l ) {
$lines[$i] = explode( "\t", $l );
}
echo '<h4 class="column-posts">' . __( 'Log entries for the most recent backup attempt', $this->text_domain ) .
'</h4><table class="widefat fixed"><thead><tr>';
foreach ( $header as $i => $h )
echo '<th ' . ( ( 3 == $i )? '' : 'class="column-rel"' ) . '>' . esc_html( ucfirst( $h ) ) . '</th>';
echo '</tr></thead><tbody>';
foreach ( $lines as $line ) {
echo '<tr>';
foreach ( $line as $l )
echo '<td class="code">' . esc_html( $l ) . '</td>';
echo '</tr>';
}
echo '</tbody></table>';
}
else
echo '<p>' . __( 'There is no log file to display.', $this->text_domain ) . '</p>';
}
/**
* Validates and sanitizes user submitted options and saves them.
*/
function options_update() {
if ( isset( $_GET['action'] ) && 'update' == $_GET['action'] ) {
check_admin_referer( 'backup_options' );
// Validate and save chunk size.
$chunk_size = floatval( $_POST['chunk_size'] );
if ( 0 < $chunk_size && 0 == ($chunk_size * 10) % 5 )
$this->options['chunk_size'] = $chunk_size;
else
$this->messages['error'][] = __( 'The chunk size must be a multiple of 0.5 MB.', $this->text_domain );
// Save time limit.
$this->options['time_limit'] = absint( $_POST['time_limit'] );
// Save max resume attempts.
$this->options['backup_attempts'] = absint( $_POST['backup_attempts'] );
// Validate and save local and drive numbers.
$local_number = absint( $_POST['local_number'] );
if ( isset( $_POST['drive_number'] ) ) {
$drive_number = absint( $_POST['drive_number'] );
if ( 0 == $local_number && 0 == $drive_number )
$this->messages['error'][] = __(
'You need to store at least one local or Drive backup.', $this->text_domain
);
else {
$this->options['drive_number'] = $drive_number;
$this->options['local_number'] = $local_number;
}
}
else
if ( 0 >= $local_number )
$this->messages['error'][] = __( 'You need to store at least one local backup.', $this->text_domain );
else
$this->options['local_number'] = $local_number;
// Handle local folder change.
if (
!defined( 'BACKUP_LOCAL_FOLDER' ) && isset( $_POST['local_folder'] ) &&
$_POST['local_folder'] != $this->options['local_folder']
) {
$path = absolute_path( $_POST['local_folder'], ABSPATH );
if ( ! @file_exists( $path ) || @is_file( path_join( $path, '.backup' ) ) ) {
if ( !$this->create_dir( $path ) )
$this->messages['error'][] = sprintf(
__( "Could not create directory %s. You might want to create it manually and set the right permissions.",
$this->text_domain ), '<kbd>' . esc_html( $path ) . '</kbd>'
);
else {
$this->options['local_folder'] = $_POST['local_folder'];
$this->local_folder = $path;
}
}
else
$this->messages['error'][] = sprintf(
__( "The directory %s already exists.", $this->text_domain ), '<kbd>' . esc_html( $path ) . '</kbd>'
);
}
// Handle transports.
if ( !isset( $_POST['transports'] ) )
$this->messages['error'][] = __( 'You cannot have all HTTP transports disabled.', $this->text_domain );
else
$this->options['enabled_transports'] = $_POST['transports'];
// Handle sources and include list.
if ( !isset( $_POST['sources'] ) && empty( $_POST['include'] ) )
$this->messages['error'][] = __( 'Please make sure you select something to back up.', $this->text_domain );
else {
if ( isset( $_POST['sources'] ) )
$this->options['source_list'] = $_POST['sources'];
else
$this->options['source_list'] = array();
if ( !empty( $_POST['include'] ) ) {
$this->options['include_list'] = explode( ',', $_POST['include'] );
foreach ( $this->options['include_list'] as $i => $v )
$this->options['include_list'][$i] = trim( $v );
}
else
$this->options['include_list'] = array();
}
// Handle exlclude list.
if ( ! empty( $_POST['exclude'] ) ) {
$this->options['exclude_list'] = explode( ',', $_POST['exclude'] );
foreach ( $this->options['exclude_list'] as $i => $v )
$this->options['exclude_list'][$i] = trim( $v );
}
else
$this->options['exclude_list'] = array();
// Save Drive folder.