class-destination-glacier.php 27.6 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
<?php
// Amazon Glacier SDK v2.8.27
// http://aws.amazon.com/de/sdkforphp2/
// https://github.com/aws/aws-sdk-php
// http://http://docs.aws.amazon.com/general/latest/gr/rande.html#glacier_region

/**
 * Documentation: http://docs.amazonwebservices.com/aws-sdk-php-2/latest/class-Aws.S3.S3Client.html
 */
class BackWPup_Pro_Destination_Glacier extends BackWPup_Destinations {

	/**
	 * @return array
	 */
	public function option_defaults() {

		return array( 'glacieraccesskey' => '', 'glaciersecretkey' => '', 'glaciervault' => '', 'glacierregion' => 'us-east-1',  'glaciermaxbackups' => 100 );
	}


	/**
	 * @param $jobid
	 */
	public function edit_tab( $jobid ) {

		?>
		<h3 class="title"><?php esc_html_e( 'Amazon Glacier', 'backwpup' ) ?></h3>
		<p></p>
		<table class="form-table">
			<tr>
				<th scope="row"><label for="glacierregion"><?php esc_html_e( 'Select a region:', 'backwpup' ) ?></label></th>
				<td>
					<select name="glacierregion" id="glacierregion" title="<?php esc_html_e( 'Amazon Glacier Region', 'backwpup' ); ?>">
						<option value="us-east-1" <?php selected( 'us-east-1', BackWPup_Option::get( $jobid, 'glacierregion' ), TRUE ) ?>><?php esc_html_e( 'US Standard', 'backwpup' ); ?></option>
						<option value="us-west-1" <?php selected( 'us-west-1', BackWPup_Option::get( $jobid, 'glacierregion' ), TRUE ) ?>><?php esc_html_e( 'US West (Northern California)', 'backwpup' ); ?></option>
						<option value="us-west-2" <?php selected( 'us-west-2', BackWPup_Option::get( $jobid, 'glacierregion' ), TRUE ) ?>><?php esc_html_e( 'US West (Oregon)', 'backwpup' ); ?></option>
						<option value="eu-west-1" <?php selected( 'eu-west-1', BackWPup_Option::get( $jobid, 'glacierregion' ), TRUE ) ?>><?php esc_html_e( 'EU (Ireland)', 'backwpup' ); ?></option>
						<option value="eu-central-1" <?php selected( 'eu-central-1', BackWPup_Option::get( $jobid, 'glacierregion' ), TRUE ) ?>><?php esc_html_e( 'EU (Germany)', 'backwpup' ); ?></option>
						<option value="ap-northeast-1" <?php selected( 'ap-northeast-1', BackWPup_Option::get( $jobid, 'glacierregion' ), TRUE ) ?>><?php esc_html_e( 'Asia Pacific (Tokyo)', 'backwpup' ); ?></option>
						<option value="ap-northeast-2" <?php selected( 'ap-northeast-2', BackWPup_Option::get( $jobid, 'glacierregion' ), TRUE ) ?>><?php esc_html_e( 'Asia Pacific (Seoul)', 'backwpup' ); ?></option>
						<option value="ap-south-1" <?php selected( 'ap-south-1', BackWPup_Option::get( $jobid, 'glacierregion' ), TRUE ) ?>><?php esc_html_e( 'Asia Pacific (Mumbai)', 'backwpup' ); ?></option>
						<option value="ap-southeast-2" <?php selected( 'ap-southeast-2', BackWPup_Option::get( $jobid, 'glacierregion' ), TRUE ) ?>><?php esc_html_e( 'Asia Pacific (Sydney)', 'backwpup' ); ?></option>
						<option value="sa-east-1" <?php selected( 'sa-east-1', BackWPup_Option::get( $jobid, 'glacierregion' ), TRUE ) ?>><?php esc_html_e( 'South America (Sao Paulo)', 'backwpup' ); ?></option>
						<option value="cn-north-1" <?php selected( 'cn-north-1', BackWPup_Option::get( $jobid, 'glacierregion' ), TRUE ) ?>><?php esc_html_e( 'China (Beijing)', 'backwpup' ); ?></option>
					</select>
				</td>
			</tr>
		</table>

		<h3 class="title"><?php esc_html_e( 'Amazon Access Keys', 'backwpup' ); ?></h3>
		<p></p>
		<table class="form-table">
			<tr>
				<th scope="row"><label for="glacieraccesskey"><?php esc_html_e( 'Access Key', 'backwpup' ); ?></label></th>
				<td>
					<input id="glacieraccesskey" name="glacieraccesskey" type="text"
						   value="<?php echo esc_attr( BackWPup_Option::get( $jobid, 'glacieraccesskey' ) );?>" class="regular-text" autocomplete="off" />
				</td>
			</tr>
			<tr>
				<th scope="row"><label for="glaciersecretkey"><?php esc_html_e( 'Secret Key', 'backwpup' ); ?></label></th>
				<td>
					<input id="glaciersecretkey" name="glaciersecretkey" type="password"
						   value="<?php echo esc_attr( BackWPup_Encryption::decrypt( BackWPup_Option::get( $jobid, 'glaciersecretkey' ) ) ); ?>" class="regular-text" autocomplete="off" />
				</td>
			</tr>
		</table>

		<h3 class="title"><?php esc_html_e( 'Vault', 'backwpup' ); ?></h3>
		<p></p>
		<table class="form-table">
			<tr>
				<th scope="row"><label for="vaultselected"><?php esc_html_e( 'Vault selection', 'backwpup' ); ?></label></th>
				<td>
					<input id="vaultselected" name="vaultselected" type="hidden" value="<?php echo esc_attr( BackWPup_Option::get( $jobid, 'glaciervault' ) ); ?>" />
					<?php if ( BackWPup_Option::get( $jobid, 'glacieraccesskey' ) && BackWPup_Option::get( $jobid, 'glaciersecretkey' ) ) $this->edit_ajax( array(
																																					   'glacieraccesskey'  => BackWPup_Option::get( $jobid, 'glacieraccesskey' ),
																																					   'glaciersecretkey'  => BackWPup_Encryption::decrypt(BackWPup_Option::get( $jobid, 'glaciersecretkey' ) ),
																																					   'vaultselected'   => BackWPup_Option::get( $jobid, 'glaciervault' ),
																																					   'glacierregion' 	=> BackWPup_Option::get( $jobid, 'glacierregion' )
																																				  ) ); ?>
				</td>
			</tr>
			<tr>
				<th scope="row"><label for="newvault"><?php esc_html_e( 'Create a new vault', 'backwpup' ); ?></label></th>
				<td>
					<input id="newvault" name="newvault" type="text" value="" class="small-text" autocomplete="off" />
				</td>
			</tr>
		</table>

		<h3 class="title"><?php esc_html_e( 'Glacier Backup settings', 'backwpup' ); ?></h3>
		<p></p>
		<table class="form-table">
			<tr>
				<th scope="row"><?php esc_html_e( 'File deletion', 'backwpup' ); ?></th>
				<td>
					<label for="glaciermaxbackups">
						<input
							id="glaciermaxbackups"
							name="glaciermaxbackups"
							type="number"
							step="1"
							min="0"
							value="<?php echo esc_attr( BackWPup_Option::get( $jobid, 'glaciermaxbackups' ) ); ?>"
							class="small-text"
						/>&nbsp;
					<?php  esc_html_e( 'Number of files to keep in folder. (Archives deleted before 3 months after they have been stored may cause extra costs when deleted.)', 'backwpup' ); ?></label>
				</td>
			</tr>
		</table>

		<?php
	}

