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

199 lines 8.2 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 $this->printInputText("text","basic_auth_plugin_username",esc_attr( $username ));
101 }
102
103 public function basic_auth_plugin_password_cb() {
104 $password = get_option( 'basic_auth_plugin_password' )
105 ? __('Password entered', 'easy-basic-authentication')
106 : __('Enter the password', 'easy-basic-authentication');
107 $this->printInputText("password","basic_auth_plugin_password",'',$password);
108 }
109
110 public function basic_auth_plugin_admin_log_enable_cb() {
111 $enable = get_option( 'basic_auth_plugin_admin_log_enable' );
112 ?>
113 <input type="checkbox" name="basic_auth_plugin_admin_log_enable" value="1" <?php checked( $enable, 1 ); ?>>
114 <?php
115 }
116
117 public function basic_auth_plugin_alert_enable_cb() {
118 $enable = get_option( 'basic_auth_plugin_alert_enable' );
119 ?>
120 <input type="checkbox" name="basic_auth_plugin_alert_enable" value="1" <?php checked( $enable, 1 ); ?>>
121 <?php
122 }
123
124 public function basic_auth_plugin_alertemail_cb() {
125 $email_alert = get_option( 'basic_auth_plugin_alertemail' );
126 $this->printInputText("text","basic_auth_plugin_alertemail",esc_attr( $email_alert ),__('Enter the email', 'easy-basic-authentication'));
127 }
128
129 public function basic_auth_plugin_whitelist_cb() {
130 $white_list = get_option( 'basic_auth_plugin_whitelist' );
131 $this->printInputText("text","basic_auth_plugin_whitelist",esc_attr( $white_list ),__('White list, separated by comma', 'easy-basic-authentication'));
132 }
133
134 public function printInputText($tipe, $name, $value='', $placeholder = '') {
135 echo "<input type='$tipe' name='$name' value='$value' placeholder='$placeholder'>";
136 }
137
138 public function basic_auth_plugin_save_settings($param) {
139 if ( isset( $param['eba_submit'] ) ) {
140 $admin_enable = isset( $param['basic_auth_plugin_admin_enable'] ) ? 1 : 0;
141 $enable = isset( $param['basic_auth_plugin_enable'] ) ? 1 : 0;
142 $username = sanitize_text_field( $param['basic_auth_plugin_username'] );
143 $password = sanitize_text_field( $param['basic_auth_plugin_password'] );
144 $log_enable = isset( $param['basic_auth_plugin_admin_log_enable'] ) ? 1 : 0;
145 $alert_enable = isset( $param['basic_auth_plugin_alert_enable'] ) ? 1 : 0;
146 $alert_email = sanitize_text_field( $param['basic_auth_plugin_alertemail'] );
147 $white_list = sanitize_text_field( $param['basic_auth_plugin_whitelist'] );
148
149 if ( !is_email( $alert_email ) ) {
150 add_settings_error(
151 'basic_auth_plugin_alertemail',
152 'basic_auth_plugin_alertemail_error',
153 __('Invalid email address', 'easy-basic-authentication'),
154 'error'
155 );
156 } else {
157 update_option( 'basic_auth_plugin_alertemail', $alert_email );
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
167 if (strlen($white_list)) {
168 if( $this->validateIpList( $white_list ) ) {
169 update_option( 'basic_auth_plugin_whitelist', $white_list );
170 } else {
171 add_settings_error(
172 'basic_auth_plugin_whitelist',
173 'basic_auth_plugin_whitelist_error',
174 __('Invalid whitelist format. Please enter valid ip addresses separated by commas.', 'easy-basic-authentication'),
175 'error'
176 );
177 }
178 }
179 if ( ! empty( $password ) ) {
180 $hashed_password = wp_hash_password( $password );
181 update_option( 'basic_auth_plugin_password', $hashed_password );
182 }
183 }
184 }
185
186 public function basic_auth_plugin_settings_page() {
187 include plugin_dir_path( __FILE__ ) . '../template/settings_page.php';
188 }
189
190 public function validateIpList($white_list) {
191 $pattern = '/^(\s*(?:\d{1,3}\.){3}\d{1,3}\s*(?:,\s*|$))+$/';
192
193 if ( preg_match( $pattern, $white_list ) ) {
194 return true;
195 }
196 return false;
197
198 }
199 }