PluginProbe
Easy Basic Authentication – Add basic auth to site or admin area / 1.1
Easy Basic Authentication – Add basic auth to site or admin area v1.1
trunk 1.1 1.2 1.3 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.4 1.4.1 1.5 1.5.1 1.5.2 1.5.3 1.6 1.6.1 1.6.2 1.6.3 1.6.4 1.7 1.7.1 1.8 1.8.1 1.9 All 50 releases
easy-basic-authentication / class / easy-basic-authentication-class.php

easy-basic-authentication-class.php in Easy Basic Authentication – Add basic auth to site or admin area 1.1, at class/easy-basic-authentication-class.php

165 lines 6.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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_options_page(
53 __('Configurations for Easy Basic Authentication', 'easy-basic-authentication'),
54 __('Easy Basic Authentication', 'easy-basic-authentication'),
55 'manage_options',
56 'basic-auth-plugin',
57 array($this,'basic_auth_plugin_settings_page')
58 );
59 }
60
61 public function basic_auth_plugin_settings_page() {
62 include plugin_dir_path( __FILE__ ) . '../template/settings_page.php';
63 }
64
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 }
161 }
162 }
163
164 }
165