	/**
	 * @param string $args
	 */
	public function edit_ajax( $args = '' ) {

		$error = '';
		$vaults_list = array();

		if ( is_array( $args ) ) {
			$ajax = FALSE;
		}
		else {
			if ( ! current_user_can( 'backwpup_jobs_edit' ) ) {
				wp_die( -1 );
			}
			check_ajax_referer( 'backwpup_ajax_nonce' );
			$args[ 'glacieraccesskey' ]  	= sanitize_text_field( $_POST[ 'glacieraccesskey' ] );
			$args[ 'glaciersecretkey' ]  	= sanitize_text_field( $_POST[ 'glaciersecretkey' ] );
			$args[ 'vaultselected' ]		= sanitize_text_field( $_POST[ 'vaultselected' ] );
			$args[ 'glacierregion' ]  	 	= sanitize_text_field( $_POST[ 'glacierregion' ] );
			$ajax         					= TRUE;
		}
		echo '<span id="glacierbucketerror" style="color:red;">';

		if ( ! empty( $args[ 'glacieraccesskey' ] ) && ! empty( $args[ 'glaciersecretkey' ] ) ) {
			try {
				$glacier = Aws\Glacier\GlacierClient::factory( array( 	'key'		=> $args[ 'glacieraccesskey' ],
																		'secret'	=> BackWPup_Encryption::decrypt( $args[ 'glaciersecretkey' ] ),
																		'region'	=> $args[ 'glacierregion' ],
																		'scheme'	=> 'https',
																		'ssl.certificate_authority' => BackWPup::get_plugin_data( 'cacert' ) ) );

				$vaults = $glacier->listVaults();
				if ( ! empty( $vaults['VaultList'] ) ) {
					$vaults_list = $vaults['VaultList'];
				}
				while ( ! empty( $vaults['Marker'] ) ) {
					$vaults = $glacier->listVaults( array( 'marker' => $vaults['Marker'] ) );
					if ( ! empty( $vaults['VaultList'] ) ) {
						$vaults_list = array_merge( $vaults_list, $vaults['VaultList'] );
					}
				}
			}
			catch ( Exception $e ) {
				$error = $e->getMessage();
			}
		}

		if ( empty( $args[ 'glacieraccesskey' ] ) )
			esc_html_e( 'Missing access key!', 'backwpup' );
		elseif ( empty( $args[ 'glaciersecretkey' ] ) )
			esc_html_e( 'Missing secret access key!', 'backwpup' );
		elseif ( ! empty( $error ) )
			echo esc_html( $error );
		elseif ( ! isset( $vaults ) || count( $vaults['VaultList']  ) < 1 )
			esc_html_e( 'No vault found!', 'backwpup' );
		echo '</span>';

		if ( $vaults_list ) {
			echo '<select name="glaciervault" id="glaciervault">';
			foreach ( $vaults_list as $vault ) {
				echo "<option " . selected( $args[ 'vaultselected' ], esc_attr( $vault['VaultName'] ), FALSE ) . ">" . esc_attr( $vault['VaultName'] ) . "</option>";
			}
			echo '</select>';
		}

		if ( $ajax )
			die();
	}

