| @@ -1,11 +1,22 @@ | ||
| 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 | + | |
| 3 | 7 | class easy_basic_authentication_class { |
| 4 | 8 | |
| 9 | + private $log; | |
| 10 | + private $email; | |
| 11 | + private $form; | |
| 5 | 12 | |
| 6 | 13 | public function __construct() |
| 7 | 14 | { |
| 15 | + $this->log = new easy_basic_authentication_log_class(); | |
| 16 | + $this->email = new easy_basic_authentication_emailalert_class(); | |
| 17 | + $this->form = new easy_basic_authentication_form_class(); | |
| 18 | + | |
| 8 | 19 | if(get_option( 'basic_auth_plugin_admin_enable' )) { |
| 9 | 20 | if (in_array($GLOBALS['pagenow'], array('wp-login.php', 'wp-register.php'))) { |
| 10 | 21 | add_action( 'init', array($this,'basic_auth_admin') ); |
| 11 | 22 | } |
| @@ -15,11 +26,14 @@ | ||
| 15 | 26 | add_action( 'init', array($this,'basic_auth_root') ); |
| 16 | 27 | } |
| 17 | 28 | |
| 18 | 29 | add_action( 'admin_menu', array($this,'basic_auth_plugin_menu' )); |
| 19 | - add_action( 'admin_init', array($this,'basic_auth_plugin_settings_init' )); | |
| 30 | + add_action( 'admin_init', array($this->form,'basic_auth_plugin_settings_init' )); | |
| 20 | 31 | |
| 21 | - add_action( 'admin_init', array($this,'basic_auth_plugin_save_settings') ); | |
| 32 | + add_action('admin_init', function () { | |
| 33 | + $post_data = $_POST; | |
| 34 | + $this->form->basic_auth_plugin_save_settings($post_data); | |
| 35 | + }); | |
| 22 | 36 | |
| 23 | 37 | } |
| 24 | 38 | |
| 25 | 39 | public function basic_auth_root() { |
| @@ -28,16 +42,9 @@ | ||
| 28 | 42 | |
| 29 | 43 | if ( !isset( $_SERVER['PHP_AUTH_USER'] ) || !isset( $_SERVER['PHP_AUTH_PW'] ) || |
| 30 | 44 | $_SERVER['PHP_AUTH_USER'] != $user || !wp_check_password( $_SERVER['PHP_AUTH_PW'], $pass ) ) { |
| 31 | 45 | |
| 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; | |
| 46 | + $this->do_exit(true); | |
| 40 | 47 | } |
| 41 | 48 | } |
| 42 | 49 | |
| 43 | 50 | public function basic_auth_admin() { |
| @@ -45,227 +52,58 @@ | ||
| 45 | 52 | $pass = get_option( 'basic_auth_plugin_password' ); |
| 46 | 53 | |
| 47 | 54 | if ( !isset( $_SERVER['PHP_AUTH_USER'] ) || !isset( $_SERVER['PHP_AUTH_PW'] ) || |
| 48 | 55 | $_SERVER['PHP_AUTH_USER'] != $user || !wp_check_password( $_SERVER['PHP_AUTH_PW'], $pass ) ) { |
| 56 | + | |
| 57 | + $this->do_exit(true); | |
| 58 | + } | |
| 59 | + } | |
| 60 | + | |
| 61 | + public function do_exit($admin_area = false) { | |
| 62 | + | |
| 63 | + if (in_array($_SERVER['REMOTE_ADDR'], $this->getWhiteList())) { | |
| 64 | + return; | |
| 65 | + } | |
| 66 | + $this->basic_auth_action_failed_access(); | |
| 67 | + do_action('basic_auth_before_401'); | |
| 68 | + | |
| 69 | + if($admin_area) { | |
| 70 | + do_action('basic_auth_before_401_admin_area'); | |
| 49 | 71 | |
| 50 | - $this->basic_auth_log_failed_access(); | |
| 51 | - | |
| 52 | - do_action('basic_auth_before_401'); | |
| 53 | - | |
| 72 | + header( 'WWW-Authenticate: Basic realm="My Website"' ); | |
| 73 | + header( 'HTTP/1.0 401 Unauthorized' ); | |
| 74 | + exit; | |
| 75 | + } else { | |
| 54 | 76 | header( 'HTTP/1.1 401 Unauthorized' ); |
| 55 | 77 | header( 'WWW-Authenticate: Basic realm="Admin Area"' ); |
| 56 | 78 | exit; |
| 57 | - } | |
| 79 | + } | |
| 58 | 80 | } |
| 59 | - | |
| 60 | - | |
| 81 | + | |
| 82 | + public function getWhiteList() { | |
| 83 | + return get_option( 'basic_auth_plugin_whitelist' )?explode(',',get_option( 'basic_auth_plugin_whitelist' )):[]; | |
| 84 | + } | |
| 85 | + | |
| 61 | 86 | public function basic_auth_plugin_menu() { |
| 62 | - $access_count = $this->basic_auth_count_not_viewed_log_failed_access(); | |
| 63 | 87 | add_menu_page( |
| 64 | 88 | __('Configurations for Easy Basic Authentication', 'easy-basic-authentication'), |
| 65 | 89 | __('Easy Basic A.', 'easy-basic-authentication'), |
| 66 | 90 | 'manage_options', |
| 67 | 91 | 'basic-auth-plugin', |
| 68 | - array($this, 'basic_auth_plugin_settings_page'), | |
| 92 | + array($this->form, 'basic_auth_plugin_settings_page'), | |
| 69 | 93 | 'dashicons-lock' |
| 70 | 94 | ); |
| 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 | - ); | |
| 95 | + if($this->log->is_enabled()) { | |
| 96 | + $this->log->getMenu(); | |
| 81 | 97 | } |
| 82 | - | |
| 83 | - } | |
| 98 | + } | |
| 84 | 99 | |
| 85 | - public function basic_auth_login_page() { | |
| 86 | - if(array_key_exists('clear_mode',$_POST)) { | |
| 87 | - update_option('basic_auth_failure_logs', array()); | |
| 100 | + public function basic_auth_action_failed_access() { | |
| 101 | + if($this->log->is_enabled()) { | |
| 102 | + $this->log->update_status($_SERVER); | |
| 88 | 103 | } |
| 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 | - } | |
| 104 | + if($this->email->is_enabled()) { | |
| 105 | + $this->email->sendAlert($_SERVER); | |
| 214 | 106 | } |
| 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 | 107 | } |
| 270 | 108 | |
| 271 | 109 | } |