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
|
<?php /* Pre-populated variables coming into this script: $destination_settings $mode */
global $pb_hide_save; global $pb_hide_test; $pb_hide_save = true; $pb_hide_test = true;
$default_name = NULL;
set_include_path( pb_backupbuddy::plugin_path() . '/destinations/gdrive/' . PATH_SEPARATOR . get_include_path());
if ( 'add' == $mode ) { if ( 'auth_gdrive' != pb_backupbuddy::_POST( 'gaction' ) ) { ?> <ol> <li><a href="https://code.google.com/apis/console" target="_new" class="button secondary-button" style="vertical-align: 0;">Open Google API Console in a new window</a></li> <li>Expand "APIs & auth" from the left menu then select "APIs" beneath it</li> <li>Under "Browse APIs" find "Drive API" and click to turn it on (Important: Enable "Drive <b>API</b>" <i>not</i> "Drive SDK")</li> <li>Below "APIs" in the left menu select "Credentials"</li> <li>Click the "Create new Client ID" button</li> <li>Select Application Type of "Installed application" with the type of "Other"</li> <li>Click "Create Client ID"</li> <li>Copy & paste the 'Client ID' & 'Client Secret' below</li> </ol> <br><br> <h3>Google Drive Client ID for native application</h3> <form method="post" action="<?php echo pb_backupbuddy::ajax_url( 'destination_picker' ) . '&add=gdrive&callback_data=' . pb_backupbuddy::_GET( 'callback_data' ); ?>"> <input type="hidden" name="gaction" value="auth_gdrive"> <table class="form-table"> <tr> <th scope="row">Client ID</th> <td><input type="text" name="client_id" style="width: 100%; max-width: 720px;"></td> </tr> <tr> <th scope="row">Client Secret</th> <td><input type="text" name="client_secret" style="width: 100%; max-width: 720px;"></td> </tr> <tr> <th scope="row"> </th> <td><input class="button-primary" type="submit" value="Continue"></td> </tr> </table> </form> <?php return; } if ( 'auth_gdrive' == pb_backupbuddy::_POST( 'gaction' ) ) { require_once( pb_backupbuddy::plugin_path() . '/destinations/gdrive/Google/Client.php' ); require_once( pb_backupbuddy::plugin_path() . '/destinations/gdrive/Google/Http/MediaFileUpload.php' ); require_once( pb_backupbuddy::plugin_path() . '/destinations/gdrive/Google/Service/Drive.php' ); $client_id = trim( pb_backupbuddy::_POST( 'client_id' ) ); $client_secret = trim( pb_backupbuddy::_POST( 'client_secret' ) ); $redirect_uri = 'urn:ietf:wg:oauth:2.0:oob';
$client = new Google_Client(); $client->setClientId($client_id); $client->setClientSecret($client_secret); $client->setRedirectUri($redirect_uri); $client->setAccessType('offline'); // Required so that Google will return the refresh token. $client->addScope("https://www.googleapis.com/auth/drive"); $service = new Google_Service_Drive($client); $auth_code = pb_backupbuddy::_POST( 'auth_code' ); if ( '' != $auth_code ) { try { $result = $client->authenticate( $auth_code ); } catch (Exception $e) { pb_backupbuddy::alert( 'Error Authenticating: ' . $e->getMessage() . ' Please go back, check codes, and try again.' ); return; } /* echo '<br>'; echo 'token: ' . $client->getAccessToken(); echo '<br><br>'; */ $destination_settings['tokens'] = $client->getAccessToken(); } if ( '' == $destination_settings['tokens'] ) { ?> <ol> <li><a href="<?php echo $client->createAuthUrl(); ?>" target="_new" class="button secondary-button" style="vertical-align: 0;">Click here to authorize BackupBuddy access to your Google Drive</a></li> <li>Copy & paste the provided code into the box below</li> </ol> <br> <form method="post"> <input type="hidden" name="gaction" value="auth_gdrive"> <input type="hidden" name="client_id" value="<?php echo $client_id; ?>"> <input type="hidden" name="client_secret" value="<?php echo $client_secret; ?>"> <table class="form-table"> <tr> <th scope="row">Auth Code</th> <td><input type="text" name="auth_code" style="width: 100%; max-width: 720px;"></td> </tr> <tr> <th scope="row"> </th> <td><input class="button-primary" type="submit" value="Continue"></td> </tr> </table> </form> <?php return; } } }
// Editing or add mode authed. Show settings. $pb_hide_test = false; $pb_hide_save = false;
if ( 'add' == $mode ) { $tokens = base64_encode( $destination_settings['tokens'] ); $default_name = 'My Google Drive'; } else { $tokens = NULL; $client_id = NULL; $client_secret = NULL; }
$settings_form->add_setting( array( 'type' => 'text', 'name' => 'title', 'title' => __( 'Destination name', 'it-l10n-backupbuddy' ), 'tip' => __( 'Name of the new destination to create. This is for your convenience only.', 'it-l10n-backupbuddy' ), 'rules' => 'required|string[1-45]', 'default' => $default_name, ) );
$settings_form->add_setting( array( 'type' => 'text', 'name' => 'max_burst', 'title' => __( 'Send per burst', 'it-l10n-backupbuddy' ), 'tip' => __( '[Default 10] - This is the amount of data that will be sent per burst within a single PHP page load. Reduce if hitting PHP memory limits. Chunking time limits will only be checked between bursts. Lower burst size if timeouts occur before chunking checks trigger.', 'it-l10n-backupbuddy' ), 'rules' => 'required|int[0-9999999]', 'css' => 'width: 50px;', 'after' => ' MB', ) );
$settings_form->add_setting( array( 'type' => 'text', 'name' => 'max_time', 'title' => __( 'Max time per chunk', 'it-l10n-backupbuddy' ), 'tip' => __( '[Example: 30] - Enter 0 for no limit (aka no chunking). This is the maximum number of seconds per chunk. If this time is exceeded when a burst finishes then the next portion will be transferred on a new page load.', 'it-l10n-backupbuddy' ), 'rules' => 'required|int[0-9999999]', 'css' => 'width: 50px;', 'after' => ' secs', ) );
$settings_form->add_setting( array( 'type' => 'hidden', 'name' => 'tokens', 'default' => $tokens, ) ); $settings_form->add_setting( array( 'type' => 'hidden', 'name' => 'client_id', 'default' => $client_id, ) ); $settings_form->add_setting( array( 'type' => 'hidden', 'name' => 'client_secret', 'default' => $client_secret, ) );
|