	/**
	 * @param $jobid
	 * @return string
	 */
	public function edit_form_post_save( $jobid ) {
		$message = '';
		BackWPup_Option::update( $jobid, 'glacieraccesskey', isset( $_POST[ 'glacieraccesskey' ] ) ? sanitize_text_field( $_POST[ 'glacieraccesskey' ] ) : '' );
		BackWPup_Option::update( $jobid, 'glaciersecretkey', isset( $_POST[ 'glaciersecretkey' ] ) ? BackWPup_Encryption::encrypt( $_POST[ 'glaciersecretkey' ] ) : '' );
		BackWPup_Option::update( $jobid, 'glacierregion', isset( $_POST[ 'glacierregion' ] ) ? sanitize_text_field( $_POST[ 'glacierregion' ] ) : '' );
		BackWPup_Option::update( $jobid, 'glaciervault', isset( $_POST[ 'glaciervault' ] ) ? sanitize_text_field( $_POST[ 'glaciervault' ] ) : '' );
		BackWPup_Option::update( $jobid, 'glaciermaxbackups', isset( $_POST[ 'glaciermaxbackups' ] ) ? absint( $_POST[ 'glaciermaxbackups' ] ) : 0 );

		//create new bucket
		if ( !empty( $_POST[ 'newvault' ] ) ) {
			try {
				$glacier = Aws\Glacier\GlacierClient::factory( array( 	 'key'	=> sanitize_text_field( $_POST[ 'glacieraccesskey' ] ),
																		 'secret'	=> sanitize_text_field( $_POST[ 'glaciersecretkey' ] ),
																		 'region'	=> sanitize_text_field( $_POST[ 'glacierregion' ] ),
																		 'scheme'	=> 'https',
																		 'ssl.certificate_authority' => BackWPup::get_plugin_data( 'cacert' ) ) );

				$vault = $glacier->createVault( array( 'vaultName' => sanitize_text_field( $_POST[ 'newvault' ] ) ) );
				$glacier->waitUntilVaultExists( array( 'vaultName' => sanitize_text_field( $_POST[ 'newvault' ] ) ) );
				if ( $vault->get( 'location' ) )
					$message .= sprintf( __( 'Vault %1$s created.','backwpup'), sanitize_text_field( $_POST[ 'newvault' ] ) ) . '<br />';
				else
					$message .= sprintf( __( 'Vault %s could not be created.','backwpup'), sanitize_text_field( $_POST[ 'newvault' ] ) ) . '<br />';

			}
			catch ( Aws\S3\Exception\S3Exception $e ) {
				$message .= $e->getMessage();
			}
			BackWPup_Option::update( $jobid, 'glaciervault', sanitize_text_field( $_POST[ 'newvault' ] ) );
		}

		return $message;
	}

