| @@ -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,140 +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 ) ) { | |
| 47 | + | |
| 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'); | |
| 63 | + | |
| 31 | 64 | header( 'WWW-Authenticate: Basic realm="My Website"' ); |
| 32 | 65 | header( 'HTTP/1.0 401 Unauthorized' ); |
| 33 | - echo 'Autenticazione richiesta'; | |
| 34 | 66 | exit; |
| 35 | - } | |
| 36 | - } | |
| 37 | - | |
| 38 | - public function basic_auth_admin() { | |
| 39 | - $user = get_option( 'basic_auth_plugin_username' ); | |
| 40 | - $pass = get_option( 'basic_auth_plugin_password' ); | |
| 41 | - | |
| 42 | - if ( !isset( $_SERVER['PHP_AUTH_USER'] ) || !isset( $_SERVER['PHP_AUTH_PW'] ) || | |
| 43 | - $_SERVER['PHP_AUTH_USER'] != $user || !wp_check_password( $_SERVER['PHP_AUTH_PW'], $pass ) ) { | |
| 67 | + } else { | |
| 44 | 68 | header( 'HTTP/1.1 401 Unauthorized' ); |
| 45 | 69 | header( 'WWW-Authenticate: Basic realm="Admin Area"' ); |
| 46 | 70 | exit; |
| 47 | 71 | } |
| 48 | 72 | } |
| 49 | - | |
| 50 | - | |
| 73 | + | |
| 74 | + public function getWhiteList() { | |
| 75 | + return get_option( 'basic_auth_plugin_whitelist' )?explode(',',get_option( 'basic_auth_plugin_whitelist' )):[]; | |
| 76 | + } | |
| 77 | + | |
| 51 | 78 | public function basic_auth_plugin_menu() { |
| 52 | - add_options_page( | |
| 79 | + add_menu_page( | |
| 53 | 80 | __('Configurations for Easy Basic Authentication', 'easy-basic-authentication'), |
| 54 | - __('Easy Basic Authentication', 'easy-basic-authentication'), | |
| 81 | + __('Easy Basic A.', 'easy-basic-authentication'), | |
| 55 | 82 | 'manage_options', |
| 56 | 83 | 'basic-auth-plugin', |
| 57 | - array($this,'basic_auth_plugin_settings_page') | |
| 84 | + array($this->form, 'basic_auth_plugin_settings_page'), | |
| 85 | + 'dashicons-lock' | |
| 58 | 86 | ); |
| 59 | - } | |
| 60 | - | |
| 61 | - public function basic_auth_plugin_settings_page() { | |
| 62 | - include plugin_dir_path( __FILE__ ) . '../template/settings_page.php'; | |
| 63 | - } | |
| 87 | + if($this->log->is_enabled()) { | |
| 88 | + $this->log->getMenu(); | |
| 89 | + } | |
| 90 | + } | |
| 64 | 91 | |
| 65 | - public function basic_auth_plugin_settings_init() { | |
| 66 | - register_setting( 'basic-auth-plugin-settings', 'basic_auth_plugin_admin_enable' ); | |
| 67 | - register_setting( 'basic-auth-plugin-settings', 'basic_auth_plugin_enable' ); | |
| 68 | - register_setting( 'basic-auth-plugin-settings', 'basic_auth_plugin_username' ); | |
| 69 | - | |
| 70 | - add_settings_section( | |
| 71 | - 'basic-auth-plugin-section', | |
| 72 | - __('Configurations for Easy Basic Authentication', 'easy-basic-authentication'), | |
| 73 | - array($this,'basic_auth_plugin_section_cb'), | |
| 74 | - 'basic-auth-plugin-settings' | |
| 75 | - ); | |
| 76 | - | |
| 77 | - add_settings_field( | |
| 78 | - 'basic-auth-plugin-admin-enable', | |
| 79 | - __('Enable for wp-admin', 'easy-basic-authentication'), | |
| 80 | - array($this,'basic_auth_plugin_admin_enable_cb'), | |
| 81 | - 'basic-auth-plugin-settings', | |
| 82 | - 'basic-auth-plugin-section' | |
| 83 | - ); | |
| 84 | - | |
| 85 | - add_settings_field( | |
| 86 | - 'basic-auth-plugin-enable', | |
| 87 | - __('Enable for the entire site (only if wp-admin is enabled)', 'easy-basic-authentication'), | |
| 88 | - array($this,'basic_auth_plugin_enable_cb'), | |
| 89 | - 'basic-auth-plugin-settings', | |
| 90 | - 'basic-auth-plugin-section' | |
| 91 | - ); | |
| 92 | - | |
| 93 | - add_settings_field( | |
| 94 | - 'basic-auth-plugin-username', | |
| 95 | - __('Username', 'easy-basic-authentication'), | |
| 96 | - array($this,'basic_auth_plugin_username_cb'), | |
| 97 | - 'basic-auth-plugin-settings', | |
| 98 | - 'basic-auth-plugin-section' | |
| 99 | - ); | |
| 100 | - | |
| 101 | - add_settings_field( | |
| 102 | - 'basic-auth-plugin-password', | |
| 103 | - __('Password', 'easy-basic-authentication'), | |
| 104 | - array($this,'basic_auth_plugin_password_cb'), | |
| 105 | - 'basic-auth-plugin-settings', | |
| 106 | - 'basic-auth-plugin-section' | |
| 107 | - ); | |
| 108 | - } | |
| 109 | - | |
| 110 | - public function basic_auth_plugin_section_cb() { | |
| 111 | - echo __('Configure basic authentication', 'easy-basic-authentication'); | |
| 112 | - } | |
| 113 | - | |
| 114 | - public function basic_auth_plugin_enable_cb() { | |
| 115 | - $admin_enable = get_option( 'basic_auth_plugin_admin_enable' ); | |
| 116 | - $enable = get_option( 'basic_auth_plugin_enable' ); | |
| 117 | - ?> | |
| 118 | - <input type="checkbox" name="basic_auth_plugin_enable" value="1" <?php checked( $enable, 1 ); ?><?php disabled( !$admin_enable ); ?>> | |
| 119 | - <?php | |
| 120 | - } | |
| 121 | - | |
| 122 | - public function basic_auth_plugin_admin_enable_cb() { | |
| 123 | - $enable = get_option( 'basic_auth_plugin_admin_enable' ); | |
| 124 | - ?> | |
| 125 | - <input type="checkbox" name="basic_auth_plugin_admin_enable" value="1" <?php checked( $enable, 1 ); ?>> | |
| 126 | - <?php | |
| 127 | - } | |
| 128 | - | |
| 129 | - public function basic_auth_plugin_username_cb() { | |
| 130 | - $username = get_option( 'basic_auth_plugin_username' ); | |
| 131 | - ?> | |
| 132 | - <input type="text" name="basic_auth_plugin_username" value="<?php echo esc_attr( $username ); ?>"> | |
| 133 | - <?php | |
| 134 | - } | |
| 135 | - | |
| 136 | - public function basic_auth_plugin_password_cb() { | |
| 137 | - $password = get_option( 'basic_auth_plugin_password' ) | |
| 138 | - ? __('Password entered', 'easy-basic-authentication') | |
| 139 | - : __('Enter the password', 'easy-basic-authentication'); | |
| 140 | - ?> | |
| 141 | - <input type="password" name="basic_auth_plugin_password" value="" placeholder="<?php echo $password; ?>"> | |
| 142 | - <?php | |
| 143 | - } | |
| 144 | - | |
| 145 | - | |
| 146 | - public function basic_auth_plugin_save_settings() { | |
| 147 | - if ( isset( $_POST['eba_submit'] ) ) { | |
| 148 | - $admin_enable = isset( $_POST['basic_auth_plugin_admin_enable'] ) ? 1 : 0; | |
| 149 | - $enable = isset( $_POST['basic_auth_plugin_enable'] ) ? 1 : 0; | |
| 150 | - $username = sanitize_text_field( $_POST['basic_auth_plugin_username'] ); | |
| 151 | - $password = sanitize_text_field( $_POST['basic_auth_plugin_password'] ); | |
| 152 | - | |
| 153 | - update_option( 'basic_auth_plugin_admin_enable', $admin_enable ); | |
| 154 | - update_option( 'basic_auth_plugin_enable', $enable ); | |
| 155 | - update_option( 'basic_auth_plugin_username', $username ); | |
| 156 | - | |
| 157 | - if ( ! empty( $password ) ) { | |
| 158 | - $hashed_password = wp_hash_password( $password ); | |
| 159 | - update_option( 'basic_auth_plugin_password', $hashed_password ); | |
| 160 | - } | |
| 92 | + public function basic_auth_action_failed_access() { | |
| 93 | + if($this->log->is_enabled()) { | |
| 94 | + $this->log->update_status($_SERVER); | |
| 161 | 95 | } |
| 96 | + if($this->email->is_enabled()) { | |
| 97 | + $this->email->sendAlert($_SERVER); | |
| 98 | + } | |
| 162 | 99 | } |
| 163 | - | |
| 100 | + | |
| 164 | 101 | } |