| 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 |
$access_count = $this->basic_auth_count_not_viewed_log_failed_access(); |
| 73 |
add_submenu_page( |
| 74 |
'basic-auth-plugin', |
| 75 |
__('Log Page', 'easy-basic-authentication'), |
| 76 |
__('Log Page', 'easy-basic-authentication'). '<span class="update-plugins count-' . $access_count . '"><span class="plugin-count">' . $access_count . '</span></span>', |
| 77 |
'manage_options', |
| 78 |
'basic-auth-login', |
| 79 |
array($this, 'basic_auth_login_page') |
| 80 |
); |
| 81 |
} |
| 82 |
|
| 83 |
} |
| 84 |
|
| 85 |
public function basic_auth_login_page() { |
| 86 |
if(array_key_exists('clear_mode',$_POST)) { |
| 87 |
update_option('basic_auth_failure_logs', array()); |
| 88 |
} |
| 89 |
$user_log_data = array_reverse($this->basic_auth_get_log_failed_access()); |
| 90 |
include plugin_dir_path( __FILE__ ) . '../template/log_page.php'; |
| 91 |
$this->update_view(); |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
public function basic_auth_plugin_settings_page() { |
| 96 |
include plugin_dir_path( __FILE__ ) . '../template/settings_page.php'; |
| 97 |
} |
| 98 |
|
| 99 |
public function basic_auth_plugin_settings_init() { |
| 100 |
register_setting( 'basic-auth-plugin-settings', 'basic_auth_plugin_admin_enable' ); |
| 101 |
register_setting( 'basic-auth-plugin-settings', 'basic_auth_plugin_enable' ); |
| 102 |
register_setting( 'basic-auth-plugin-settings', 'basic_auth_plugin_username' ); |
| 103 |
register_setting( 'basic-auth-plugin-settings', 'basic_auth_plugin_admin_log_enable' ); |
| 104 |
|
| 105 |
add_settings_section( |
| 106 |
'basic-auth-plugin-section', |
| 107 |
__('Configurations for Easy Basic Authentication', 'easy-basic-authentication'), |
| 108 |
array($this,'basic_auth_plugin_section_cb'), |
| 109 |
'basic-auth-plugin-settings' |
| 110 |
); |
| 111 |
|
| 112 |
add_settings_field( |
| 113 |
'basic-auth-plugin-admin-enable', |
| 114 |
__('Enable for wp-admin', 'easy-basic-authentication'), |
| 115 |
array($this,'basic_auth_plugin_admin_enable_cb'), |
| 116 |
'basic-auth-plugin-settings', |
| 117 |
'basic-auth-plugin-section' |
| 118 |
); |
| 119 |
|
| 120 |
add_settings_field( |
| 121 |
'basic-auth-plugin-enable', |
| 122 |
__('Enable for the entire site (only if wp-admin is enabled)', 'easy-basic-authentication'), |
| 123 |
array($this,'basic_auth_plugin_enable_cb'), |
| 124 |
'basic-auth-plugin-settings', |
| 125 |
'basic-auth-plugin-section' |
| 126 |
); |
| 127 |
|
| 128 |
add_settings_field( |
| 129 |
'basic-auth-plugin-username', |
| 130 |
__('Username', 'easy-basic-authentication'), |
| 131 |
array($this,'basic_auth_plugin_username_cb'), |
| 132 |
'basic-auth-plugin-settings', |
| 133 |
'basic-auth-plugin-section' |
| 134 |
); |
| 135 |
|
| 136 |
add_settings_field( |
| 137 |
'basic-auth-plugin-password', |
| 138 |
__('Password', 'easy-basic-authentication'), |
| 139 |
array($this,'basic_auth_plugin_password_cb'), |
| 140 |
'basic-auth-plugin-settings', |
| 141 |
'basic-auth-plugin-section' |
| 142 |
); |
| 143 |
|
| 144 |
add_settings_field( |
| 145 |
'basic-auth-plugin-admin-log-enable', |
| 146 |
__('Enable access logs', 'easy-basic-authentication'), |
| 147 |
array($this,'basic_auth_plugin_admin_log_enable_cb'), |
| 148 |
'basic-auth-plugin-settings', |
| 149 |
'basic-auth-plugin-section' |
| 150 |
); |
| 151 |
|
| 152 |
} |
| 153 |
|
| 154 |
public function basic_auth_plugin_section_cb() { |
| 155 |
echo __('Configure basic authentication', 'easy-basic-authentication'); |
| 156 |
} |
| 157 |
|
| 158 |
public function basic_auth_plugin_enable_cb() { |
| 159 |
$admin_enable = get_option( 'basic_auth_plugin_admin_enable' ); |
| 160 |
$enable = get_option( 'basic_auth_plugin_enable' ); |
| 161 |
?> |
| 162 |
<input type="checkbox" name="basic_auth_plugin_enable" value="1" <?php checked( $enable, 1 ); ?><?php disabled( !$admin_enable ); ?>> |
| 163 |
<?php |
| 164 |
} |
| 165 |
|
| 166 |
public function basic_auth_plugin_admin_enable_cb() { |
| 167 |
$enable = get_option( 'basic_auth_plugin_admin_enable' ); |
| 168 |
?> |
| 169 |
<input type="checkbox" name="basic_auth_plugin_admin_enable" value="1" <?php checked( $enable, 1 ); ?>> |
| 170 |
<?php |
| 171 |
} |
| 172 |
|
| 173 |
public function basic_auth_plugin_username_cb() { |
| 174 |
$username = get_option( 'basic_auth_plugin_username' ); |
| 175 |
?> |
| 176 |
<input type="text" name="basic_auth_plugin_username" value="<?php echo esc_attr( $username ); ?>"> |
| 177 |
<?php |
| 178 |
} |
| 179 |
|
| 180 |
public function basic_auth_plugin_password_cb() { |
| 181 |
$password = get_option( 'basic_auth_plugin_password' ) |
| 182 |
? __('Password entered', 'easy-basic-authentication') |
| 183 |
: __('Enter the password', 'easy-basic-authentication'); |
| 184 |
?> |
| 185 |
<input type="password" name="basic_auth_plugin_password" value="" placeholder="<?php echo $password; ?>"> |
| 186 |
<?php |
| 187 |
} |
| 188 |
|
| 189 |
public function basic_auth_plugin_admin_log_enable_cb() { |
| 190 |
$enable = get_option( 'basic_auth_plugin_admin_log_enable' ); |
| 191 |
?> |
| 192 |
<input type="checkbox" name="basic_auth_plugin_admin_log_enable" value="1" <?php checked( $enable, 1 ); ?>> |
| 193 |
<?php |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
public function basic_auth_plugin_save_settings() { |
| 198 |
if ( isset( $_POST['eba_submit'] ) ) { |
| 199 |
$admin_enable = isset( $_POST['basic_auth_plugin_admin_enable'] ) ? 1 : 0; |
| 200 |
$enable = isset( $_POST['basic_auth_plugin_enable'] ) ? 1 : 0; |
| 201 |
$username = sanitize_text_field( $_POST['basic_auth_plugin_username'] ); |
| 202 |
$password = sanitize_text_field( $_POST['basic_auth_plugin_password'] ); |
| 203 |
$log_enable = isset( $_POST['basic_auth_plugin_admin_log_enable'] ) ? 1 : 0; |
| 204 |
|
| 205 |
update_option( 'basic_auth_plugin_admin_enable', $admin_enable ); |
| 206 |
update_option( 'basic_auth_plugin_enable', $enable ); |
| 207 |
update_option( 'basic_auth_plugin_username', $username ); |
| 208 |
update_option( 'basic_auth_plugin_admin_log_enable', $log_enable ); |
| 209 |
|
| 210 |
if ( ! empty( $password ) ) { |
| 211 |
$hashed_password = wp_hash_password( $password ); |
| 212 |
update_option( 'basic_auth_plugin_password', $hashed_password ); |
| 213 |
} |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
public function basic_auth_log_failed_access() { |
| 218 |
if($this->log_is_enabled()) { |
| 219 |
$logs = get_option('basic_auth_failure_logs', array()); |
| 220 |
|
| 221 |
$log_entry = array( |
| 222 |
'id' => uniqid(), |
| 223 |
'ip' => esc_attr($_SERVER['REMOTE_ADDR']), |
| 224 |
'data' => current_time('mysql'), |
| 225 |
'browser' => esc_attr($_SERVER['HTTP_USER_AGENT']), |
| 226 |
'request_uri' => esc_attr($_SERVER["REQUEST_URI"]), |
| 227 |
'viewed' => 0 |
| 228 |
); |
| 229 |
|
| 230 |
$log_entry = apply_filters('basic_auth_single_log_entry', $log_entry); |
| 231 |
|
| 232 |
$logs[] = $log_entry; |
| 233 |
|
| 234 |
$logs = apply_filters('basic_auth_logs_entry', $logs); |
| 235 |
|
| 236 |
update_option('basic_auth_failure_logs', $logs); |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
public function basic_auth_get_log_failed_access() { |
| 241 |
return get_option('basic_auth_failure_logs', array()); |
| 242 |
} |
| 243 |
|
| 244 |
public function basic_auth_count_log_failed_access() { |
| 245 |
return count($this->basic_auth_get_log_failed_access()); |
| 246 |
} |
| 247 |
|
| 248 |
public function basic_auth_not_viewed_log_failed_access() { |
| 249 |
$return_not_viewed = array_filter($this->basic_auth_get_log_failed_access(), function ($entry) { |
| 250 |
return isset($entry['viewed']) && $entry['viewed'] === 0; |
| 251 |
}); |
| 252 |
return $return_not_viewed; |
| 253 |
} |
| 254 |
|
| 255 |
public function basic_auth_count_not_viewed_log_failed_access() { |
| 256 |
return count($this->basic_auth_not_viewed_log_failed_access()); |
| 257 |
} |
| 258 |
|
| 259 |
public function update_view() { |
| 260 |
$logs = get_option('basic_auth_failure_logs', array()); |
| 261 |
foreach ($logs as &$entry) { |
| 262 |
$entry['viewed'] = 1; |
| 263 |
} |
| 264 |
update_option('basic_auth_failure_logs', $logs); |
| 265 |
} |
| 266 |
|
| 267 |
public function log_is_enabled() { |
| 268 |
return get_option( 'basic_auth_plugin_admin_log_enable' ); |
| 269 |
} |
| 270 |
|
| 271 |
} |
| 272 |
|