PluginProbe
Easy Basic Authentication – Add basic auth to site or admin area / 1.3.4
Easy Basic Authentication – Add basic auth to site or admin area v1.3.4
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
easy-basic-authentication / class / easy-basic-authentication-class.php

easy-basic-authentication-class.php in Easy Basic Authentication – Add basic auth to site or admin area 1.3.4, at class/easy-basic-authentication-class.php

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