| 1 |
<?php |
| 2 |
/** |
| 3 |
* Destination SFTP test. |
| 4 |
* |
| 5 |
* @package wpdbbkp |
| 6 |
*/ |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; // Exit if accessed directly. |
| 10 |
} |
| 11 |
|
| 12 |
if ( ! function_exists( 'add_action' ) ) { |
| 13 |
header( 'Status: 403 Forbidden' ); |
| 14 |
header( 'HTTP/1.1 403 Forbidden' ); |
| 15 |
exit(); |
| 16 |
} |
| 17 |
|
| 18 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 19 |
header( 'Status: 403 Forbidden' ); |
| 20 |
header( 'HTTP/1.1 403 Forbidden' ); |
| 21 |
exit(); |
| 22 |
} |
| 23 |
|
| 24 |
require __DIR__ . '/vendor/autoload.php'; |
| 25 |
use phpseclib3\Net\SFTP; |
| 26 |
use phpseclib3\Crypt\PublicKeyLoader; |
| 27 |
/** |
| 28 |
* Test app. |
| 29 |
*/ |
| 30 |
function wpdbbkp_test_sftp() { |
| 31 |
|
| 32 |
// Now let's see if we can connect to the SFTP repo. |
| 33 |
|
| 34 |
$wpdbbkp_sftp_details = get_option( 'wp_db_backup_sftp_details',array()); |
| 35 |
|
| 36 |
$host = isset($wpdbbkp_sftp_details['host'])?$wpdbbkp_sftp_details['host']:''; |
| 37 |
$port = isset($wpdbbkp_sftp_details['port'])?$wpdbbkp_sftp_details['port']:22; |
| 38 |
$user = isset($wpdbbkp_sftp_details['username'])?$wpdbbkp_sftp_details['username']:''; |
| 39 |
$pass = isset($wpdbbkp_sftp_details['password'])?$wpdbbkp_sftp_details['password']:''; |
| 40 |
$pkey = isset($wpdbbkp_sftp_details['sftp_key'])?base64_decode($wpdbbkp_sftp_details['sftp_key']):''; |
| 41 |
$key_pass = isset($wpdbbkp_sftp_details['key_password'])?$wpdbbkp_sftp_details['key_password']:false; |
| 42 |
$directory = isset($wpdbbkp_sftp_details['directory'])?$wpdbbkp_sftp_details['directory']:''; |
| 43 |
if ( '' === $directory ) { |
| 44 |
$directory = '/'; |
| 45 |
} |
| 46 |
$wpdbbkp_auth_type_ = isset($wpdbbkp_sftp_details[ 'auth_type' ])?$wpdbbkp_sftp_details[ 'auth_type' ]:'password'; |
| 47 |
if ( is_admin() ) { |
| 48 |
// If user has WP manage options permissions. |
| 49 |
if ( current_user_can( 'manage_options' ) ) { |
| 50 |
// Connect to host ONLY if the 2 security conditions are valid / met. |
| 51 |
$sftp = new SFTP( $host , $port ); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
if ( ! $sftp ) { |
| 56 |
$trouble = 'Could not connect to SFTP server.<br />Please check your SFTP Host and try again.'; |
| 57 |
return $trouble; |
| 58 |
} |
| 59 |
|
| 60 |
if($wpdbbkp_auth_type_=='key'){ |
| 61 |
$key = PublicKeyLoader::load($pkey,$key_pass); |
| 62 |
$result = $sftp->login($user, $key); |
| 63 |
}else{ |
| 64 |
$result = $sftp->login($user, $pass); |
| 65 |
} |
| 66 |
if ( ! $result ) { |
| 67 |
$trouble = 'Connected to the SFTP server but could not log in.<br />Please check your credentials and try again.'; |
| 68 |
return $trouble; |
| 69 |
} |
| 70 |
|
| 71 |
$trouble = 'OK'; |
| 72 |
|
| 73 |
// Lose this connection. |
| 74 |
$sftp->disconnect(); |
| 75 |
return $trouble; |
| 76 |
|
| 77 |
} |
| 78 |
|