	/**
	 * @param $job_settings
	 */
	public function wizard_page( array $job_settings ) {
		?>
		<table class="form-table">
			<tr>
				<td>
					<fieldset>
						<label for="glacierregion"><?php esc_html_e( 'Select an Amazon Glacier region:', 'backwpup' ); ?><br />
							<select name="glacierregion" id="glacierregion" title="<?php esc_html_e( 'Amazon Glacier Region', 'backwpup' ); ?>">
								<option value="us-east-1" <?php selected( 'us-east-1', $job_settings[ 'glacierregion' ], TRUE ) ?>><?php esc_html_e( 'US Standard', 'backwpup' ); ?></option>
								<option value="us-west-1" <?php selected( 'us-west-1', $job_settings[ 'glacierregion' ], TRUE ) ?>><?php esc_html_e( 'US West (Northern California)', 'backwpup' ); ?></option>
								<option value="us-west-2" <?php selected( 'us-west-2', $job_settings[ 'glacierregion' ], TRUE ) ?>><?php esc_html_e( 'US West (Oregon)', 'backwpup' ); ?></option>
								<option value="eu-west-1" <?php selected( 'eu-west-1', $job_settings[ 'glacierregion' ], TRUE ) ?>><?php esc_html_e( 'EU (Ireland)', 'backwpup' ); ?></option>
								<option value="eu-central-1" <?php selected( 'eu-central-1', $job_settings[ 'glacierregion' ], TRUE ) ?>><?php esc_html_e( 'EU (Germany)', 'backwpup' ); ?></option>
								<option value="ap-northeast-1" <?php selected( 'ap-northeast-1', $job_settings[ 'glacierregion' ], TRUE ) ?>><?php esc_html_e( 'Asia Pacific (Tokyo)', 'backwpup' ); ?></option>
								<option value="ap-northeast-2" <?php selected( 'ap-northeast-2', $job_settings[ 'glacierregion' ], TRUE ) ?>><?php esc_html_e( 'Asia Pacific (Seoul)', 'backwpup' ); ?></option>
								<option value="ap-southeast-1" <?php selected( 'ap-southeast-1', $job_settings[ 'glacierregion' ], TRUE ) ?>><?php esc_html_e( 'Asia Pacific (Singapore)', 'backwpup' ); ?></option>
								<option value="ap-southeast-2" <?php selected( 'ap-southeast-2', $job_settings[ 'glacierregion' ], TRUE ) ?>><?php esc_html_e( 'Asia Pacific (Sydney)', 'backwpup' ); ?></option>
								<option value="sa-east-1" <?php selected( 'sa-east-1', $job_settings[ 'glacierregion' ], TRUE ) ?>><?php esc_html_e( 'South America (Sao Paulo)', 'backwpup' ); ?></option>
								<option value="cn-north-1" <?php selected( 'cn-north-1', $job_settings[ 'glacierregion' ], TRUE ) ?>><?php esc_html_e( 'China (Beijing)', 'backwpup' ); ?></option>
							</select></label><br/>
						<label for="glacieraccesskey"><strong><?php esc_html_e( 'Access Key:', 'backwpup' ); ?></strong>
							<input id="glacieraccesskey" name="glacieraccesskey" type="text" value="<?php echo esc_attr( $job_settings[ 'glacieraccesskey' ] );?>" class="large-text" autocomplete="off" /></label><br/>
						<label for="glaciersecretkey"><strong><?php esc_html_e( 'Secret Key:', 'backwpup' ); ?></strong><br/>
							<input id="glaciersecretkey" name="glaciersecretkey" type="password" value="<?php echo esc_attr( BackWPup_Encryption::decrypt( $job_settings[ 'glaciersecretkey' ] ) );?>" class="large-text" autocomplete="off" /></label><br/>
						<label for="glaciervault"><strong><?php esc_html_e( 'Vault:', 'backwpup' ); ?></strong><br/>
							<input id="vaultselected" name="vaultselected" type="hidden" value="<?php echo esc_attr( $job_settings[ 'vaultselected' ] ); ?>" />
							<?php if ( $job_settings[ 'glacieraccesskey' ] && $job_settings[ 'glaciersecretkey' ] ) $this->edit_ajax( array(
																																 'glacieraccesskey'  	=> $job_settings[  'glacieraccesskey' ],
																																 'glaciersecretkey'  	=> BackWPup_Encryption::decrypt( $job_settings[ 'glaciersecretkey' ] ),
																																 'vaultselected'   		=> $job_settings[ 'glaciervault' ],
																																 'glacierregion' 		=> $job_settings[ 'glacierregion' ]
																															) ); ?></label>

						&nbsp;&nbsp;&nbsp;<label for="newvault"><?php esc_html_e('New Vault:', 'backwpup'); ?><input id="newvault" name="newvault" type="text" value="" class="small-text" autocomplete="off" /></label><br/>
						<br/>
						<label id="glaciermaxbackups"><input name="glaciermaxbackups" id="glaciermaxbackups" type="number" step="1" min="0" value="<?php echo esc_attr( $job_settings[ 'glaciermaxbackups' ] );?>" class="small-text" />
							<?php  esc_html_e( 'Number of files to keep in folder. (Archives deleted before 3 months after they have been stored may cause extra costs when deleted.)', 'backwpup' ); ?></label>
						<br/>
					</fieldset>
				</td>
			</tr>
		</table>
		<?php
	}

