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