| 1 |
<?php |
| 2 |
|
| 3 |
class easy_basic_authentication_class { |
| 4 |
|
| 5 |
|
| 6 |
public function __construct() |
| 7 |
{ |
| 8 |
if(get_option( 'basic_auth_plugin_admin_enable' )) { |
| 9 |
if (in_array($GLOBALS['pagenow'], array('wp-login.php', 'wp-register.php'))) { |
| 10 |
add_action( 'init', array($this,'basic_auth_admin') ); |
| 11 |
} |
| 12 |
} |
| 13 |
|
| 14 |
if(get_option( 'basic_auth_plugin_enable' ) && get_option( 'basic_auth_plugin_admin_enable' )){ |
| 15 |
add_action( 'init', array($this,'basic_auth_root') ); |
| 16 |
} |
| 17 |
|
| 18 |
add_action( 'admin_menu', array($this,'basic_auth_plugin_menu' )); |
| 19 |
add_action( 'admin_init', array($this,'basic_auth_plugin_settings_init' )); |
| 20 |
|
| 21 |
add_action( 'admin_init', array($this,'basic_auth_plugin_save_settings') ); |
| 22 |
|
| 23 |
} |
| 24 |
|
| 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 |
header( 'WWW-Authenticate: Basic realm="My Website"' ); |
| 32 |
header( 'HTTP/1.0 401 Unauthorized' ); |
| 33 |
echo 'Autenticazione richiesta'; |
| 34 |
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 ) ) { |
| 44 |
header( 'HTTP/1.1 401 Unauthorized' ); |
| 45 |
header( 'WWW-Authenticate: Basic realm="Admin Area"' ); |
| 46 |
exit; |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
public function basic_auth_plugin_menu() { |
| 52 |
add_menu_page( |
| 53 |
__('Configurations for Easy Basic Authentication', 'easy-basic-authentication'), |
| 54 |
__('Easy Basic A.', 'easy-basic-authentication'), |
| 55 |
'manage_options', |
| 56 |
'basic-auth-plugin', |
| 57 |
array($this,'basic_auth_plugin_settings_page'), |
| 58 |
'dashicons-lock' |
| 59 |
); |
| 60 |
} |
| 61 |
|
| 62 |
public function basic_auth_plugin_settings_page() { |
| 63 |
include plugin_dir_path( __FILE__ ) . '../template/settings_page.php'; |
| 64 |
} |
| 65 |
|
| 66 |
public function basic_auth_plugin_settings_init() { |
| 67 |
register_setting( 'basic-auth-plugin-settings', 'basic_auth_plugin_admin_enable' ); |
| 68 |
register_setting( 'basic-auth-plugin-settings', 'basic_auth_plugin_enable' ); |
| 69 |
register_setting( 'basic-auth-plugin-settings', 'basic_auth_plugin_username' ); |
| 70 |
|
| 71 |
add_settings_section( |
| 72 |
'basic-auth-plugin-section', |
| 73 |
__('Configurations for Easy Basic Authentication', 'easy-basic-authentication'), |
| 74 |
array($this,'basic_auth_plugin_section_cb'), |
| 75 |
'basic-auth-plugin-settings' |
| 76 |
); |
| 77 |
|
| 78 |
add_settings_field( |
| 79 |
'basic-auth-plugin-admin-enable', |
| 80 |
__('Enable for wp-admin', 'easy-basic-authentication'), |
| 81 |
array($this,'basic_auth_plugin_admin_enable_cb'), |
| 82 |
'basic-auth-plugin-settings', |
| 83 |
'basic-auth-plugin-section' |
| 84 |
); |
| 85 |
|
| 86 |
add_settings_field( |
| 87 |
'basic-auth-plugin-enable', |
| 88 |
__('Enable for the entire site (only if wp-admin is enabled)', 'easy-basic-authentication'), |
| 89 |
array($this,'basic_auth_plugin_enable_cb'), |
| 90 |
'basic-auth-plugin-settings', |
| 91 |
'basic-auth-plugin-section' |
| 92 |
); |
| 93 |
|
| 94 |
add_settings_field( |
| 95 |
'basic-auth-plugin-username', |
| 96 |
__('Username', 'easy-basic-authentication'), |
| 97 |
array($this,'basic_auth_plugin_username_cb'), |
| 98 |
'basic-auth-plugin-settings', |
| 99 |
'basic-auth-plugin-section' |
| 100 |
); |
| 101 |
|
| 102 |
add_settings_field( |
| 103 |
'basic-auth-plugin-password', |
| 104 |
__('Password', 'easy-basic-authentication'), |
| 105 |
array($this,'basic_auth_plugin_password_cb'), |
| 106 |
'basic-auth-plugin-settings', |
| 107 |
'basic-auth-plugin-section' |
| 108 |
); |
| 109 |
} |
| 110 |
|
| 111 |
public function basic_auth_plugin_section_cb() { |
| 112 |
echo __('Configure basic authentication', 'easy-basic-authentication'); |
| 113 |
} |
| 114 |
|
| 115 |
public function basic_auth_plugin_enable_cb() { |
| 116 |
$admin_enable = get_option( 'basic_auth_plugin_admin_enable' ); |
| 117 |
$enable = get_option( 'basic_auth_plugin_enable' ); |
| 118 |
?> |
| 119 |
<input type="checkbox" name="basic_auth_plugin_enable" value="1" <?php checked( $enable, 1 ); ?><?php disabled( !$admin_enable ); ?>> |
| 120 |
<?php |
| 121 |
} |
| 122 |
|
| 123 |
public function basic_auth_plugin_admin_enable_cb() { |
| 124 |
$enable = get_option( 'basic_auth_plugin_admin_enable' ); |
| 125 |
?> |
| 126 |
<input type="checkbox" name="basic_auth_plugin_admin_enable" value="1" <?php checked( $enable, 1 ); ?>> |
| 127 |
<?php |
| 128 |
} |
| 129 |
|
| 130 |
public function basic_auth_plugin_username_cb() { |
| 131 |
$username = get_option( 'basic_auth_plugin_username' ); |
| 132 |
?> |
| 133 |
<input type="text" name="basic_auth_plugin_username" value="<?php echo esc_attr( $username ); ?>"> |
| 134 |
<?php |
| 135 |
} |
| 136 |
|
| 137 |
public function basic_auth_plugin_password_cb() { |
| 138 |
$password = get_option( 'basic_auth_plugin_password' ) |
| 139 |
? __('Password entered', 'easy-basic-authentication') |
| 140 |
: __('Enter the password', 'easy-basic-authentication'); |
| 141 |
?> |
| 142 |
<input type="password" name="basic_auth_plugin_password" value="" placeholder="<?php echo $password; ?>"> |
| 143 |
<?php |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
public function basic_auth_plugin_save_settings() { |
| 148 |
if ( isset( $_POST['eba_submit'] ) ) { |
| 149 |
$admin_enable = isset( $_POST['basic_auth_plugin_admin_enable'] ) ? 1 : 0; |
| 150 |
$enable = isset( $_POST['basic_auth_plugin_enable'] ) ? 1 : 0; |
| 151 |
$username = sanitize_text_field( $_POST['basic_auth_plugin_username'] ); |
| 152 |
$password = sanitize_text_field( $_POST['basic_auth_plugin_password'] ); |
| 153 |
|
| 154 |
update_option( 'basic_auth_plugin_admin_enable', $admin_enable ); |
| 155 |
update_option( 'basic_auth_plugin_enable', $enable ); |
| 156 |
update_option( 'basic_auth_plugin_username', $username ); |
| 157 |
|
| 158 |
if ( ! empty( $password ) ) { |
| 159 |
$hashed_password = wp_hash_password( $password ); |
| 160 |
update_option( 'basic_auth_plugin_password', $hashed_password ); |
| 161 |
} |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
} |
| 166 |
|