| 1 |
<?php |
| 2 |
|
| 3 |
class easy_basic_authentication_form_class { |
| 4 |
|
| 5 |
public $form_field = []; |
| 6 |
|
| 7 |
public function __construct() |
| 8 |
{ |
| 9 |
// Qui non si traduce nulla. Il plugin parte su plugins_loaded, che viene |
| 10 |
// prima di init: da WordPress 6.7 chiamare __() a quel punto fa scattare |
| 11 |
// la notice "Translation loading was triggered too early". L'elenco dei |
| 12 |
// campi si costruisce quando serve davvero, cioe su admin_init. |
| 13 |
} |
| 14 |
|
| 15 |
/** |
| 16 |
* Definizione dei campi del modulo impostazioni. |
| 17 |
* |
| 18 |
* @return array |
| 19 |
*/ |
| 20 |
public function get_form_fields() |
| 21 |
{ |
| 22 |
if ( ! empty( $this->form_field ) ) { |
| 23 |
return $this->form_field; |
| 24 |
} |
| 25 |
|
| 26 |
$this->form_field = array( |
| 27 |
'0' => array( |
| 28 |
'id' => 'basic-auth-plugin-admin-enable', |
| 29 |
'title' => __('Enable for wp-admin', 'easy-basic-authentication'), |
| 30 |
'callback' => 'basic_auth_plugin_admin_enable_cb', |
| 31 |
), |
| 32 |
'1' => array( |
| 33 |
'id' => 'basic-auth-plugin-enable', |
| 34 |
'title' => __('Enable for the entire site (only if wp-admin is enabled)', 'easy-basic-authentication'), |
| 35 |
'callback' => 'basic_auth_plugin_enable_cb', |
| 36 |
), |
| 37 |
'2' => array( |
| 38 |
'id' => 'basic-auth-plugin-username', |
| 39 |
'title' => __('Username', 'easy-basic-authentication'), |
| 40 |
'callback' => 'basic_auth_plugin_username_cb', |
| 41 |
), |
| 42 |
'3' => array( |
| 43 |
'id' => 'basic-auth-plugin-password', |
| 44 |
'title' => __('Password', 'easy-basic-authentication'), |
| 45 |
'callback' => 'basic_auth_plugin_password_cb', |
| 46 |
), |
| 47 |
'4' => array( |
| 48 |
'id' => 'basic-auth-plugin-admin-log-enable', |
| 49 |
'title' => __('Enable access logs', 'easy-basic-authentication'), |
| 50 |
'callback' => 'basic_auth_plugin_admin_log_enable_cb', |
| 51 |
), |
| 52 |
'5' => array( |
| 53 |
'id' => 'basic-auth-plugin-alert-enable', |
| 54 |
'title' => __('Enable email alert', 'easy-basic-authentication'), |
| 55 |
'callback' => 'basic_auth_plugin_alert_enable_cb', |
| 56 |
), |
| 57 |
'6' => array( |
| 58 |
'id' => 'basic-auth-plugin-email-alert', |
| 59 |
'title' => __('Email for alert', 'easy-basic-authentication'), |
| 60 |
'callback' => 'basic_auth_plugin_alertemail_cb', |
| 61 |
), |
| 62 |
'7' => array( |
| 63 |
'id' => 'basic-auth-plugin-white-list', |
| 64 |
'title' => __('Ip White list', 'easy-basic-authentication'), |
| 65 |
'callback' => 'basic_auth_plugin_whitelist_cb', |
| 66 |
), |
| 67 |
'8' => array( |
| 68 |
'id' => 'basic-auth-plugin-url-white-list', |
| 69 |
'title' => __('Url White list', 'easy-basic-authentication'), |
| 70 |
'callback' => 'basic_auth_plugin_urlwhitelist_cb', |
| 71 |
), |
| 72 |
'9' => array( |
| 73 |
'id' => 'basic-auth-plugin-access-token', |
| 74 |
'title' => __('Access token for crawlers', 'easy-basic-authentication'), |
| 75 |
'callback' => 'basic_auth_plugin_access_token_cb', |
| 76 |
), |
| 77 |
'10' => array( |
| 78 |
'id' => 'basic-auth-plugin-remove-data-after-uninstall', |
| 79 |
'title' => __('Remove plugin data after uninstall', 'easy-basic-authentication'), |
| 80 |
'callback' => 'basic_auth_plugin_remove_data_after_uninstall_cb', |
| 81 |
), |
| 82 |
|
| 83 |
); |
| 84 |
|
| 85 |
return $this->form_field; |
| 86 |
} |
| 87 |
|
| 88 |
public function basic_auth_plugin_settings_init() { |
| 89 |
register_setting( 'basic-auth-plugin-settings', 'basic_auth_plugin_admin_enable', array( 'sanitize_callback' => 'absint' ) ); |
| 90 |
register_setting( 'basic-auth-plugin-settings', 'basic_auth_plugin_enable', array( 'sanitize_callback' => 'absint' ) ); |
| 91 |
register_setting( 'basic-auth-plugin-settings', 'basic_auth_plugin_username', array( 'sanitize_callback' => 'sanitize_text_field' ) ); |
| 92 |
register_setting( 'basic-auth-plugin-settings', 'basic_auth_plugin_admin_log_enable', array( 'sanitize_callback' => 'absint' ) ); |
| 93 |
|
| 94 |
add_settings_section( |
| 95 |
'basic-auth-plugin-section', |
| 96 |
__('Configurations for Easy Basic Authentication', 'easy-basic-authentication'), |
| 97 |
array($this,'basic_auth_plugin_section_cb'), |
| 98 |
'basic-auth-plugin-settings' |
| 99 |
); |
| 100 |
|
| 101 |
foreach($this->get_form_fields() as $field) { |
| 102 |
add_settings_field( |
| 103 |
$field['id'], |
| 104 |
esc_html($field['title']), |
| 105 |
array($this,$field['callback']), |
| 106 |
'basic-auth-plugin-settings', |
| 107 |
'basic-auth-plugin-section' |
| 108 |
); |
| 109 |
} |
| 110 |
|
| 111 |
} |
| 112 |
|
| 113 |
public function basic_auth_plugin_section_cb() { |
| 114 |
echo esc_html__('Configure basic authentication', 'easy-basic-authentication'); |
| 115 |
} |
| 116 |
|
| 117 |
public function basic_auth_plugin_enable_cb() { |
| 118 |
$admin_enable = get_option( 'basic_auth_plugin_admin_enable' ); |
| 119 |
$enable = get_option( 'basic_auth_plugin_enable' ); |
| 120 |
?> |
| 121 |
<input type="checkbox" name="basic_auth_plugin_enable" value="1" <?php checked( $enable, 1 ); ?><?php disabled( !$admin_enable ); ?>> |
| 122 |
<?php |
| 123 |
} |
| 124 |
|
| 125 |
public function basic_auth_plugin_admin_enable_cb() { |
| 126 |
$enable = get_option( 'basic_auth_plugin_admin_enable' ); |
| 127 |
?> |
| 128 |
<input type="checkbox" name="basic_auth_plugin_admin_enable" value="1" <?php checked( $enable, 1 ); ?>> |
| 129 |
<?php |
| 130 |
} |
| 131 |
|
| 132 |
public function basic_auth_plugin_username_cb() { |
| 133 |
$username = get_option( 'basic_auth_plugin_username' ); |
| 134 |
$this->printInputText("text","basic_auth_plugin_username",esc_attr( $username ),'','off'); |
| 135 |
} |
| 136 |
|
| 137 |
public function basic_auth_plugin_password_cb() { |
| 138 |
$password = get_option( 'basic_auth_plugin_password' ); |
| 139 |
?> |
| 140 |
<input type="password" name="basic_auth_plugin_password" value="" placeholder="<?php esc_attr_e('Enter the password', 'easy-basic-authentication'); ?>" autocomplete="off"> |
| 141 |
<p class="description"><?php esc_html_e('Leave blank to keep the current password.', 'easy-basic-authentication'); ?></p> |
| 142 |
<?php |
| 143 |
} |
| 144 |
|
| 145 |
public function basic_auth_plugin_admin_log_enable_cb() { |
| 146 |
$enable = get_option( 'basic_auth_plugin_admin_log_enable' ); |
| 147 |
?> |
| 148 |
<input type="checkbox" name="basic_auth_plugin_admin_log_enable" value="1" <?php checked( $enable, 1 ); ?>> |
| 149 |
<?php |
| 150 |
} |
| 151 |
|
| 152 |
public function basic_auth_plugin_alert_enable_cb() { |
| 153 |
$enable = get_option( 'basic_auth_plugin_alert_enable' ); |
| 154 |
?> |
| 155 |
<input type="checkbox" name="basic_auth_plugin_alert_enable" value="1" <?php checked( $enable, 1 ); ?>> |
| 156 |
<?php |
| 157 |
} |
| 158 |
|
| 159 |
public function basic_auth_plugin_alertemail_cb() { |
| 160 |
$email_alert = get_option( 'basic_auth_plugin_alertemail' ); |
| 161 |
$this->printInputText("text","basic_auth_plugin_alertemail",esc_attr( $email_alert ),__('Enter the email', 'easy-basic-authentication')); |
| 162 |
} |
| 163 |
|
| 164 |
public function basic_auth_plugin_access_token_cb() { |
| 165 |
$token = (string) get_option( 'basic_auth_plugin_access_token', '' ); |
| 166 |
?> |
| 167 |
<input type="text" name="basic_auth_plugin_access_token" value="<?php echo esc_attr( $token ); ?>" |
| 168 |
class="regular-text code" readonly onclick="this.select();" autocomplete="off"> |
| 169 |
<label style="display:block;margin-top:6px;"> |
| 170 |
<input type="checkbox" name="basic_auth_plugin_generate_token" value="1"> |
| 171 |
<?php echo '' === $token |
| 172 |
? esc_html__( 'Generate a token when saving', 'easy-basic-authentication' ) |
| 173 |
: esc_html__( 'Replace this token with a new one when saving', 'easy-basic-authentication' ); ?> |
| 174 |
</label> |
| 175 |
<?php if ( '' !== $token ) : ?> |
| 176 |
<label style="display:block;margin-top:4px;"> |
| 177 |
<input type="checkbox" name="basic_auth_plugin_delete_token" value="1"> |
| 178 |
<?php esc_html_e( 'Delete the token and close this door', 'easy-basic-authentication' ); ?> |
| 179 |
</label> |
| 180 |
<?php endif; ?> |
| 181 |
<p class="description"> |
| 182 |
<?php esc_html_e( 'Lets a crawler or an external service through without a username and password. Send it as an X-Basic-Auth-Token header, or as Authorization: Bearer.', 'easy-basic-authentication' ); ?> |
| 183 |
<br> |
| 184 |
<strong><?php esc_html_e( 'It opens the site only — the admin area and the login page stay protected. Anyone holding this token can read your whole site, so treat it as a password.', 'easy-basic-authentication' ); ?></strong> |
| 185 |
</p> |
| 186 |
<?php |
| 187 |
} |
| 188 |
|
| 189 |
public function basic_auth_plugin_whitelist_cb() { |
| 190 |
$white_list = get_option( 'basic_auth_plugin_whitelist' ); |
| 191 |
$this->printInputText("text","basic_auth_plugin_whitelist",esc_attr( $white_list ),__('White list, separated by comma. Ex. 127.0.0.1, 1.1.1.1/20, 1.1.0.0 - 1.1.0.255', 'easy-basic-authentication')); |
| 192 |
} |
| 193 |
|
| 194 |
public function basic_auth_plugin_urlwhitelist_cb() { |
| 195 |
$white_list = get_option( 'basic_auth_plugin_urlwhitelist' ); |
| 196 |
$this->printInputText("text","basic_auth_plugin_urlwhitelist",esc_attr( $white_list ),__('Url white list, separated by comma. Ex. /json/wp-, www.google.it,', 'easy-basic-authentication')); |
| 197 |
} |
| 198 |
|
| 199 |
public function basic_auth_plugin_remove_data_after_uninstall_cb() { |
| 200 |
$enable = get_option( 'basic_auth_plugin_remove_data_after_uninstall' ); |
| 201 |
?> |
| 202 |
<input type="checkbox" name="basic_auth_plugin_remove_data_after_uninstall" value="1" <?php checked( $enable, 1 ); ?>> |
| 203 |
<?php |
| 204 |
} |
| 205 |
|
| 206 |
public function printInputText($type, $name, $value='', $placeholder = '', $autocomplete = 'off') { |
| 207 |
echo "<input type='" . esc_attr($type) . "' name='" . esc_attr($name) . "' value='" . esc_attr($value) . "' placeholder='" . esc_attr($placeholder) . "' autocomplete='" . esc_attr($autocomplete) . "' >"; |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
public function basic_auth_plugin_save_settings($param) { |
| 212 |
if ( isset( $param['eba_submit'] ) ) { |
| 213 |
if ( ! isset( $param['_wpnonce'] ) || ! wp_verify_nonce( $param['_wpnonce'], 'basic-auth-plugin-settings-options' ) ) { |
| 214 |
return; |
| 215 |
} |
| 216 |
$admin_enable = isset( $param['basic_auth_plugin_admin_enable'] ) ? 1 : 0; |
| 217 |
$enable = isset( $param['basic_auth_plugin_enable'] ) ? 1 : 0; |
| 218 |
$username = sanitize_text_field( $param['basic_auth_plugin_username'] ); |
| 219 |
$password = isset( $param['basic_auth_plugin_password'] ) ? wp_unslash( $param['basic_auth_plugin_password'] ) : ''; |
| 220 |
$log_enable = isset( $param['basic_auth_plugin_admin_log_enable'] ) ? 1 : 0; |
| 221 |
$alert_enable = isset( $param['basic_auth_plugin_alert_enable'] ) ? 1 : 0; |
| 222 |
$alert_email = sanitize_text_field( $param['basic_auth_plugin_alertemail'] ); |
| 223 |
$white_list = sanitize_text_field( $param['basic_auth_plugin_whitelist'] ); |
| 224 |
$remove_data = isset( $param['basic_auth_plugin_remove_data_after_uninstall'] ) ? 1 : 0; |
| 225 |
$url_white_list = sanitize_text_field( $param['basic_auth_plugin_urlwhitelist'] ); |
| 226 |
|
| 227 |
if ( empty( $username ) ) { |
| 228 |
add_settings_error( |
| 229 |
'basic_auth_plugin_username', |
| 230 |
'basic_auth_plugin_username_error', |
| 231 |
__('Username cannot be empty', 'easy-basic-authentication'), |
| 232 |
'error' |
| 233 |
); |
| 234 |
return; |
| 235 |
} |
| 236 |
|
| 237 |
if ( ! empty( $password ) ) { |
| 238 |
$hashed_password = wp_hash_password( $password ); |
| 239 |
update_option( 'basic_auth_plugin_password', $hashed_password ); |
| 240 |
} |
| 241 |
|
| 242 |
if ( is_email( $alert_email ) || (!$alert_enable && $alert_email=='' ) ) { |
| 243 |
update_option( 'basic_auth_plugin_alertemail', $alert_email ); |
| 244 |
} else { |
| 245 |
add_settings_error( |
| 246 |
'basic_auth_plugin_alertemail', |
| 247 |
'basic_auth_plugin_alertemail_error', |
| 248 |
__('Invalid email address', 'easy-basic-authentication'), |
| 249 |
'error' |
| 250 |
); |
| 251 |
return; |
| 252 |
} |
| 253 |
|
| 254 |
update_option( 'basic_auth_plugin_admin_enable', $admin_enable ); |
| 255 |
update_option( 'basic_auth_plugin_enable', $enable ); |
| 256 |
update_option( 'basic_auth_plugin_username', $username ); |
| 257 |
update_option( 'basic_auth_plugin_admin_log_enable', $log_enable ); |
| 258 |
update_option( 'basic_auth_plugin_alert_enable', $alert_enable ); |
| 259 |
update_option( 'basic_auth_plugin_remove_data_after_uninstall', $remove_data ); |
| 260 |
update_option( 'basic_auth_plugin_urlwhitelist', $url_white_list ); |
| 261 |
|
| 262 |
// Il token non si scrive a mano: o lo si genera o lo si cancella. Cosi |
| 263 |
// non puo essere corto o indovinabile. |
| 264 |
if ( ! empty( $param['basic_auth_plugin_delete_token'] ) ) { |
| 265 |
delete_option( 'basic_auth_plugin_access_token' ); |
| 266 |
} elseif ( ! empty( $param['basic_auth_plugin_generate_token'] ) ) { |
| 267 |
update_option( 'basic_auth_plugin_access_token', wp_generate_password( 48, false, false ) ); |
| 268 |
} |
| 269 |
|
| 270 |
if( $this->validateIpList( $white_list ) || strlen($white_list) == 0 ) { |
| 271 |
update_option( 'basic_auth_plugin_whitelist', $white_list ); |
| 272 |
} else { |
| 273 |
add_settings_error( |
| 274 |
'basic_auth_plugin_whitelist', |
| 275 |
'basic_auth_plugin_whitelist_error', |
| 276 |
__('Invalid IP list format. Please enter valid IP addresses separated by commas.', 'easy-basic-authentication'), |
| 277 |
'error' |
| 278 |
); |
| 279 |
return; |
| 280 |
|
| 281 |
} |
| 282 |
|
| 283 |
$this->send_credentials_email($username, $password); |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
public function basic_auth_plugin_settings_page() { |
| 288 |
include plugin_dir_path( __FILE__ ) . '../template/settings_page.php'; |
| 289 |
} |
| 290 |
|
| 291 |
public function validateIpList($white_list) { |
| 292 |
$ips = explode(',', $white_list); |
| 293 |
foreach ($ips as $ip) { |
| 294 |
$ip = trim($ip); |
| 295 |
|
| 296 |
// � |
| 297 |
Valida IP singoli |
| 298 |
if (filter_var($ip, FILTER_VALIDATE_IP)) { |
| 299 |
continue; |
| 300 |
} |
| 301 |
|
| 302 |
// � |
| 303 |
Valida subnet CIDR (es. 1.1.1.1/20) |
| 304 |
if (preg_match('/^(\d{1,3}\.){3}\d{1,3}\/\d{1,2}$/', $ip)) { |
| 305 |
list($subnet, $mask) = explode('/', $ip); |
| 306 |
if (filter_var($subnet, FILTER_VALIDATE_IP) && $mask >= 0 && $mask <= 32) { |
| 307 |
continue; |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
// � |
| 312 |
Valida range IP (es. 1.1.0.0 - 1.1.0.255) |
| 313 |
if (preg_match('/^(\d{1,3}\.){3}\d{1,3}\s*-\s*(\d{1,3}\.){3}\d{1,3}$/', $ip)) { |
| 314 |
list($start_ip, $end_ip) = array_map('trim', explode('-', $ip)); |
| 315 |
if (filter_var($start_ip, FILTER_VALIDATE_IP) && filter_var($end_ip, FILTER_VALIDATE_IP)) { |
| 316 |
continue; |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
// ❌ Se non passa nessuna validazione, ritorna false |
| 321 |
return false; |
| 322 |
} |
| 323 |
|
| 324 |
return true; |
| 325 |
} |
| 326 |
|
| 327 |
public function send_credentials_email($username, $password) { |
| 328 |
$admin_email = get_option('admin_email'); |
| 329 |
$site_url = home_url(); |
| 330 |
$plugin_url = 'https://wordpress.org/plugins/easy-basic-authentication/'; |
| 331 |
|
| 332 |
$subject = __('Basic Authentication Credentials Updated', 'easy-basic-authentication'); |
| 333 |
|
| 334 |
$message = '<html><body>'; |
| 335 |
$message .= '<p>' . __('Hi,', 'easy-basic-authentication') . '</p>'; |
| 336 |
$message .= '<p>' . __('The basic authentication credentials for your site have been updated.', 'easy-basic-authentication') . '</p>'; |
| 337 |
$message .= '<ul style="list-style-type: none; padding-left: 0;">' . |
| 338 |
'<li>' . __('Site URL: ', 'easy-basic-authentication') . '<strong>' . esc_url($site_url) . '</strong></li>' . |
| 339 |
'<li>' . __('Username: ', 'easy-basic-authentication') . '<strong>' . esc_html($username) . '</strong></li>' . |
| 340 |
'<li>' . __('Password: ', 'easy-basic-authentication') . '<strong>' . __('[updated — not shown for security]', 'easy-basic-authentication') . '</strong></li>' . |
| 341 |
'</ul>'; |
| 342 |
$message .= '<p>' . sprintf( |
| 343 |
/* translators: tag "a" and link */ |
| 344 |
__('For more information about this plugin, visit: %s', 'easy-basic-authentication'), |
| 345 |
'<a href="' . esc_url($plugin_url) . '">' . esc_url($plugin_url) . '</a>' |
| 346 |
) . '</p>'; |
| 347 |
$message .= '<p>' . __('Grazie and buona giornata,', 'easy-basic-authentication') . '</p>'; |
| 348 |
$message .= '<p>' . __('The Easy Basic Authentication Plugin Team', 'easy-basic-authentication') . '</p>'; |
| 349 |
$message .= '</body></html>'; |
| 350 |
|
| 351 |
$headers = array( |
| 352 |
'Content-Type: text/html; charset=UTF-8', |
| 353 |
'From: ' . $admin_email, |
| 354 |
); |
| 355 |
|
| 356 |
wp_mail($admin_email, $subject, $message, $headers); |
| 357 |
} |
| 358 |
|
| 359 |
|
| 360 |
} |