PluginProbe
Easy Basic Authentication – Add basic auth to site or admin area / 4.1.0
Easy Basic Authentication – Add basic auth to site or admin area v4.1.0
4.1.0 trunk 1.1 1.2 1.3 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.4 1.4.1 1.5 1.5.1 1.5.2 1.5.3 1.6 1.6.1 1.6.2 1.6.3 1.6.4 1.7 1.7.1 1.8 1.8.1 All 51 releases
← All changes | class/easy-basic-authentication-form-class.php +187 -86 1.64.1.0 View file →
@@ -1,62 +1,72 @@
1 1 <?php
2 2
3 3 class easy_basic_authentication_form_class {
4 4
5 - const FORM_FIELD = array(
6 - '0' => array(
7 - 'id' => 'basic-auth-plugin-admin-enable',
8 - 'title' => 'Enable for wp-admin',
9 - 'callback' => 'basic_auth_plugin_admin_enable_cb',
10 - ),
11 - '1' => array(
12 - 'id' => 'basic-auth-plugin-enable',
13 - 'title' => 'Enable for the entire site (only if wp-admin is enabled)',
14 - 'callback' => 'basic_auth_plugin_enable_cb',
15 - ),
16 - '2' => array(
17 - 'id' => 'basic-auth-plugin-username',
18 - 'title' => 'Username',
19 - 'callback' => 'basic_auth_plugin_username_cb',
20 - ),
21 - '3' => array(
22 - 'id' => 'basic-auth-plugin-password',
23 - 'title' => 'Password',
24 - 'callback' => 'basic_auth_plugin_password_cb',
25 - ),
26 - '4' => array(
27 - 'id' => 'basic-auth-plugin-admin-log-enable',
28 - 'title' => 'Enable access logs',
29 - 'callback' => 'basic_auth_plugin_admin_log_enable_cb',
30 - ),
31 - '5' => array(
32 - 'id' => 'basic-auth-plugin-alert-enable',
33 - 'title' => 'Enable email alert',
34 - 'callback' => 'basic_auth_plugin_alert_enable_cb',
35 - ),
36 - '6' => array(
37 - 'id' => 'basic-auth-plugin-email-alert',
38 - 'title' => 'Email for alert',
39 - 'callback' => 'basic_auth_plugin_alertemail_cb',
40 - ),
41 - '7' => array(
42 - 'id' => 'basic-auth-plugin-white-list',
43 - 'title' => 'Ip White list',
44 - 'callback' => 'basic_auth_plugin_whitelist_cb',
45 - )
46 - );
5 + public $form_field = [];
47 6
48 7 public function __construct()
49 - {
50 -
8 + {
9 + $this->form_field = array(
10 + '0' => array(
11 + 'id' => 'basic-auth-plugin-admin-enable',
12 + 'title' => __('Enable for wp-admin', 'easy-basic-authentication'),
13 + 'callback' => 'basic_auth_plugin_admin_enable_cb',
14 + ),
15 + '1' => array(
16 + 'id' => 'basic-auth-plugin-enable',
17 + 'title' => __('Enable for the entire site (only if wp-admin is enabled)', 'easy-basic-authentication'),
18 + 'callback' => 'basic_auth_plugin_enable_cb',
19 + ),
20 + '2' => array(
21 + 'id' => 'basic-auth-plugin-username',
22 + 'title' => __('Username', 'easy-basic-authentication'),
23 + 'callback' => 'basic_auth_plugin_username_cb',
24 + ),
25 + '3' => array(
26 + 'id' => 'basic-auth-plugin-password',
27 + 'title' => __('Password', 'easy-basic-authentication'),
28 + 'callback' => 'basic_auth_plugin_password_cb',
29 + ),
30 + '4' => array(
31 + 'id' => 'basic-auth-plugin-admin-log-enable',
32 + 'title' => __('Enable access logs', 'easy-basic-authentication'),
33 + 'callback' => 'basic_auth_plugin_admin_log_enable_cb',
34 + ),
35 + '5' => array(
36 + 'id' => 'basic-auth-plugin-alert-enable',
37 + 'title' => __('Enable email alert', 'easy-basic-authentication'),
38 + 'callback' => 'basic_auth_plugin_alert_enable_cb',
39 + ),
40 + '6' => array(
41 + 'id' => 'basic-auth-plugin-email-alert',
42 + 'title' => __('Email for alert', 'easy-basic-authentication'),
43 + 'callback' => 'basic_auth_plugin_alertemail_cb',
44 + ),
45 + '7' => array(
46 + 'id' => 'basic-auth-plugin-white-list',
47 + 'title' => __('Ip White list', 'easy-basic-authentication'),
48 + 'callback' => 'basic_auth_plugin_whitelist_cb',
49 + ),
50 + '8' => array(
51 + 'id' => 'basic-auth-plugin-url-white-list',
52 + 'title' => __('Url White list', 'easy-basic-authentication'),
53 + 'callback' => 'basic_auth_plugin_urlwhitelist_cb',
54 + ),
55 + '9' => array(
56 + 'id' => 'basic-auth-plugin-remove-data-after-uninstall',
57 + 'title' => __('Remove plugin data after uninstall', 'easy-basic-authentication'),
58 + 'callback' => 'basic_auth_plugin_remove_data_after_uninstall_cb',
59 + ),
60 +
61 + );
51 62 }
52 63
53 64 public function basic_auth_plugin_settings_init() {
54 - register_setting( 'basic-auth-plugin-settings', 'basic_auth_plugin_admin_enable' );
55 - register_setting( 'basic-auth-plugin-settings', 'basic_auth_plugin_enable' );
56 - register_setting( 'basic-auth-plugin-settings', 'basic_auth_plugin_username' );
57 - register_setting( 'basic-auth-plugin-settings', 'basic_auth_plugin_admin_log_enable' );
58 - register_setting( 'basic-auth-plugin-settings', 'basic_auth_plugin_whitelist' );
65 + register_setting( 'basic-auth-plugin-settings', 'basic_auth_plugin_admin_enable', array( 'sanitize_callback' => 'absint' ) );
66 + register_setting( 'basic-auth-plugin-settings', 'basic_auth_plugin_enable', array( 'sanitize_callback' => 'absint' ) );
67 + register_setting( 'basic-auth-plugin-settings', 'basic_auth_plugin_username', array( 'sanitize_callback' => 'sanitize_text_field' ) );
68 + register_setting( 'basic-auth-plugin-settings', 'basic_auth_plugin_admin_log_enable', array( 'sanitize_callback' => 'absint' ) );
59 69
60 70 add_settings_section(
61 71 'basic-auth-plugin-section',
62 72 __('Configurations for Easy Basic Authentication', 'easy-basic-authentication'),
@@ -63,12 +73,12 @@
63 73 array($this,'basic_auth_plugin_section_cb'),
64 74 'basic-auth-plugin-settings'
65 75 );
66 76
67 - foreach(self::FORM_FIELD as $field) {
77 + foreach($this->form_field as $field) {
68 78 add_settings_field(
69 79 $field['id'],
70 - __($field['title'], 'easy-basic-authentication'),
80 + esc_html($field['title']),
71 81 array($this,$field['callback']),
72 82 'basic-auth-plugin-settings',
73 83 'basic-auth-plugin-section'
74 84 );
@@ -76,9 +86,9 @@
76 86
77 87 }
78 88
79 89 public function basic_auth_plugin_section_cb() {
80 - echo __('Configure basic authentication', 'easy-basic-authentication');
90 + echo esc_html__('Configure basic authentication', 'easy-basic-authentication');
81 91 }
82 92
83 93 public function basic_auth_plugin_enable_cb() {
84 94 $admin_enable = get_option( 'basic_auth_plugin_admin_enable' );
@@ -96,16 +106,17 @@
96 106 }
97 107
98 108 public function basic_auth_plugin_username_cb() {
99 109 $username = get_option( 'basic_auth_plugin_username' );
100 - $this->printInputText("text","basic_auth_plugin_username",esc_attr( $username ));
110 + $this->printInputText("text","basic_auth_plugin_username",esc_attr( $username ),'','off');
101 111 }
102 112
103 113 public function basic_auth_plugin_password_cb() {
104 - $password = get_option( 'basic_auth_plugin_password' )
105 - ? __('Password entered', 'easy-basic-authentication')
106 - : __('Enter the password', 'easy-basic-authentication');
107 - $this->printInputText("password","basic_auth_plugin_password",'',$password);
114 + $password = get_option( 'basic_auth_plugin_password' );
115 + ?>
116 + <input type="password" name="basic_auth_plugin_password" value="" placeholder="<?php esc_attr_e('Enter the password', 'easy-basic-authentication'); ?>" autocomplete="off">
117 + <p class="description"><?php esc_html_e('Leave blank to keep the current password.', 'easy-basic-authentication'); ?></p>
118 + <?php
108 119 }
109 120
110 121 public function basic_auth_plugin_admin_log_enable_cb() {
111 122 $enable = get_option( 'basic_auth_plugin_admin_log_enable' );
@@ -127,35 +138,69 @@
127 138 }
128 139
129 140 public function basic_auth_plugin_whitelist_cb() {
130 141 $white_list = get_option( 'basic_auth_plugin_whitelist' );
131 - $this->printInputText("text","basic_auth_plugin_whitelist",esc_attr( $white_list ),__('White list, separated by comma', 'easy-basic-authentication'));
142 + $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'));
132 143 }
144 +
145 + public function basic_auth_plugin_urlwhitelist_cb() {
146 + $white_list = get_option( 'basic_auth_plugin_urlwhitelist' );
147 + $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'));
148 + }
133 149
134 - public function printInputText($tipe, $name, $value='', $placeholder = '') {
135 - echo "<input type='$tipe' name='$name' value='$value' placeholder='$placeholder'>";
150 + public function basic_auth_plugin_remove_data_after_uninstall_cb() {
151 + $enable = get_option( 'basic_auth_plugin_remove_data_after_uninstall' );
152 + ?>
153 + <input type="checkbox" name="basic_auth_plugin_remove_data_after_uninstall" value="1" <?php checked( $enable, 1 ); ?>>
154 + <?php
136 155 }
137 156
157 + public function printInputText($type, $name, $value='', $placeholder = '', $autocomplete = 'off') {
158 + echo "<input type='" . esc_attr($type) . "' name='" . esc_attr($name) . "' value='" . esc_attr($value) . "' placeholder='" . esc_attr($placeholder) . "' autocomplete='" . esc_attr($autocomplete) . "' >";
159 + }
160 +
161 +
138 162 public function basic_auth_plugin_save_settings($param) {
139 163 if ( isset( $param['eba_submit'] ) ) {
164 + if ( ! isset( $param['_wpnonce'] ) || ! wp_verify_nonce( $param['_wpnonce'], 'basic-auth-plugin-settings-options' ) ) {
165 + return;
166 + }
140 167 $admin_enable = isset( $param['basic_auth_plugin_admin_enable'] ) ? 1 : 0;
141 168 $enable = isset( $param['basic_auth_plugin_enable'] ) ? 1 : 0;
142 169 $username = sanitize_text_field( $param['basic_auth_plugin_username'] );
143 - $password = sanitize_text_field( $param['basic_auth_plugin_password'] );
170 + $password = isset( $param['basic_auth_plugin_password'] ) ? wp_unslash( $param['basic_auth_plugin_password'] ) : '';
144 171 $log_enable = isset( $param['basic_auth_plugin_admin_log_enable'] ) ? 1 : 0;
145 172 $alert_enable = isset( $param['basic_auth_plugin_alert_enable'] ) ? 1 : 0;
146 173 $alert_email = sanitize_text_field( $param['basic_auth_plugin_alertemail'] );
147 174 $white_list = sanitize_text_field( $param['basic_auth_plugin_whitelist'] );
175 + $remove_data = isset( $param['basic_auth_plugin_remove_data_after_uninstall'] ) ? 1 : 0;
176 + $url_white_list = sanitize_text_field( $param['basic_auth_plugin_urlwhitelist'] );
148 177
149 - if ( !is_email( $alert_email ) ) {
178 + if ( empty( $username ) ) {
150 179 add_settings_error(
180 + 'basic_auth_plugin_username',
181 + 'basic_auth_plugin_username_error',
182 + __('Username cannot be empty', 'easy-basic-authentication'),
183 + 'error'
184 + );
185 + return;
186 + }
187 +
188 + if ( ! empty( $password ) ) {
189 + $hashed_password = wp_hash_password( $password );
190 + update_option( 'basic_auth_plugin_password', $hashed_password );
191 + }
192 +
193 + if ( is_email( $alert_email ) || (!$alert_enable && $alert_email=='' ) ) {
194 + update_option( 'basic_auth_plugin_alertemail', $alert_email );
195 + } else {
196 + add_settings_error(
151 197 'basic_auth_plugin_alertemail',
152 198 'basic_auth_plugin_alertemail_error',
153 199 __('Invalid email address', 'easy-basic-authentication'),
154 200 'error'
155 201 );
156 - } else {
157 - update_option( 'basic_auth_plugin_alertemail', $alert_email );
202 + return;
158 203 }
159 204
160 205 update_option( 'basic_auth_plugin_admin_enable', $admin_enable );
161 206 update_option( 'basic_auth_plugin_enable', $enable );
@@ -161,26 +206,25 @@
161 206 update_option( 'basic_auth_plugin_enable', $enable );
162 207 update_option( 'basic_auth_plugin_username', $username );
163 208 update_option( 'basic_auth_plugin_admin_log_enable', $log_enable );
164 209 update_option( 'basic_auth_plugin_alert_enable', $alert_enable );
165 -
210 + update_option( 'basic_auth_plugin_remove_data_after_uninstall', $remove_data );
211 + update_option( 'basic_auth_plugin_urlwhitelist', $url_white_list );
166 212
167 - if (strlen($white_list)) {
168 - if( $this->validateIpList( $white_list ) ) {
169 - update_option( 'basic_auth_plugin_whitelist', $white_list );
170 - } else {
171 - add_settings_error(
172 - 'basic_auth_plugin_whitelist',
173 - 'basic_auth_plugin_whitelist_error',
174 - __('Invalid whitelist format. Please enter valid ip addresses separated by commas.', 'easy-basic-authentication'),
175 - 'error'
176 - );
177 - }
213 + if( $this->validateIpList( $white_list ) || strlen($white_list) == 0 ) {
214 + update_option( 'basic_auth_plugin_whitelist', $white_list );
215 + } else {
216 + add_settings_error(
217 + 'basic_auth_plugin_whitelist',
218 + 'basic_auth_plugin_whitelist_error',
219 + __('Invalid IP list format. Please enter valid IP addresses separated by commas.', 'easy-basic-authentication'),
220 + 'error'
221 + );
222 + return;
223 +
178 224 }
179 - if ( ! empty( $password ) ) {
180 - $hashed_password = wp_hash_password( $password );
181 - update_option( 'basic_auth_plugin_password', $hashed_password );
182 - }
225 +
226 + $this->send_credentials_email($username, $password);
183 227 }
184 228 }
185 229
186 230 public function basic_auth_plugin_settings_page() {
@@ -187,13 +231,70 @@
187 231 include plugin_dir_path( __FILE__ ) . '../template/settings_page.php';
188 232 }
189 233
190 234 public function validateIpList($white_list) {
191 - $pattern = '/^(\s*(?:\d{1,3}\.){3}\d{1,3}\s*(?:,\s*|$))+$/';
192 -
193 - if ( preg_match( $pattern, $white_list ) ) {
194 - return true;
235 + $ips = explode(',', $white_list);
236 + foreach ($ips as $ip) {
237 + $ip = trim($ip);
238 +
239 + // ✅ Valida IP singoli
240 + if (filter_var($ip, FILTER_VALIDATE_IP)) {
241 + continue;
242 + }
243 +
244 + // ✅ Valida subnet CIDR (es. 1.1.1.1/20)
245 + if (preg_match('/^(\d{1,3}\.){3}\d{1,3}\/\d{1,2}$/', $ip)) {
246 + list($subnet, $mask) = explode('/', $ip);
247 + if (filter_var($subnet, FILTER_VALIDATE_IP) && $mask >= 0 && $mask <= 32) {
248 + continue;
249 + }
250 + }
251 +
252 + // ✅ Valida range IP (es. 1.1.0.0 - 1.1.0.255)
253 + if (preg_match('/^(\d{1,3}\.){3}\d{1,3}\s*-\s*(\d{1,3}\.){3}\d{1,3}$/', $ip)) {
254 + list($start_ip, $end_ip) = array_map('trim', explode('-', $ip));
255 + if (filter_var($start_ip, FILTER_VALIDATE_IP) && filter_var($end_ip, FILTER_VALIDATE_IP)) {
256 + continue;
257 + }
258 + }
259 +
260 + // ❌ Se non passa nessuna validazione, ritorna false
261 + return false;
195 262 }
196 - return false;
197 -
263 +
264 + return true;
198 265 }
266 +
267 + public function send_credentials_email($username, $password) {
268 + $admin_email = get_option('admin_email');
269 + $site_url = home_url();
270 + $plugin_url = 'https://wordpress.org/plugins/easy-basic-authentication/';
271 +
272 + $subject = __('Basic Authentication Credentials Updated', 'easy-basic-authentication');
273 +
274 + $message = '<html><body>';
275 + $message .= '<p>' . __('Hi,', 'easy-basic-authentication') . '</p>';
276 + $message .= '<p>' . __('The basic authentication credentials for your site have been updated.', 'easy-basic-authentication') . '</p>';
277 + $message .= '<ul style="list-style-type: none; padding-left: 0;">' .
278 + '<li>' . __('Site URL: ', 'easy-basic-authentication') . '<strong>' . esc_url($site_url) . '</strong></li>' .
279 + '<li>' . __('Username: ', 'easy-basic-authentication') . '<strong>' . esc_html($username) . '</strong></li>' .
280 + '<li>' . __('Password: ', 'easy-basic-authentication') . '<strong>' . __('[updated — not shown for security]', 'easy-basic-authentication') . '</strong></li>' .
281 + '</ul>';
282 + $message .= '<p>' . sprintf(
283 + /* translators: tag "a" and link */
284 + __('For more information about this plugin, visit: %s', 'easy-basic-authentication'),
285 + '<a href="' . esc_url($plugin_url) . '">' . esc_url($plugin_url) . '</a>'
286 + ) . '</p>';
287 + $message .= '<p>' . __('Grazie and buona giornata,', 'easy-basic-authentication') . '</p>';
288 + $message .= '<p>' . __('The Easy Basic Authentication Plugin Team', 'easy-basic-authentication') . '</p>';
289 + $message .= '</body></html>';
290 +
291 + $headers = array(
292 + 'Content-Type: text/html; charset=UTF-8',
293 + 'From: ' . $admin_email,
294 + );
295 +
296 + wp_mail($admin_email, $subject, $message, $headers);
297 + }
298 +
299 +
199 300 }