PluginProbe
Easy Basic Authentication – Add basic auth to site or admin area / 3.2
Easy Basic Authentication – Add basic auth to site or admin area v3.2
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 1.9 All 50 releases
← All changes | class/easy-basic-authentication-class.php +36 -193 1.43.2 View file →
@@ -1,22 +1,27 @@
1 1 <?php
2 2
3 3 require_once (dirname(__FILE__).'/easy-basic-authentication-log-class.php');
4 4 require_once (dirname(__FILE__).'/easy-basic-authentication-emailalert-class.php');
5 +require_once (dirname(__FILE__).'/easy-basic-authentication-form-class.php');
6 +require_once (dirname(__FILE__).'/easy-basic-authentication-notice-class.php');
5 7
6 8 class easy_basic_authentication_class {
7 9
8 10 private $log;
9 11 private $email;
12 + private $form;
10 13
11 14 public function __construct()
12 15 {
13 16 $this->log = new easy_basic_authentication_log_class();
14 17 $this->email = new easy_basic_authentication_emailalert_class();
18 + $this->form = new easy_basic_authentication_form_class();
19 + $notice = new easy_basic_authentication_notice_class();
15 20
16 21 if(get_option( 'basic_auth_plugin_admin_enable' )) {
17 22 if (in_array($GLOBALS['pagenow'], array('wp-login.php', 'wp-register.php'))) {
18 - add_action( 'init', array($this,'basic_auth_admin') );
23 + add_action( 'init', array($this,'basic_auth_root') );
19 24 }
20 25 }
21 26
22 27 if(get_option( 'basic_auth_plugin_enable' ) && get_option( 'basic_auth_plugin_admin_enable' )){
@@ -23,11 +28,14 @@
23 28 add_action( 'init', array($this,'basic_auth_root') );
24 29 }
25 30
26 31 add_action( 'admin_menu', array($this,'basic_auth_plugin_menu' ));
27 - add_action( 'admin_init', array($this,'basic_auth_plugin_settings_init' ));
32 + add_action( 'admin_init', array($this->form,'basic_auth_plugin_settings_init' ));
28 33
29 - add_action( 'admin_init', array($this,'basic_auth_plugin_save_settings') );
34 + add_action('admin_init', function () {
35 + $post_data = $_POST;
36 + $this->form->basic_auth_plugin_save_settings($post_data);
37 + });
30 38
31 39 }
32 40
33 41 public function basic_auth_root() {
@@ -33,40 +41,41 @@
33 41 public function basic_auth_root() {
34 42 $user = get_option( 'basic_auth_plugin_username' );
35 43 $pass = get_option( 'basic_auth_plugin_password' );
36 44
37 - if ( !isset( $_SERVER['PHP_AUTH_USER'] ) || !isset( $_SERVER['PHP_AUTH_PW'] ) ||
38 - $_SERVER['PHP_AUTH_USER'] != $user || !wp_check_password( $_SERVER['PHP_AUTH_PW'], $pass ) ) {
45 + if ( !isset( $_SERVER['PHP_AUTH_USER'] ) || !isset( $_SERVER['PHP_AUTH_PW'] ) ||
46 + $_SERVER['PHP_AUTH_USER'] != $user || !wp_check_password( wp_unslash( $_SERVER['PHP_AUTH_PW'] ), $pass ) ) {
39 47
40 - $this->basic_auth_action_failed_access();
48 + $this->do_exit(true);
49 + }
50 + }
51 +
52 + public function do_exit($admin_area = false) {
41 53
42 - do_action('basic_auth_before_401');
54 + if (isset($_SERVER['REMOTE_ADDR']) && in_array($_SERVER['REMOTE_ADDR'], $this->getWhiteList())) {
55 + return;
56 + }
57 +
58 + $this->basic_auth_action_failed_access();
59 + do_action('basic_auth_before_401');
60 +
61 + if($admin_area) {
62 + do_action('basic_auth_before_401_admin_area');
43 63
44 64 header( 'WWW-Authenticate: Basic realm="My Website"' );
45 65 header( 'HTTP/1.0 401 Unauthorized' );
46 - echo 'Autenticazione richiesta';
47 66 exit;
67 + } else {
68 + header( 'HTTP/1.1 401 Unauthorized' );
69 + header( 'WWW-Authenticate: Basic realm="Admin Area"' );
70 + exit;
48 71 }
49 72 }
50 -
51 - public function basic_auth_admin() {
52 - $user = get_option( 'basic_auth_plugin_username' );
53 - $pass = get_option( 'basic_auth_plugin_password' );
54 -
55 - if ( !isset( $_SERVER['PHP_AUTH_USER'] ) || !isset( $_SERVER['PHP_AUTH_PW'] ) ||
56 - $_SERVER['PHP_AUTH_USER'] != $user || !wp_check_password( $_SERVER['PHP_AUTH_PW'], $pass ) ) {
57 -
58 - $this->basic_auth_action_failed_access();
59 -
60 - do_action('basic_auth_before_401');
61 73
62 - header( 'HTTP/1.1 401 Unauthorized' );
63 - header( 'WWW-Authenticate: Basic realm="Admin Area"' );
64 - exit;
65 - }
74 + public function getWhiteList() {
75 + return get_option( 'basic_auth_plugin_whitelist' )?explode(',',get_option( 'basic_auth_plugin_whitelist' )):[];
66 76 }
67 -
68 -
77 +
69 78 public function basic_auth_plugin_menu() {
70 79 add_menu_page(
71 80 __('Configurations for Easy Basic Authentication', 'easy-basic-authentication'),
72 81 __('Easy Basic A.', 'easy-basic-authentication'),
@@ -71,182 +80,16 @@
71 80 __('Configurations for Easy Basic Authentication', 'easy-basic-authentication'),
72 81 __('Easy Basic A.', 'easy-basic-authentication'),
73 82 'manage_options',
74 83 'basic-auth-plugin',
75 - array($this, 'basic_auth_plugin_settings_page'),
84 + array($this->form, 'basic_auth_plugin_settings_page'),
76 85 'dashicons-lock'
77 86 );
78 87 if($this->log->is_enabled()) {
79 88 $this->log->getMenu();
80 89 }
81 - }
90 + }
82 91
83 - public function basic_auth_plugin_settings_page() {
84 - include plugin_dir_path( __FILE__ ) . '../template/settings_page.php';
85 - }
86 -
87 - public function basic_auth_plugin_settings_init() {
88 - register_setting( 'basic-auth-plugin-settings', 'basic_auth_plugin_admin_enable' );
89 - register_setting( 'basic-auth-plugin-settings', 'basic_auth_plugin_enable' );
90 - register_setting( 'basic-auth-plugin-settings', 'basic_auth_plugin_username' );
91 - register_setting( 'basic-auth-plugin-settings', 'basic_auth_plugin_admin_log_enable' );
92 -
93 - add_settings_section(
94 - 'basic-auth-plugin-section',
95 - __('Configurations for Easy Basic Authentication', 'easy-basic-authentication'),
96 - array($this,'basic_auth_plugin_section_cb'),
97 - 'basic-auth-plugin-settings'
98 - );
99 -
100 - add_settings_field(
101 - 'basic-auth-plugin-admin-enable',
102 - __('Enable for wp-admin', 'easy-basic-authentication'),
103 - array($this,'basic_auth_plugin_admin_enable_cb'),
104 - 'basic-auth-plugin-settings',
105 - 'basic-auth-plugin-section'
106 - );
107 -
108 - add_settings_field(
109 - 'basic-auth-plugin-enable',
110 - __('Enable for the entire site (only if wp-admin is enabled)', 'easy-basic-authentication'),
111 - array($this,'basic_auth_plugin_enable_cb'),
112 - 'basic-auth-plugin-settings',
113 - 'basic-auth-plugin-section'
114 - );
115 -
116 - add_settings_field(
117 - 'basic-auth-plugin-username',
118 - __('Username', 'easy-basic-authentication'),
119 - array($this,'basic_auth_plugin_username_cb'),
120 - 'basic-auth-plugin-settings',
121 - 'basic-auth-plugin-section'
122 - );
123 -
124 - add_settings_field(
125 - 'basic-auth-plugin-password',
126 - __('Password', 'easy-basic-authentication'),
127 - array($this,'basic_auth_plugin_password_cb'),
128 - 'basic-auth-plugin-settings',
129 - 'basic-auth-plugin-section'
130 - );
131 -
132 - add_settings_field(
133 - 'basic-auth-plugin-admin-log-enable',
134 - __('Enable access logs', 'easy-basic-authentication'),
135 - array($this,'basic_auth_plugin_admin_log_enable_cb'),
136 - 'basic-auth-plugin-settings',
137 - 'basic-auth-plugin-section'
138 - );
139 -
140 - add_settings_field(
141 - 'basic-auth-plugin-alert-enable',
142 - __('Enable email alert', 'easy-basic-authentication'),
143 - array($this,'basic_auth_plugin_alert_enable_cb'),
144 - 'basic-auth-plugin-settings',
145 - 'basic-auth-plugin-section'
146 - );
147 -
148 - add_settings_field(
149 - 'basic-auth-plugin-email-alert',
150 - __('Email for alert', 'easy-basic-authentication'),
151 - array($this,'basic_auth_plugin_alertemail_cb'),
152 - 'basic-auth-plugin-settings',
153 - 'basic-auth-plugin-section'
154 - );
155 -
156 - }
157 -
158 - public function basic_auth_plugin_section_cb() {
159 - echo __('Configure basic authentication', 'easy-basic-authentication');
160 - }
161 -
162 - public function basic_auth_plugin_enable_cb() {
163 - $admin_enable = get_option( 'basic_auth_plugin_admin_enable' );
164 - $enable = get_option( 'basic_auth_plugin_enable' );
165 - ?>
166 - <input type="checkbox" name="basic_auth_plugin_enable" value="1" <?php checked( $enable, 1 ); ?><?php disabled( !$admin_enable ); ?>>
167 - <?php
168 - }
169 -
170 - public function basic_auth_plugin_admin_enable_cb() {
171 - $enable = get_option( 'basic_auth_plugin_admin_enable' );
172 - ?>
173 - <input type="checkbox" name="basic_auth_plugin_admin_enable" value="1" <?php checked( $enable, 1 ); ?>>
174 - <?php
175 - }
176 -
177 - public function basic_auth_plugin_username_cb() {
178 - $username = get_option( 'basic_auth_plugin_username' );
179 - ?>
180 - <input type="text" name="basic_auth_plugin_username" value="<?php echo esc_attr( $username ); ?>">
181 - <?php
182 - }
183 -
184 - public function basic_auth_plugin_password_cb() {
185 - $password = get_option( 'basic_auth_plugin_password' )
186 - ? __('Password entered', 'easy-basic-authentication')
187 - : __('Enter the password', 'easy-basic-authentication');
188 - ?>
189 - <input type="password" name="basic_auth_plugin_password" value="" placeholder="<?php echo $password; ?>">
190 - <?php
191 - }
192 -
193 - public function basic_auth_plugin_admin_log_enable_cb() {
194 - $enable = get_option( 'basic_auth_plugin_admin_log_enable' );
195 - ?>
196 - <input type="checkbox" name="basic_auth_plugin_admin_log_enable" value="1" <?php checked( $enable, 1 ); ?>>
197 - <?php
198 - }
199 -
200 - public function basic_auth_plugin_alert_enable_cb() {
201 - $enable = get_option( 'basic_auth_plugin_alert_enable' );
202 - ?>
203 - <input type="checkbox" name="basic_auth_plugin_alert_enable" value="1" <?php checked( $enable, 1 ); ?>>
204 - <?php
205 - }
206 -
207 - public function basic_auth_plugin_alertemail_cb() {
208 - $email_alert = get_option( 'basic_auth_plugin_alertemail' );
209 - ?>
210 - <input type="text" name="basic_auth_plugin_alertemail" value="<?php echo esc_attr( $email_alert ); ?>" placeholder="<?php echo __('Enter the email', 'easy-basic-authentication'); ?>">
211 - <?php
212 - }
213 -
214 -
215 - public function basic_auth_plugin_save_settings() {
216 - if ( isset( $_POST['eba_submit'] ) ) {
217 - $admin_enable = isset( $_POST['basic_auth_plugin_admin_enable'] ) ? 1 : 0;
218 - $enable = isset( $_POST['basic_auth_plugin_enable'] ) ? 1 : 0;
219 - $username = sanitize_text_field( $_POST['basic_auth_plugin_username'] );
220 - $password = sanitize_text_field( $_POST['basic_auth_plugin_password'] );
221 - $log_enable = isset( $_POST['basic_auth_plugin_admin_log_enable'] ) ? 1 : 0;
222 - $alert_enable = isset( $_POST['basic_auth_plugin_alert_enable'] ) ? 1 : 0;
223 - $alert_email = sanitize_text_field( $_POST['basic_auth_plugin_alertemail'] );
224 -
225 - if ( !is_email( $alert_email ) ) {
226 - add_settings_error(
227 - 'basic_auth_plugin_alertemail',
228 - 'basic_auth_plugin_alertemail_error',
229 - __('Invalid email address', 'easy-basic-authentication'),
230 - 'error'
231 - );
232 - } else {
233 - update_option( 'basic_auth_plugin_alertemail', $alert_email );
234 - }
235 -
236 - update_option( 'basic_auth_plugin_admin_enable', $admin_enable );
237 - update_option( 'basic_auth_plugin_enable', $enable );
238 - update_option( 'basic_auth_plugin_username', $username );
239 - update_option( 'basic_auth_plugin_admin_log_enable', $log_enable );
240 - update_option( 'basic_auth_plugin_alert_enable', $alert_enable );
241 -
242 - if ( ! empty( $password ) ) {
243 - $hashed_password = wp_hash_password( $password );
244 - update_option( 'basic_auth_plugin_password', $hashed_password );
245 - }
246 - }
247 - }
248 -
249 92 public function basic_auth_action_failed_access() {
250 93 if($this->log->is_enabled()) {
251 94 $this->log->update_status($_SERVER);
252 95 }