| 1 |
<?php |
| 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 |
|
| 7 |
class easy_basic_authentication_class { |
| 8 |
|
| 9 |
private $log; |
| 10 |
private $email; |
| 11 |
private $form; |
| 12 |
|
| 13 |
public function __construct() |
| 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 |
|
| 19 |
if(get_option( 'basic_auth_plugin_admin_enable' )) { |
| 20 |
if (in_array($GLOBALS['pagenow'], array('wp-login.php', 'wp-register.php'))) { |
| 21 |
add_action( 'init', array($this,'basic_auth_admin') ); |
| 22 |
} |
| 23 |
} |
| 24 |
|
| 25 |
if(get_option( 'basic_auth_plugin_enable' ) && get_option( 'basic_auth_plugin_admin_enable' )){ |
| 26 |
add_action( 'init', array($this,'basic_auth_root') ); |
| 27 |
} |
| 28 |
|
| 29 |
add_action( 'admin_menu', array($this,'basic_auth_plugin_menu' )); |
| 30 |
add_action( 'admin_init', array($this->form,'basic_auth_plugin_settings_init' )); |
| 31 |
|
| 32 |
add_action('admin_init', function () { |
| 33 |
$post_data = $_POST; |
| 34 |
$this->form->basic_auth_plugin_save_settings($post_data); |
| 35 |
}); |
| 36 |
|
| 37 |
} |
| 38 |
|
| 39 |
public function basic_auth_root() { |
| 40 |
$user = get_option( 'basic_auth_plugin_username' ); |
| 41 |
$pass = get_option( 'basic_auth_plugin_password' ); |
| 42 |
|
| 43 |
if ( !isset( $_SERVER['PHP_AUTH_USER'] ) || !isset( $_SERVER['PHP_AUTH_PW'] ) || |
| 44 |
$_SERVER['PHP_AUTH_USER'] != $user || !wp_check_password( $_SERVER['PHP_AUTH_PW'], $pass ) ) { |
| 45 |
|
| 46 |
$this->do_exit(true); |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
public function basic_auth_admin() { |
| 51 |
$user = get_option( 'basic_auth_plugin_username' ); |
| 52 |
$pass = get_option( 'basic_auth_plugin_password' ); |
| 53 |
|
| 54 |
if ( !isset( $_SERVER['PHP_AUTH_USER'] ) || !isset( $_SERVER['PHP_AUTH_PW'] ) || |
| 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'); |
| 71 |
|
| 72 |
header( 'WWW-Authenticate: Basic realm="My Website"' ); |
| 73 |
header( 'HTTP/1.0 401 Unauthorized' ); |
| 74 |
exit; |
| 75 |
} else { |
| 76 |
header( 'HTTP/1.1 401 Unauthorized' ); |
| 77 |
header( 'WWW-Authenticate: Basic realm="Admin Area"' ); |
| 78 |
exit; |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
public function getWhiteList() { |
| 83 |
return get_option( 'basic_auth_plugin_whitelist' )?explode(',',get_option( 'basic_auth_plugin_whitelist' )):[]; |
| 84 |
} |
| 85 |
|
| 86 |
public function basic_auth_plugin_menu() { |
| 87 |
add_menu_page( |
| 88 |
__('Configurations for Easy Basic Authentication', 'easy-basic-authentication'), |
| 89 |
__('Easy Basic A.', 'easy-basic-authentication'), |
| 90 |
'manage_options', |
| 91 |
'basic-auth-plugin', |
| 92 |
array($this->form, 'basic_auth_plugin_settings_page'), |
| 93 |
'dashicons-lock' |
| 94 |
); |
| 95 |
if($this->log->is_enabled()) { |
| 96 |
$this->log->getMenu(); |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
public function basic_auth_action_failed_access() { |
| 101 |
if($this->log->is_enabled()) { |
| 102 |
$this->log->update_status($_SERVER); |
| 103 |
} |
| 104 |
if($this->email->is_enabled()) { |
| 105 |
$this->email->sendAlert($_SERVER); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
} |
| 110 |
|