<?php
/**
 * Destination aws.
 *
 * @package wpdbbkp
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

add_action( 'wpdbbkp_db_backup_completed', array( 'WPDatabaseBackupS3', 'wp_db_backup_completed' ) );

/**
 * WPDatabaseBackupS3 Class.
 *
 * @class WPDatabaseBackupS3
 */
class WPDatabaseBackupS3 {

	/**
	 * Run after complete backup.
	 *
	 * @param array $args - backup details.
	 */
	public static function wp_db_backup_completed( &$args ) {
		$destination_s3 = get_option( 'wp_db_backup_destination_s3' );
		if ( isset( $destination_s3 ) && 1 == $destination_s3 && get_option( 'wpdb_dest_amazon_s3_bucket' ) && get_option( 'wpdb_dest_amazon_s3_bucket_key' ) && get_option( 'wpdb_dest_amazon_s3_bucket_secret' ) ) {
			update_option('wpdbbkp_backupcron_current','Processing Amazon S3 Backup', false);
			try {
				if ( ! class_exists( 'S3' ) ) {
					require_once 'S3.php';
				}
				// AWS access info.
				if ( ! defined( 'WPDBBKP_AWS_ACCESS_KEY' ) ) {
					define( 'WPDBBKP_AWS_ACCESS_KEY', get_option( 'wpdb_dest_amazon_s3_bucket_key' ) );
				}
				if ( ! defined( 'WPDBBKP_AWS_SECRET_KEY' ) ) {
					define( 'WPDBBKP_AWS_SECRET_KEY', get_option( 'wpdb_dest_amazon_s3_bucket_secret' ) );
				}

				// Check for CURL.
				if ( ! extension_loaded( 'curl' ) && ! dl( 'so' === PHP_SHLIB_SUFFIX ? 'curl.so' : 'php_curl.dll' ) ) { // phpcs:ignore
					$message_error = 'No Curl';
				}

				$wpdbbkp_s3          = new S3( WPDBBKP_AWS_ACCESS_KEY, WPDBBKP_AWS_SECRET_KEY );
				$wpdbbkp_bucket_name = get_option( 'wpdb_dest_amazon_s3_bucket' );
				$wpdbbkp_result      = $wpdbbkp_s3->listBuckets();
				if ( get_option( 'wpdb_dest_amazon_s3_bucket' ) ) {
					if ( true === in_array( get_option( 'wpdb_dest_amazon_s3_bucket' ), $wpdbbkp_result ) ) { // phpcs:ignore
						if ( $wpdbbkp_s3->putObjectFile( $args[1], $wpdbbkp_bucket_name, baseName( $args[1] ), S3::ACL_PUBLIC_READ ) ) {
							$args[2] = $args[2] . '<br> '.esc_html__('Upload Database Backup on s3 bucket','wpdbbkp') . $wpdbbkp_bucket_name;
						} else {
							$args[2] = $args[2] . '<br>'.esc_html__('Failed to upload Database Backup on s3 bucket','wpdbbkp') . $wpdbbkp_bucket_name;
						}
					} else {
						$args[2] = $args[2] . '<br>'.esc_html__('Invalid bucket name or AWS details','wpdbbkp');
						$args[4] = $args[4] .= 'S3, ';
					}
				}
			} catch ( Exception $e ) {
				$error_msg = 'Error log.';
			}
		}
	}

}
