PluginProbe
Easy Basic Authentication – Add basic auth to site or admin area / 3.2
Easy Basic Authentication – Add basic auth to site or admin area v3.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 3.2, at class/easy-basic-authentication-form-class.php

268 lines 12.1 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 public $form_field = [];
6
7 public function __construct()
8 {
9 $this->form_field = array(
10 '0' => array(
11 'id' => 'basic-auth-plugin-admin-enable',
12 'title' => __('Enable for wp-admin', 'easy-basic-authentication'),
13 'callback' => 'basic_auth_plugin_admin_enable_cb',
14 ),
15 '1' => array(
16 'id' => 'basic-auth-plugin-enable',
17 'title' => __('Enable for the entire site (only if wp-admin is enabled)', 'easy-basic-authentication'),
18 'callback' => 'basic_auth_plugin_enable_cb',
19 ),
20 '2' => array(
21 'id' => 'basic-auth-plugin-username',
22 'title' => __('Username', 'easy-basic-authentication'),
23 'callback' => 'basic_auth_plugin_username_cb',
24 ),
25 '3' => array(
26 'id' => 'basic-auth-plugin-password',
27 'title' => __('Password', 'easy-basic-authentication'),
28 'callback' => 'basic_auth_plugin_password_cb',
29 ),
30 '4' => array(
31 'id' => 'basic-auth-plugin-admin-log-enable',
32 'title' => __('Enable access logs', 'easy-basic-authentication'),
33 'callback' => 'basic_auth_plugin_admin_log_enable_cb',
34 ),
35 '5' => array(
36 'id' => 'basic-auth-plugin-alert-enable',
37 'title' => __('Enable email alert', 'easy-basic-authentication'),
38 'callback' => 'basic_auth_plugin_alert_enable_cb',
39 ),
40 '6' => array(
41 'id' => 'basic-auth-plugin-email-alert',
42 'title' => __('Email for alert', 'easy-basic-authentication'),
43 'callback' => 'basic_auth_plugin_alertemail_cb',
44 ),
45 '7' => array(
46 'id' => 'basic-auth-plugin-white-list',
47 'title' => __('Ip White list', 'easy-basic-authentication'),
48 'callback' => 'basic_auth_plugin_whitelist_cb',
49 ),
50 '8' => array(
51 'id' => 'basic-auth-plugin-remove-data-after-uninstall',
52 'title' => __('Remove plugin data after uninstall', 'easy-basic-authentication'),
53 'callback' => 'basic_auth_plugin_remove_data_after_uninstall_cb',
54 ),
55
56 );
57 }
58
59 public function basic_auth_plugin_settings_init() {
60 register_setting( 'basic-auth-plugin-settings', 'basic_auth_plugin_admin_enable' );
61 register_setting( 'basic-auth-plugin-settings', 'basic_auth_plugin_enable' );
62 register_setting( 'basic-auth-plugin-settings', 'basic_auth_plugin_username' );
63 register_setting( 'basic-auth-plugin-settings', 'basic_auth_plugin_admin_log_enable' );
64
65 add_settings_section(
66 'basic-auth-plugin-section',
67 __('Configurations for Easy Basic Authentication', 'easy-basic-authentication'),
68 array($this,'basic_auth_plugin_section_cb'),
69 'basic-auth-plugin-settings'
70 );
71
72 foreach($this->form_field as $field) {
73 add_settings_field(
74 $field['id'],
75 esc_html($field['title']),
76 array($this,$field['callback']),
77 'basic-auth-plugin-settings',
78 'basic-auth-plugin-section'
79 );
80 }
81
82 }
83
84 public function basic_auth_plugin_section_cb() {
85 echo esc_html__('Configure basic authentication', 'easy-basic-authentication');
86 }
87
88 public function basic_auth_plugin_enable_cb() {
89 $admin_enable = get_option( 'basic_auth_plugin_admin_enable' );
90 $enable = get_option( 'basic_auth_plugin_enable' );
91 ?>
92 <input type="checkbox" name="basic_auth_plugin_enable" value="1" <?php checked( $enable, 1 ); ?><?php disabled( !$admin_enable ); ?>>
93 <?php
94 }
95
96 public function basic_auth_plugin_admin_enable_cb() {
97 $enable = get_option( 'basic_auth_plugin_admin_enable' );
98 ?>
99 <input type="checkbox" name="basic_auth_plugin_admin_enable" value="1" <?php checked( $enable, 1 ); ?>>
100 <?php
101 }
102
103 public function basic_auth_plugin_username_cb() {
104 $username = get_option( 'basic_auth_plugin_username' );
105 $this->printInputText("text","basic_auth_plugin_username",esc_attr( $username ),'','off');
106 }
107
108 public function basic_auth_plugin_password_cb() {
109 $password = get_option( 'basic_auth_plugin_password' );
110 ?>
111 <input type="password" name="basic_auth_plugin_password" value="" placeholder="<?php esc_attr_e('Enter the password', 'easy-basic-authentication'); ?>" autocomplete="off">
112 <p class="description"><?php esc_html_e('Leave blank to keep the current password.', 'easy-basic-authentication'); ?></p>
113 <?php
114 }
115
116 public function basic_auth_plugin_admin_log_enable_cb() {
117 $enable = get_option( 'basic_auth_plugin_admin_log_enable' );
118 ?>
119 <input type="checkbox" name="basic_auth_plugin_admin_log_enable" value="1" <?php checked( $enable, 1 ); ?>>
120 <?php
121 }
122
123 public function basic_auth_plugin_alert_enable_cb() {
124 $enable = get_option( 'basic_auth_plugin_alert_enable' );
125 ?>
126 <input type="checkbox" name="basic_auth_plugin_alert_enable" value="1" <?php checked( $enable, 1 ); ?>>
127 <?php
128 }
129
130 public function basic_auth_plugin_alertemail_cb() {
131 $email_alert = get_option( 'basic_auth_plugin_alertemail' );
132 $this->printInputText("text","basic_auth_plugin_alertemail",esc_attr( $email_alert ),__('Enter the email', 'easy-basic-authentication'));
133 }
134
135 public function basic_auth_plugin_whitelist_cb() {
136 $white_list = get_option( 'basic_auth_plugin_whitelist' );
137 $this->printInputText("text","basic_auth_plugin_whitelist",esc_attr( $white_list ),__('White list, separated by comma', 'easy-basic-authentication'));
138 }
139
140 public function basic_auth_plugin_remove_data_after_uninstall_cb() {
141 $enable = get_option( 'basic_auth_plugin_remove_data_after_uninstall' );
142 ?>
143 <input type="checkbox" name="basic_auth_plugin_remove_data_after_uninstall" value="1" <?php checked( $enable, 1 ); ?>>
144 <?php
145 }
146
147 public function printInputText($type, $name, $value='', $placeholder = '', $autocomplete = 'off') {
148 echo "<input type='" . esc_attr($type) . "' name='" . esc_attr($name) . "' value='" . esc_attr($value) . "' placeholder='" . esc_attr($placeholder) . "' autocomplete='" . esc_attr($autocomplete) . "' >";
149 }
150
151
152 public function basic_auth_plugin_save_settings($param) {
153 if ( isset( $param['eba_submit'] ) ) {
154 $admin_enable = isset( $param['basic_auth_plugin_admin_enable'] ) ? 1 : 0;
155 $enable = isset( $param['basic_auth_plugin_enable'] ) ? 1 : 0;
156 $username = sanitize_text_field( $param['basic_auth_plugin_username'] );
157 $password = sanitize_text_field( $param['basic_auth_plugin_password'] );
158 $log_enable = isset( $param['basic_auth_plugin_admin_log_enable'] ) ? 1 : 0;
159 $alert_enable = isset( $param['basic_auth_plugin_alert_enable'] ) ? 1 : 0;
160 $alert_email = sanitize_text_field( $param['basic_auth_plugin_alertemail'] );
161 $white_list = sanitize_text_field( $param['basic_auth_plugin_whitelist'] );
162 $remove_data = isset( $param['basic_auth_plugin_remove_data_after_uninstall'] ) ? 1 : 0;
163
164 if ( empty( $username ) ) {
165 add_settings_error(
166 'basic_auth_plugin_username',
167 'basic_auth_plugin_username_error',
168 __('Username cannot be empty', 'easy-basic-authentication'),
169 'error'
170 );
171 return;
172 }
173
174 if ( ! empty( $password ) ) {
175 $hashed_password = wp_hash_password( $password );
176 update_option( 'basic_auth_plugin_password', $hashed_password );
177 }
178
179 if ( is_email( $alert_email ) || (!$alert_enable && $alert_email=='' ) ) {
180 update_option( 'basic_auth_plugin_alertemail', $alert_email );
181 } else {
182 add_settings_error(
183 'basic_auth_plugin_alertemail',
184 'basic_auth_plugin_alertemail_error',
185 __('Invalid email address', 'easy-basic-authentication'),
186 'error'
187 );
188 return;
189 }
190
191 update_option( 'basic_auth_plugin_admin_enable', $admin_enable );
192 update_option( 'basic_auth_plugin_enable', $enable );
193 update_option( 'basic_auth_plugin_username', $username );
194 update_option( 'basic_auth_plugin_admin_log_enable', $log_enable );
195 update_option( 'basic_auth_plugin_alert_enable', $alert_enable );
196 update_option( 'basic_auth_plugin_remove_data_after_uninstall', $remove_data );
197
198 if( $this->validateIpList( $white_list ) || strlen($white_list) == 0 ) {
199 update_option( 'basic_auth_plugin_whitelist', $white_list );
200 } else {
201 add_settings_error(
202 'basic_auth_plugin_whitelist',
203 'basic_auth_plugin_whitelist_error',
204 __('Invalid IP list format. Please enter valid IP addresses separated by commas.', 'easy-basic-authentication'),
205 'error'
206 );
207 return;
208
209 }
210
211 if ( ! empty( $password ) ) {
212 $hashed_password = wp_hash_password( $password );
213 update_option( 'basic_auth_plugin_password', $hashed_password );
214 }
215
216 $this->send_credentials_email($username, $password);
217 }
218 }
219
220 public function basic_auth_plugin_settings_page() {
221 include plugin_dir_path( __FILE__ ) . '../template/settings_page.php';
222 }
223
224 public function validateIpList($white_list) {
225 $ips = explode(',', $white_list);
226 foreach ($ips as $ip) {
227 $ip = trim($ip);
228 if (!filter_var($ip, FILTER_VALIDATE_IP)) {
229 return false;
230 }
231 }
232 return true;
233 }
234
235 public function send_credentials_email($username, $password) {
236 $admin_email = get_option('admin_email');
237 $site_url = home_url();
238 $plugin_url = 'https://wordpress.org/plugins/easy-basic-authentication/';
239
240 $subject = __('Basic Authentication Credentials Updated', 'easy-basic-authentication');
241
242 $message = '<html><body>';
243 $message .= '<p>' . __('Hi,', 'easy-basic-authentication') . '</p>';
244 $message .= '<p>' . __('The basic authentication credentials for your site have been updated.', 'easy-basic-authentication') . '</p>';
245 $message .= '<ul style="list-style-type: none; padding-left: 0;">' .
246 '<li>' . __('Site URL: ', 'easy-basic-authentication') . '<strong>' . esc_url($site_url) . '</strong></li>' .
247 '<li>' . __('Username: ', 'easy-basic-authentication') . '<strong>' . esc_html($username) . '</strong></li>' .
248 '<li>' . __('Password: ', 'easy-basic-authentication') . '<strong>' . esc_html($password) . '</strong></li>' .
249 '</ul>';
250 $message .= '<p>' . sprintf(
251 /* translators: tag "a" and link */
252 __('For more information about this plugin, visit: %s', 'easy-basic-authentication'),
253 '<a href="' . esc_url($plugin_url) . '">' . esc_url($plugin_url) . '</a>'
254 ) . '</p>';
255 $message .= '<p>' . __('Grazie and buona giornata,', 'easy-basic-authentication') . '</p>';
256 $message .= '<p>' . __('The Easy Basic Authentication Plugin Team', 'easy-basic-authentication') . '</p>';
257 $message .= '</body></html>';
258
259 $headers = array(
260 'Content-Type: text/html; charset=UTF-8',
261 'From: ' . $admin_email,
262 );
263
264 wp_mail($admin_email, $subject, $message, $headers);
265 }
266
267
268 }