| @@ -1,31 +1,14 @@ | ||
| 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 | -require_once (dirname(__FILE__).'/easy-basic-authentication-compatcheck-class.php'); | |
| 8 | - | |
| 9 | 3 | class easy_basic_authentication_class { |
| 10 | 4 | |
| 11 | - private $log; | |
| 12 | - private $email; | |
| 13 | - private $form; | |
| 14 | - private $compatcheck; | |
| 15 | 5 | |
| 16 | 6 | public function __construct() |
| 17 | 7 | { |
| 18 | - $this->log = new easy_basic_authentication_log_class(); | |
| 19 | - $this->email = new easy_basic_authentication_emailalert_class(); | |
| 20 | - $this->form = new easy_basic_authentication_form_class(); | |
| 21 | - $notice = new easy_basic_authentication_notice_class(); | |
| 22 | - $this->compatcheck = new easy_basic_authentication_compatcheck_class(); | |
| 23 | - $this->compatcheck->register_hooks(); | |
| 24 | - | |
| 25 | 8 | if(get_option( 'basic_auth_plugin_admin_enable' )) { |
| 26 | 9 | if (in_array($GLOBALS['pagenow'], array('wp-login.php', 'wp-register.php'))) { |
| 27 | - add_action( 'init', array($this,'basic_auth_root') ); | |
| 10 | + add_action( 'init', array($this,'basic_auth_admin') ); | |
| 28 | 11 | } |
| 29 | 12 | } |
| 30 | 13 | |
| 31 | 14 | if(get_option( 'basic_auth_plugin_enable' ) && get_option( 'basic_auth_plugin_admin_enable' )){ |
| @@ -32,167 +15,224 @@ | ||
| 32 | 15 | add_action( 'init', array($this,'basic_auth_root') ); |
| 33 | 16 | } |
| 34 | 17 | |
| 35 | 18 | add_action( 'admin_menu', array($this,'basic_auth_plugin_menu' )); |
| 36 | - add_action( 'admin_init', array($this->form,'basic_auth_plugin_settings_init' )); | |
| 19 | + add_action( 'admin_init', array($this,'basic_auth_plugin_settings_init' )); | |
| 37 | 20 | |
| 38 | - add_action('admin_init', function () { | |
| 39 | - $post_data = $_POST; | |
| 40 | - $this->form->basic_auth_plugin_save_settings($post_data); | |
| 41 | - }); | |
| 21 | + add_action( 'admin_init', array($this,'basic_auth_plugin_save_settings') ); | |
| 42 | 22 | |
| 43 | 23 | } |
| 44 | 24 | |
| 45 | - public function basic_auth_root() | |
| 46 | - { | |
| 47 | - $user = get_option('basic_auth_plugin_username'); | |
| 48 | - $pass = get_option('basic_auth_plugin_password'); | |
| 49 | - | |
| 50 | - if ($this->whiteListChecker()) { | |
| 51 | - return; | |
| 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 | + header( 'WWW-Authenticate: Basic realm="My Website"' ); | |
| 35 | + header( 'HTTP/1.0 401 Unauthorized' ); | |
| 36 | + echo 'Autenticazione richiesta'; | |
| 37 | + exit; | |
| 52 | 38 | } |
| 53 | - | |
| 54 | - if ($this->urlWhiteListChecker()) { | |
| 55 | - return; | |
| 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 | + | |
| 50 | + header( 'HTTP/1.1 401 Unauthorized' ); | |
| 51 | + header( 'WWW-Authenticate: Basic realm="Admin Area"' ); | |
| 52 | + exit; | |
| 53 | + } | |
| 54 | + } | |
| 55 | + | |
| 56 | + | |
| 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(); | |
| 62 | + add_menu_page( | |
| 63 | + __('Configurations for Easy Basic Authentication', '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>', | |
| 65 | + 'manage_options', | |
| 66 | + 'basic-auth-plugin', | |
| 67 | + array($this, 'basic_auth_plugin_settings_page'), | |
| 68 | + 'dashicons-lock' | |
| 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 | + ); | |
| 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()); | |
| 56 | 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 | + | |
| 57 | 90 | |
| 58 | - if (!isset($_SERVER['PHP_AUTH_USER']) && isset($_SERVER['HTTP_AUTHORIZATION'])) { | |
| 59 | - if (stripos($_SERVER['HTTP_AUTHORIZATION'], 'basic') === 0) { | |
| 60 | - list($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']) = explode(':', base64_decode(substr($_SERVER['HTTP_AUTHORIZATION'], 6)), 2); | |
| 61 | - } | |
| 62 | - } | |
| 63 | - | |
| 64 | - if (!isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW']) || | |
| 65 | - $_SERVER['PHP_AUTH_USER'] !== $user || | |
| 66 | - !wp_check_password(wp_unslash($_SERVER['PHP_AUTH_PW']), $pass)) { | |
| 67 | - $this->do_exit(true); | |
| 68 | - } | |
| 91 | + public function basic_auth_plugin_settings_page() { | |
| 92 | + include plugin_dir_path( __FILE__ ) . '../template/settings_page.php'; | |
| 69 | 93 | } |
| 70 | - | |
| 71 | - public function urlWhiteListChecker() { | |
| 72 | - if (empty($_SERVER['HTTP_HOST']) || empty($_SERVER['REQUEST_URI'])) { | |
| 73 | - return false; | |
| 74 | - } | |
| 75 | - | |
| 76 | - $scheme = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https' : 'http'; | |
| 77 | - $currentUrl = $scheme . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; | |
| 78 | - | |
| 79 | - $whitelist = $this->getUrlWhiteList(); | |
| 80 | - | |
| 81 | - foreach ($whitelist as $entry) { | |
| 82 | - if ($this->isUrlAllowed($currentUrl, $entry)) { | |
| 83 | - return true; | |
| 84 | - } | |
| 85 | - } | |
| 86 | - | |
| 87 | - return false; | |
| 94 | + | |
| 95 | + public function basic_auth_plugin_settings_init() { | |
| 96 | + register_setting( 'basic-auth-plugin-settings', 'basic_auth_plugin_admin_enable' ); | |
| 97 | + register_setting( 'basic-auth-plugin-settings', 'basic_auth_plugin_enable' ); | |
| 98 | + register_setting( 'basic-auth-plugin-settings', 'basic_auth_plugin_username' ); | |
| 99 | + | |
| 100 | + add_settings_section( | |
| 101 | + 'basic-auth-plugin-section', | |
| 102 | + __('Configurations for Easy Basic Authentication', 'easy-basic-authentication'), | |
| 103 | + array($this,'basic_auth_plugin_section_cb'), | |
| 104 | + 'basic-auth-plugin-settings' | |
| 105 | + ); | |
| 106 | + | |
| 107 | + add_settings_field( | |
| 108 | + 'basic-auth-plugin-admin-enable', | |
| 109 | + __('Enable for wp-admin', 'easy-basic-authentication'), | |
| 110 | + array($this,'basic_auth_plugin_admin_enable_cb'), | |
| 111 | + 'basic-auth-plugin-settings', | |
| 112 | + 'basic-auth-plugin-section' | |
| 113 | + ); | |
| 114 | + | |
| 115 | + add_settings_field( | |
| 116 | + 'basic-auth-plugin-enable', | |
| 117 | + __('Enable for the entire site (only if wp-admin is enabled)', 'easy-basic-authentication'), | |
| 118 | + array($this,'basic_auth_plugin_enable_cb'), | |
| 119 | + 'basic-auth-plugin-settings', | |
| 120 | + 'basic-auth-plugin-section' | |
| 121 | + ); | |
| 122 | + | |
| 123 | + add_settings_field( | |
| 124 | + 'basic-auth-plugin-username', | |
| 125 | + __('Username', 'easy-basic-authentication'), | |
| 126 | + array($this,'basic_auth_plugin_username_cb'), | |
| 127 | + 'basic-auth-plugin-settings', | |
| 128 | + 'basic-auth-plugin-section' | |
| 129 | + ); | |
| 130 | + | |
| 131 | + add_settings_field( | |
| 132 | + 'basic-auth-plugin-password', | |
| 133 | + __('Password', 'easy-basic-authentication'), | |
| 134 | + array($this,'basic_auth_plugin_password_cb'), | |
| 135 | + 'basic-auth-plugin-settings', | |
| 136 | + 'basic-auth-plugin-section' | |
| 137 | + ); | |
| 138 | + | |
| 88 | 139 | } |
| 89 | - | |
| 90 | - private function isUrlAllowed($currentUrl, $entry) { | |
| 91 | - $currentUrl = rtrim($currentUrl, '/'); | |
| 92 | - $entry = rtrim($entry, '/'); | |
| 93 | - | |
| 94 | - if (strpos($entry, '/') === 0) { | |
| 95 | - $path = wp_parse_url($currentUrl, PHP_URL_PATH); | |
| 96 | - return stripos($path, $entry) === 0; | |
| 97 | - } | |
| 98 | - | |
| 99 | - if (!preg_match('#^https?://#i', $entry)) { | |
| 100 | - $scheme = wp_parse_url($currentUrl, PHP_URL_SCHEME) ?: 'https'; | |
| 101 | - $entry = $scheme . '://' . $entry; | |
| 102 | - } | |
| 103 | - | |
| 104 | - return stripos($currentUrl, $entry) === 0; | |
| 140 | + | |
| 141 | + public function basic_auth_plugin_section_cb() { | |
| 142 | + echo __('Configure basic authentication', 'easy-basic-authentication'); | |
| 105 | 143 | } |
| 106 | - | |
| 107 | - public function whiteListChecker() { | |
| 108 | - if (!isset($_SERVER['REMOTE_ADDR'])) { | |
| 109 | - return false; | |
| 110 | - } | |
| 111 | 144 | |
| 112 | - $ip = $_SERVER['REMOTE_ADDR']; | |
| 113 | - $whitelist = $this->getWhiteList(); | |
| 114 | - | |
| 115 | - foreach ($whitelist as $entry) { | |
| 116 | - if ($this->isIpAllowed($ip, $entry)) { | |
| 117 | - return true; | |
| 118 | - } | |
| 119 | - } | |
| 120 | - return false; | |
| 145 | + public function basic_auth_plugin_enable_cb() { | |
| 146 | + $admin_enable = get_option( 'basic_auth_plugin_admin_enable' ); | |
| 147 | + $enable = get_option( 'basic_auth_plugin_enable' ); | |
| 148 | + ?> | |
| 149 | + <input type="checkbox" name="basic_auth_plugin_enable" value="1" <?php checked( $enable, 1 ); ?><?php disabled( !$admin_enable ); ?>> | |
| 150 | + <?php | |
| 121 | 151 | } |
| 122 | 152 | |
| 123 | - private function isIpAllowed($ip, $entry) { | |
| 124 | - if (filter_var($entry, FILTER_VALIDATE_IP)) { | |
| 125 | - return $ip === $entry; | |
| 126 | - } elseif (strpos($entry, '/') !== false) { | |
| 127 | - return $this->isIpInCidr($ip, $entry); | |
| 128 | - } elseif (strpos($entry, '-') !== false) { | |
| 129 | - return $this->isIpInRange($ip, $entry); | |
| 130 | - } | |
| 131 | - return false; | |
| 153 | + public function basic_auth_plugin_admin_enable_cb() { | |
| 154 | + $enable = get_option( 'basic_auth_plugin_admin_enable' ); | |
| 155 | + ?> | |
| 156 | + <input type="checkbox" name="basic_auth_plugin_admin_enable" value="1" <?php checked( $enable, 1 ); ?>> | |
| 157 | + <?php | |
| 132 | 158 | } |
| 133 | 159 | |
| 134 | - private function isIpInCidr($ip, $cidr) { | |
| 135 | - list($subnet, $mask) = explode('/', $cidr); | |
| 136 | - $ipLong = ip2long($ip); | |
| 137 | - $subnetLong = ip2long($subnet); | |
| 138 | - $maskLong = -1 << (32 - $mask); | |
| 139 | - return ($ipLong & $maskLong) === ($subnetLong & $maskLong); | |
| 160 | + public function basic_auth_plugin_username_cb() { | |
| 161 | + $username = get_option( 'basic_auth_plugin_username' ); | |
| 162 | + ?> | |
| 163 | + <input type="text" name="basic_auth_plugin_username" value="<?php echo esc_attr( $username ); ?>"> | |
| 164 | + <?php | |
| 140 | 165 | } |
| 141 | 166 | |
| 142 | - private function isIpInRange($ip, $range) { | |
| 143 | - list($start, $end) = array_map('trim', explode('-', $range)); | |
| 144 | - $ipLong = ip2long($ip); | |
| 145 | - $startLong = ip2long($start); | |
| 146 | - $endLong = ip2long($end); | |
| 147 | - return ($ipLong >= $startLong && $ipLong <= $endLong); | |
| 167 | + public function basic_auth_plugin_password_cb() { | |
| 168 | + $password = get_option( 'basic_auth_plugin_password' ) | |
| 169 | + ? __('Password entered', 'easy-basic-authentication') | |
| 170 | + : __('Enter the password', 'easy-basic-authentication'); | |
| 171 | + ?> | |
| 172 | + <input type="password" name="basic_auth_plugin_password" value="" placeholder="<?php echo $password; ?>"> | |
| 173 | + <?php | |
| 148 | 174 | } |
| 175 | + | |
| 176 | + | |
| 177 | + public function basic_auth_plugin_save_settings() { | |
| 178 | + if ( isset( $_POST['eba_submit'] ) ) { | |
| 179 | + $admin_enable = isset( $_POST['basic_auth_plugin_admin_enable'] ) ? 1 : 0; | |
| 180 | + $enable = isset( $_POST['basic_auth_plugin_enable'] ) ? 1 : 0; | |
| 181 | + $username = sanitize_text_field( $_POST['basic_auth_plugin_username'] ); | |
| 182 | + $password = sanitize_text_field( $_POST['basic_auth_plugin_password'] ); | |
| 183 | + | |
| 184 | + update_option( 'basic_auth_plugin_admin_enable', $admin_enable ); | |
| 185 | + update_option( 'basic_auth_plugin_enable', $enable ); | |
| 186 | + update_option( 'basic_auth_plugin_username', $username ); | |
| 187 | + | |
| 188 | + if ( ! empty( $password ) ) { | |
| 189 | + $hashed_password = wp_hash_password( $password ); | |
| 190 | + update_option( 'basic_auth_plugin_password', $hashed_password ); | |
| 191 | + } | |
| 192 | + } | |
| 193 | + } | |
| 149 | 194 | |
| 150 | - public function do_exit($admin_area = false) { | |
| 195 | + public function basic_auth_log_failed_access() { | |
| 196 | + $logs = get_option('basic_auth_failure_logs', array()); | |
| 151 | 197 | |
| 152 | - $this->basic_auth_action_failed_access(); | |
| 153 | - do_action('basic_auth_before_401'); | |
| 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 | + ); | |
| 154 | 205 | |
| 155 | - if($admin_area) { | |
| 156 | - do_action('basic_auth_before_401_admin_area'); | |
| 157 | - header( 'WWW-Authenticate: Basic realm="My Website"' ); | |
| 158 | - header( 'HTTP/1.0 401 Unauthorized' ); | |
| 159 | - exit; | |
| 160 | - } else { | |
| 161 | - header( 'HTTP/1.1 401 Unauthorized' ); | |
| 162 | - header( 'WWW-Authenticate: Basic realm="Admin Area"' ); | |
| 163 | - exit; | |
| 164 | - } | |
| 206 | + $logs[] = $log_entry; | |
| 207 | + | |
| 208 | + update_option('basic_auth_failure_logs', $logs); | |
| 165 | 209 | } |
| 166 | 210 | |
| 167 | - public function getWhiteList() { | |
| 168 | - return get_option( 'basic_auth_plugin_whitelist' )?explode(',',get_option( 'basic_auth_plugin_whitelist' )):[]; | |
| 211 | + public function basic_auth_get_log_failed_access() { | |
| 212 | + return get_option('basic_auth_failure_logs', array()); | |
| 169 | 213 | } |
| 170 | 214 | |
| 171 | - public function getUrlWhiteList() { | |
| 172 | - return get_option( 'basic_auth_plugin_urlwhitelist' )?explode(',',get_option( 'basic_auth_plugin_urlwhitelist' )):[]; | |
| 215 | + public function basic_auth_count_log_failed_access() { | |
| 216 | + return count($this->basic_auth_get_log_failed_access()); | |
| 173 | 217 | } |
| 174 | 218 | |
| 175 | - public function basic_auth_plugin_menu() { | |
| 176 | - add_menu_page( | |
| 177 | - __('Configurations for Easy Basic Authentication', 'easy-basic-authentication'), | |
| 178 | - __('Easy Basic A.', 'easy-basic-authentication'), | |
| 179 | - 'manage_options', | |
| 180 | - 'basic-auth-plugin', | |
| 181 | - array($this->form, 'basic_auth_plugin_settings_page'), | |
| 182 | - 'dashicons-lock' | |
| 183 | - ); | |
| 184 | - if($this->log->is_enabled()) { | |
| 185 | - $this->log->getMenu(); | |
| 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; | |
| 186 | 234 | } |
| 187 | - } | |
| 235 | + update_option('basic_auth_failure_logs', $logs); | |
| 236 | + } | |
| 188 | 237 | |
| 189 | - public function basic_auth_action_failed_access() { | |
| 190 | - if($this->log->is_enabled()) { | |
| 191 | - $this->log->update_status($_SERVER); | |
| 192 | - } | |
| 193 | - if($this->email->is_enabled()) { | |
| 194 | - $this->email->sendAlert($_SERVER); | |
| 195 | - } | |
| 196 | - } | |
| 197 | - | |
| 198 | 238 | } |