| 1 |
<?php |
| 2 |
|
| 3 |
class easy_basic_authentication_compatcheck_class { |
| 4 |
|
| 5 |
public function register_hooks() { |
| 6 |
add_action('wp_ajax_eba_test_auth', array($this, 'ajax_test_auth')); |
| 7 |
} |
| 8 |
|
| 9 |
public function ajax_test_auth() { |
| 10 |
header('Content-Type: application/json'); |
| 11 |
|
| 12 |
$response = [ |
| 13 |
'success' => false, |
| 14 |
'message' => __('Unknown error.', 'easy-basic-authentication'), |
| 15 |
]; |
| 16 |
|
| 17 |
if (!isset($_SERVER['PHP_AUTH_USER'])) { |
| 18 |
$response['message'] = __('Authorization header not received. It may have been blocked by the browser or the server (especially on HTTP).', 'easy-basic-authentication'); |
| 19 |
} else { |
| 20 |
// Qui la "password" e un nonce alfanumerico generato da noi, quindi a |
| 21 |
// differenza del percorso di autenticazione vero si puo sanitizzare. |
| 22 |
$received_user = sanitize_text_field( wp_unslash( $_SERVER['PHP_AUTH_USER'] ) ); |
| 23 |
$received_token = isset( $_SERVER['PHP_AUTH_PW'] ) |
| 24 |
? sanitize_text_field( wp_unslash( $_SERVER['PHP_AUTH_PW'] ) ) |
| 25 |
: ''; |
| 26 |
|
| 27 |
if ( |
| 28 |
$received_user === 'eba-compat-test' && |
| 29 |
wp_verify_nonce($received_token, 'eba_compat_test') |
| 30 |
) { |
| 31 |
$response['success'] = true; |
| 32 |
$response['message'] = __('Authentication received successfully. Basic Auth is compatible!', 'easy-basic-authentication'); |
| 33 |
} else { |
| 34 |
$response['message'] = __('Authorization header received but could not be verified. Please try again.', 'easy-basic-authentication'); |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
echo json_encode($response); |
| 39 |
exit; |
| 40 |
} |
| 41 |
|
| 42 |
public static function render_button_html() { |
| 43 |
$test_nonce = wp_create_nonce('eba_compat_test'); |
| 44 |
?> |
| 45 |
<h2><?php esc_html_e('Compatibility Test', 'easy-basic-authentication'); ?></h2> |
| 46 |
<p><?php esc_html_e('Click the button below to check if your server correctly supports Basic Authentication.', 'easy-basic-authentication'); ?></p> |
| 47 |
<button id="eba-test-button" class="button button-secondary"><?php esc_html_e('Run Compatibility Test', 'easy-basic-authentication'); ?></button> |
| 48 |
<div id="eba-test-result" style="margin-top: 1em;"></div> |
| 49 |
<script> |
| 50 |
document.getElementById('eba-test-button').addEventListener('click', function(e) { |
| 51 |
e.preventDefault(); |
| 52 |
|
| 53 |
const testToken = <?php echo json_encode($test_nonce); ?>; |
| 54 |
const resultBox = document.getElementById('eba-test-result'); |
| 55 |
|
| 56 |
resultBox.innerHTML = '⌛ ' + <?php echo json_encode(__('Waiting for response...', 'easy-basic-authentication')); ?>; |
| 57 |
|
| 58 |
fetch(ajaxurl + '?action=eba_test_auth', { |
| 59 |
headers: { |
| 60 |
'Authorization': 'Basic ' + btoa('eba-compat-test:' + testToken) |
| 61 |
} |
| 62 |
}) |
| 63 |
.then(response => response.text()) |
| 64 |
.then(text => { |
| 65 |
let data; |
| 66 |
try { |
| 67 |
data = JSON.parse(text); |
| 68 |
} catch (e) { |
| 69 |
resultBox.innerHTML = '<span style="color: red;">✘ ' + <?php echo json_encode(__('Invalid JSON response.', 'easy-basic-authentication')); ?> + '</span>'; |
| 70 |
console.error('Invalid JSON:', text); |
| 71 |
return; |
| 72 |
} |
| 73 |
|
| 74 |
if (data.success) { |
| 75 |
resultBox.innerHTML = '<span style="color: green;">✔ ' + data.message + '</span>'; |
| 76 |
} else { |
| 77 |
resultBox.innerHTML = '<span style="color: red;">✘ ' + data.message + '</span>'; |
| 78 |
} |
| 79 |
}) |
| 80 |
.catch(error => { |
| 81 |
resultBox.innerHTML = '<span style="color: red;">' + <?php echo json_encode(__('Error: ', 'easy-basic-authentication')); ?> + error.message + '</span>'; |
| 82 |
}); |
| 83 |
}); |
| 84 |
</script> |
| 85 |
<?php |
| 86 |
} |
| 87 |
} |
| 88 |
|