class-destination-rsc.php
18.9 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
<?php
// Rackspace OpenCloud SDK v1.12.2
// http://www.rackspace.com/cloud/files/
// https://github.com/rackspace/php-opencloud
/**
*
*/
class BackWPup_Destination_RSC extends BackWPup_Destinations {
/**
* @return array
*/
public function option_defaults() {
return array( 'rscusername' => '', 'rscapikey' => '', 'rsccontainer' => '', 'rscregion' => 'DFW', 'rscdir' => trailingslashit( sanitize_file_name( get_bloginfo( 'name' ) ) ), 'rscmaxbackups' => 15, 'rscsyncnodelete' => TRUE );
}
/**
* Get Auht url by region code
*
* @param $region string region code
* @return string
*/
public static function get_auth_url_by_region( $region ) {
$region = strtoupper( $region );
if ( $region === 'LON' ) {
return RACKSPACE_UK;
}
return RACKSPACE_US;
}
/**
* @param $jobid
*/
public function edit_tab( $jobid ) {
?>
<h3 class="title"><?php esc_html_e( 'Rack Space Cloud Keys', 'backwpup' ); ?></h3>
<p></p>
<table class="form-table">
<tr>
<th scope="row"><label for="rscusername"><?php esc_html_e( 'Username', 'backwpup' ); ?></label></th>
<td>
<input id="rscusername" name="rscusername" type="text" value="<?php echo esc_attr( BackWPup_Option::get( $jobid, 'rscusername' ) );?>" class="regular-text" autocomplete="off" />
</td>
</tr>
<tr>
<th scope="row"><label for="rscapikey"><?php esc_html_e( 'API Key', 'backwpup' ); ?></label></th>
<td>
<input id="rscapikey" name="rscapikey" type="password" value="<?php echo esc_attr( BackWPup_Encryption::decrypt( BackWPup_Option::get( $jobid, 'rscapikey' ) ) ); ?>" class="regular-text" autocomplete="off" />
</td>
</tr>
</table>
<h3 class="title"><?php esc_html_e( 'Select region', 'backwpup' ); ?></h3>
<p></p>
<table class="form-table">
<tr>
<th scope="row"><label for="rscregion"><?php esc_html_e( 'Rackspace Cloud Files Region', 'backwpup' ); ?></label></th>
<td>
<select name="rscregion" id="rscregion" title="<?php esc_html_e( 'Rackspace Cloud Files Region', 'backwpup' ); ?>">
<option value="DFW" <?php selected( 'DFW', BackWPup_Option::get( $jobid, 'rscregion' ), TRUE ) ?>><?php esc_html_e( 'Dallas (DFW)', 'backwpup' ); ?></option>
<option value="ORD" <?php selected( 'ORD', BackWPup_Option::get( $jobid, 'rscregion' ), TRUE ) ?>><?php esc_html_e( 'Chicago (ORD)', 'backwpup' ); ?></option>
<option value="SYD" <?php selected( 'SYD', BackWPup_Option::get( $jobid, 'rscregion' ), TRUE ) ?>><?php esc_html_e( 'Sydney (SYD)', 'backwpup' ); ?></option>
<option value="LON" <?php selected( 'LON', BackWPup_Option::get( $jobid, 'rscregion' ), TRUE ) ?>><?php esc_html_e( 'London (LON)', 'backwpup' ); ?></option>
<option value="IAD" <?php selected( 'IAD', BackWPup_Option::get( $jobid, 'rscregion' ), TRUE ) ?>><?php esc_html_e( 'Northern Virginia (IAD)', 'backwpup' ); ?></option>
<option value="HKG" <?php selected( 'HKG', BackWPup_Option::get( $jobid, 'rscregion' ), TRUE ) ?>><?php esc_html_e( 'Hong Kong (HKG)', 'backwpup' ); ?></option>
</select><br/>
</td>
</tr>
<tr>
<th scope="row"><label for="rsccontainerselected"><?php esc_html_e( 'Container selection', 'backwpup' ); ?></label></th>
<td>
<input id="rsccontainerselected" name="rsccontainerselected" type="hidden" value="<?php echo esc_attr( BackWPup_Option::get( $jobid, 'rsccontainer' ) ); ?>" />
<?php if ( BackWPup_Option::get( $jobid, 'rscusername' ) && BackWPup_Option::get( $jobid, 'rscapikey' ) ) $this->edit_ajax( array(
'rscusername' => BackWPup_Option::get( $jobid, 'rscusername' ),
'rscregion' => BackWPup_Option::get( $jobid, 'rscregion' ),
'rscapikey' => BackWPup_Encryption::decrypt( BackWPup_Option::get( $jobid, 'rscapikey' ) ),
'rscselected' => BackWPup_Option::get( $jobid, 'rsccontainer' )
) ); ?>
</td>
</tr>
<tr>
<th scope="row"><label for="idnewrsccontainer"><?php esc_html_e( 'Create a new container', 'backwpup' ); ?></label></th>
<td>
<input id="idnewrsccontainer" name="newrsccontainer" type="text" value="" class="text" />
</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="idrscdir"><?php esc_html_e( 'Folder in bucket', 'backwpup' ); ?></label></th>
<td>
<input id="idrscdir" name="rscdir" type="text" value="<?php echo esc_attr( BackWPup_Option::get( $jobid, 'rscdir' ) ); ?>" 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="idrscmaxbackups">
<input id="idrscmaxbackups" name="rscmaxbackups" type="number" min="0" step="1" value="<?php echo esc_attr( BackWPup_Option::get( $jobid, 'rscmaxbackups' ) ); ?>" class="small-text" />
<?php esc_html_e( 'Number of files to keep in folder.', 'backwpup' ); ?>
</label>
<?php } else { ?>
<label for="idrscsyncnodelete">
<input class="checkbox" value="1" type="checkbox" <?php checked( BackWPup_Option::get( $jobid, 'rscsyncnodelete' ), true ); ?> name="rscsyncnodelete" id="idrscsyncnodelete" />
<?php esc_html_e( 'Do not delete files while syncing to destination!', 'backwpup' ); ?>
</label>
<?php } ?>
</td>
</tr>
</table>
<?php
}
/**
* @param $id
*/
public function edit_form_post_save( $id ) {
BackWPup_Option::update( $id, 'rscusername', sanitize_text_field( $_POST[ 'rscusername' ] ) );
BackWPup_Option::update( $id, 'rscapikey', sanitize_text_field( $_POST[ 'rscapikey' ] ) );
BackWPup_Option::update( $id, 'rsccontainer', isset( $_POST[ 'rsccontainer' ] ) ? sanitize_text_field( $_POST[ 'rsccontainer' ] ) : '' );
BackWPup_Option::update( $id, 'rscregion', ! empty( $_POST[ 'rscregion' ] ) ? sanitize_text_field( $_POST[ 'rscregion' ] ) : 'DFW' );
$_POST[ 'rscdir' ] = trailingslashit( str_replace( '//', '/', str_replace( '\\', '/', trim( sanitize_text_field( $_POST[ 'rscdir' ] ) ) ) ) );
if ( substr( $_POST[ 'rscdir' ], 0, 1 ) === '/' )
$_POST[ 'rscdir' ] = substr( $_POST[ 'rscdir' ], 1 );
if ( $_POST[ 'rscdir' ] === '/' )
$_POST[ 'rscdir' ] = '';
BackWPup_Option::update( $id, 'rscdir', $_POST[ 'rscdir' ] );
BackWPup_Option::update( $id, 'rscmaxbackups', ! empty( $_POST[ 'rscmaxbackups' ] ) ? absint( $_POST[ 'rscmaxbackups' ] ) : 0 );
BackWPup_Option::update( $id, 'rscsyncnodelete', ! empty( $_POST[ 'rscsyncnodelete' ] ) );
if ( ! empty( $_POST[ 'rscusername' ] ) && ! empty( $_POST[ 'rscapikey' ] ) && ! empty( $_POST[ 'newrsccontainer' ] ) ) {
try {
$conn = new OpenCloud\Rackspace(
self::get_auth_url_by_region( $_POST[ 'rscregion' ] ),
array(
'username' => $_POST[ 'rscusername' ],
'apiKey' => $_POST[ 'rscapikey' ]
));
$ostore = $conn->objectStoreService( 'cloudFiles', sanitize_text_field( $_POST[ 'rscregion' ] ), 'publicURL' );
$ostore->createContainer( $_POST[ 'newrsccontainer' ] );
BackWPup_Option::update( $id, 'rsccontainer', sanitize_text_field( $_POST[ 'newrsccontainer' ] ) );
BackWPup_Admin::message( sprintf( __( 'Rackspace Cloud container "%s" created.', 'backwpup' ), esc_html( sanitize_text_field( $_POST[ 'newrsccontainer' ] ) ) ) );
}
catch ( Exception $e ) {
BackWPup_Admin::message( sprintf( __( 'Rackspace Cloud API: %s', 'backwpup' ), $e->getMessage() ), TRUE );
}
}
}
/**
* @param $jobdest
* @param $backupfile
*/
public function file_delete( $jobdest, $backupfile ) {
$files = get_site_transient( 'backwpup_'. strtolower( $jobdest ) );
list( $jobid, $dest ) = explode( '_', $jobdest );
if ( BackWPup_Option::get( $jobid, 'rscusername' ) && BackWPup_Option::get( $jobid, 'rscapikey' ) && BackWPup_Option::get( $jobid, 'rsccontainer' ) ) {
try {
$conn = new OpenCloud\Rackspace(
self::get_auth_url_by_region( BackWPup_Option::get( $jobid, 'rscregion' ) ),
array(
'username' => BackWPup_Option::get( $jobid, 'rscusername' ),
'apiKey' => BackWPup_Encryption::decrypt( BackWPup_Option::get( $jobid, 'rscapikey' ) )
));
$conn->
$ostore = $conn->objectStoreService( 'cloudFiles' , BackWPup_Option::get( $jobid, 'rscregion' ), 'publicURL');
$container = $ostore->getContainer( BackWPup_Option::get( $jobid, 'rsccontainer' ) );
$fileobject = $container->getObject( $backupfile );
$fileobject->delete();
//update file list
foreach ( $files as $key => $file ) {
if ( is_array( $file ) && $file[ 'file' ] == $backupfile )
unset( $files[ $key ] );
}
}
catch ( Exception $e ) {
BackWPup_Admin::message( 'RSC: ' . $e->getMessage(), TRUE );
}
}
set_site_transient( 'backwpup_'. strtolower( $jobdest ), $files, YEAR_IN_SECONDS );
}
/**
* @param $jobid
* @param $get_file
*/
public function file_download( $jobid, $get_file ) {
try {
$conn = new OpenCloud\Rackspace(
self::get_auth_url_by_region( BackWPup_Option::get( $jobid, 'rscregion' ) ),
array(
'username' => BackWPup_Option::get( $jobid, 'rscusername' ),
'apiKey' => BackWPup_Encryption::decrypt( BackWPup_Option::get( $jobid, 'rscapikey' ) )
));
$ostore = $conn->objectStoreService( 'cloudFiles' , BackWPup_Option::get( $jobid, 'rscregion' ), 'publicURL');
$container = $ostore->getContainer( BackWPup_Option::get( $jobid, 'rsccontainer' ) );
$backupfile = $container->getObject( $get_file );
if ( $level = ob_get_level() ) {
for ( $i = 0; $i < $level; $i ++ ) {
ob_end_clean();
}
}
@set_time_limit( 300 );
nocache_headers();
header( 'Content-Description: File Transfer' );
header( 'Content-Type: ' . BackWPup_Job::get_mime_type( $get_file ) );
header( 'Content-Disposition: attachment; filename="' . basename( $get_file ) . '"' );
header( 'Content-Transfer-Encoding: binary' );
header( 'Content-Length: ' . $backupfile->getContentLength() );
echo $backupfile->getContent();
die();
}
catch ( Exception $e ) {
die( $e->getMessage() );
}
}
/**
* @param $jobdest
* @return mixed
*/
public function file_get_list( $jobdest ) {
return get_site_transient( 'backwpup_' . strtolower( $jobdest ) );
}
/**
* @param $job_object BAckWPup_Job
* @return bool
*/
public function job_run_archive( BackWPup_Job $job_object ) {
$job_object->substeps_todo = 2 + $job_object->backup_filesize;
$job_object->substeps_done = 0;
$job_object->log( sprintf( __( '%d. Trying to send backup file to Rackspace cloud …', 'backwpup' ), $job_object->steps_data[ $job_object->step_working ][ 'STEP_TRY' ] ), E_USER_NOTICE );
try {
$conn = new OpenCloud\Rackspace(
self::get_auth_url_by_region( $job_object->job[ 'rscregion' ] ),
array(
'username' => $job_object->job[ 'rscusername' ],
'apiKey' => BackWPup_Encryption::decrypt( $job_object->job[ 'rscapikey' ] )
));
//connect to cloud files
$ostore = $conn->objectStoreService( 'cloudFiles' , $job_object->job[ 'rscregion' ], 'publicURL' );
$container = $ostore->getContainer( $job_object->job[ 'rsccontainer' ] );
$job_object->log( sprintf(__( 'Connected to Rackspace cloud files container %s', 'backwpup' ), $job_object->job[ 'rsccontainer' ] ) );
}
catch ( Exception $e ) {
$job_object->log( E_USER_ERROR, sprintf( __( 'Rackspace Cloud API: %s', 'backwpup' ), $e->getMessage() ), $e->getFile(), $e->getLine() );
return FALSE;
}
try {
//Transfer Backup to Rackspace Cloud
$job_object->substeps_done = 0;
$job_object->log( __( 'Upload to Rackspace cloud started …', 'backwpup' ), E_USER_NOTICE );
if ( $handle = fopen( $job_object->backup_folder . $job_object->backup_file, 'rb' ) ) {
$uploded = $container->uploadObject( $job_object->job[ 'rscdir' ] . $job_object->backup_file, $handle );
fclose( $handle );
} else {
$job_object->log( __( 'Can not open source file for transfer.', 'backwpup' ), E_USER_ERROR );
return FALSE;
}
// $transfer = $container->setupObjectTransfer( array(
// 'name' => $job_object->job[ 'rscdir' ] . $job_object->backup_file,
// 'path' => $job_object->backup_folder . $job_object->backup_file,
// 'concurrency' => 1,
// 'partSize' => 4 * 1024 * 1024
// ) );
// $uploded = $transfer->upload();
if ( $uploded ) {
$job_object->log( __( 'Backup File transferred to RSC://', 'backwpup' ) . $job_object->job[ 'rsccontainer' ] . '/' . $job_object->job[ 'rscdir' ] . $job_object->backup_file, E_USER_NOTICE );
$job_object->substeps_done = 1 + $job_object->backup_filesize;
if ( ! empty( $job_object->job[ 'jobid' ] ) ) {
BackWPup_Option::update( $job_object->job[ 'jobid' ], 'lastbackupdownloadurl', network_admin_url( 'admin.php' ) . '?page=backwpupbackups&action=downloadrsc&file=' . $job_object->job[ 'rscdir' ] . $job_object->backup_file . '&jobid=' . $job_object->job[ 'jobid' ] );
}
} else {
$job_object->log( __( 'Cannot transfer backup to Rackspace cloud.', 'backwpup' ), E_USER_ERROR );
return FALSE;
}
}
catch ( Exception $e ) {
$job_object->log( E_USER_ERROR, sprintf( __( 'Rackspace Cloud API: %s', 'backwpup' ), $e->getMessage() ), $e->getFile(), $e->getLine() );
return FALSE;
}
try {
$backupfilelist = array();
$filecounter = 0;
$files = array();
$objlist = $container->objectList( array( 'prefix' => $job_object->job[ 'rscdir' ] ) );
while ( $object = $objlist->next() ) {
$file = basename( $object->getName() );
if ( $job_object->job[ 'rscdir' ] . $file == $object->getName() ) { //only in the folder and not in complete bucket
if ( $job_object->is_backup_archive( $file ) )
$backupfilelist[ strtotime( $object->getLastModified() ) ] = $object;
}
$files[ $filecounter ][ 'folder' ] = "RSC://" . $job_object->job[ 'rsccontainer' ] . "/" . dirname( $object->getName() ) . "/";
$files[ $filecounter ][ 'file' ] = $object->getName();
$files[ $filecounter ][ 'filename' ] = basename( $object->getName() );
$files[ $filecounter ][ 'downloadurl' ] = network_admin_url( 'admin.php' ) . '?page=backwpupbackups&action=downloadrsc&file=' . $object->getName() . '&jobid=' . $job_object->job[ 'jobid' ];
$files[ $filecounter ][ 'filesize' ] = $object->getContentLength();
$files[ $filecounter ][ 'time' ] = strtotime( $object->getLastModified() );
$filecounter ++;
}
if ( ! empty( $job_object->job[ 'rscmaxbackups' ] ) && $job_object->job[ 'rscmaxbackups' ] > 0 ) { //Delete old backups
if ( count( $backupfilelist ) > $job_object->job[ 'rscmaxbackups' ] ) {
ksort( $backupfilelist );
$numdeltefiles = 0;
while ( $file = array_shift( $backupfilelist ) ) {
if ( count( $backupfilelist ) < $job_object->job[ 'rscmaxbackups' ] )
break;
foreach ( $files as $key => $filedata ) {
if ( $filedata[ 'file' ] == $file->getName() )
unset( $files[ $key ] );
}
$file->delete();
$numdeltefiles ++;
}
if ( $numdeltefiles > 0 )
$job_object->log( sprintf( _n( 'One file deleted on Rackspace cloud container.', '%d files deleted on Rackspace cloud container.', $numdeltefiles, 'backwpup' ), $numdeltefiles ), E_USER_NOTICE );
}
}
set_site_transient( 'backwpup_' . $job_object->job[ 'jobid' ] . '_rsc', $files, YEAR_IN_SECONDS );
}
catch ( Exception $e ) {
$job_object->log( E_USER_ERROR, sprintf( __( 'Rackspace Cloud API: %s', 'backwpup' ), $e->getMessage() ), $e->getFile(), $e->getLine() );
return FALSE;
}
$job_object->substeps_done ++;
return TRUE;
}
/**
* @param $job_settings array
* @return bool
*/
public function can_run( array $job_settings ) {
if ( empty( $job_settings[ 'rscusername'] ) )
return FALSE;
if ( empty( $job_settings[ 'rscapikey'] ) )
return FALSE;
if ( empty( $job_settings[ 'rsccontainer'] ) )
return FALSE;
return TRUE;
}
/**
*
*/
public function edit_inline_js() {
?>
<script type="text/javascript">
jQuery(document).ready(function ($) {
function rscgetcontainer() {
var data = {
action: 'backwpup_dest_rsc',
rscusername: $('#rscusername').val(),
rscapikey: $('#rscapikey').val(),
rscregion: $('#rscregion').val(),
rscselected: $('#rsccontainerselected').val(),
_ajax_nonce: $('#backwpupajaxnonce').val()
};
$.post(ajaxurl, data, function (response) {
$('#rsccontainererror').remove();
$('#rsccontainer').remove();
$('#rsccontainerselected').after(response);
});
}
$('#rscregion').change(function () {
rscgetcontainer();
});
$('#rscusername').backwpupDelayKeyup(function () {
rscgetcontainer();
});
$('#rscapikey').backwpupDelayKeyup(function () {
rscgetcontainer();
});
});
</script>
<?php
}
/**
* @param string $args
*/
public function edit_ajax( $args = '' ) {
$error = '';
if ( is_array( $args ) ) {
$ajax = FALSE;
} else {
if ( ! current_user_can( 'backwpup_jobs_edit' ) )
wp_die( -1 );
check_ajax_referer( 'backwpup_ajax_nonce' );
$args[ 'rscusername' ] = sanitize_text_field( $_POST[ 'rscusername' ] );
$args[ 'rscapikey' ] = sanitize_text_field( $_POST[ 'rscapikey' ] );
$args[ 'rscselected' ] = sanitize_text_field( $_POST[ 'rscselected' ] );
$args[ 'rscregion' ] = sanitize_text_field( $_POST[ 'rscregion' ] );
$ajax = TRUE;
}
echo '<span id="rsccontainererror" style="color:red;">';
$container_list = array();
if ( ! empty( $args[ 'rscusername' ] ) && ! empty( $args[ 'rscapikey' ] ) && ! empty( $args[ 'rscregion' ] ) ) {
try {
$conn = new OpenCloud\Rackspace(
self::get_auth_url_by_region( $args[ 'rscregion' ] ),
array(
'username' => $args[ 'rscusername' ],
'apiKey' => BackWPup_Encryption::decrypt( $args[ 'rscapikey' ] )
));
$ostore = $conn->objectStoreService( 'cloudFiles' , $args[ 'rscregion' ], 'publicURL' );
$containerlist = $ostore->listContainers();
while( $container = $containerlist->next() ) {
$container_list[] = $container->name;
}
}
catch ( Exception $e ) {
$error = $e->getMessage();
}
}
if ( empty( $args[ 'rscusername' ] ) )
_e( 'Missing username!', 'backwpup' );
elseif ( empty( $args[ 'rscapikey' ] ) )
_e( 'Missing API Key!', 'backwpup' );
elseif ( ! empty( $error ) )
echo esc_html( $error );
elseif ( empty( $container_list ) )
_e( "A container could not be found!", 'backwpup' );
echo '</span>';
if ( ! empty( $container_list ) ) {
echo '<select name="rsccontainer" id="rsccontainer">';
foreach( $container_list as $container_name )
echo "<option " . selected( strtolower( $args[ 'rscselected' ] ), strtolower( $container_name ), FALSE ) . ">" . $container_name . "</option>";
echo '</select>';
}
if ( $ajax )
die();
else
return;
}
}