	/**
	 * @param $job_settings
	 *
	 * @return array
	 */
	public function wizard_save( array $job_settings ) {

		$job_settings[ 'glacieraccesskey' ] = isset( $_POST[ 'glacieraccesskey' ] ) ? sanitize_text_field( $_POST[ 'glacieraccesskey' ] ) : '';
		$job_settings[ 'glaciersecretkey' ] = isset( $_POST[ 'glaciersecretkey' ] ) ? BackWPup_Encryption::encrypt( (string) $_POST[ 'glaciersecretkey' ] ) : '';
		$job_settings[ 'glacierregion' ] = isset( $_POST[ 'glacierregion' ] ) ? sanitize_text_field( $_POST[ 'glacierregion' ] ) : '';
		$job_settings[ 'glaciervault' ] = isset( $_POST[ 'glaciervault' ] ) ? sanitize_text_field( $_POST[ 'glaciervault' ] ) : '';
		$job_settings[ 'glaciermaxbackups' ] = !empty( $_POST[ 'glaciermaxbackups' ] ) ? absint($_POST[ 'glaciermaxbackups' ]) : 0;

		//create new bucket
		if ( !empty( $_POST[ 'newvault' ] ) ) {
			try {
				$glacier = Aws\Glacier\GlacierClient::factory( array( 	'key'		=> $job_settings[ 'glacieraccesskey' ],
																		'secret'	=> BackWPup_Encryption::decrypt( $job_settings[ 'glaciersecretkey' ] ),
																		'region'	=> $job_settings[ 'glacierregion' ],
																		'scheme'	=> 'https',
																		'ssl.certificate_authority' => BackWPup::get_plugin_data( 'cacert' ) ) );

				$vault = $glacier->createVault( array( 'vaultName' => sanitize_text_field( $_POST[ 'newvault' ] ) ) );
				$glacier->waitUntilVaultExists( array( 'vaultName' => sanitize_text_field( $_POST[ 'newvault' ] ) ) );

				if ( $vault->get( 'location' ) )
					BackWPup_Admin::message( sprintf( __( 'Vault %1$s created.','backwpup'), sanitize_text_field( $_POST[ 'newvault' ] ) ) );
				else
					BackWPup_Admin::message( sprintf( __( 'Vault %s could not be created.','backwpup'), sanitize_text_field( $_POST[ 'newvault' ] ) ) );

			}
			catch ( Exception $e ) {
				BackWPup_Admin::message( $e->getMessage() );
			}
			$job_settings[ 'newvault' ] = sanitize_text_field( $_POST[ 'newvault' ] );
		}

		return $job_settings;
	}

