132 lines
3.9 KiB
PHP
132 lines
3.9 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Basic logging function for debugging. Allows passing of an object or array for the $data paramater
|
|
*
|
|
* Set the CUSTOM_DEBUG_LOG file in wp-config.php
|
|
*
|
|
*/
|
|
function log_to_file($message = false, $data = false){
|
|
if ($message) {
|
|
$log_File = CUSTOM_DEBUG_LOG;
|
|
|
|
$date = new DateTime();
|
|
$date = $date->format("Y/m/d h:i:s");
|
|
|
|
// Convert arrays and objects to JSON format
|
|
if (is_array($data) || is_object($data)) {
|
|
$data = json_encode($data);
|
|
$message = $message . " " . $data;
|
|
} else if ($data) {
|
|
$message = $message . " " . $data;
|
|
}
|
|
|
|
error_log("[$date] " . $message ."\r\n",3,$log_File);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
* Add a meta box for testing the SSH connection.
|
|
*/
|
|
add_action('add_meta_boxes', function () {
|
|
add_meta_box(
|
|
'test_ssh_connection_box',
|
|
__('Test SSH Connection', 'rl-mailwarmer'),
|
|
'rl_mailwarmer_render_test_ssh_connection_box',
|
|
'server',
|
|
'side',
|
|
'default'
|
|
);
|
|
});
|
|
|
|
/**
|
|
* Render the SSH connection test meta box.
|
|
*
|
|
* @param WP_Post $post The current post object.
|
|
*/
|
|
function rl_mailwarmer_render_test_ssh_connection_box($post)
|
|
{
|
|
// Add a nonce field for security
|
|
wp_nonce_field('test_ssh_connection_nonce', 'test_ssh_connection_nonce_field');
|
|
|
|
// Render the button
|
|
?>
|
|
<form id="test-ssh-connection-form">
|
|
<p>
|
|
<button type="button" id="test-ssh-connection-button" class="button button-primary">
|
|
<?php esc_html_e('Test SSH Connection', 'rl-mailwarmer'); ?>
|
|
</button>
|
|
</p>
|
|
<div id="test-ssh-connection-result"></div>
|
|
</form>
|
|
<?php
|
|
}
|
|
|
|
add_action('admin_enqueue_scripts', function ($hook) {
|
|
global $post;
|
|
|
|
// Ensure this is the Add/Edit Server screen
|
|
if (($hook === 'post.php' || $hook === 'post-new.php') && $post->post_type === 'server') {
|
|
wp_enqueue_script(
|
|
'rl-mailwarmer-test-ssh-js',
|
|
RL_MAILWARMER_URL . 'js/test-ssh-connection.js', // Adjust path as needed
|
|
['jquery'],
|
|
null,
|
|
true
|
|
);
|
|
|
|
wp_localize_script('rl-mailwarmer-test-ssh-js', 'rlMailWarmerTestSSH', [
|
|
'ajax_url' => admin_url('admin-ajax.php'),
|
|
'nonce' => wp_create_nonce('test_ssh_connection_nonce'),
|
|
'post_id' => $post->ID
|
|
]);
|
|
}
|
|
});
|
|
|
|
add_action('wp_ajax_rl_mailwarmer_test_ssh_connection', function () {
|
|
// Verify nonce
|
|
if (!isset($_POST['security']) || !wp_verify_nonce($_POST['security'], 'test_ssh_connection_nonce')) {
|
|
wp_send_json_error(__('Invalid nonce', 'rl-mailwarmer'));
|
|
}
|
|
|
|
// Validate and sanitize input
|
|
$post_id = intval($_POST['post_id']);
|
|
if (!$post_id || get_post_type($post_id) !== 'server') {
|
|
wp_send_json_error(__('Invalid server ID.', 'rl-mailwarmer'));
|
|
}
|
|
|
|
// Fetch server details
|
|
$server_ip = get_post_meta($post_id, 'ip_address', true);
|
|
$server_port = get_post_meta($post_id, 'ssh_port', true);
|
|
$server_user = get_post_meta($post_id, 'username', true);
|
|
$server_key = get_post_meta($post_id, 'ssh_private_key', true);
|
|
|
|
// log_to_file("wp_ajax_rl_mailwarmer_test_ssh_connection - SSH $server_user $server_ip $server_port");
|
|
// log_to_file("wp_ajax_rl_mailwarmer_test_ssh_connection - SSH Private Key: $server_key");
|
|
|
|
if (empty($server_ip) || empty($server_user) || empty($server_key)) {
|
|
wp_send_json_error(__('Missing server credentials.', 'rl-mailwarmer'));
|
|
}
|
|
|
|
// Test SSH connection
|
|
try {
|
|
$ssh = new phpseclib3\Net\SSH2($server_ip, $server_port);
|
|
$key = phpseclib3\Crypt\PublicKeyLoader::loadPrivateKey($server_key);
|
|
|
|
if (!$ssh->login($server_user, $key)) {
|
|
throw new Exception(__('SSH login failed.', 'rl-mailwarmer'));
|
|
}
|
|
|
|
wp_send_json_success(__('SSH connection successful.', 'rl-mailwarmer'));
|
|
} catch (Exception $e) {
|
|
wp_send_json_error($e->getMessage());
|
|
}
|
|
});
|
|
|
|
|