PluginProbe
Easy Basic Authentication – Add basic auth to site or admin area / 1.5.2
Easy Basic Authentication – Add basic auth to site or admin area v1.5.2
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-form-class.php

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

203 lines 8.3 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_form_class {
4
5 const FORM_FIELD = array(
6 '0' => array(
7 'id' => 'basic-auth-plugin-admin-enable',
8 'title' => 'Enable for wp-admin',
9 'callback' => 'basic_auth_plugin_admin_enable_cb',
10 ),
11 '1' => array(
12 'id' => 'basic-auth-plugin-enable',
13 'title' => 'Enable for the entire site (only if wp-admin is enabled)',
14 'callback' => 'basic_auth_plugin_enable_cb',
15 ),
16 '2' => array(
17 'id' => 'basic-auth-plugin-username',
18 'title' => 'Username',
19 'callback' => 'basic_auth_plugin_username_cb',
20 ),
21 '3' => array(
22 'id' => 'basic-auth-plugin-password',
23 'title' => 'Password',
24 'callback' => 'basic_auth_plugin_password_cb',
25 ),
26 '4' => array(
27 'id' => 'basic-auth-plugin-admin-log-enable',
28 'title' => 'Enable access logs',
29 'callback' => 'basic_auth_plugin_admin_log_enable_cb',
30 ),
31 '5' => array(
32 'id' => 'basic-auth-plugin-alert-enable',
33 'title' => 'Enable email alert',
34 'callback' => 'basic_auth_plugin_alert_enable_cb',
35 ),
36 '6' => array(
37 'id' => 'basic-auth-plugin-email-alert',
38 'title' => 'Email for alert',
39 'callback' => 'basic_auth_plugin_alertemail_cb',
40 ),
41 '7' => array(
42 'id' => 'basic-auth-plugin-white-list',
43 'title' => 'Ip White list',
44 'callback' => 'basic_auth_plugin_whitelist_cb',
45 )
46 );
47
48 public function __construct()
49 {
50
51 }
52
53 public function basic_auth_plugin_settings_init() {
54 register_setting( 'basic-auth-plugin-settings', 'basic_auth_plugin_admin_enable' );
55 register_setting( 'basic-auth-plugin-settings', 'basic_auth_plugin_enable' );
56 register_setting( 'basic-auth-plugin-settings', 'basic_auth_plugin_username' );
57 register_setting( 'basic-auth-plugin-settings', 'basic_auth_plugin_admin_log_enable' );
58 register_setting( 'basic-auth-plugin-settings', 'basic_auth_plugin_whitelist' );
59
60 add_settings_section(
61 'basic-auth-plugin-section',
62 __('Configurations for Easy Basic Authentication', 'easy-basic-authentication'),
63 array($this,'basic_auth_plugin_section_cb'),
64 'basic-auth-plugin-settings'
65 );
66
67 foreach(self::FORM_FIELD as $field) {
68 add_settings_field(
69 $field['id'],
70 __($field['title'], 'easy-basic-authentication'),
71 array($this,$field['callback']),
72 'basic-auth-plugin-settings',
73 'basic-auth-plugin-section'
74 );
75 }
76
77 }
78
79 public function basic_auth_plugin_section_cb() {
80 echo __('Configure basic authentication', 'easy-basic-authentication');
81 }
82
83 public function basic_auth_plugin_enable_cb() {
84 $admin_enable = get_option( 'basic_auth_plugin_admin_enable' );
85 $enable = get_option( 'basic_auth_plugin_enable' );
86 ?>
87 <input type="checkbox" name="basic_auth_plugin_enable" value="1" <?php checked( $enable, 1 ); ?><?php disabled( !$admin_enable ); ?>>
88 <?php
89 }
90
91 public function basic_auth_plugin_admin_enable_cb() {
92 $enable = get_option( 'basic_auth_plugin_admin_enable' );
93 ?>
94 <input type="checkbox" name="basic_auth_plugin_admin_enable" value="1" <?php checked( $enable, 1 ); ?>>
95 <?php
96 }
97
98 public function basic_auth_plugin_username_cb() {
99 $username = get_option( 'basic_auth_plugin_username' );
100 ?>
101 <input type="text" name="basic_auth_plugin_username" value="<?php echo esc_attr( $username ); ?>">
102 <?php
103 }
104
105 public function basic_auth_plugin_password_cb() {
106 $password = get_option( 'basic_auth_plugin_password' )
107 ? __('Password entered', 'easy-basic-authentication')
108 : __('Enter the password', 'easy-basic-authentication');
109 ?>
110 <input type="password" name="basic_auth_plugin_password" value="" placeholder="<?php echo $password; ?>">
111 <?php
112 }
113
114 public function basic_auth_plugin_admin_log_enable_cb() {
115 $enable = get_option( 'basic_auth_plugin_admin_log_enable' );
116 ?>
117 <input type="checkbox" name="basic_auth_plugin_admin_log_enable" value="1" <?php checked( $enable, 1 ); ?>>
118 <?php
119 }
120
121 public function basic_auth_plugin_alert_enable_cb() {
122 $enable = get_option( 'basic_auth_plugin_alert_enable' );
123 ?>
124 <input type="checkbox" name="basic_auth_plugin_alert_enable" value="1" <?php checked( $enable, 1 ); ?>>
125 <?php
126 }
127
128 public function basic_auth_plugin_alertemail_cb() {
129 $email_alert = get_option( 'basic_auth_plugin_alertemail' );
130 ?>
131 <input type="text" name="basic_auth_plugin_alertemail" value="<?php echo esc_attr( $email_alert ); ?>" placeholder="<?php echo __('Enter the email', 'easy-basic-authentication'); ?>">
132 <?php
133 }
134
135 public function basic_auth_plugin_whitelist_cb() {
136 $email_alert = get_option( 'basic_auth_plugin_whitelist' );
137 ?>
138 <input type="text" name="basic_auth_plugin_whitelist" value="<?php echo esc_attr( $email_alert ); ?>" placeholder="<?php echo __('White list, separated by comma', 'easy-basic-authentication'); ?>">
139 <?php
140 }
141
142 public function basic_auth_plugin_save_settings($param) {
143 if ( isset( $param['eba_submit'] ) ) {
144 $admin_enable = isset( $param['basic_auth_plugin_admin_enable'] ) ? 1 : 0;
145 $enable = isset( $param['basic_auth_plugin_enable'] ) ? 1 : 0;
146 $username = sanitize_text_field( $param['basic_auth_plugin_username'] );
147 $password = sanitize_text_field( $param['basic_auth_plugin_password'] );
148 $log_enable = isset( $param['basic_auth_plugin_admin_log_enable'] ) ? 1 : 0;
149 $alert_enable = isset( $param['basic_auth_plugin_alert_enable'] ) ? 1 : 0;
150 $alert_email = sanitize_text_field( $param['basic_auth_plugin_alertemail'] );
151 $white_list = sanitize_text_field( $param['basic_auth_plugin_whitelist'] );
152
153 if ( !is_email( $alert_email ) ) {
154 add_settings_error(
155 'basic_auth_plugin_alertemail',
156 'basic_auth_plugin_alertemail_error',
157 __('Invalid email address', 'easy-basic-authentication'),
158 'error'
159 );
160 } else {
161 update_option( 'basic_auth_plugin_alertemail', $alert_email );
162 }
163
164 update_option( 'basic_auth_plugin_admin_enable', $admin_enable );
165 update_option( 'basic_auth_plugin_enable', $enable );
166 update_option( 'basic_auth_plugin_username', $username );
167 update_option( 'basic_auth_plugin_admin_log_enable', $log_enable );
168 update_option( 'basic_auth_plugin_alert_enable', $alert_enable );
169
170
171 if (strlen($white_list)) {
172 if( $this->validateIpList( $white_list ) ) {
173 update_option( 'basic_auth_plugin_whitelist', $white_list );
174 } else {
175 add_settings_error(
176 'basic_auth_plugin_whitelist',
177 'basic_auth_plugin_whitelist_error',
178 __('Invalid whitelist format. Please enter valid email addresses separated by commas.', 'easy-basic-authentication'),
179 'error'
180 );
181 }
182 }
183 if ( ! empty( $password ) ) {
184 $hashed_password = wp_hash_password( $password );
185 update_option( 'basic_auth_plugin_password', $hashed_password );
186 }
187 }
188 }
189
190 public function basic_auth_plugin_settings_page() {
191 include plugin_dir_path( __FILE__ ) . '../template/settings_page.php';
192 }
193
194 public function validateIpList($white_list) {
195 $pattern = '/^(\s*(?:\d{1,3}\.){3}\d{1,3}\s*(?:,\s*|$))+$/';
196
197 if ( preg_match( $pattern, $white_list ) ) {
198 return true;
199 }
200 return false;
201
202 }
203 }