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

199 lines 8.7 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 );
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
59 add_settings_section(
60 'basic-auth-plugin-section',
61 __('Configurations for Easy Basic Authentication', 'easy-basic-authentication'),
62 array($this,'basic_auth_plugin_section_cb'),
63 'basic-auth-plugin-settings'
64 );
65
66 foreach($this->form_field as $field) {
67 add_settings_field(
68 $field['id'],
69 esc_html($field['title']),
70 array($this,$field['callback']),
71 'basic-auth-plugin-settings',
72 'basic-auth-plugin-section'
73 );
74 }
75
76 }
77
78 public function basic_auth_plugin_section_cb() {
79 echo esc_html__('Configure basic authentication', 'easy-basic-authentication');
80 }
81
82 public function basic_auth_plugin_enable_cb() {
83 $admin_enable = get_option( 'basic_auth_plugin_admin_enable' );
84 $enable = get_option( 'basic_auth_plugin_enable' );
85 ?>
86 <input type="checkbox" name="basic_auth_plugin_enable" value="1" <?php checked( $enable, 1 ); ?><?php disabled( !$admin_enable ); ?>>
87 <?php
88 }
89
90 public function basic_auth_plugin_admin_enable_cb() {
91 $enable = get_option( 'basic_auth_plugin_admin_enable' );
92 ?>
93 <input type="checkbox" name="basic_auth_plugin_admin_enable" value="1" <?php checked( $enable, 1 ); ?>>
94 <?php
95 }
96
97 public function basic_auth_plugin_username_cb() {
98 $username = get_option( 'basic_auth_plugin_username' );
99 $this->printInputText("text","basic_auth_plugin_username",esc_attr( $username ));
100 }
101
102 public function basic_auth_plugin_password_cb() {
103 $password = get_option( 'basic_auth_plugin_password' )
104 ? __('Password entered', 'easy-basic-authentication')
105 : __('Enter the password', 'easy-basic-authentication');
106 $this->printInputText("password","basic_auth_plugin_password",'',$password);
107 }
108
109 public function basic_auth_plugin_admin_log_enable_cb() {
110 $enable = get_option( 'basic_auth_plugin_admin_log_enable' );
111 ?>
112 <input type="checkbox" name="basic_auth_plugin_admin_log_enable" value="1" <?php checked( $enable, 1 ); ?>>
113 <?php
114 }
115
116 public function basic_auth_plugin_alert_enable_cb() {
117 $enable = get_option( 'basic_auth_plugin_alert_enable' );
118 ?>
119 <input type="checkbox" name="basic_auth_plugin_alert_enable" value="1" <?php checked( $enable, 1 ); ?>>
120 <?php
121 }
122
123 public function basic_auth_plugin_alertemail_cb() {
124 $email_alert = get_option( 'basic_auth_plugin_alertemail' );
125 $this->printInputText("text","basic_auth_plugin_alertemail",esc_attr( $email_alert ),__('Enter the email', 'easy-basic-authentication'));
126 }
127
128 public function basic_auth_plugin_whitelist_cb() {
129 $white_list = get_option( 'basic_auth_plugin_whitelist' );
130 $this->printInputText("text","basic_auth_plugin_whitelist",esc_attr( $white_list ),__('White list, separated by comma', 'easy-basic-authentication'));
131 }
132
133 public function printInputText($tipe, $name, $value='', $placeholder = '') {
134 echo "<input type='" . esc_attr($tipe) . "' name='" . esc_attr($name) . "' value='" . esc_attr($value) . "' placeholder='" . esc_attr($placeholder) . "'>";
135 }
136
137 public function basic_auth_plugin_save_settings($param) {
138 if ( isset( $param['eba_submit'] ) ) {
139 $admin_enable = isset( $param['basic_auth_plugin_admin_enable'] ) ? 1 : 0;
140 $enable = isset( $param['basic_auth_plugin_enable'] ) ? 1 : 0;
141 $username = sanitize_text_field( $param['basic_auth_plugin_username'] );
142 $password = sanitize_text_field( $param['basic_auth_plugin_password'] );
143 $log_enable = isset( $param['basic_auth_plugin_admin_log_enable'] ) ? 1 : 0;
144 $alert_enable = isset( $param['basic_auth_plugin_alert_enable'] ) ? 1 : 0;
145 $alert_email = sanitize_text_field( $param['basic_auth_plugin_alertemail'] );
146 $white_list = sanitize_text_field( $param['basic_auth_plugin_whitelist'] );
147
148 if ( is_email( $alert_email ) || (!$alert_enable && $alert_email=='' ) ) {
149 update_option( 'basic_auth_plugin_alertemail', $alert_email );
150 } else {
151 add_settings_error(
152 'basic_auth_plugin_alertemail',
153 'basic_auth_plugin_alertemail_error',
154 __('Invalid email address', 'easy-basic-authentication'),
155 'error'
156 );
157 return;
158 }
159
160 update_option( 'basic_auth_plugin_admin_enable', $admin_enable );
161 update_option( 'basic_auth_plugin_enable', $enable );
162 update_option( 'basic_auth_plugin_username', $username );
163 update_option( 'basic_auth_plugin_admin_log_enable', $log_enable );
164 update_option( 'basic_auth_plugin_alert_enable', $alert_enable );
165
166 if( $this->validateIpList( $white_list ) || strlen($white_list) == 0 ) {
167 update_option( 'basic_auth_plugin_whitelist', $white_list );
168 } else {
169 add_settings_error(
170 'basic_auth_plugin_whitelist',
171 'basic_auth_plugin_whitelist_error',
172 __('Invalid IP list format. Please enter valid IP addresses separated by commas.', 'easy-basic-authentication'),
173 'error'
174 );
175 return;
176 }
177
178 if ( ! empty( $password ) ) {
179 $hashed_password = wp_hash_password( $password );
180 update_option( 'basic_auth_plugin_password', $hashed_password );
181 }
182 }
183 }
184
185 public function basic_auth_plugin_settings_page() {
186 include plugin_dir_path( __FILE__ ) . '../template/settings_page.php';
187 }
188
189 public function validateIpList($white_list) {
190 $ips = explode(',', $white_list);
191 foreach ($ips as $ip) {
192 $ip = trim($ip);
193 if (!filter_var($ip, FILTER_VALIDATE_IP)) {
194 return false;
195 }
196 }
197 return true;
198 }
199 }