class-destination-gdrive.php
41.4 KB
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
<?php
/**
* Documentation: https://github.com/google/google-api-php-client, https://developers.google.com/drive/quickstart-php
*
* google-api-php-client Version 1.1.7
*/
class BackWPup_Pro_Destination_GDrive extends BackWPup_Destinations {
/**
* @var array
*/
private $gdrive_folders_cache = array();
/**
* @var $service Google_Service_Drive
*/
private $service = null;
/**
* @return array
*/
public function option_defaults() {
return array(
'gdriverefreshtoken' => '',
'gdrivemaxbackups' => 15,
'gdrivesyncnodelete' => true,
'gdriveusetrash' => true,
'gdrivedir' => trailingslashit( sanitize_file_name( get_bloginfo( 'name' ) ) )
);
}
/**
* @param $jobid
*/
public function edit_tab( $jobid ) {
if ( ! get_site_option( 'backwpup_cfg_googleclientid' ) || ! get_site_option( 'backwpup_cfg_googleclientsecret' ) ) {
BackWPup_Admin::message( sprintf( __( 'Looks like you haven’t set up any API keys yet. Head over to <a href="%s">Settings | API-Keys</a> and get Google Drive all set up, then come back here.', 'backwpup' ), network_admin_url( 'admin.php' ) . '?page=backwpupsettings#backwpup-tab-apikey' ), true );
}
//google authentication
set_site_transient( 'backwpup_gdrive_jobid_' . get_current_user_id(), $jobid, HOUR_IN_SECONDS );
$refresh_token = BackWPup_Encryption::decrypt( BackWPup_Option::get( $jobid, 'gdriverefreshtoken' ) );
BackWPup_Admin::display_messages();
?>
<h3 class="title"><?php esc_html_e( 'Login', 'backwpup' ); ?></h3>
<p></p>
<table class="form-table">
<tr>
<th scope="row"><?php esc_html_e( 'Authenticate', 'backwpup' ); ?></th>
<td><?php if ( empty( $refresh_token ) ) { ?>
<span style="color:red;"><?php esc_html_e( 'Not authenticated!', 'backwpup' ); ?></span><br/>
<?php } else { ?>
<span style="color:green;"><?php esc_html_e( 'Authenticated!', 'backwpup' ); ?></span><br/>
<?php } ?>
<a class="button secondary"
href="<?php echo admin_url( 'admin-ajax.php' ); ?>?action=backwpup_dest_gdrive"><?php esc_html_e( 'Reauthenticate', 'backwpup' ); ?></a>
</td>
</tr>
</table>
<h3 class="title"><?php esc_html_e( 'Backup settings', 'backwpup' ); ?></h3>
<p></p>
<table class="form-table">
<tr>
<th scope="row"><label
for="idgdrivedir"><?php esc_html_e( 'Folder in Google Drive', 'backwpup' ) ?></label></th>
<td>
<input id="idgdrivedir" name="gdrivedir" type="text"
value="<?php echo esc_attr( BackWPup_Option::get( $jobid, 'gdrivedir' ) ); ?>"
class="regular-text"/>
</td>
</tr>
<tr>
<th scope="row"><?php esc_html_e( 'File Deletion', 'backwpup' ); ?></th>
<td>
<?php
if ( BackWPup_Option::get( $jobid, 'backuptype' ) === 'archive' ) {
?>
<label for="idgdrivemaxbackups">
<input id="idgdrivemaxbackups" name="gdrivemaxbackups" type="number" min="0" step="1"
value="<?php echo esc_attr( BackWPup_Option::get( $jobid, 'gdrivemaxbackups' ) ); ?>"
class="small-text"/>
<?php _e( 'Number of files to keep in folder.', 'backwpup' ); ?>
</label>
<?php } else { ?>
<label for="idgdrivesyncnodelete">
<input class="checkbox" value="1"
type="checkbox" <?php checked( BackWPup_Option::get( $jobid, 'gdrivemaxbackups' ), true ); ?>
name="gdrivemaxbackups" id="idgdrivesyncnodelete"/>
<?php _e( 'Do not delete files while syncing to destination!', 'backwpup' ); ?>
</label>
<?php } ?>
<br/> <br/>
<label for="idgdriveusetrash">
<input
class="checkbox"
value="1"
type="checkbox"
<?php checked( BackWPup_Option::get( $jobid, 'gdriveusetrash' ), true ); ?>
name="gdriveusetrash"
id="idggdriveusetrash"/>
<?php esc_html_e( 'Consider using trash to delete files. If trash is not enabled, files will be deleted permanently.', 'backwpup' ); ?>
</label>
</td>
</tr>
</table>
<?php
}
/**
* Authentication over ajax
*/
public function edit_ajax() {
// on wizards
$wiz_data_id = '';
$wiz_data = array();
if ( ! empty( $_COOKIE['BackWPup_Wizard_ID'] ) ) {
$wiz_data_id = $_COOKIE['BackWPup_Wizard_ID'];
}
if ( empty( $wiz_data_id ) && ! empty( $_POST['BackWPup_Wizard_ID'] ) ) {
$wiz_data_id = $_POST['BackWPup_Wizard_ID'];
}
//start using sessions
if ( ! empty( $wiz_data_id ) ) {
$wiz_data = get_site_transient( 'BackWPup_Wiz_' . $wiz_data_id );
}
//google authentication
if ( ! class_exists( 'Google_Client' ) ) {
require_once BackWPup::get_plugin_data( 'plugindir' ) . '/vendor/Google/autoload.php';
}
$client = new Google_Client();
if ( BackWPup::get_plugin_data( 'cacert' ) ) {
$client->getIo()->setOptions( array( CURLOPT_CAINFO => BackWPup::get_plugin_data( 'cacert' ) ) );
} else {
$client->getIo()->setOptions( array( CURLOPT_SSL_VERIFYPEER => false ) );
}
$client->setApplicationName( BackWPup::get_plugin_data( 'name' ) );
$client->setClientId( get_site_option( 'backwpup_cfg_googleclientid' ) );
$client->setClientSecret( BackWPup_Encryption::decrypt( get_site_option( 'backwpup_cfg_googleclientsecret' ) ) );
$client->setScopes( array( 'https://www.googleapis.com/auth/drive' ) );
$client->setRedirectUri( admin_url( 'admin-ajax.php' ) . '?action=backwpup_dest_gdrive' );
$client->setAccessType( 'offline' );
if ( isset( $_GET['code'] ) ) {
if ( ! empty( $wiz_data ) ) {
try {
$client->authenticate( $_GET['code'] );
$access_token = $client->getAccessToken();
$access_token = json_decode( $access_token );
print_r( $access_token );
if ( ! empty( $access_token->refresh_token ) ) {
$wiz_data['job_settings']['gdriverefreshtoken'] = BackWPup_Encryption::encrypt( $access_token->refresh_token );
BackWPup_Admin::message( __( 'GDrive: Authenticated.', 'backwpup' ) );
} else {
$wiz_data['job_settings']['gdriverefreshtoken'] = '';
$client->revokeToken( $access_token->access_token );
BackWPup_Admin::message( __( 'GDrive: No refresh token received. Try to Authenticate again!', 'backwpup' ), true );
}
set_site_transient( 'BackWPup_Wiz_' . $wiz_data_id, $wiz_data, HOUR_IN_SECONDS );
wp_redirect( network_admin_url( 'admin.php' ) . '?page=backwpupwizard&BackWPup_Wizard_ID=' . $wiz_data_id . '&step=DEST-GDRIVE', 302 );
wp_die();
} catch ( Exception $e ) {
BackWPup_Admin::message( sprintf( __( 'GDrive API: %s', 'backwpup' ), $e->getMessage() ), true );
wp_redirect( network_admin_url( 'admin.php' ) . '?page=backwpupwizard&BackWPup_Wizard_ID=' . $wiz_data_id . '&step=DEST-GDRIVE', 302 );
wp_die();
}
}
// on edit job
$jobid = get_site_transient( 'backwpup_gdrive_jobid_' . get_current_user_id() );
if ( ! empty( $jobid ) ) {
try {
$client->authenticate( $_GET['code'] );
$access_token = $client->getAccessToken();
$access_token = json_decode( $access_token );
if ( ! empty( $access_token->refresh_token ) ) {
BackWPup_Option::update( $jobid, 'gdriverefreshtoken', BackWPup_Encryption::encrypt( $access_token->refresh_token ) );
BackWPup_Admin::message( __( 'GDrive: Authenticated.', 'backwpup' ) );
} else {
$client->revokeToken( $access_token->access_token );
BackWPup_Option::delete( $jobid, 'gdriverefreshtoken' );
BackWPup_Admin::message( __( 'GDrive: No refresh token received. Try to Authenticate again!', 'backwpup' ), true );
}
wp_redirect( network_admin_url( 'admin.php' ) . '?page=backwpupeditjob&jobid=' . $jobid . '&tab=dest-gdrive&_wpnonce=' . wp_create_nonce( 'edit-job' ), 302 );
wp_die();
} catch ( Exception $e ) {
BackWPup_Admin::message( sprintf( __( 'GDrive API: %s', 'backwpup' ), $e->getMessage() ), true );
BackWPup_Option::delete( $jobid, 'gdriverefreshtoken' );
wp_redirect( network_admin_url( 'admin.php' ) . '?page=backwpupeditjob&jobid=' . $jobid . '&tab=dest-gdrive&_wpnonce=' . wp_create_nonce( 'edit-job' ), 302 );
wp_die();
}
}
} else {
if ( ! empty( $wiz_data ) ) {
try {
$refresh_token = BackWPup_Encryption::decrypt( $wiz_data['job_settings']['gdriverefreshtoken'] );
if ( ! empty( $refresh_token ) ) {
$client->refreshToken( $refresh_token );
$client->revokeToken( $refresh_token );
$wiz_data['job_settings']['gdriverefreshtoken'] = '';
set_site_transient( 'BackWPup_Wiz_' . $wiz_data_id, $wiz_data, HOUR_IN_SECONDS );
}
$client->setRedirectUri( admin_url( 'admin-ajax.php' ) . '?action=backwpup_dest_gdrive' );
$auth_url = $client->createAuthUrl();
wp_redirect( $auth_url, 302 );
wp_die();
} catch ( Exception $e ) {
BackWPup_Admin::message( sprintf( __( 'GDrive API: %s', 'backwpup' ), $e->getMessage() ), true );
$wiz_data['job_settings']['gdriverefreshtoken'] = '';
set_site_transient( 'BackWPup_Wiz_' . $wiz_data_id, $wiz_data, HOUR_IN_SECONDS );
wp_redirect( network_admin_url( 'admin.php' ) . '?page=backwpupwizard&BackWPup_Wizard_ID=' . $wiz_data_id . '&step=DEST-GDRIVE', 302 );
wp_die();
}
}
$jobid = get_site_transient( 'backwpup_gdrive_jobid_' . get_current_user_id() );
if ( ! empty( $jobid ) ) {
try {
$refresh_token = BackWPup_Encryption::decrypt( BackWPup_Option::get( $jobid, 'gdriverefreshtoken' ) );
if ( ! empty( $refresh_token ) ) {
$client->refreshToken( $refresh_token );
$client->revokeToken( $refresh_token );
BackWPup_Option::delete( $jobid, 'gdriverefreshtoken' );
}
$client->setRedirectUri( admin_url( 'admin-ajax.php' ) . '?action=backwpup_dest_gdrive' );
$auth_url = $client->createAuthUrl();
wp_redirect( $auth_url, 302 );
wp_die();
} catch ( Exception $e ) {
BackWPup_Admin::message( sprintf( __( 'GDrive API: %s', 'backwpup' ), $e->getMessage() ), true );
BackWPup_Option::delete( $jobid, 'gdriverefreshtoken' );
wp_redirect( network_admin_url( 'admin.php' ) . '?page=backwpupeditjob&jobid=' . $jobid . '&tab=dest-gdrive&_wpnonce=' . wp_create_nonce( 'edit-job' ), 302 );
wp_die();
}
}
}
}
/**
* @param $jobid
*
* @return string|void
*/
public function edit_form_post_save( $jobid ) {
BackWPup_Option::update( $jobid, 'gdrivesyncnodelete', ! empty( $_POST['gdrivesyncnodelete'] ) );
BackWPup_Option::update( $jobid, 'gdriveusetrash', ! empty( $_POST['gdriveusetrash'] ) );
BackWPup_Option::update( $jobid, 'gdrivemaxbackups', ! empty( $_POST['gdrivemaxbackups'] ) ? absint( $_POST['gdrivemaxbackups'] ) : 0 );
$_POST['gdrivedir'] = untrailingslashit( str_replace( '//', '/', str_replace( '\\', '/', trim( stripslashes( $_POST['gdrivedir'] ) ) ) ) );
if ( substr( $_POST['gdrivedir'], 0, 1 ) != '/' ) {
$_POST['gdrivedir'] = '/' . $_POST['gdrivedir'];
}
BackWPup_Option::update( $jobid, 'gdrivedir', $_POST['gdrivedir'] );
}
/**
* @param $job_settings
*/
public function wizard_page( array $job_settings ) {
if ( ! get_site_option( 'backwpup_cfg_googleclientid' ) || ! get_site_option( 'backwpup_cfg_googleclientsecret' ) ) {
BackWPup_Admin::message( sprintf( __( 'Looks like you haven’t set up any API keys yet. Head over to <a href="%s">Settings | API-Keys</a> and get Google Drive all set up, then come back here.', 'backwpup' ), network_admin_url( 'admin.php' ) . '?page=backwpupsettings#backwpup-tab-apikey' ), true );
}
BackWPup_Admin::display_messages();
?>
<table class="form-table">
<tr>
<td>
<fieldset>
<?php
//display if not automatized
if ( empty( $job_settings['gdriverefreshtoken'] ) ) { ?>
<strong><?php esc_html_e( 'Login:', 'backwpup' ); ?></strong>
<span style="color:red;"><?php esc_html_e( 'Not authenticated!', 'backwpup' ); ?></span>
<a class="button secondary"
href="<?php echo admin_url( 'admin-ajax.php' ); ?>?action=backwpup_dest_gdrive"><?php esc_html_e( 'Authenticate', 'backwpup' ); ?></a>
<?php } else { ?>
<strong><?php esc_html_e( 'Login:', 'backwpup' ); ?></strong>
<span style="color:green;"><?php esc_html_e( 'Authenticated!', 'backwpup' ); ?></span>
<a class="button secondary"
href="<?php echo admin_url( 'admin-ajax.php' ); ?>?action=backwpup_dest_gdrive"><?php esc_html_e( 'Reauthenticate', 'backwpup' ); ?></a>
<br/>
<br/>
<strong><label
for="idgdrivedir"><?php esc_html_e( 'Folder:', 'backwpup' ); ?></label></strong>
<br/>
<input name="gdrivedir" id="idgdrivedir" type="text"
value="<?php echo esc_attr( $job_settings['gdrivedir'] ); ?>"
class="user large-text"/><br/>
<?php
if ( $job_settings['backuptype'] === 'archive' ) { ?>
<label for="idgdrivemaxbackups">
<input
name="gdrivemaxbackups"
id="idgdrivemaxbackups"
type="text"
size="3"
value="<?php echo esc_attr( $job_settings['gdrivemaxbackups'] ); ?>"
class="small-text"
title="<?php esc_attr_e( 'Oldest files will be deleted first.', 'backwpup' ); ?>"
/>
<?php esc_html_e( 'Number of files to keep in folder.', 'backwpup' ); ?></label>
<?php } else { ?>
<label for="idgdrivesyncnodelete"><input class="checkbox" value="1"
type="checkbox" <?php checked( $job_settings['gdrivesyncnodelete'], true ); ?>
name="gdrivesyncnodelete"
id="idgdrivesyncnodelete"/> <?php esc_html_e( 'Do not delete files while syncing to destination!', 'backwpup' ); ?>
</label>
<?php
}
} ?>
</fieldset>
</td>
</tr>
</table>
<?php
}
/**
* @param $job_settings
*
* @return mixed
*/
public function wizard_save( array $job_settings ) {
if ( isset( $_POST['gdrivedir'] ) ) {
$_POST['gdrivedir'] = untrailingslashit( str_replace( '//', '/', str_replace( '\\', '/', trim( sanitize_text_field( $_POST['gdrivedir'] ) ) ) ) );
if ( substr( $_POST['gdrivedir'], 0, 1 ) != '/' ) {
$_POST['gdrivedir'] = '/' . $_POST['gdrivedir'];
}
$job_settings['gdrivedir'] = $_POST['gdrivedir'];
}
$job_settings['gdrivesyncnodelete'] = ! empty( $_POST['gdrivesyncnodelete'] );
if ( isset( $_POST['gdrivemaxbackups'] ) ) {
$job_settings['gdrivemaxbackups'] = absint( $_POST['gdrivemaxbackups'] );
}
return $job_settings;
}
/**
* @param $jobdest
* @param $backupfile
*/
public function file_delete( $jobdest, $backupfile ) {
if ( ! class_exists( 'Google_Client' ) ) {
require_once BackWPup::get_plugin_data( 'plugindir' ) . '/vendor/Google/autoload.php';
}
$files = get_site_transient( 'backwpup_' . strtolower( $jobdest ) );
list( $jobid, $dest ) = explode( '_', $jobdest );
if ( BackWPup_Option::get( $jobid, 'gdriverefreshtoken' ) ) {
try {
$client = new Google_Client();
if ( BackWPup::get_plugin_data( 'cacert' ) ) {
$client->getIo()->setOptions( array( CURLOPT_CAINFO => BackWPup::get_plugin_data( 'cacert' ) ) );
} else {
$client->getIo()->setOptions( array( CURLOPT_SSL_VERIFYPEER => false ) );
}
$client->setApplicationName( BackWPup::get_plugin_data( 'name' ) );
$client->setClientId( get_site_option( 'backwpup_cfg_googleclientid' ) );
$client->setClientSecret( BackWPup_Encryption::decrypt( get_site_option( 'backwpup_cfg_googleclientsecret' ) ) );
$client->setScopes( array( 'https://www.googleapis.com/auth/drive' ) );
$client->setAccessType( 'offline' );
$refresh_token = BackWPup_Encryption::decrypt( BackWPup_Option::get( $jobid, 'gdriverefreshtoken' ) );
$client->refreshToken( $refresh_token );
$service = new Google_Service_Drive( $client );
if ( BackWPup_Option::get( $jobid, 'gdriveusetrash' ) ) {
$service->files->trash( $backupfile );
} else {
$service->files->delete( $backupfile );
}
//update file list
foreach ( $files as $key => $file ) {
if ( is_array( $file ) && $file['file'] == $backupfile ) {
unset( $files[ $key ] );
}
}
} catch ( Exception $e ) {
BackWPup_Admin::message( 'Google Drive: ' . $e->getMessage() );
}
}
set_site_transient( 'backwpup_' . strtolower( $jobdest ), $files, YEAR_IN_SECONDS );
}
/**
* @param $jobdest
*
* @return mixed
*/
public function file_get_list( $jobdest ) {
return get_site_transient( 'backwpup_' . strtolower( $jobdest ) );
}
/**
* @param $job_object
*
* @return bool
*/
public function job_run_archive( BackWPup_Job $job_object ) {
if ( ! class_exists( 'Google_Client' ) ) {
require_once BackWPup::get_plugin_data( 'plugindir' ) . '/vendor/Google/autoload.php';
}
$job_object->substeps_todo = 2 + $job_object->backup_filesize;
if ( $job_object->steps_data[ $job_object->step_working ]['SAVE_STEP_TRY'] != $job_object->steps_data[ $job_object->step_working ]['STEP_TRY'] ) {
$job_object->log( sprintf( __( '%d. Try to send backup file to Google Drive …', 'backwpup' ), $job_object->steps_data[ $job_object->step_working ]['STEP_TRY'] ), E_USER_NOTICE );
}
try {
$client = new Google_Client();
if ( BackWPup::get_plugin_data( 'cacert' ) ) {
$client->getIo()->setOptions( array( CURLOPT_CAINFO => BackWPup::get_plugin_data( 'cacert' ) ) );
} else {
$client->getIo()->setOptions( array( CURLOPT_SSL_VERIFYPEER => false ) );
}
$client->setApplicationName( BackWPup::get_plugin_data( 'name' ) );
$client->setClientId( get_site_option( 'backwpup_cfg_googleclientid' ) );
$client->setClientSecret( BackWPup_Encryption::decrypt( get_site_option( 'backwpup_cfg_googleclientsecret' ) ) );
$client->setScopes( array( 'https://www.googleapis.com/auth/drive' ) );
$client->setAccessType( 'offline' );
$refresh_token = BackWPup_Encryption::decrypt( $job_object->job['gdriverefreshtoken'] );
$client->refreshToken( $refresh_token );
$this->service = new Google_Service_Drive( $client );
//get the folder id and create folder
if ( empty( $job_object->steps_data[ $job_object->step_working ]['folder_id'] ) ) {
$job_object->steps_data[ $job_object->step_working ]['folder_id'] = $this->get_folder_id( $job_object->job['gdrivedir'] );
}
// put the file
if ( $job_object->steps_data[ $job_object->step_working ]['SAVE_STEP_TRY'] != $job_object->steps_data[ $job_object->step_working ]['STEP_TRY'] && $job_object->substeps_done < $job_object->backup_filesize ) {
$job_object->log( __( 'Uploading to Google Drive …', 'backwpup' ) );
}
if ( $job_object->substeps_done < $job_object->backup_filesize ) {
// Get resumable session url
if ( empty ( $job_object->steps_data[ $job_object->step_working ]['resumable_uri'] ) ) {
$post_data = new stdClass();
$post_data->title = $job_object->backup_file;
$post_data->mimeType = $job_object->get_mime_type( $job_object->backup_folder . $job_object->backup_file );
if ( $job_object->steps_data[ $job_object->step_working ]['folder_id'] !== 'root' ) {
$post_data_parent = new stdClass();
$post_data_parent->kind = 'drive#fileLink';
$post_data_parent->id = $job_object->steps_data[ $job_object->step_working ]['folder_id'];
$post_data->parents = array( $post_data_parent );
}
$post_fields = json_encode( $post_data );
$ch = curl_init();
curl_setopt(
$ch, CURLOPT_URL, 'https://www.googleapis.com/upload/drive/v2/files?uploadType=resumable'
);
curl_setopt( $ch, CURLOPT_POST, TRUE );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $post_fields );
curl_setopt( $ch, CURLOPT_HEADER, true );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
if ( BackWPup::get_plugin_data( 'cacert' ) ) {
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, true );
curl_setopt( $ch, CURLOPT_CAINFO, BackWPup::get_plugin_data( 'cacert' ) );
curl_setopt( $ch, CURLOPT_CAPATH, dirname( BackWPup::get_plugin_data( 'cacert' ) ) );
} else {
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
}
$access_token = json_decode( $client->getAccessToken() );
curl_setopt(
$ch, CURLOPT_HTTPHEADER, array(
'Authorization: Bearer ' . $access_token->access_token,
'Content-Length: ' . strlen( $post_fields ),
'X-Upload-Content-Type: ' . $job_object->get_mime_type(
$job_object->backup_folder . $job_object->backup_file
),
'X-Upload-Content-Length: ' . $job_object->backup_filesize,
'Content-Type: application/json; charset=UTF-8'
)
);
$response = curl_exec( $ch );
$curlgetinfo = curl_getinfo( $ch );
curl_close( $ch );
if ( $curlgetinfo['http_code'] == 200 || $curlgetinfo['http_code'] == 201 ) {
if ( preg_match( '/Location:(.*?)\r/i', $response, $matches ) ) {
$job_object->steps_data[ $job_object->step_working ]['resumable_uri'] = trim(
$matches[1]
);
}
}
// error checking
if ( empty( $job_object->steps_data[ $job_object->step_working ]['resumable_uri'] ) ) {
$job_object->log(
__( 'Could not create resumable file transfer to Google Drive', 'backwpup' ), E_USER_ERROR
);
return false;
}
} else {
//get actual position
$ch = curl_init();
curl_setopt(
$ch, CURLOPT_URL, $job_object->steps_data[ $job_object->step_working ]['resumable_uri']
);
curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, 'PUT' );
curl_setopt( $ch, CURLOPT_HEADER, true );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
if ( BackWPup::get_plugin_data( 'cacert' ) ) {
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, true );
curl_setopt( $ch, CURLOPT_CAINFO, BackWPup::get_plugin_data( 'cacert' ) );
curl_setopt( $ch, CURLOPT_CAPATH, dirname( BackWPup::get_plugin_data( 'cacert' ) ) );
} else {
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
}
$access_token = json_decode( $client->getAccessToken() );
curl_setopt(
$ch, CURLOPT_HTTPHEADER, array(
'Authorization: Bearer ' . $access_token->access_token,
'Content-Length: 0',
'Content-Range: bytes */' . $job_object->backup_filesize
)
);
$response = curl_exec( $ch );
$curlgetinfo = curl_getinfo( $ch );
curl_close( $ch );
if ( $curlgetinfo['http_code'] == 308
&& preg_match(
'/Range:(.*?)\r/i', $response, $matches
)
) {
$range = trim( $matches[1] );
$ranges = explode( '-', $range );
$job_object->substeps_done = $ranges[1] + 1;
} else {
$job_object->log(
__( 'Can not resume transfer backup to Google Drive!', 'backwpup' ), E_USER_ERROR
);
return false;
}
}
//Upload in chunks
$chunk_size = 4194304; //4194304 = 4MB
$created_file = null;
if ( $file_handel = fopen( $job_object->backup_folder . $job_object->backup_file, 'rb' ) ) {
//seek to file pos
if ( ! empty( $job_object->substeps_done ) ) {
fseek( $file_handel, $job_object->substeps_done );
}
while ( $data_chunk = fread( $file_handel, $chunk_size ) ) {
$chunk_upload_start = microtime( true );
$ch = curl_init();
curl_setopt(
$ch, CURLOPT_URL, $job_object->steps_data[ $job_object->step_working ]['resumable_uri']
);
curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, 'PUT' );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $data_chunk );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
if ( BackWPup::get_plugin_data( 'cacert' ) ) {
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, true );
curl_setopt( $ch, CURLOPT_CAINFO, BackWPup::get_plugin_data( 'cacert' ) );
curl_setopt( $ch, CURLOPT_CAPATH, dirname( BackWPup::get_plugin_data( 'cacert' ) ) );
} else {
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
}
$access_token = json_decode( $client->getAccessToken() );
$end_pos = $job_object->substeps_done + strlen( $data_chunk ) - 1;
curl_setopt(
$ch, CURLOPT_HTTPHEADER, array(
'Authorization: Bearer' . $access_token->access_token,
'Content-Length: ' . strlen( $data_chunk ),
'Content-Range: bytes ' . $job_object->substeps_done . '-' . $end_pos . '/' . $job_object->backup_filesize
)
);
$response = curl_exec( $ch );
$curlgetinfo = curl_getinfo( $ch );
curl_close( $ch );
$chunk_upload_time = microtime( true ) - $chunk_upload_start;
if ( $curlgetinfo['http_code'] == 200 || $curlgetinfo['http_code'] == 201 || $curlgetinfo['http_code'] == 308 ) {
$created_file = json_decode( $response );
$job_object->substeps_done = $end_pos + 1;
if ( $curlgetinfo['http_code'] == 308 ) {
$time_remaining = $job_object->do_restart_time();
//calc next chunk
if ( $time_remaining < $chunk_upload_time ) {
$chunk_size = floor( $chunk_size / $chunk_upload_time * ( $time_remaining - 3 ) );
if ( $chunk_size < 0 ) {
$chunk_size = 1024;
}
if ( $chunk_size > 4194304 ) {
$chunk_size = 4194304;
}
}
}
$job_object->update_working_data();
} else {
$job_object->log(
sprintf(
__( 'Error transfering file chunks to %s.', 'backwpup' ),
__( 'Google Drive', 'backwpup' )
), E_USER_WARNING
);
return false;
}
}
}
fclose( $file_handel );
} else {
$job_object->log( __( 'Can not open source file for transfer.', 'backwpup' ), E_USER_ERROR );
return false;
}
if ( is_object( $created_file ) && isset( $created_file->id ) && $created_file->fileSize == $job_object->backup_filesize ) {
if ( ! empty( $job_object->job['jobid'] ) ) {
BackWPup_Option::update( $job_object->job['jobid'], 'lastbackupdownloadurl', str_replace( '&gd=true', '', $created_file->downloadUrl ) );
}
$job_object->substeps_done = 1 + $job_object->backup_filesize;
$job_object->log( sprintf( __( 'Backup transferred to %s', 'backwpup' ), $created_file->alternateLink ), E_USER_NOTICE );
} else {
if ( $created_file->fileSize != $job_object->backup_filesize ) {
$job_object->log( __( 'Uploaded file size and local file size don\'t match.', 'backwpup' ), E_USER_ERROR );
} else {
$job_object->log( sprintf( __( 'Error transfering backup to %s.', 'backwpup' ), __( 'Google Drive', 'backwpup' ) ), E_USER_ERROR );
}
return false;
}
$backupfilelist = array();
$filecounter = 0;
$files = array();
$metadata = $this->search_files( "'" . $job_object->steps_data[ $job_object->step_working ]['folder_id'] . "' in parents and mimeType = '" . $job_object->get_mime_type( $job_object->backup_folder . $job_object->backup_file ) . "' ", $this->service );
if ( is_array( $metadata ) ) {
foreach ( $metadata as $data ) {
$file = $data->title;
if ( $job_object->is_backup_archive( $file ) ) {
$backupfilelist[ strtotime( $data->modifiedDate ) ] = $data->id;
}
$files[ $filecounter ]['folder'] = $job_object->job['gdrivedir'];
$files[ $filecounter ]['file'] = $data->id;
$files[ $filecounter ]['filename'] = $data->title;
$files[ $filecounter ]['downloadurl'] = str_replace( '&gd=true', '', $data->downloadUrl );
$files[ $filecounter ]['filesize'] = $data->fileSize;
$files[ $filecounter ]['time'] = strtotime( $data->modifiedDate ) + ( get_option( 'gmt_offset' ) * 3600 );
$filecounter ++;
}
}
if ( $job_object->job['gdrivemaxbackups'] > 0 && is_object( $this->service ) ) { //Delete old backups
if ( count( $backupfilelist ) > $job_object->job['gdrivemaxbackups'] ) {
ksort( $backupfilelist );
$numdeltefiles = 0;
while ( $file = array_shift( $backupfilelist ) ) {
if ( count( $backupfilelist ) < $job_object->job['gdrivemaxbackups'] ) {
break;
}
//delete files on Cloud
if ( $job_object->job['gdriveusetrash'] ) {
$this->service->files->trash( $file );
} else {
$this->service->files->delete( $file );
}
foreach ( $files as $key => $filedata ) {
if ( $filedata['file'] == $file ) {
unset( $files[ $key ] );
}
}
$numdeltefiles ++;
}
if ( $numdeltefiles > 0 ) {
$job_object->log( sprintf( _n( 'One file deleted from Google Drive', '%d files deleted on Google Drive', $numdeltefiles, 'backwpup' ), $numdeltefiles ), E_USER_NOTICE );
}
}
}
set_site_transient( 'backwpup_' . $job_object->job['jobid'] . '_gdrive', $files, YEAR_IN_SECONDS );
} catch ( Exception $e ) {
$job_object->log( E_USER_ERROR, sprintf( __( 'Google Drive API: %s', 'backwpup' ), $e->getMessage() ), $e->getFile(), $e->getLine() );
return false;
}
$job_object->substeps_done ++;
return true;
}
/**
* Returns folder id of path and creates it if it not exists
*
* @param $path
*
* @return string
*/
private function get_folder_id( $path ) {
$folder_id = 'root';
if ( $path != '/' ) {
$current_path = '';
$folder_names = explode( '/', trim( $path, '/' ) );
foreach ( $folder_names as $folder_name ) {
$create_folder = true;
$parent_path = $current_path;
$current_path .= '/' . $folder_name;
if ( isset( $this->gdrive_folders_cache[ $current_path ] ) ) {
$folder_id = $this->gdrive_folders_cache[ $current_path ];
continue;
}
$g_sub_folders = $this->search_files( "'$folder_id' in parents and mimeType = 'application/vnd.google-apps.folder'" );
foreach ( $g_sub_folders AS $g_sub_folder ) {
$this->gdrive_folders_cache[ $parent_path . '/' . (string) $g_sub_folder->title ] = $g_sub_folder->id;
if ( (string) $g_sub_folder->title === $folder_name ) {
$folder_id = $g_sub_folder->id;
$create_folder = false;
}
}
//create not existing folder
if ( $create_folder ) {
$file = new Google_Service_Drive_DriveFile();
$file->setTitle( $folder_name );
$file->setMimeType( 'application/vnd.google-apps.folder' );
if ( $folder_id != 'root' ) {
$parent_reference = new Google_Service_Drive_ParentReference();
$parent_reference->setId( $folder_id );
$file->setParents( array( $parent_reference ) );
}
$created_folder = $this->service->files->insert( $file, array( 'mimeType' => 'application/vnd.google-apps.folder' ) );
$folder_id = $created_folder->id;
$this->gdrive_folders_cache[ $current_path ] = $created_folder->id;
}
}
}
return $folder_id;
}
/**
* https://developers.google.com/drive/search-parameters
*
* @param $query string
*
* @return array of results
*/
private function search_files( $query ) {
$result = array();
$pageToken = null;
//exclude trashed from query
if ( ! empty( $query ) ) {
$query .= ' and trashed != true';
} else {
$query = 'trashed != true';
}
do {
$parameters = array( 'q' => $query, 'maxResults' => 1000 );
if ( $pageToken ) {
$parameters['pageToken'] = $pageToken;
}
$files = $this->service->files->listFiles( $parameters );
$result = array_merge( $result, $files->items );
$pageToken = ! empty( $files->getNextPageToken ) ? $files->getNextPageToken : '';
} while ( $pageToken );
return $result;
}
/**
* @param $job_settings
*
* @return bool
*/
public function can_run( array $job_settings ) {
if ( ! empty( $job_settings['gdriveaccesstoken'] ) ) {
$access_token = BackWPup_Encryption::decrypt( $job_settings['gdriveaccesstoken'] );
$access_token = json_decode( $access_token );
if ( $access_token->refresh_token ) {
BackWPup_Option::update( $job_settings['jobid'], 'gdriverefreshtoken', BackWPup_Encryption::encrypt( $access_token->refresh_token ) );
$job_settings['gdriverefreshtoken'] = BackWPup_Encryption::encrypt( $access_token->refresh_token );
}
BackWPup_Option::delete( $job_settings['jobid'], 'gdriveaccesstoken' );
}
if ( empty( $job_settings['gdriverefreshtoken'] ) ) {
return false;
}
return true;
}
public function can_sync() {
return true;
}
public function job_run_sync( BackWPup_Job $job_object ) {
if ( ! class_exists( 'Google_Client' ) ) {
require_once BackWPup::get_plugin_data( 'plugindir' ) . '/vendor/Google/autoload.php';
}
$job_object->substeps_todo = $job_object->count_folder + count( $job_object->additional_files_to_backup );
$job_object->substeps_done = 0;
if ( $job_object->steps_data[ $job_object->step_working ]['SAVE_STEP_TRY'] != $job_object->steps_data[ $job_object->step_working ]['STEP_TRY'] ) {
$job_object->log( sprintf( __( '%d. Try to sync files to Google Drive …', 'backwpup' ), $job_object->steps_data[ $job_object->step_working ]['STEP_TRY'] ), E_USER_NOTICE );
}
try {
$client = new Google_Client();
if ( BackWPup::get_plugin_data( 'cacert' ) ) {
$client->getIo()->setOptions( array( CURLOPT_CAINFO => BackWPup::get_plugin_data( 'cacert' ) ) );
} else {
$client->getIo()->setOptions( array( CURLOPT_SSL_VERIFYPEER => false ) );
}
$client->setApplicationName( BackWPup::get_plugin_data( 'name' ) );
$client->setClientId( get_site_option( 'backwpup_cfg_googleclientid' ) );
$client->setClientSecret( BackWPup_Encryption::decrypt( get_site_option( 'backwpup_cfg_googleclientsecret' ) ) );
$client->setScopes( array( 'https://www.googleapis.com/auth/drive' ) );
$client->setAccessType( 'offline' );
$refresh_token = BackWPup_Encryption::decrypt( $job_object->job['gdriverefreshtoken'] );
$client->refreshToken( $refresh_token );
$this->service = new Google_Service_Drive( $client );
$backup_root_folder_id = $this->get_folder_id( $job_object->job['gdrivedir'] );
//Sync files
if ( $job_object->steps_data[ $job_object->step_working ]['SAVE_STEP_TRY'] != $job_object->steps_data[ $job_object->step_working ]['STEP_TRY'] ) {
$job_object->log( __( 'Syncing changed files to Google Drive', 'backwpup' ) );
}
foreach ( $job_object->get_folders_to_backup() as $folder_to_backup_key => $folder_to_backup ) {
//generate dest folder name
$dest_folder_name = $job_object->job['gdrivedir'] . '/' . trim( $job_object->get_destination_path_replacement( $folder_to_backup ), '/' );
//get google folder id
$folder_id = $this->get_folder_id( $dest_folder_name );
//jump over if not the actual folder
if ( ! empty( $job_object->steps_data[ $job_object->step_working ]['on_folder_id'] ) && $job_object->steps_data[ $job_object->step_working ]['on_folder_id'] > $folder_to_backup_key ) {
$job_object->substeps_done ++;
continue;
}
//get files in folder
$gdrive_files = $this->search_files( "'" . $folder_id . "' in parents and mimeType != 'application/vnd.google-apps.folder'" );
// get local files
$files_in_folder = $job_object->get_files_in_folder( $folder_to_backup );
foreach ( $files_in_folder as $file_in_folder ) {
$dest_file_name = $job_object->job['gdrivedir'] . '/' . trim( $job_object->get_destination_path_replacement( $file_in_folder ), '/' );
foreach ( $gdrive_files as $gdrive_file_key => $gdrive_file ) {
//file exists on gdrive
if ( (string) $gdrive_file->title === basename( $file_in_folder ) ) {
//Upload file again if filesize not the same
if ( (int) $gdrive_file->fileSize !== filesize( $file_in_folder ) ) {
$this->service->files->update( $gdrive_file->id, $gdrive_file, array(
'data' => file_get_contents( $file_in_folder ),
'convert' => false,
'uploadType' => 'multipart',
'mimeType' => $job_object->get_mime_type( $file_in_folder )
) );
$job_object->log( sprintf( __( 'File %s updated on Google Drive', 'backwpup' ), $dest_file_name ) );
$job_object->do_restart_time();
}
//remove found file from array
unset( $gdrive_files[ $gdrive_file_key ] );
continue 2;
}
}
// if file not on gdrive upload it
$file = new Google_Service_Drive_DriveFile();
$file->setTitle( basename( $file_in_folder ) );
$file->setMimeType( $job_object->get_mime_type( $file_in_folder ) );
$parent = new Google_Service_Drive_ParentReference();
$parent->setId( $folder_id );
$file->setParents( array( $parent ) );
$this->service->files->insert( $file, array(
'data' => file_get_contents( $file_in_folder ),
'convert' => false,
'uploadType' => 'multipart',
'mimeType' => $job_object->get_mime_type( $file_in_folder )
) );
$job_object->log( sprintf( __( 'File %s uploaded to Google Drive', 'backwpup' ), $dest_file_name ) );
$job_object->do_restart_time();
}
//remove extra files from file list so that the file can updated and will not deleted
if ( $backup_root_folder_id === $folder_id && ! empty( $job_object->additional_files_to_backup ) ) {
foreach ( $job_object->additional_files_to_backup as $additional_file ) {
foreach ( $gdrive_files as $gdrive_file_key => $gdrive_file ) {
if ( (string) $gdrive_file->title === basename( $additional_file ) ) {
unset( $gdrive_files[ $gdrive_file_key ] );
}
}
}
}
//delete files/folder that not longer exists
if ( ! $job_object->job['gdrivesyncnodelete'] ) {
foreach ( $gdrive_files as $gdrive_file ) {
if ( empty( $gdrive_file->id ) ) {
continue;
}
if ( $job_object->job['gdriveusetrash'] ) {
$this->service->files->trash( $gdrive_file->id );
$job_object->log( sprintf( __( 'File %s moved to trash in Google Drive', 'backwpup' ), $dest_folder_name . $gdrive_file->title ) );
} else {
$this->service->files->delete( $gdrive_file->id );
$job_object->log( sprintf( __( 'File %s deleted permanently in Google Drive', 'backwpup' ), $dest_folder_name . $gdrive_file->title ) );
}
$job_object->do_restart_time();
}
//delete folder not work with special WP_* phates
//$gdrive_folders = $this->search_files( "'". $folder_id ."' in parents and mimeType = 'application/vnd.google-apps.folder'" );
//foreach( $gdrive_folders as $gdrive_folder ) {
// $folder_dir = trailingslashit( $folder_to_backup ) . (string) $gdrive_folder->title;
// $job_object->log( $folder_dir );
// if ( is_dir( $folder_dir ) ) {
// continue;
// }
// if ( $job_object->job[ 'gdriveusetrash' ] ) {
// $this->service->files->trash( $gdrive_folder->id );
// $job_object->log( sprintf( __( 'Folder %s moved to trash in Google Drive', 'backwpup' ), $dest_folder_name . $gdrive_folder->title ) );
// } else {
// $this->service->files->delete( $gdrive_folder->id );
// $job_object->log( sprintf( __( 'Folder %s deleted permanently in Google Drive', 'backwpup' ), $dest_folder_name . $gdrive_folder->title ) );
// }
//}
}
$job_object->steps_data[ $job_object->step_working ]['on_folder_id'] = $folder_to_backup_key;
$job_object->substeps_done ++;
$job_object->do_restart_time();
$job_object->update_working_data();
}
//sync extra files
if ( empty( $job_object->steps_data[ $job_object->step_working ]['on_file'] ) ) {
$job_object->steps_data[ $job_object->step_working ]['on_file'] = 0;
}
if ( ! empty( $job_object->additional_files_to_backup ) ) {
$gdrive_files = $this->search_files( "'$backup_root_folder_id' in parents and mimeType != 'application/vnd.google-apps.folder'" );
for ( $i = $job_object->steps_data[ $job_object->step_working ]['on_file']; $i < count( $job_object->additional_files_to_backup ); $i ++ ) {
$additional_file = $job_object->additional_files_to_backup[ $i ];
foreach ( $gdrive_files as $gdrive_file ) {
//file exists on gdrive
if ( (string) $gdrive_file->title === basename( $additional_file ) ) {
//Update exciting file
$responce = $this->service->files->update( $gdrive_file->id, $gdrive_file, array(
'data' => file_get_contents( $additional_file ),
'convert' => false,
'uploadType' => 'multipart',
'mimeType' => $job_object->get_mime_type( $additional_file )
) );
if ( $responce->fileSize == filesize( $additional_file ) ) {
$job_object->log( sprintf( __( 'Extra file %s updated on Google Drive', 'backwpup' ), $job_object->job['gdrivedir'] . '/' . basename( $additional_file ) ) );
}
$job_object->substeps_done ++;
$job_object->steps_data[ $job_object->step_working ]['on_file'] = $i + 1;
$job_object->do_restart_time();
$job_object->update_working_data();
continue 2;
}
}
$file = new Google_Service_Drive_DriveFile();
$file->setTitle( basename( $additional_file ) );
$file->setMimeType( $job_object->get_mime_type( $additional_file ) );
$parent = new Google_Service_Drive_ParentReference();
$parent->setId( $backup_root_folder_id );
$file->setParents( array( $parent ) );
$responce = $this->service->files->insert( $file, array(
'data' => file_get_contents( $additional_file ),
'convert' => false,
'uploadType' => 'multipart',
'mimeType' => $job_object->get_mime_type( $additional_file )
) );
if ( $responce->fileSize == filesize( $additional_file ) ) {
$job_object->log( sprintf( __( 'Extra file %s uploaded to Google Drive', 'backwpup' ), $job_object->job['gdrivedir'] . '/' . basename( $additional_file ) ) );
}
$job_object->substeps_done ++;
$job_object->steps_data[ $job_object->step_working ]['on_file'] = $i + 1;
$job_object->do_restart_time();
$job_object->update_working_data();
}
}
} catch ( Exception $e ) {
$job_object->log( E_USER_ERROR, sprintf( __( 'Google Drive API: %s', 'backwpup' ), $e->getMessage() ), $e->getFile(), $e->getLine() );
return false;
}
return true;
}
}