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