| @@ -27,8 +27,11 @@ | ||
| 27 | 27 | $pass = get_option( 'basic_auth_plugin_password' ); |
| 28 | 28 | |
| 29 | 29 | if ( !isset( $_SERVER['PHP_AUTH_USER'] ) || !isset( $_SERVER['PHP_AUTH_PW'] ) || |
| 30 | 30 | $_SERVER['PHP_AUTH_USER'] != $user || !wp_check_password( $_SERVER['PHP_AUTH_PW'], $pass ) ) { |
| 31 | + | |
| 32 | + $this->basic_auth_log_failed_access(); | |
| 33 | + | |
| 31 | 34 | header( 'WWW-Authenticate: Basic realm="My Website"' ); |
| 32 | 35 | header( 'HTTP/1.0 401 Unauthorized' ); |
| 33 | 36 | echo 'Autenticazione richiesta'; |
| 34 | 37 | exit; |
| @@ -40,25 +43,51 @@ | ||
| 40 | 43 | $pass = get_option( 'basic_auth_plugin_password' ); |
| 41 | 44 | |
| 42 | 45 | if ( !isset( $_SERVER['PHP_AUTH_USER'] ) || !isset( $_SERVER['PHP_AUTH_PW'] ) || |
| 43 | 46 | $_SERVER['PHP_AUTH_USER'] != $user || !wp_check_password( $_SERVER['PHP_AUTH_PW'], $pass ) ) { |
| 47 | + | |
| 48 | + $this->basic_auth_log_failed_access(); | |
| 49 | + | |
| 44 | 50 | header( 'HTTP/1.1 401 Unauthorized' ); |
| 45 | 51 | header( 'WWW-Authenticate: Basic realm="Admin Area"' ); |
| 46 | 52 | exit; |
| 47 | - } | |
| 53 | + } | |
| 48 | 54 | } |
| 49 | 55 | |
| 50 | 56 | |
| 51 | 57 | public function basic_auth_plugin_menu() { |
| 58 | + ini_set('display_errors', 1); | |
| 59 | +ini_set('display_startup_errors', 1); | |
| 60 | +error_reporting(E_ALL); | |
| 61 | + $access_count = $this->basic_auth_count_not_viewed_log_failed_access(); | |
| 52 | 62 | add_menu_page( |
| 53 | 63 | __('Configurations for Easy Basic Authentication', 'easy-basic-authentication'), |
| 54 | - __('Easy Basic A.', 'easy-basic-authentication'), | |
| 64 | + __('Easy Basic A.', 'easy-basic-authentication') . '<span class="update-plugins count-' . $access_count . '"><span class="plugin-count">' . $access_count . '</span></span>', | |
| 55 | 65 | 'manage_options', |
| 56 | 66 | 'basic-auth-plugin', |
| 57 | - array($this,'basic_auth_plugin_settings_page'), | |
| 67 | + array($this, 'basic_auth_plugin_settings_page'), | |
| 58 | 68 | 'dashicons-lock' |
| 59 | 69 | ); |
| 70 | + | |
| 71 | + add_submenu_page( | |
| 72 | + 'basic-auth-plugin', | |
| 73 | + __('Log Page', 'easy-basic-authentication'), | |
| 74 | + __('Log Page', 'easy-basic-authentication'), | |
| 75 | + 'manage_options', | |
| 76 | + 'basic-auth-login', | |
| 77 | + array($this, 'basic_auth_login_page') | |
| 78 | + ); | |
| 60 | 79 | } |
| 80 | + | |
| 81 | + public function basic_auth_login_page() { | |
| 82 | + if(array_key_exists('clear_mode',$_POST)) { | |
| 83 | + update_option('basic_auth_failure_logs', array()); | |
| 84 | + } | |
| 85 | + $user_log_data = array_reverse($this->basic_auth_get_log_failed_access()); | |
| 86 | + include plugin_dir_path( __FILE__ ) . '../template/log_page.php'; | |
| 87 | + $this->update_view(); | |
| 88 | + } | |
| 89 | + | |
| 61 | 90 | |
| 62 | 91 | public function basic_auth_plugin_settings_page() { |
| 63 | 92 | include plugin_dir_path( __FILE__ ) . '../template/settings_page.php'; |
| 64 | 93 | } |
| @@ -105,8 +134,9 @@ | ||
| 105 | 134 | array($this,'basic_auth_plugin_password_cb'), |
| 106 | 135 | 'basic-auth-plugin-settings', |
| 107 | 136 | 'basic-auth-plugin-section' |
| 108 | 137 | ); |
| 138 | + | |
| 109 | 139 | } |
| 110 | 140 | |
| 111 | 141 | public function basic_auth_plugin_section_cb() { |
| 112 | 142 | echo __('Configure basic authentication', 'easy-basic-authentication'); |
| @@ -160,6 +190,49 @@ | ||
| 160 | 190 | update_option( 'basic_auth_plugin_password', $hashed_password ); |
| 161 | 191 | } |
| 162 | 192 | } |
| 163 | 193 | } |
| 164 | - | |
| 194 | + | |
| 195 | + public function basic_auth_log_failed_access() { | |
| 196 | + $logs = get_option('basic_auth_failure_logs', array()); | |
| 197 | + | |
| 198 | + $log_entry = array( | |
| 199 | + 'id' => uniqid(), | |
| 200 | + 'ip' => $_SERVER['REMOTE_ADDR'], | |
| 201 | + 'data' => current_time('mysql'), | |
| 202 | + 'browser' => $_SERVER['HTTP_USER_AGENT'], | |
| 203 | + 'viewed' => 0 | |
| 204 | + ); | |
| 205 | + | |
| 206 | + $logs[] = $log_entry; | |
| 207 | + | |
| 208 | + update_option('basic_auth_failure_logs', $logs); | |
| 209 | + } | |
| 210 | + | |
| 211 | + public function basic_auth_get_log_failed_access() { | |
| 212 | + return get_option('basic_auth_failure_logs', array()); | |
| 213 | + } | |
| 214 | + | |
| 215 | + public function basic_auth_count_log_failed_access() { | |
| 216 | + return count($this->basic_auth_get_log_failed_access()); | |
| 217 | + } | |
| 218 | + | |
| 219 | + public function basic_auth_not_viewed_log_failed_access() { | |
| 220 | + $return_not_viewed = array_filter($this->basic_auth_get_log_failed_access(), function ($entry) { | |
| 221 | + return isset($entry['viewed']) && $entry['viewed'] === 0; | |
| 222 | + }); | |
| 223 | + return $return_not_viewed; | |
| 224 | + } | |
| 225 | + | |
| 226 | + public function basic_auth_count_not_viewed_log_failed_access() { | |
| 227 | + return count($this->basic_auth_not_viewed_log_failed_access()); | |
| 228 | + } | |
| 229 | + | |
| 230 | + public function update_view() { | |
| 231 | + $logs = get_option('basic_auth_failure_logs', array()); | |
| 232 | + foreach ($logs as &$entry) { | |
| 233 | + $entry['viewed'] = 1; | |
| 234 | + } | |
| 235 | + update_option('basic_auth_failure_logs', $logs); | |
| 236 | + } | |
| 237 | + | |
| 165 | 238 | } |