	/**
	 * @param $jobdest
	 * @param $backupfile
	 */
	public function file_delete( $jobdest, $backupfile ) {

		$files =  get_option( 'backwpup_' . strtolower( $jobdest ), array() );
		list( $jobid, $dest ) = explode( '_', $jobdest );

		if ( BackWPup_Option::get( $jobid, 'glacieraccesskey' ) && BackWPup_Option::get( $jobid, 'glaciersecretkey' ) && BackWPup_Option::get( $jobid, 'glaciervault' ) ) {
			try {
				$glacier = Aws\Glacier\GlacierClient::factory( array(	'key'		=> BackWPup_Option::get( $jobid, 'glacieraccesskey' ),
																		'secret'	=> BackWPup_Encryption::decrypt( BackWPup_Option::get( $jobid, 'glaciersecretkey' ) ),
																		'region'	=> BackWPup_Option::get( $jobid, 'glacierregion' ),
																		'scheme'	=> 'https',
																		'ssl.certificate_authority' => BackWPup::get_plugin_data( 'cacert' ) ) );

				$glacier->deleteArchive( array(
										 'vaultName' =>  BackWPup_Option::get( $jobid,'glaciervault' ),
										 'archiveId' => $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( sprintf( __('AWS API: %s','backwpup'), $e->getMessage() ) );
			}
		}
		update_option( 'backwpup_'. strtolower( $jobdest ), $files );
	}

	/**
	 * @param $jobdest
	 * @return mixed
	 */
	public function file_get_list( $jobdest ) {

		return get_site_option( 'backwpup_' . strtolower( $jobdest ) );
	}

	/**
	 * @param $job_object
	 * @return bool
	 */
	public function job_run_archive( BackWPup_Job $job_object ) {

		$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. Trying to send backup file to Amazon Glacier&#160;&hellip;', 'backwpup' ), $job_object->steps_data[ $job_object->step_working ][ 'STEP_TRY' ] ), E_USER_NOTICE );

		try {
			$glacier = Aws\Glacier\GlacierClient::factory( array( 	'key'		=> $job_object->job[ 'glacieraccesskey' ],
																	'secret'	=> BackWPup_Encryption::decrypt( $job_object->job[ 'glaciersecretkey' ] ),
																	'region'	=> $job_object->job[ 'glacierregion' ],
																	'scheme'	=> 'https',
																	'ssl.certificate_authority' => BackWPup::get_plugin_data( 'cacert' ) ) );

			$vault = $glacier->describeVault( array( 'vaultName' => $job_object->job[ 'glaciervault' ] ) );
			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 ) {

				if ( $vault->get( 'VaultName' ) ) {
					$job_object->log( sprintf( __( 'Connected to Glacier vault "%1$s" with %2$d archives and size of %3$d', 'backwpup' ), $vault->get( 'VaultName' ), $vault->get( 'NumberOfArchives' ), size_format( $vault->get( 'SizeInBytes' ), 2 ) ), E_USER_NOTICE );
				} else {
					$job_object->log( sprintf( __( 'Glacier vault "%s" does not exist!', 'backwpup' ), $job_object->job[ 'glaciervault' ] ), E_USER_ERROR );

					return TRUE;
				}

				//transfer file to Glacier
				$job_object->log( __( 'Starting upload to Amazon Glacier&#160;&hellip;', 'backwpup' ), E_USER_NOTICE );
			}

			//Prepare Upload
			$job_object->steps_data[ $job_object->step_working ][ 'partSize' ] 	= 4194304; //4MB
			$parts = Aws\Glacier\Model\MultipartUpload\UploadPartGenerator::factory( fopen( $job_object->backup_folder . $job_object->backup_file, 'rb' ), $job_object->steps_data[ $job_object->step_working ][ 'partSize' ] );
			//UploadPartGenerator closes $file_handel
			if ( $file_handel = fopen( $job_object->backup_folder . $job_object->backup_file, 'rb' ) ) {

				try {

					if ( empty ( $job_object->steps_data[ $job_object->step_working ][ 'uploadId' ] ) ) {
						$upload = $glacier->initiateMultipartUpload( array(	'vaultName' => $job_object->job[ 'glaciervault' ],
																			'partSize' => $job_object->steps_data[ $job_object->step_working ][ 'partSize' ] ) );

						$job_object->steps_data[ $job_object->step_working ][ 'uploadId' ] = $upload->get( 'uploadId' );
						$job_object->steps_data[ $job_object->step_working ][ 'part' ] = 1;
					}

					$part_count = 1;
					foreach ( $parts as $part ) {
						$part_count ++;
						if ( $part_count <= $job_object->steps_data[ $job_object->step_working ][ 'part' ] )
							continue;
						$chunk_upload_start = microtime( TRUE );
						fseek( $file_handel, $part->getOffset() );
						$glacier->uploadMultipartPart( array( 	'vaultName' => $job_object->job[ 'glaciervault' ],
																'uploadId' => $job_object->steps_data[ $job_object->step_working ][ 'uploadId' ],
																'checksum' => $part->getChecksum(),
																'range' =>  $part->getFormattedRange(),
																'body' => fread( $file_handel, $part->getSize() ),
																'ContentSHA256' => $part->getContentHash() ) );
						$chunk_upload_time = microtime( TRUE ) - $chunk_upload_start;
						$job_object->substeps_done = $job_object->substeps_done + $part->getSize();
						$job_object->steps_data[ $job_object->step_working ][ 'part' ] ++;
						$time_remaining = $job_object->do_restart_time();
						if ( $time_remaining < $chunk_upload_time )
							$job_object->do_restart_time( TRUE );
						$job_object->update_working_data();
					}

					$result = $glacier->completeMultipartUpload( array(	'vaultName' => $job_object->job[ 'glaciervault' ],
																		'uploadId' => $job_object->steps_data[ $job_object->step_working ][ 'uploadId' ],
																		'archiveSize' => $parts->getArchiveSize(),
																		'checksum' => $parts->getRootChecksum() ) );

					if ( $result->get( 'archiveId' ) ) {
						$job_object->substeps_done = 1 + $job_object->backup_filesize;
						//sore file data
						$backup_files = get_site_option( 'backwpup_' . $job_object->job[ 'jobid' ] . '_glacier', array() );
						$backup_files[] = array( 	'folder' 	=> '/',
													'file' 	 	=> $result->get( 'archiveId' ),
													'filename' 	=> $job_object->backup_file,
													'info'		=> sprintf( __( 'Archive ID: %s', 'backwpup' ), $result->get( 'archiveId' ) ),
													'downloadurl' => '',
													'filesize'  => $job_object->backup_filesize,
													'time' 		=> current_time( 'timestamp', TRUE ) );
						update_site_option( 'backwpup_' . $job_object->job[ 'jobid' ] . '_glacier' , $backup_files );
						$job_object->substeps_done = 1 + $job_object->backup_filesize;
						$job_object->log( sprintf( __( 'Backup transferred to %s.', 'backwpup' ), $result->get( 'location' ) ), E_USER_NOTICE );
					} else {
						$job_object->log(
										sprintf(
											__( 'Error transfering backup to %s.', 'backwpup' ),
											__( 'Glacier', 'backwpup' )
										),
										E_USER_ERROR
										);
					}

				} catch ( Exception $e ) {
					$job_object->log( E_USER_ERROR, sprintf( __( 'AWS API: %s', 'backwpup' ), $e->getMessage() ), $e->getFile(), $e->getLine() );
					if ( ! empty( $job_object->steps_data[ $job_object->step_working ][ 'uploadId' ] ) )
						$glacier->abortMultipartUpload( array(	'vaultName' => $job_object->job[ 'glaciervault' ],
															    'uploadId' => $job_object->steps_data[ $job_object->step_working ][ 'uploadId' ] ) );
					unset( $job_object->steps_data[ $job_object->step_working ][ 'uploadId' ] );
					unset( $job_object->steps_data[ $job_object->step_working ][ 'part' ] );
					$job_object->substeps_done = 0;
					fclose( $file_handel );
					return FALSE;
				}
				fclose( $file_handel );
			} else {
				$job_object->log( __( 'Can not open source file for transfer.', 'backwpup' ), E_USER_ERROR );
				return FALSE;
			}


		}
		catch ( Exception $e ) {
			$job_object->log( E_USER_ERROR, sprintf( __( 'AWS API: %s', 'backwpup' ), $e->getMessage() ), $e->getFile(), $e->getLine() );

			return FALSE;
		}

		try {
			$backupfilelist = array();

			foreach ( $backup_files as $file )
				$backupfilelist[ $file[ 'time' ] ] = $file[ 'file' ];

			if ( $job_object->job[ 's3maxbackups' ] > 0 && is_object( $glacier ) ) { //Delete old backups
				if ( count( $backupfilelist ) > $job_object->job[ 'glaciermaxbackups' ] ) {
					ksort( $backupfilelist );
					$numdeltefiles = 0;
					while ( $file = array_shift( $backupfilelist ) ) {
						if ( count( $backupfilelist ) < $job_object->job[ 'glaciermaxbackups' ] )
							break;
						//delete files on S3
						$args = array(
							'vaultName' => $job_object->job[ 'glaciervault' ],
							'archiveId' => $file
						);
						if (  $glacier->deleteArchive( $args )  ) {
							foreach ( $backup_files as $key => $filedata ) {
								if ( $filedata[ 'file' ] == $file )
									unset( $backup_files[ $key ] );
							}
							$numdeltefiles ++;
						} else {
							$job_object->log( sprintf( __( 'Cannot delete archive from %s.', 'backwpup' ), $job_object->job[ 'glaciervault' ] ), E_USER_ERROR );
						}
					}
					if ( $numdeltefiles > 0 )
						$job_object->log( sprintf( _n( 'One file deleted on vault.', '%d files deleted on vault', $numdeltefiles, 'backwpup' ), $numdeltefiles ), E_USER_NOTICE );
				}
			}
			update_site_option( 'backwpup_' . $job_object->job[ 'jobid' ] . '_glacier', $backup_files );
		}
		catch ( Exception $e ) {
			$job_object->log( E_USER_ERROR, sprintf( __( 'AWS API: %s', 'backwpup' ), $e->getMessage() ), $e->getFile(), $e->getLine() );

			return FALSE;
		}
		$job_object->substeps_done = 2 + $job_object->backup_filesize;

		return TRUE;
	}

	/**
	 * @param $job_settings
	 * @return bool
	 */
	public function can_run( array $job_settings ) {

		if ( empty( $job_settings[ 'glacieraccesskey' ] ) )
			return FALSE;

		if ( empty( $job_settings[ 'glaciersecretkey' ] ) )
			return FALSE;

		if ( empty( $job_settings[ 'glaciervault' ] ) )
			return FALSE;

		return TRUE;
	}

	/**
	 *
	 */
	public function wizard_inline_js() {

		$this->edit_inline_js();
	}

	/**
	 *
	 */
	public function edit_inline_js() {
		?>
		<script type="text/javascript">
			jQuery(document).ready(function ($) {
				function awsgetvault() {
					var data = {
						action: 'backwpup_dest_glacier',
						glacieraccesskey: $('input[name="glacieraccesskey"]').val(),
						glaciersecretkey: $('input[name="glaciersecretkey"]').val(),
						vaultselected: $('input[name="vaultselected"]').val(),
						glacierregion: $('#glacierregion').val(),
						_ajax_nonce: $('#backwpupajaxnonce').val()
					};
					$.post(ajaxurl, data, function (response) {
						$('#glacierbucketerror').remove();
						$('#glaciervault').remove();
						$('#vaultselected').after(response);
					});
				}

				$('input[name="glacieraccesskey"]').backwpupDelayKeyup(function () {
					awsgetvault();
				});
				$('input[name="glaciersecretkey"]').backwpupDelayKeyup(function () {
					awsgetvault();
				});
				$('#glacierregion').change(function () {
					awsgetvault();
				});
			});
			</script>
		<?php
	}
}