PluginProbe
Authorizer / 3.15.2
Authorizer v3.15.2
3.15.3 3.15.2 3.15.1 3.15.0 3.14.3 3.14.4 3.14.2 3.14.1 2.8.1 2.8.2 2.8.3 2.8.4 2.8.5 2.8.6 2.8.7 2.8.8 2.9.0 2.9.1 2.9.10 2.9.11 2.9.12 2.9.13 2.9.2 2.9.3 2.9.6 All 126 releases
authorizer / src / authorizer / class-options.php

class-options.php in Authorizer 3.15.2, at src/authorizer/class-options.php

1,760 lines 94.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Authorizer
4 *
5 * @license GPL-2.0+
6 * @link https://github.com/uhm-coe/authorizer
7 * @package authorizer
8 */
9
10 namespace Authorizer;
11
12 use Authorizer\Helper;
13
14 // Prevent direct access.
15 defined( 'ABSPATH' ) || exit;
16
17 /**
18 * Contains functions for rendering the Access Lists tab in Authorizer Settings.
19 */
20 class Options extends Singleton {
21
22 /**
23 * Retrieves a specific plugin option from db. Multisite enabled.
24 *
25 * @param string $option Option name.
26 * @param string $admin_mode Helper::NETWORK_CONTEXT will retrieve the multisite value.
27 * @param string $override_mode 'allow override' will retrieve the multisite value if it exists.
28 * @param string $print_mode 'print overlay' will output overlay that hides this option on the settings page.
29 * @return mixed Option value, or null on failure.
30 */
31 public function get( $option, $admin_mode = Helper::SINGLE_CONTEXT, $override_mode = 'no override', $print_mode = 'no overlay' ) {
32 // Special case for user lists (they are saved seperately to prevent concurrency issues).
33 if ( in_array( $option, array( 'access_users_pending', 'access_users_approved', 'access_users_blocked' ), true ) ) {
34 $list = Helper::NETWORK_CONTEXT === $admin_mode ? array() : get_option( 'auth_settings_' . $option, array() );
35 if ( is_multisite() && Helper::NETWORK_CONTEXT === $admin_mode ) {
36 $list = get_blog_option( get_main_site_id( get_main_network_id() ), 'auth_multisite_settings_' . $option, array() );
37 }
38 return $list;
39 }
40
41 // Get all plugin options.
42 $auth_settings = $this->get_all( $admin_mode, $override_mode );
43
44 // Get multisite options (for checking if multisite override is prevented).
45 $auth_multisite_settings = is_multisite() ? get_blog_option( get_main_site_id( get_main_network_id() ), 'auth_multisite_settings', array() ) : array();
46
47 // Set option to null if it wasn't found.
48 if ( ! array_key_exists( $option, $auth_settings ) ) {
49 return null;
50 }
51
52 // If requested and appropriate, print the overlay hiding the
53 // single site option that is overridden by a multisite option.
54 if (
55 Helper::NETWORK_CONTEXT !== $admin_mode &&
56 'allow override' === $override_mode &&
57 'print overlay' === $print_mode &&
58 array_key_exists( 'multisite_override', $auth_settings ) &&
59 '1' === $auth_settings['multisite_override'] &&
60 (
61 ! array_key_exists( 'advanced_override_multisite', $auth_settings ) ||
62 1 !== intval( $auth_settings['advanced_override_multisite'] ) ||
63 ! empty( $auth_multisite_settings['prevent_override_multisite'] )
64 )
65 ) {
66 // Get original plugin options (not overridden value). We'll
67 // show this old value behind the disabled overlay.
68 // $auth_settings = $this->get_all( $admin_mode, 'no override' );
69 // (This feature is disabled).
70 //
71 $name = "auth_settings[$option]";
72 $id = "auth_settings_$option";
73 // Get category of option so we can link directly to the appropriate tab
74 // in multisite options (most options are on the External Service tab;
75 // only access_who_can_login and access_who_can_view are on the Access
76 // Lists tab; all options on the Advanced tab start with "advanced_").
77 $tab = '&tab=external';
78 if ( 'access_who_can_login' === $option || 'access_who_can_view' === $option ) {
79 $tab = '&tab=access_lists';
80 } elseif ( 0 === strpos( $option, 'advanced_' ) ) {
81 $tab = '&tab=advanced';
82 } elseif ( str_starts_with( $option, 'oauth2_' ) ) {
83 $tab = '&tab=external_oauth2';
84 } elseif ( str_starts_with( $option, 'oidc_' ) ) {
85 $tab = '&tab=external_oidc';
86 } elseif ( str_starts_with( $option, 'google_' ) ) {
87 $tab = '&tab=external_google';
88 } elseif ( str_starts_with( $option, 'cas_' ) ) {
89 $tab = '&tab=external_cas';
90 } elseif ( str_starts_with( $option, 'ldap_' ) ) {
91 $tab = '&tab=external_ldap';
92 }
93 ?>
94 <div id="overlay-hide-auth_settings_<?php echo esc_attr( $option ); ?>" class="auth_multisite_override_overlay">
95 <span class="overlay-note">
96 <?php esc_html_e( 'This setting is overridden by a', 'authorizer' ); ?> <a href="<?php echo esc_attr( network_admin_url( 'admin.php?page=authorizer' . $tab ) ); ?>"><?php esc_html_e( 'multisite option', 'authorizer' ); ?></a>.
97 </span>
98 </div>
99 <?php
100 }
101
102 // If we're getting an option in a site that has overridden the multisite
103 // override (and is not prevented from doing so), make sure we are returning
104 // the option value from that site (not the multisite value).
105 if (
106 array_key_exists( 'advanced_override_multisite', $auth_settings ) &&
107 1 === intval( $auth_settings['advanced_override_multisite'] ) &&
108 empty( $auth_multisite_settings['prevent_override_multisite'] )
109 ) {
110 $auth_settings = $this->get_all( $admin_mode, 'no override' );
111 }
112
113 // Set option to null if it wasn't found.
114 if ( ! array_key_exists( $option, $auth_settings ) ) {
115 return null;
116 }
117
118 return $auth_settings[ $option ];
119 }
120
121 /**
122 * Retrieves all plugin options from db. Multisite enabled.
123 *
124 * @param string $admin_mode Helper::NETWORK_CONTEXT will retrieve the multisite value.
125 * @param string $override_mode 'allow override' will retrieve the multisite value if it exists.
126 * @return mixed Option value, or null on failure.
127 */
128 public function get_all( $admin_mode = Helper::SINGLE_CONTEXT, $override_mode = 'no override' ) {
129 // Grab plugin settings (skip if in Helper::NETWORK_CONTEXT mode).
130 $auth_settings = Helper::NETWORK_CONTEXT === $admin_mode ? array() : get_option( 'auth_settings' );
131
132 // Initialize to default values if the plugin option doesn't exist.
133 if ( false === $auth_settings ) {
134 $auth_settings = $this->set_default_options();
135 }
136
137 // Merge multisite options if we're in a network.
138 if ( is_multisite() ) {
139 // Get multisite options.
140 $auth_multisite_settings = get_blog_option( get_main_site_id( get_main_network_id() ), 'auth_multisite_settings', array() );
141
142 // Return the multisite options if we're viewing the network admin options page.
143 // Otherwise override options with their multisite equivalents.
144 if ( Helper::NETWORK_CONTEXT === $admin_mode ) {
145 $auth_settings = $auth_multisite_settings;
146 } elseif (
147 'allow override' === $override_mode &&
148 array_key_exists( 'multisite_override', $auth_multisite_settings ) &&
149 '1' === $auth_multisite_settings['multisite_override']
150 ) {
151 // Keep track of the multisite override (and prevention) selection.
152 $auth_settings['multisite_override'] = $auth_multisite_settings['multisite_override'];
153 $auth_settings['prevent_override_multisite'] = $auth_multisite_settings['prevent_override_multisite'];
154
155 // Don't merge multisite options if the current site has overridden them
156 // (and isn't prevented from doing so).
157 if (
158 array_key_exists( 'advanced_override_multisite', $auth_settings ) &&
159 1 === intval( $auth_settings['advanced_override_multisite'] ) &&
160 empty( $auth_settings['prevent_override_multisite'] )
161 ) {
162 return $auth_settings;
163 }
164
165 /**
166 * Note: the options below should be the complete list of overridden
167 * options. It is *not* the complete list of all options (some options
168 * don't have a multisite equivalent).
169 */
170
171 /**
172 * Note: access_users_approved, access_users_pending, and
173 * access_users_blocked do not get overridden. However, since
174 * access_users_approved has a multisite equivalent, you must retrieve
175 * them both seperately. This is done because the two lists should be
176 * treated differently.
177 *
178 * $approved_users = $options->get( 'access_users_approved', Helper::SINGLE_CONTEXT );
179 * $ms_approved_users = $options->get( 'access_users_approved', Helper::NETWORK_CONTEXT );
180 */
181
182 // Override external service (Oauth2) and associated options.
183 $auth_settings['oauth2'] = $auth_multisite_settings['oauth2'];
184 $auth_settings['oauth2_auto_login'] = $auth_multisite_settings['oauth2_auto_login'] ?? '';
185 $auth_settings['oauth2_num_servers'] = $auth_multisite_settings['oauth2_num_servers'] ?? 1;
186 $auth_settings['oauth2_provider'] = $auth_multisite_settings['oauth2_provider'] ?? '';
187 $auth_settings['oauth2_custom_label'] = $auth_multisite_settings['oauth2_custom_label'] ?? 'OAuth2';
188 $auth_settings['oauth2_clientid'] = $auth_multisite_settings['oauth2_clientid'] ?? '';
189 $auth_settings['oauth2_clientsecret'] = $auth_multisite_settings['oauth2_clientsecret'] ?? '';
190 $auth_settings['oauth2_hosteddomain'] = $auth_multisite_settings['oauth2_hosteddomain'] ?? '';
191 $auth_settings['oauth2_tenant_id'] = $auth_multisite_settings['oauth2_tenant_id'] ?? '';
192 $auth_settings['oauth2_url_authorize'] = $auth_multisite_settings['oauth2_url_authorize'] ?? '';
193 $auth_settings['oauth2_url_token'] = $auth_multisite_settings['oauth2_url_token'] ?? '';
194 $auth_settings['oauth2_url_resource'] = $auth_multisite_settings['oauth2_url_resource'] ?? '';
195 $auth_settings['oauth2_attr_username'] = $auth_multisite_settings['oauth2_attr_username'] ?? '';
196 $auth_settings['oauth2_attr_email'] = $auth_multisite_settings['oauth2_attr_email'] ?? '';
197 $auth_settings['oauth2_attr_first_name'] = $auth_multisite_settings['oauth2_attr_first_name'] ?? '';
198 $auth_settings['oauth2_attr_last_name'] = $auth_multisite_settings['oauth2_attr_last_name'] ?? '';
199 $auth_settings['oauth2_attr_update_on_login'] = $auth_multisite_settings['oauth2_attr_update_on_login'] ?? '';
200 $auth_settings['oauth2_require_verified_email'] = $auth_multisite_settings['oauth2_require_verified_email'] ?? '';
201 $auth_settings['oauth2_link_on_username'] = $auth_multisite_settings['oauth2_link_on_username'] ?? '';
202 // Add any options for extra OAuth2 servers.
203 if ( ! empty( $auth_multisite_settings['oauth2_num_servers'] ) && intval( $auth_multisite_settings['oauth2_num_servers'] ) > 1 ) {
204 foreach ( range( 2, min( intval( $auth_multisite_settings['oauth2_num_servers'] ), 20 ) ) as $oauth2_num_server ) {
205 $auth_settings[ 'oauth2_provider_' . $oauth2_num_server ] = $auth_multisite_settings[ 'oauth2_provider_' . $oauth2_num_server ] ?? '';
206 $auth_settings[ 'oauth2_custom_label_' . $oauth2_num_server ] = $auth_multisite_settings[ 'oauth2_custom_label_' . $oauth2_num_server ] ?? 'OAuth2';
207 $auth_settings[ 'oauth2_clientid_' . $oauth2_num_server ] = $auth_multisite_settings[ 'oauth2_clientid_' . $oauth2_num_server ] ?? '';
208 $auth_settings[ 'oauth2_clientsecret_' . $oauth2_num_server ] = $auth_multisite_settings[ 'oauth2_clientsecret_' . $oauth2_num_server ] ?? '';
209 $auth_settings[ 'oauth2_hosteddomain_' . $oauth2_num_server ] = $auth_multisite_settings[ 'oauth2_hosteddomain_' . $oauth2_num_server ] ?? '';
210 $auth_settings[ 'oauth2_tenant_id_' . $oauth2_num_server ] = $auth_multisite_settings[ 'oauth2_tenant_id_' . $oauth2_num_server ] ?? '';
211 $auth_settings[ 'oauth2_url_authorize_' . $oauth2_num_server ] = $auth_multisite_settings[ 'oauth2_url_authorize_' . $oauth2_num_server ] ?? '';
212 $auth_settings[ 'oauth2_url_token_' . $oauth2_num_server ] = $auth_multisite_settings[ 'oauth2_url_token_' . $oauth2_num_server ] ?? '';
213 $auth_settings[ 'oauth2_url_resource_' . $oauth2_num_server ] = $auth_multisite_settings[ 'oauth2_url_resource_' . $oauth2_num_server ] ?? '';
214 $auth_settings[ 'oauth2_attr_username_' . $oauth2_num_server ] = $auth_multisite_settings[ 'oauth2_attr_username_' . $oauth2_num_server ] ?? '';
215 $auth_settings[ 'oauth2_attr_email_' . $oauth2_num_server ] = $auth_multisite_settings[ 'oauth2_attr_email_' . $oauth2_num_server ] ?? '';
216 $auth_settings[ 'oauth2_attr_first_name_' . $oauth2_num_server ] = $auth_multisite_settings[ 'oauth2_attr_first_name_' . $oauth2_num_server ] ?? '';
217 $auth_settings[ 'oauth2_attr_last_name_' . $oauth2_num_server ] = $auth_multisite_settings[ 'oauth2_attr_last_name_' . $oauth2_num_server ] ?? '';
218 $auth_settings[ 'oauth2_attr_update_on_login_' . $oauth2_num_server ] = $auth_multisite_settings[ 'oauth2_attr_update_on_login_' . $oauth2_num_server ] ?? '';
219 $auth_settings[ 'oauth2_require_verified_email_' . $oauth2_num_server ] = $auth_multisite_settings[ 'oauth2_require_verified_email_' . $oauth2_num_server ] ?? '';
220 $auth_settings[ 'oauth2_link_on_username_' . $oauth2_num_server ] = $auth_multisite_settings[ 'oauth2_link_on_username_' . $oauth2_num_server ] ?? '';
221 }
222 }
223
224 // Override external service (OIDC) and associated options.
225 $auth_settings['oidc'] = $auth_multisite_settings['oidc'] ?? '';
226 $auth_settings['oidc_num_servers'] = $auth_multisite_settings['oidc_num_servers'] ?? 1;
227 $auth_settings['oidc_auto_login'] = $auth_multisite_settings['oidc_auto_login'] ?? '';
228 $auth_settings['oidc_custom_label'] = $auth_multisite_settings['oidc_custom_label'] ?? 'OIDC';
229 $auth_settings['oidc_issuer'] = $auth_multisite_settings['oidc_issuer'] ?? '';
230 $auth_settings['oidc_client_id'] = $auth_multisite_settings['oidc_client_id'] ?? '';
231 $auth_settings['oidc_client_secret'] = $auth_multisite_settings['oidc_client_secret'] ?? '';
232 $auth_settings['oidc_scopes'] = $auth_multisite_settings['oidc_scopes'] ?? 'openid email profile';
233 $auth_settings['oidc_prompt'] = $auth_multisite_settings['oidc_prompt'] ?? '';
234 $auth_settings['oidc_login_hint'] = $auth_multisite_settings['oidc_login_hint'] ?? '';
235 $auth_settings['oidc_max_age'] = $auth_multisite_settings['oidc_max_age'] ?? '';
236 $auth_settings['oidc_attr_username'] = $auth_multisite_settings['oidc_attr_username'] ?? 'preferred_username';
237 $auth_settings['oidc_attr_email'] = $auth_multisite_settings['oidc_attr_email'] ?? 'email';
238 $auth_settings['oidc_attr_first_name'] = $auth_multisite_settings['oidc_attr_first_name'] ?? 'given_name';
239 $auth_settings['oidc_attr_last_name'] = $auth_multisite_settings['oidc_attr_last_name'] ?? 'family_name';
240 $auth_settings['oidc_attr_update_on_login'] = $auth_multisite_settings['oidc_attr_update_on_login'] ?? '';
241 $auth_settings['oidc_force_auth_method'] = $auth_multisite_settings['oidc_force_auth_method'] ?? '';
242 $auth_settings['oidc_require_verified_email'] = $auth_multisite_settings['oidc_require_verified_email'] ?? '';
243 $auth_settings['oidc_link_on_username'] = $auth_multisite_settings['oidc_link_on_username'] ?? '';
244 $auth_settings['oidc_hosteddomain'] = $auth_multisite_settings['oidc_hosteddomain'] ?? '';
245 // Add any options for extra OIDC servers.
246 if ( ! empty( $auth_multisite_settings['oidc_num_servers'] ) && intval( $auth_multisite_settings['oidc_num_servers'] ) > 1 ) {
247 foreach ( range( 2, min( intval( $auth_multisite_settings['oidc_num_servers'] ), 20 ) ) as $oidc_num_server ) {
248 $auth_settings[ 'oidc_custom_label_' . $oidc_num_server ] = $auth_multisite_settings[ 'oidc_custom_label_' . $oidc_num_server ] ?? 'OIDC';
249 $auth_settings[ 'oidc_issuer_' . $oidc_num_server ] = $auth_multisite_settings[ 'oidc_issuer_' . $oidc_num_server ] ?? '';
250 $auth_settings[ 'oidc_client_id_' . $oidc_num_server ] = $auth_multisite_settings[ 'oidc_client_id_' . $oidc_num_server ] ?? '';
251 $auth_settings[ 'oidc_client_secret_' . $oidc_num_server ] = $auth_multisite_settings[ 'oidc_client_secret_' . $oidc_num_server ] ?? '';
252 $auth_settings[ 'oidc_scopes_' . $oidc_num_server ] = $auth_multisite_settings[ 'oidc_scopes_' . $oidc_num_server ] ?? 'openid email profile';
253 $auth_settings[ 'oidc_prompt_' . $oidc_num_server ] = $auth_multisite_settings[ 'oidc_prompt_' . $oidc_num_server ] ?? '';
254 $auth_settings[ 'oidc_login_hint_' . $oidc_num_server ] = $auth_multisite_settings[ 'oidc_login_hint_' . $oidc_num_server ] ?? '';
255 $auth_settings[ 'oidc_max_age_' . $oidc_num_server ] = $auth_multisite_settings[ 'oidc_max_age_' . $oidc_num_server ] ?? '';
256 $auth_settings[ 'oidc_attr_username_' . $oidc_num_server ] = $auth_multisite_settings[ 'oidc_attr_username_' . $oidc_num_server ] ?? 'preferred_username';
257 $auth_settings[ 'oidc_attr_email_' . $oidc_num_server ] = $auth_multisite_settings[ 'oidc_attr_email_' . $oidc_num_server ] ?? 'email';
258 $auth_settings[ 'oidc_attr_first_name_' . $oidc_num_server ] = $auth_multisite_settings[ 'oidc_attr_first_name_' . $oidc_num_server ] ?? 'given_name';
259 $auth_settings[ 'oidc_attr_last_name_' . $oidc_num_server ] = $auth_multisite_settings[ 'oidc_attr_last_name_' . $oidc_num_server ] ?? 'family_name';
260 $auth_settings[ 'oidc_attr_update_on_login_' . $oidc_num_server ] = $auth_multisite_settings[ 'oidc_attr_update_on_login_' . $oidc_num_server ] ?? '';
261 $auth_settings[ 'oidc_force_auth_method_' . $oidc_num_server ] = $auth_multisite_settings[ 'oidc_force_auth_method_' . $oidc_num_server ] ?? '';
262 $auth_settings[ 'oidc_require_verified_email_' . $oidc_num_server ] = $auth_multisite_settings[ 'oidc_require_verified_email_' . $oidc_num_server ] ?? '';
263 $auth_settings[ 'oidc_link_on_username_' . $oidc_num_server ] = $auth_multisite_settings[ 'oidc_link_on_username_' . $oidc_num_server ] ?? '';
264 $auth_settings[ 'oidc_hosteddomain_' . $oidc_num_server ] = $auth_multisite_settings[ 'oidc_hosteddomain_' . $oidc_num_server ] ?? '';
265 }
266 }
267
268 // Override external service (Google) and associated options.
269 $auth_settings['google'] = $auth_multisite_settings['google'];
270 $auth_settings['google_clientid'] = $auth_multisite_settings['google_clientid'];
271 $auth_settings['google_clientsecret'] = $auth_multisite_settings['google_clientsecret'];
272 $auth_settings['google_hosteddomain'] = $auth_multisite_settings['google_hosteddomain'];
273
274 // Override external service (CAS) and associated options.
275 $auth_settings['cas'] = $auth_multisite_settings['cas'];
276 $auth_settings['cas_auto_login'] = $auth_multisite_settings['cas_auto_login'];
277 $auth_settings['cas_num_servers'] = $auth_multisite_settings['cas_num_servers'] ?? 1;
278 $auth_settings['cas_custom_label'] = $auth_multisite_settings['cas_custom_label'];
279 $auth_settings['cas_host'] = $auth_multisite_settings['cas_host'];
280 $auth_settings['cas_port'] = $auth_multisite_settings['cas_port'];
281 $auth_settings['cas_path'] = $auth_multisite_settings['cas_path'];
282 $auth_settings['cas_method'] = $auth_multisite_settings['cas_method'];
283 $auth_settings['cas_version'] = $auth_multisite_settings['cas_version'];
284 $auth_settings['cas_attr_email'] = $auth_multisite_settings['cas_attr_email'];
285 $auth_settings['cas_attr_first_name'] = $auth_multisite_settings['cas_attr_first_name'];
286 $auth_settings['cas_attr_last_name'] = $auth_multisite_settings['cas_attr_last_name'];
287 $auth_settings['cas_attr_update_on_login'] = $auth_multisite_settings['cas_attr_update_on_login'];
288 $auth_settings['cas_link_on_username'] = $auth_multisite_settings['cas_link_on_username'];
289 // Add any options for extra CAS servers.
290 if ( ! empty( $auth_multisite_settings['cas_num_servers'] ) && intval( $auth_multisite_settings['cas_num_servers'] ) > 1 ) {
291 foreach ( range( 2, min( intval( $auth_multisite_settings['cas_num_servers'] ), 10 ) ) as $cas_num_server ) {
292 $auth_settings[ 'cas_custom_label_' . $cas_num_server ] = $auth_multisite_settings[ 'cas_custom_label_' . $cas_num_server ] ?? 'CAS';
293 $auth_settings[ 'cas_host_' . $cas_num_server ] = $auth_multisite_settings[ 'cas_host_' . $cas_num_server ] ?? '';
294 $auth_settings[ 'cas_port_' . $cas_num_server ] = $auth_multisite_settings[ 'cas_port_' . $cas_num_server ] ?? '';
295 $auth_settings[ 'cas_path_' . $cas_num_server ] = $auth_multisite_settings[ 'cas_path_' . $cas_num_server ] ?? '';
296 $auth_settings[ 'cas_method_' . $cas_num_server ] = $auth_multisite_settings[ 'cas_method_' . $cas_num_server ] ?? Options\External\Cas::get_instance()->sanitize_cas_method();
297 $auth_settings[ 'cas_version_' . $cas_num_server ] = $auth_multisite_settings[ 'cas_version_' . $cas_num_server ] ?? Options\External\Cas::get_instance()->sanitize_cas_version();
298 $auth_settings[ 'cas_attr_email_' . $cas_num_server ] = $auth_multisite_settings[ 'cas_attr_email_' . $cas_num_server ] ?? '';
299 $auth_settings[ 'cas_attr_first_name_' . $cas_num_server ] = $auth_multisite_settings[ 'cas_attr_first_name_' . $cas_num_server ] ?? '';
300 $auth_settings[ 'cas_attr_last_name_' . $cas_num_server ] = $auth_multisite_settings[ 'cas_attr_last_name_' . $cas_num_server ] ?? '';
301 $auth_settings[ 'cas_attr_update_on_login_' . $cas_num_server ] = $auth_multisite_settings[ 'cas_attr_update_on_login_' . $cas_num_server ] ?? '';
302 $auth_settings[ 'cas_link_on_username_' . $cas_num_server ] = $auth_multisite_settings[ 'cas_link_on_username_' . $cas_num_server ] ?? '';
303 }
304 }
305
306 // Override external service (LDAP) and associated options.
307 $auth_settings['ldap'] = $auth_multisite_settings['ldap'];
308 $auth_settings['ldap_host'] = $auth_multisite_settings['ldap_host'];
309 $auth_settings['ldap_port'] = $auth_multisite_settings['ldap_port'];
310 $auth_settings['ldap_tls'] = $auth_multisite_settings['ldap_tls'];
311 $auth_settings['ldap_search_base'] = $auth_multisite_settings['ldap_search_base'];
312 $auth_settings['ldap_search_filter'] = $auth_multisite_settings['ldap_search_filter'];
313 $auth_settings['ldap_uid'] = $auth_multisite_settings['ldap_uid'];
314 $auth_settings['ldap_attr_email'] = $auth_multisite_settings['ldap_attr_email'];
315 $auth_settings['ldap_user'] = $auth_multisite_settings['ldap_user'];
316 $auth_settings['ldap_password'] = $auth_multisite_settings['ldap_password'];
317 $auth_settings['ldap_lostpassword_url'] = $auth_multisite_settings['ldap_lostpassword_url'];
318 $auth_settings['ldap_attr_first_name'] = $auth_multisite_settings['ldap_attr_first_name'];
319 $auth_settings['ldap_attr_last_name'] = $auth_multisite_settings['ldap_attr_last_name'];
320 $auth_settings['ldap_attr_update_on_login'] = $auth_multisite_settings['ldap_attr_update_on_login'];
321 $auth_settings['ldap_test_user'] = $auth_multisite_settings['ldap_test_user'] ?? '';
322
323 // Override access_who_can_login and access_who_can_view.
324 $auth_settings['access_who_can_login'] = $auth_multisite_settings['access_who_can_login'];
325 $auth_settings['access_who_can_view'] = $auth_multisite_settings['access_who_can_view'];
326
327 // Override access_default_role.
328 $auth_settings['access_default_role'] = $auth_multisite_settings['access_default_role'];
329
330 // Override lockouts.
331 $auth_settings['advanced_lockouts'] = $auth_multisite_settings['advanced_lockouts'];
332
333 // Override Hide WordPress login.
334 $auth_settings['advanced_hide_wp_login'] = $auth_multisite_settings['advanced_hide_wp_login'];
335
336 // Override Disable WordPress login.
337 $auth_settings['advanced_disable_wp_login'] = $auth_multisite_settings['advanced_disable_wp_login'];
338
339 // Override bypass users.
340 $auth_settings['advanced_disable_wp_login_bypass_usernames'] = $auth_multisite_settings['advanced_disable_wp_login_bypass_usernames'] ?? '';
341
342 // Override Users per page.
343 $auth_settings['advanced_users_per_page'] = $auth_multisite_settings['advanced_users_per_page'];
344
345 // Override Sort users by.
346 $auth_settings['advanced_users_sort_by'] = $auth_multisite_settings['advanced_users_sort_by'];
347
348 // Override Sort users order.
349 $auth_settings['advanced_users_sort_order'] = $auth_multisite_settings['advanced_users_sort_order'];
350
351 // Override Show usernames in approved users list.
352 $auth_settings['advanced_show_usernames'] = $auth_multisite_settings['advanced_show_usernames'] ?? '';
353
354 // Override Show Dashboard Widget.
355 $auth_settings['advanced_widget_enabled'] = $auth_multisite_settings['advanced_widget_enabled'];
356 }
357 }
358 return $auth_settings;
359 }
360
361
362 /**
363 * Set meaningful defaults for the plugin options.
364 *
365 * Note: This function is called on plugin activation.
366 *
367 * @param array $args {
368 * Optional.
369 *
370 * @type bool $set_multisite_options Whether to also set the default
371 * multisite options, if in multisite.
372 * Defaults to true.
373 * }
374 */
375 public function set_default_options( $args = array() ) {
376 global $wp_roles;
377
378 // Set default args.
379 $defaults = array(
380 'set_multisite_options' => true,
381 );
382 $args = wp_parse_args( $args, $defaults );
383
384 $auth_settings = get_option( 'auth_settings' );
385 if ( false === $auth_settings ) {
386 $auth_settings = array();
387 }
388
389 // Access Lists Defaults.
390 $auth_settings_access_users_pending = get_option( 'auth_settings_access_users_pending' );
391 if ( false === $auth_settings_access_users_pending ) {
392 $auth_settings_access_users_pending = array();
393 }
394 $auth_settings_access_users_approved = get_option( 'auth_settings_access_users_approved' );
395 if ( false === $auth_settings_access_users_approved ) {
396 $auth_settings_access_users_approved = array();
397 }
398 $auth_settings_access_users_blocked = get_option( 'auth_settings_access_users_blocked' );
399 if ( false === $auth_settings_access_users_blocked ) {
400 $auth_settings_access_users_blocked = array();
401 }
402
403 // Login Access Defaults.
404 if ( ! array_key_exists( 'access_who_can_login', $auth_settings ) ) {
405 $auth_settings['access_who_can_login'] = 'approved_users';
406 }
407 if ( ! array_key_exists( 'access_role_receive_pending_emails', $auth_settings ) ) {
408 $auth_settings['access_role_receive_pending_emails'] = '---';
409 }
410 if ( ! array_key_exists( 'access_users_receive_pending_emails', $auth_settings ) ) {
411 $auth_settings['access_users_receive_pending_emails'] = array();
412 }
413 if ( ! array_key_exists( 'access_pending_redirect_to_message', $auth_settings ) ) {
414 $auth_settings['access_pending_redirect_to_message'] = '<p>' . __( "You're not currently allowed to view this site. Your administrator has been notified, and once he/she has approved your request, you will be able to log in. If you need any other help, please contact your administrator.", 'authorizer' ) . '</p>';
415 }
416 if ( ! array_key_exists( 'access_blocked_redirect_to_message', $auth_settings ) ) {
417 $auth_settings['access_blocked_redirect_to_message'] = '<p>' . __( "You're not currently allowed to log into this site. If you think this is a mistake, please contact your administrator.", 'authorizer' ) . '</p>';
418 }
419 if ( ! array_key_exists( 'access_should_email_approved_users', $auth_settings ) ) {
420 $auth_settings['access_should_email_approved_users'] = '';
421 }
422 if ( ! array_key_exists( 'access_email_approved_users_subject', $auth_settings ) ) {
423 $auth_settings['access_email_approved_users_subject'] = sprintf(
424 /* TRANSLATORS: %s: Shortcode for name of site */
425 __( 'Welcome to %s!', 'authorizer' ),
426 '[site_name]'
427 );
428 }
429 if ( ! array_key_exists( 'access_email_approved_users_body', $auth_settings ) ) {
430 $auth_settings['access_email_approved_users_body'] = sprintf(
431 /* TRANSLATORS: 1: Shortcode for user email 2: Shortcode for site name 3: Shortcode for site URL */
432 __( "Hello %1\$s,\nWelcome to %2\$s! You now have access to all content on the site. Please visit us here:\n%3\$s\n", 'authorizer' ),
433 '[user_email]',
434 '[site_name]',
435 '[site_url]'
436 );
437 }
438
439 // Public Access to Private Page Defaults.
440 if ( ! array_key_exists( 'access_who_can_view', $auth_settings ) ) {
441 $auth_settings['access_who_can_view'] = 'everyone';
442 }
443 if ( ! array_key_exists( 'access_public_pages', $auth_settings ) ) {
444 $auth_settings['access_public_pages'] = array();
445 }
446 if ( ! array_key_exists( 'access_redirect', $auth_settings ) ) {
447 $auth_settings['access_redirect'] = 'login';
448 }
449 if ( ! array_key_exists( 'access_public_warning', $auth_settings ) ) {
450 $auth_settings['access_public_warning'] = 'no_warning';
451 }
452 if ( ! array_key_exists( 'access_redirect_to_message', $auth_settings ) ) {
453 $auth_settings['access_redirect_to_message'] = '<p>' . __( 'Notice: You are browsing this site anonymously, and only have access to a portion of its content.', 'authorizer' ) . '</p>';
454 }
455
456 // External Service Defaults.
457 if ( ! array_key_exists( 'access_default_role', $auth_settings ) ) {
458 // Set default role to 'subscriber', or 'student' if that role exists.
459 $auth_settings['access_default_role'] = 'subscriber';
460 if ( ! empty( $wp_roles ) ) {
461 $all_roles = $wp_roles->roles;
462 $editable_roles = apply_filters( 'editable_roles', $all_roles );
463 if ( is_array( $editable_roles ) && array_key_exists( 'student', $editable_roles ) ) {
464 $auth_settings['access_default_role'] = 'student';
465 }
466 }
467 }
468
469 if ( ! array_key_exists( 'oauth2', $auth_settings ) ) {
470 $auth_settings['oauth2'] = '';
471 }
472 if ( ! array_key_exists( 'oauth2_auto_login', $auth_settings ) ) {
473 $auth_settings['oauth2_auto_login'] = '';
474 }
475 if ( ! array_key_exists( 'oauth2_num_servers', $auth_settings ) ) {
476 $auth_settings['oauth2_num_servers'] = '1';
477 }
478 if ( ! array_key_exists( 'oauth2_provider', $auth_settings ) ) {
479 $auth_settings['oauth2_provider'] = '';
480 }
481 if ( ! array_key_exists( 'oauth2_custom_label', $auth_settings ) ) {
482 $auth_settings['oauth2_custom_label'] = 'OAuth2';
483 }
484 if ( ! array_key_exists( 'oauth2_clientid', $auth_settings ) ) {
485 $auth_settings['oauth2_clientid'] = '';
486 }
487 if ( ! array_key_exists( 'oauth2_clientsecret', $auth_settings ) ) {
488 $auth_settings['oauth2_clientsecret'] = '';
489 }
490 if ( ! array_key_exists( 'oauth2_hosteddomain', $auth_settings ) ) {
491 $auth_settings['oauth2_hosteddomain'] = '';
492 }
493 if ( ! array_key_exists( 'oauth2_tenant_id', $auth_settings ) ) {
494 $auth_settings['oauth2_tenant_id'] = 'common';
495 }
496 if ( ! array_key_exists( 'oauth2_url_authorize', $auth_settings ) ) {
497 $auth_settings['oauth2_url_authorize'] = '';
498 }
499 if ( ! array_key_exists( 'oauth2_url_token', $auth_settings ) ) {
500 $auth_settings['oauth2_url_token'] = '';
501 }
502 if ( ! array_key_exists( 'oauth2_url_resource', $auth_settings ) ) {
503 $auth_settings['oauth2_url_resource'] = '';
504 }
505 if ( ! array_key_exists( 'oauth2_attr_username', $auth_settings ) ) {
506 $auth_settings['oauth2_attr_username'] = '';
507 }
508 if ( ! array_key_exists( 'oauth2_attr_email', $auth_settings ) ) {
509 $auth_settings['oauth2_attr_email'] = '';
510 }
511 if ( ! array_key_exists( 'oauth2_attr_first_name', $auth_settings ) ) {
512 $auth_settings['oauth2_attr_first_name'] = '';
513 }
514 if ( ! array_key_exists( 'oauth2_attr_last_name', $auth_settings ) ) {
515 $auth_settings['oauth2_attr_last_name'] = '';
516 }
517 if ( ! array_key_exists( 'oauth2_attr_update_on_login', $auth_settings ) ) {
518 $auth_settings['oauth2_attr_update_on_login'] = '';
519 }
520 if ( ! array_key_exists( 'oauth2_require_verified_email', $auth_settings ) ) {
521 $auth_settings['oauth2_require_verified_email'] = '';
522 }
523 if ( ! array_key_exists( 'oauth2_link_on_username', $auth_settings ) ) {
524 $auth_settings['oauth2_link_on_username'] = '';
525 }
526 if ( intval( $auth_settings['oauth2_num_servers'] ) > 1 ) {
527 foreach ( range( 2, min( intval( $auth_settings['oauth2_num_servers'] ), 20 ) ) as $oauth2_num_server ) {
528 if ( ! array_key_exists( 'oauth2_provider_' . $oauth2_num_server, $auth_settings ) ) {
529 $auth_settings[ 'oauth2_provider_' . $oauth2_num_server ] = '';
530 }
531 if ( ! array_key_exists( 'oauth2_custom_label_' . $oauth2_num_server, $auth_settings ) ) {
532 $auth_settings[ 'oauth2_custom_label_' . $oauth2_num_server ] = 'OAuth2';
533 }
534 if ( ! array_key_exists( 'oauth2_clientid_' . $oauth2_num_server, $auth_settings ) ) {
535 $auth_settings[ 'oauth2_clientid_' . $oauth2_num_server ] = '';
536 }
537 if ( ! array_key_exists( 'oauth2_clientsecret_' . $oauth2_num_server, $auth_settings ) ) {
538 $auth_settings[ 'oauth2_clientsecret_' . $oauth2_num_server ] = '';
539 }
540 if ( ! array_key_exists( 'oauth2_hosteddomain_' . $oauth2_num_server, $auth_settings ) ) {
541 $auth_settings[ 'oauth2_hosteddomain_' . $oauth2_num_server ] = '';
542 }
543 if ( ! array_key_exists( 'oauth2_tenant_id_' . $oauth2_num_server, $auth_settings ) ) {
544 $auth_settings[ 'oauth2_tenant_id_' . $oauth2_num_server ] = 'common';
545 }
546 if ( ! array_key_exists( 'oauth2_url_authorize_' . $oauth2_num_server, $auth_settings ) ) {
547 $auth_settings[ 'oauth2_url_authorize_' . $oauth2_num_server ] = '';
548 }
549 if ( ! array_key_exists( 'oauth2_url_token_' . $oauth2_num_server, $auth_settings ) ) {
550 $auth_settings[ 'oauth2_url_token_' . $oauth2_num_server ] = '';
551 }
552 if ( ! array_key_exists( 'oauth2_url_resource_' . $oauth2_num_server, $auth_settings ) ) {
553 $auth_settings[ 'oauth2_url_resource_' . $oauth2_num_server ] = '';
554 }
555 if ( ! array_key_exists( 'oauth2_attr_username_' . $oauth2_num_server, $auth_settings ) ) {
556 $auth_settings[ 'oauth2_attr_username_' . $oauth2_num_server ] = '';
557 }
558 if ( ! array_key_exists( 'oauth2_attr_email_' . $oauth2_num_server, $auth_settings ) ) {
559 $auth_settings[ 'oauth2_attr_email_' . $oauth2_num_server ] = '';
560 }
561 if ( ! array_key_exists( 'oauth2_attr_first_name_' . $oauth2_num_server, $auth_settings ) ) {
562 $auth_settings[ 'oauth2_attr_first_name_' . $oauth2_num_server ] = '';
563 }
564 if ( ! array_key_exists( 'oauth2_attr_last_name_' . $oauth2_num_server, $auth_settings ) ) {
565 $auth_settings[ 'oauth2_attr_last_name_' . $oauth2_num_server ] = '';
566 }
567 if ( ! array_key_exists( 'oauth2_attr_update_on_login_' . $oauth2_num_server, $auth_settings ) ) {
568 $auth_settings[ 'oauth2_attr_update_on_login_' . $oauth2_num_server ] = '';
569 }
570 if ( ! array_key_exists( 'oauth2_require_verified_email_' . $oauth2_num_server, $auth_settings ) ) {
571 $auth_settings[ 'oauth2_require_verified_email_' . $oauth2_num_server ] = '';
572 }
573 if ( ! array_key_exists( 'oauth2_link_on_username_' . $oauth2_num_server, $auth_settings ) ) {
574 $auth_settings[ 'oauth2_link_on_username_' . $oauth2_num_server ] = '';
575 }
576 }
577 }
578
579 if ( ! array_key_exists( 'oidc', $auth_settings ) ) {
580 $auth_settings['oidc'] = '';
581 }
582 if ( ! array_key_exists( 'oidc_custom_label', $auth_settings ) ) {
583 $auth_settings['oidc_custom_label'] = 'OIDC';
584 }
585 if ( ! array_key_exists( 'oidc_issuer', $auth_settings ) ) {
586 $auth_settings['oidc_issuer'] = '';
587 }
588 if ( ! array_key_exists( 'oidc_client_id', $auth_settings ) ) {
589 $auth_settings['oidc_client_id'] = '';
590 }
591 if ( ! array_key_exists( 'oidc_client_secret', $auth_settings ) ) {
592 $auth_settings['oidc_client_secret'] = '';
593 }
594 if ( ! array_key_exists( 'oidc_scopes', $auth_settings ) ) {
595 $auth_settings['oidc_scopes'] = 'openid email profile';
596 }
597 if ( ! array_key_exists( 'oidc_prompt', $auth_settings ) ) {
598 $auth_settings['oidc_prompt'] = '';
599 }
600 if ( ! array_key_exists( 'oidc_login_hint', $auth_settings ) ) {
601 $auth_settings['oidc_login_hint'] = '';
602 }
603 if ( ! array_key_exists( 'oidc_max_age', $auth_settings ) ) {
604 $auth_settings['oidc_max_age'] = '';
605 }
606 if ( ! array_key_exists( 'oidc_attr_username', $auth_settings ) ) {
607 $auth_settings['oidc_attr_username'] = 'preferred_username';
608 }
609 if ( ! array_key_exists( 'oidc_attr_email', $auth_settings ) ) {
610 $auth_settings['oidc_attr_email'] = 'email';
611 }
612 if ( ! array_key_exists( 'oidc_attr_first_name', $auth_settings ) ) {
613 $auth_settings['oidc_attr_first_name'] = 'given_name';
614 }
615 if ( ! array_key_exists( 'oidc_attr_last_name', $auth_settings ) ) {
616 $auth_settings['oidc_attr_last_name'] = 'family_name';
617 }
618 if ( ! array_key_exists( 'oidc_attr_update_on_login', $auth_settings ) ) {
619 $auth_settings['oidc_attr_update_on_login'] = '';
620 }
621 if ( ! array_key_exists( 'oidc_force_auth_method', $auth_settings ) ) {
622 $auth_settings['oidc_force_auth_method'] = '';
623 }
624 if ( ! array_key_exists( 'oidc_require_verified_email', $auth_settings ) ) {
625 $auth_settings['oidc_require_verified_email'] = '';
626 }
627 if ( ! array_key_exists( 'oidc_link_on_username', $auth_settings ) ) {
628 $auth_settings['oidc_link_on_username'] = '';
629 }
630 if ( ! array_key_exists( 'oidc_hosteddomain', $auth_settings ) ) {
631 $auth_settings['oidc_hosteddomain'] = '';
632 }
633 if ( ! array_key_exists( 'oidc_num_servers', $auth_settings ) ) {
634 $auth_settings['oidc_num_servers'] = '1';
635 }
636 if ( ! array_key_exists( 'oidc_auto_login', $auth_settings ) ) {
637 $auth_settings['oidc_auto_login'] = '';
638 }
639
640 // Add any options for extra OIDC servers.
641 if ( intval( $auth_settings['oidc_num_servers'] ) > 1 ) {
642 foreach ( range( 2, min( intval( $auth_settings['oidc_num_servers'] ), 20 ) ) as $oidc_num_server ) {
643 if ( ! array_key_exists( 'oidc_custom_label_' . $oidc_num_server, $auth_settings ) ) {
644 $auth_settings[ 'oidc_custom_label_' . $oidc_num_server ] = 'OIDC';
645 }
646 if ( ! array_key_exists( 'oidc_issuer_' . $oidc_num_server, $auth_settings ) ) {
647 $auth_settings[ 'oidc_issuer_' . $oidc_num_server ] = '';
648 }
649 if ( ! array_key_exists( 'oidc_client_id_' . $oidc_num_server, $auth_settings ) ) {
650 $auth_settings[ 'oidc_client_id_' . $oidc_num_server ] = '';
651 }
652 if ( ! array_key_exists( 'oidc_client_secret_' . $oidc_num_server, $auth_settings ) ) {
653 $auth_settings[ 'oidc_client_secret_' . $oidc_num_server ] = '';
654 }
655 if ( ! array_key_exists( 'oidc_scopes_' . $oidc_num_server, $auth_settings ) ) {
656 $auth_settings[ 'oidc_scopes_' . $oidc_num_server ] = 'openid email profile';
657 }
658 if ( ! array_key_exists( 'oidc_prompt_' . $oidc_num_server, $auth_settings ) ) {
659 $auth_settings[ 'oidc_prompt_' . $oidc_num_server ] = '';
660 }
661 if ( ! array_key_exists( 'oidc_login_hint_' . $oidc_num_server, $auth_settings ) ) {
662 $auth_settings[ 'oidc_login_hint_' . $oidc_num_server ] = '';
663 }
664 if ( ! array_key_exists( 'oidc_max_age_' . $oidc_num_server, $auth_settings ) ) {
665 $auth_settings[ 'oidc_max_age_' . $oidc_num_server ] = '';
666 }
667 if ( ! array_key_exists( 'oidc_attr_username_' . $oidc_num_server, $auth_settings ) ) {
668 $auth_settings[ 'oidc_attr_username_' . $oidc_num_server ] = 'preferred_username';
669 }
670 if ( ! array_key_exists( 'oidc_attr_email_' . $oidc_num_server, $auth_settings ) ) {
671 $auth_settings[ 'oidc_attr_email_' . $oidc_num_server ] = 'email';
672 }
673 if ( ! array_key_exists( 'oidc_attr_first_name_' . $oidc_num_server, $auth_settings ) ) {
674 $auth_settings[ 'oidc_attr_first_name_' . $oidc_num_server ] = 'given_name';
675 }
676 if ( ! array_key_exists( 'oidc_attr_last_name_' . $oidc_num_server, $auth_settings ) ) {
677 $auth_settings[ 'oidc_attr_last_name_' . $oidc_num_server ] = 'family_name';
678 }
679 if ( ! array_key_exists( 'oidc_attr_update_on_login_' . $oidc_num_server, $auth_settings ) ) {
680 $auth_settings[ 'oidc_attr_update_on_login_' . $oidc_num_server ] = '';
681 }
682 if ( ! array_key_exists( 'oidc_force_auth_method_' . $oidc_num_server, $auth_settings ) ) {
683 $auth_settings[ 'oidc_force_auth_method_' . $oidc_num_server ] = '';
684 }
685 if ( ! array_key_exists( 'oidc_require_verified_email_' . $oidc_num_server, $auth_settings ) ) {
686 $auth_settings[ 'oidc_require_verified_email_' . $oidc_num_server ] = '';
687 }
688 if ( ! array_key_exists( 'oidc_link_on_username_' . $oidc_num_server, $auth_settings ) ) {
689 $auth_settings[ 'oidc_link_on_username_' . $oidc_num_server ] = '';
690 }
691 if ( ! array_key_exists( 'oidc_hosteddomain_' . $oidc_num_server, $auth_settings ) ) {
692 $auth_settings[ 'oidc_hosteddomain_' . $oidc_num_server ] = '';
693 }
694 }
695 }
696
697 if ( ! array_key_exists( 'google', $auth_settings ) ) {
698 $auth_settings['google'] = '';
699 }
700 if ( ! array_key_exists( 'google_clientid', $auth_settings ) ) {
701 $auth_settings['google_clientid'] = '';
702 }
703 if ( ! array_key_exists( 'google_clientsecret', $auth_settings ) ) {
704 $auth_settings['google_clientsecret'] = '';
705 }
706 if ( ! array_key_exists( 'google_hosteddomain', $auth_settings ) ) {
707 $auth_settings['google_hosteddomain'] = '';
708 }
709
710 if ( ! array_key_exists( 'cas', $auth_settings ) ) {
711 $auth_settings['cas'] = '';
712 }
713 if ( ! array_key_exists( 'cas_auto_login', $auth_settings ) ) {
714 $auth_settings['cas_auto_login'] = '';
715 }
716 if ( ! array_key_exists( 'cas_num_servers', $auth_settings ) ) {
717 $auth_settings['cas_num_servers'] = '1';
718 }
719 if ( ! array_key_exists( 'cas_custom_label', $auth_settings ) ) {
720 $auth_settings['cas_custom_label'] = 'CAS';
721 }
722 if ( ! array_key_exists( 'cas_host', $auth_settings ) ) {
723 $auth_settings['cas_host'] = '';
724 }
725 if ( ! array_key_exists( 'cas_port', $auth_settings ) ) {
726 $auth_settings['cas_port'] = '';
727 }
728 if ( ! array_key_exists( 'cas_path', $auth_settings ) ) {
729 $auth_settings['cas_path'] = '';
730 }
731 if ( ! array_key_exists( 'cas_method', $auth_settings ) ) {
732 $auth_settings['cas_method'] = Options\External\Cas::get_instance()->sanitize_cas_method();
733 }
734 if ( ! array_key_exists( 'cas_version', $auth_settings ) ) {
735 $auth_settings['cas_version'] = Options\External\Cas::get_instance()->sanitize_cas_version();
736 }
737 if ( ! array_key_exists( 'cas_attr_email', $auth_settings ) ) {
738 $auth_settings['cas_attr_email'] = '';
739 }
740 if ( ! array_key_exists( 'cas_attr_first_name', $auth_settings ) ) {
741 $auth_settings['cas_attr_first_name'] = '';
742 }
743 if ( ! array_key_exists( 'cas_attr_last_name', $auth_settings ) ) {
744 $auth_settings['cas_attr_last_name'] = '';
745 }
746 if ( ! array_key_exists( 'cas_attr_update_on_login', $auth_settings ) ) {
747 $auth_settings['cas_attr_update_on_login'] = '';
748 }
749 if ( ! array_key_exists( 'cas_link_on_username', $auth_settings ) ) {
750 $auth_settings['cas_link_on_username'] = '';
751 }
752 if ( intval( $auth_settings['cas_num_servers'] ) > 1 ) {
753 foreach ( range( 2, min( intval( $auth_settings['cas_num_servers'] ), 10 ) ) as $cas_num_server ) {
754 if ( ! array_key_exists( 'cas_custom_label_' . $cas_num_server, $auth_settings ) ) {
755 $auth_settings[ 'cas_custom_label_' . $cas_num_server ] = 'CAS';
756 }
757 if ( ! array_key_exists( 'cas_host_' . $cas_num_server, $auth_settings ) ) {
758 $auth_settings[ 'cas_host_' . $cas_num_server ] = '';
759 }
760 if ( ! array_key_exists( 'cas_port_' . $cas_num_server, $auth_settings ) ) {
761 $auth_settings[ 'cas_port_' . $cas_num_server ] = '';
762 }
763 if ( ! array_key_exists( 'cas_path_' . $cas_num_server, $auth_settings ) ) {
764 $auth_settings[ 'cas_path_' . $cas_num_server ] = '';
765 }
766 if ( ! array_key_exists( 'cas_method_' . $cas_num_server, $auth_settings ) ) {
767 $auth_settings[ 'cas_method_' . $cas_num_server ] = Options\External\Cas::get_instance()->sanitize_cas_method();
768 }
769 if ( ! array_key_exists( 'cas_version_' . $cas_num_server, $auth_settings ) ) {
770 $auth_settings[ 'cas_version_' . $cas_num_server ] = Options\External\Cas::get_instance()->sanitize_cas_version();
771 }
772 if ( ! array_key_exists( 'cas_attr_email_' . $cas_num_server, $auth_settings ) ) {
773 $auth_settings[ 'cas_attr_email_' . $cas_num_server ] = '';
774 }
775 if ( ! array_key_exists( 'cas_attr_first_name_' . $cas_num_server, $auth_settings ) ) {
776 $auth_settings[ 'cas_attr_first_name_' . $cas_num_server ] = '';
777 }
778 if ( ! array_key_exists( 'cas_attr_last_name_' . $cas_num_server, $auth_settings ) ) {
779 $auth_settings[ 'cas_attr_last_name_' . $cas_num_server ] = '';
780 }
781 if ( ! array_key_exists( 'cas_attr_update_on_login_' . $cas_num_server, $auth_settings ) ) {
782 $auth_settings[ 'cas_attr_update_on_login_' . $cas_num_server ] = '';
783 }
784 if ( ! array_key_exists( 'cas_link_on_username_' . $cas_num_server, $auth_settings ) ) {
785 $auth_settings[ 'cas_link_on_username_' . $cas_num_server ] = '';
786 }
787 }
788 }
789
790 if ( ! array_key_exists( 'ldap', $auth_settings ) ) {
791 $auth_settings['ldap'] = '';
792 }
793 if ( ! array_key_exists( 'ldap_host', $auth_settings ) ) {
794 $auth_settings['ldap_host'] = '';
795 }
796 if ( ! array_key_exists( 'ldap_port', $auth_settings ) ) {
797 $auth_settings['ldap_port'] = '389';
798 }
799 if ( ! array_key_exists( 'ldap_tls', $auth_settings ) ) {
800 $auth_settings['ldap_tls'] = '1';
801 }
802 if ( ! array_key_exists( 'ldap_search_base', $auth_settings ) ) {
803 $auth_settings['ldap_search_base'] = '';
804 }
805 if ( ! array_key_exists( 'ldap_search_filter', $auth_settings ) ) {
806 $auth_settings['ldap_search_filter'] = '';
807 }
808 if ( ! array_key_exists( 'ldap_uid', $auth_settings ) ) {
809 $auth_settings['ldap_uid'] = 'uid';
810 }
811 if ( ! array_key_exists( 'ldap_attr_email', $auth_settings ) ) {
812 $auth_settings['ldap_attr_email'] = '';
813 }
814 if ( ! array_key_exists( 'ldap_user', $auth_settings ) ) {
815 $auth_settings['ldap_user'] = '';
816 }
817 if ( ! array_key_exists( 'ldap_password', $auth_settings ) ) {
818 $auth_settings['ldap_password'] = '';
819 }
820 if ( ! array_key_exists( 'ldap_lostpassword_url', $auth_settings ) ) {
821 $auth_settings['ldap_lostpassword_url'] = '';
822 }
823 if ( ! array_key_exists( 'ldap_attr_first_name', $auth_settings ) ) {
824 $auth_settings['ldap_attr_first_name'] = '';
825 }
826 if ( ! array_key_exists( 'ldap_attr_last_name', $auth_settings ) ) {
827 $auth_settings['ldap_attr_last_name'] = '';
828 }
829 if ( ! array_key_exists( 'ldap_attr_update_on_login', $auth_settings ) ) {
830 $auth_settings['ldap_attr_update_on_login'] = '';
831 }
832 if ( ! array_key_exists( 'ldap_test_user', $auth_settings ) ) {
833 $auth_settings['ldap_test_user'] = '';
834 }
835
836 // Advanced defaults.
837 if ( ! array_key_exists( 'advanced_lockouts', $auth_settings ) ) {
838 $auth_settings['advanced_lockouts'] = array(
839 'attempts_1' => 10,
840 'duration_1' => 1,
841 'attempts_2' => 10,
842 'duration_2' => 10,
843 'reset_duration' => 120,
844 );
845 }
846 if ( ! array_key_exists( 'advanced_hide_wp_login', $auth_settings ) ) {
847 $auth_settings['advanced_hide_wp_login'] = '';
848 }
849 if ( ! array_key_exists( 'advanced_disable_wp_login', $auth_settings ) ) {
850 $auth_settings['advanced_disable_wp_login'] = '';
851 }
852 if ( ! array_key_exists( 'advanced_disable_wp_login_bypass_usernames', $auth_settings ) ) {
853 $auth_settings['advanced_disable_wp_login_bypass_usernames'] = '';
854 }
855 if ( ! array_key_exists( 'advanced_branding', $auth_settings ) ) {
856 $auth_settings['advanced_branding'] = 'default';
857 }
858 if ( ! array_key_exists( 'advanced_admin_menu', $auth_settings ) ) {
859 $auth_settings['advanced_admin_menu'] = 'top';
860 }
861 if ( ! array_key_exists( 'advanced_usermeta', $auth_settings ) ) {
862 $auth_settings['advanced_usermeta'] = '';
863 }
864 if ( ! array_key_exists( 'advanced_users_per_page', $auth_settings ) ) {
865 $auth_settings['advanced_users_per_page'] = 20;
866 }
867 if ( ! array_key_exists( 'advanced_users_sort_by', $auth_settings ) ) {
868 $auth_settings['advanced_users_sort_by'] = 'created';
869 }
870 if ( ! array_key_exists( 'advanced_users_sort_order', $auth_settings ) ) {
871 $auth_settings['advanced_users_sort_order'] = 'asc';
872 }
873 if ( ! array_key_exists( 'advanced_show_usernames', $auth_settings ) ) {
874 $auth_settings['advanced_show_usernames'] = '';
875 }
876 if ( ! array_key_exists( 'advanced_widget_enabled', $auth_settings ) ) {
877 $auth_settings['advanced_widget_enabled'] = '1';
878 }
879 if ( ! array_key_exists( 'advanced_override_multisite', $auth_settings ) ) {
880 $auth_settings['advanced_override_multisite'] = '';
881 }
882
883 // Save default options to database.
884 update_option( 'auth_settings', $auth_settings, true );
885 update_option( 'auth_settings_access_users_pending', $auth_settings_access_users_pending, false );
886 update_option( 'auth_settings_access_users_approved', $auth_settings_access_users_approved, false );
887 update_option( 'auth_settings_access_users_blocked', $auth_settings_access_users_blocked, false );
888
889 // Multisite defaults.
890 if ( is_multisite() && $args['set_multisite_options'] ) {
891 $auth_multisite_settings = get_blog_option( get_main_site_id( get_main_network_id() ), 'auth_multisite_settings', array() );
892
893 if ( false === $auth_multisite_settings ) {
894 $auth_multisite_settings = array();
895 }
896 // Global switch for enabling multisite options.
897 if ( ! array_key_exists( 'multisite_override', $auth_multisite_settings ) ) {
898 $auth_multisite_settings['multisite_override'] = '';
899 }
900 // Global switch for preventing sites from overriding multisite options.
901 if ( ! array_key_exists( 'prevent_override_multisite', $auth_multisite_settings ) ) {
902 $auth_multisite_settings['prevent_override_multisite'] = '';
903 }
904 // Access Lists Defaults.
905 $auth_multisite_settings_access_users_approved = get_blog_option( get_main_site_id( get_main_network_id() ), 'auth_multisite_settings_access_users_approved' );
906 if ( false === $auth_multisite_settings_access_users_approved ) {
907 $auth_multisite_settings_access_users_approved = array();
908 }
909 // Login Access Defaults.
910 if ( ! array_key_exists( 'access_who_can_login', $auth_multisite_settings ) ) {
911 $auth_multisite_settings['access_who_can_login'] = 'approved_users';
912 }
913 // View Access Defaults.
914 if ( ! array_key_exists( 'access_who_can_view', $auth_multisite_settings ) ) {
915 $auth_multisite_settings['access_who_can_view'] = 'everyone';
916 }
917 // External Service Defaults.
918 if ( ! array_key_exists( 'access_default_role', $auth_multisite_settings ) ) {
919 // Set default role to 'subscriber', or 'student' if that role exists.
920 $auth_multisite_settings['access_default_role'] = 'subscriber';
921 if ( ! empty( $wp_roles ) ) {
922 $all_roles = $wp_roles->roles;
923 $editable_roles = apply_filters( 'editable_roles', $all_roles );
924 if ( is_array( $editable_roles ) && array_key_exists( 'student', $editable_roles ) ) {
925 $auth_multisite_settings['access_default_role'] = 'student';
926 }
927 }
928 }
929 if ( ! array_key_exists( 'oauth2', $auth_multisite_settings ) ) {
930 $auth_multisite_settings['oauth2'] = '';
931 }
932 if ( ! array_key_exists( 'oauth2_auto_login', $auth_multisite_settings ) ) {
933 $auth_multisite_settings['oauth2_auto_login'] = '';
934 }
935 if ( ! array_key_exists( 'oauth2_num_servers', $auth_multisite_settings ) ) {
936 $auth_multisite_settings['oauth2_num_servers'] = '1';
937 }
938 if ( ! array_key_exists( 'oauth2_provider', $auth_multisite_settings ) ) {
939 $auth_multisite_settings['oauth2_provider'] = '';
940 }
941 if ( ! array_key_exists( 'oauth2_custom_label', $auth_multisite_settings ) ) {
942 $auth_multisite_settings['oauth2_custom_label'] = 'OAuth2';
943 }
944 if ( ! array_key_exists( 'oauth2_clientid', $auth_multisite_settings ) ) {
945 $auth_multisite_settings['oauth2_clientid'] = '';
946 }
947 if ( ! array_key_exists( 'oauth2_clientsecret', $auth_multisite_settings ) ) {
948 $auth_multisite_settings['oauth2_clientsecret'] = '';
949 }
950 if ( ! array_key_exists( 'oauth2_hosteddomain', $auth_multisite_settings ) ) {
951 $auth_multisite_settings['oauth2_hosteddomain'] = '';
952 }
953 if ( ! array_key_exists( 'oauth2_tenant_id', $auth_multisite_settings ) ) {
954 $auth_multisite_settings['oauth2_tenant_id'] = 'common';
955 }
956 if ( ! array_key_exists( 'oauth2_url_authorize', $auth_multisite_settings ) ) {
957 $auth_multisite_settings['oauth2_url_authorize'] = '';
958 }
959 if ( ! array_key_exists( 'oauth2_url_token', $auth_multisite_settings ) ) {
960 $auth_multisite_settings['oauth2_url_token'] = '';
961 }
962 if ( ! array_key_exists( 'oauth2_url_resource', $auth_multisite_settings ) ) {
963 $auth_multisite_settings['oauth2_url_resource'] = '';
964 }
965 if ( ! array_key_exists( 'oauth2_attr_username', $auth_multisite_settings ) ) {
966 $auth_multisite_settings['oauth2_attr_username'] = '';
967 }
968 if ( ! array_key_exists( 'oauth2_attr_email', $auth_multisite_settings ) ) {
969 $auth_multisite_settings['oauth2_attr_email'] = '';
970 }
971 if ( ! array_key_exists( 'oauth2_attr_first_name', $auth_multisite_settings ) ) {
972 $auth_multisite_settings['oauth2_attr_first_name'] = '';
973 }
974 if ( ! array_key_exists( 'oauth2_attr_last_name', $auth_multisite_settings ) ) {
975 $auth_multisite_settings['oauth2_attr_last_name'] = '';
976 }
977 if ( ! array_key_exists( 'oauth2_attr_update_on_login', $auth_multisite_settings ) ) {
978 $auth_multisite_settings['oauth2_attr_update_on_login'] = '';
979 }
980 if ( ! array_key_exists( 'oauth2_require_verified_email', $auth_multisite_settings ) ) {
981 $auth_multisite_settings['oauth2_require_verified_email'] = '';
982 }
983 if ( ! array_key_exists( 'oauth2_link_on_username', $auth_multisite_settings ) ) {
984 $auth_multisite_settings['oauth2_link_on_username'] = '';
985 }
986 if ( intval( $auth_multisite_settings['oauth2_num_servers'] ) > 1 ) {
987 foreach ( range( 2, min( intval( $auth_multisite_settings['oauth2_num_servers'] ), 20 ) ) as $oauth2_num_server ) {
988 if ( ! array_key_exists( 'oauth2_provider_' . $oauth2_num_server, $auth_multisite_settings ) ) {
989 $auth_multisite_settings[ 'oauth2_provider_' . $oauth2_num_server ] = '';
990 }
991 if ( ! array_key_exists( 'oauth2_custom_label_' . $oauth2_num_server, $auth_multisite_settings ) ) {
992 $auth_multisite_settings[ 'oauth2_custom_label_' . $oauth2_num_server ] = 'OAuth2';
993 }
994 if ( ! array_key_exists( 'oauth2_clientid_' . $oauth2_num_server, $auth_multisite_settings ) ) {
995 $auth_multisite_settings[ 'oauth2_clientid_' . $oauth2_num_server ] = '';
996 }
997 if ( ! array_key_exists( 'oauth2_clientsecret_' . $oauth2_num_server, $auth_multisite_settings ) ) {
998 $auth_multisite_settings[ 'oauth2_clientsecret_' . $oauth2_num_server ] = '';
999 }
1000 if ( ! array_key_exists( 'oauth2_hosteddomain_' . $oauth2_num_server, $auth_multisite_settings ) ) {
1001 $auth_multisite_settings[ 'oauth2_hosteddomain_' . $oauth2_num_server ] = '';
1002 }
1003 if ( ! array_key_exists( 'oauth2_tenant_id_' . $oauth2_num_server, $auth_multisite_settings ) ) {
1004 $auth_multisite_settings[ 'oauth2_tenant_id_' . $oauth2_num_server ] = 'common';
1005 }
1006 if ( ! array_key_exists( 'oauth2_url_authorize_' . $oauth2_num_server, $auth_multisite_settings ) ) {
1007 $auth_multisite_settings[ 'oauth2_url_authorize_' . $oauth2_num_server ] = '';
1008 }
1009 if ( ! array_key_exists( 'oauth2_url_token_' . $oauth2_num_server, $auth_multisite_settings ) ) {
1010 $auth_multisite_settings[ 'oauth2_url_token_' . $oauth2_num_server ] = '';
1011 }
1012 if ( ! array_key_exists( 'oauth2_url_resource_' . $oauth2_num_server, $auth_multisite_settings ) ) {
1013 $auth_multisite_settings[ 'oauth2_url_resource_' . $oauth2_num_server ] = '';
1014 }
1015 if ( ! array_key_exists( 'oauth2_attr_username_' . $oauth2_num_server, $auth_multisite_settings ) ) {
1016 $auth_multisite_settings[ 'oauth2_attr_username_' . $oauth2_num_server ] = '';
1017 }
1018 if ( ! array_key_exists( 'oauth2_attr_email_' . $oauth2_num_server, $auth_multisite_settings ) ) {
1019 $auth_multisite_settings[ 'oauth2_attr_email_' . $oauth2_num_server ] = '';
1020 }
1021 if ( ! array_key_exists( 'oauth2_attr_first_name_' . $oauth2_num_server, $auth_multisite_settings ) ) {
1022 $auth_multisite_settings[ 'oauth2_attr_first_name_' . $oauth2_num_server ] = '';
1023 }
1024 if ( ! array_key_exists( 'oauth2_attr_last_name_' . $oauth2_num_server, $auth_multisite_settings ) ) {
1025 $auth_multisite_settings[ 'oauth2_attr_last_name_' . $oauth2_num_server ] = '';
1026 }
1027 if ( ! array_key_exists( 'oauth2_attr_update_on_login_' . $oauth2_num_server, $auth_multisite_settings ) ) {
1028 $auth_multisite_settings[ 'oauth2_attr_update_on_login_' . $oauth2_num_server ] = '';
1029 }
1030 if ( ! array_key_exists( 'oauth2_require_verified_email_' . $oauth2_num_server, $auth_multisite_settings ) ) {
1031 $auth_multisite_settings[ 'oauth2_require_verified_email_' . $oauth2_num_server ] = '';
1032 }
1033 if ( ! array_key_exists( 'oauth2_link_on_username_' . $oauth2_num_server, $auth_multisite_settings ) ) {
1034 $auth_multisite_settings[ 'oauth2_link_on_username_' . $oauth2_num_server ] = '';
1035 }
1036 }
1037 }
1038 if ( ! array_key_exists( 'oidc', $auth_multisite_settings ) ) {
1039 $auth_multisite_settings['oidc'] = '';
1040 }
1041 if ( ! array_key_exists( 'oidc_num_servers', $auth_multisite_settings ) ) {
1042 $auth_multisite_settings['oidc_num_servers'] = '1';
1043 }
1044 if ( ! array_key_exists( 'oidc_auto_login', $auth_multisite_settings ) ) {
1045 $auth_multisite_settings['oidc_auto_login'] = '';
1046 }
1047 if ( ! array_key_exists( 'oidc_custom_label', $auth_multisite_settings ) ) {
1048 $auth_multisite_settings['oidc_custom_label'] = 'OIDC';
1049 }
1050 if ( ! array_key_exists( 'oidc_issuer', $auth_multisite_settings ) ) {
1051 $auth_multisite_settings['oidc_issuer'] = '';
1052 }
1053 if ( ! array_key_exists( 'oidc_client_id', $auth_multisite_settings ) ) {
1054 $auth_multisite_settings['oidc_client_id'] = '';
1055 }
1056 if ( ! array_key_exists( 'oidc_client_secret', $auth_multisite_settings ) ) {
1057 $auth_multisite_settings['oidc_client_secret'] = '';
1058 }
1059 if ( ! array_key_exists( 'oidc_scopes', $auth_multisite_settings ) ) {
1060 $auth_multisite_settings['oidc_scopes'] = 'openid email profile';
1061 }
1062 if ( ! array_key_exists( 'oidc_prompt', $auth_multisite_settings ) ) {
1063 $auth_multisite_settings['oidc_prompt'] = '';
1064 }
1065 if ( ! array_key_exists( 'oidc_login_hint', $auth_multisite_settings ) ) {
1066 $auth_multisite_settings['oidc_login_hint'] = '';
1067 }
1068 if ( ! array_key_exists( 'oidc_max_age', $auth_multisite_settings ) ) {
1069 $auth_multisite_settings['oidc_max_age'] = '';
1070 }
1071 if ( ! array_key_exists( 'oidc_attr_username', $auth_multisite_settings ) ) {
1072 $auth_multisite_settings['oidc_attr_username'] = 'preferred_username';
1073 }
1074 if ( ! array_key_exists( 'oidc_attr_email', $auth_multisite_settings ) ) {
1075 $auth_multisite_settings['oidc_attr_email'] = 'email';
1076 }
1077 if ( ! array_key_exists( 'oidc_attr_first_name', $auth_multisite_settings ) ) {
1078 $auth_multisite_settings['oidc_attr_first_name'] = 'given_name';
1079 }
1080 if ( ! array_key_exists( 'oidc_attr_last_name', $auth_multisite_settings ) ) {
1081 $auth_multisite_settings['oidc_attr_last_name'] = 'family_name';
1082 }
1083 if ( ! array_key_exists( 'oidc_attr_update_on_login', $auth_multisite_settings ) ) {
1084 $auth_multisite_settings['oidc_attr_update_on_login'] = '';
1085 }
1086 if ( ! array_key_exists( 'oidc_force_auth_method', $auth_multisite_settings ) ) {
1087 $auth_multisite_settings['oidc_force_auth_method'] = '';
1088 }
1089 if ( ! array_key_exists( 'oidc_require_verified_email', $auth_multisite_settings ) ) {
1090 $auth_multisite_settings['oidc_require_verified_email'] = '';
1091 }
1092 if ( ! array_key_exists( 'oidc_link_on_username', $auth_multisite_settings ) ) {
1093 $auth_multisite_settings['oidc_link_on_username'] = '';
1094 }
1095 if ( ! array_key_exists( 'oidc_hosteddomain', $auth_multisite_settings ) ) {
1096 $auth_multisite_settings['oidc_hosteddomain'] = '';
1097 }
1098 if ( intval( $auth_multisite_settings['oidc_num_servers'] ) > 1 ) {
1099 foreach ( range( 2, min( intval( $auth_multisite_settings['oidc_num_servers'] ), 20 ) ) as $oidc_num_server ) {
1100 if ( ! array_key_exists( 'oidc_custom_label_' . $oidc_num_server, $auth_multisite_settings ) ) {
1101 $auth_multisite_settings[ 'oidc_custom_label_' . $oidc_num_server ] = 'OIDC';
1102 }
1103 if ( ! array_key_exists( 'oidc_issuer_' . $oidc_num_server, $auth_multisite_settings ) ) {
1104 $auth_multisite_settings[ 'oidc_issuer_' . $oidc_num_server ] = '';
1105 }
1106 if ( ! array_key_exists( 'oidc_client_id_' . $oidc_num_server, $auth_multisite_settings ) ) {
1107 $auth_multisite_settings[ 'oidc_client_id_' . $oidc_num_server ] = '';
1108 }
1109 if ( ! array_key_exists( 'oidc_client_secret_' . $oidc_num_server, $auth_multisite_settings ) ) {
1110 $auth_multisite_settings[ 'oidc_client_secret_' . $oidc_num_server ] = '';
1111 }
1112 if ( ! array_key_exists( 'oidc_scopes_' . $oidc_num_server, $auth_multisite_settings ) ) {
1113 $auth_multisite_settings[ 'oidc_scopes_' . $oidc_num_server ] = 'openid email profile';
1114 }
1115 if ( ! array_key_exists( 'oidc_prompt_' . $oidc_num_server, $auth_multisite_settings ) ) {
1116 $auth_multisite_settings[ 'oidc_prompt_' . $oidc_num_server ] = '';
1117 }
1118 if ( ! array_key_exists( 'oidc_login_hint_' . $oidc_num_server, $auth_multisite_settings ) ) {
1119 $auth_multisite_settings[ 'oidc_login_hint_' . $oidc_num_server ] = '';
1120 }
1121 if ( ! array_key_exists( 'oidc_max_age_' . $oidc_num_server, $auth_multisite_settings ) ) {
1122 $auth_multisite_settings[ 'oidc_max_age_' . $oidc_num_server ] = '';
1123 }
1124 if ( ! array_key_exists( 'oidc_attr_username_' . $oidc_num_server, $auth_multisite_settings ) ) {
1125 $auth_multisite_settings[ 'oidc_attr_username_' . $oidc_num_server ] = 'preferred_username';
1126 }
1127 if ( ! array_key_exists( 'oidc_attr_email_' . $oidc_num_server, $auth_multisite_settings ) ) {
1128 $auth_multisite_settings[ 'oidc_attr_email_' . $oidc_num_server ] = 'email';
1129 }
1130 if ( ! array_key_exists( 'oidc_attr_first_name_' . $oidc_num_server, $auth_multisite_settings ) ) {
1131 $auth_multisite_settings[ 'oidc_attr_first_name_' . $oidc_num_server ] = 'given_name';
1132 }
1133 if ( ! array_key_exists( 'oidc_attr_last_name_' . $oidc_num_server, $auth_multisite_settings ) ) {
1134 $auth_multisite_settings[ 'oidc_attr_last_name_' . $oidc_num_server ] = 'family_name';
1135 }
1136 if ( ! array_key_exists( 'oidc_attr_update_on_login_' . $oidc_num_server, $auth_multisite_settings ) ) {
1137 $auth_multisite_settings[ 'oidc_attr_update_on_login_' . $oidc_num_server ] = '';
1138 }
1139 if ( ! array_key_exists( 'oidc_force_auth_method_' . $oidc_num_server, $auth_multisite_settings ) ) {
1140 $auth_multisite_settings[ 'oidc_force_auth_method_' . $oidc_num_server ] = '';
1141 }
1142 if ( ! array_key_exists( 'oidc_require_verified_email_' . $oidc_num_server, $auth_multisite_settings ) ) {
1143 $auth_multisite_settings[ 'oidc_require_verified_email_' . $oidc_num_server ] = '';
1144 }
1145 if ( ! array_key_exists( 'oidc_link_on_username_' . $oidc_num_server, $auth_multisite_settings ) ) {
1146 $auth_multisite_settings[ 'oidc_link_on_username_' . $oidc_num_server ] = '';
1147 }
1148 if ( ! array_key_exists( 'oidc_hosteddomain_' . $oidc_num_server, $auth_multisite_settings ) ) {
1149 $auth_multisite_settings[ 'oidc_hosteddomain_' . $oidc_num_server ] = '';
1150 }
1151 }
1152 }
1153 if ( ! array_key_exists( 'google', $auth_multisite_settings ) ) {
1154 $auth_multisite_settings['google'] = '';
1155 }
1156 if ( ! array_key_exists( 'google_clientid', $auth_multisite_settings ) ) {
1157 $auth_multisite_settings['google_clientid'] = '';
1158 }
1159 if ( ! array_key_exists( 'google_clientsecret', $auth_multisite_settings ) ) {
1160 $auth_multisite_settings['google_clientsecret'] = '';
1161 }
1162 if ( ! array_key_exists( 'google_hosteddomain', $auth_multisite_settings ) ) {
1163 $auth_multisite_settings['google_hosteddomain'] = '';
1164 }
1165 if ( ! array_key_exists( 'cas', $auth_multisite_settings ) ) {
1166 $auth_multisite_settings['cas'] = '';
1167 }
1168 if ( ! array_key_exists( 'cas_auto_login', $auth_multisite_settings ) ) {
1169 $auth_multisite_settings['cas_auto_login'] = '';
1170 }
1171 if ( ! array_key_exists( 'cas_num_servers', $auth_multisite_settings ) ) {
1172 $auth_multisite_settings['cas_num_servers'] = '1';
1173 }
1174 if ( ! array_key_exists( 'cas_custom_label', $auth_multisite_settings ) ) {
1175 $auth_multisite_settings['cas_custom_label'] = 'CAS';
1176 }
1177 if ( ! array_key_exists( 'cas_host', $auth_multisite_settings ) ) {
1178 $auth_multisite_settings['cas_host'] = '';
1179 }
1180 if ( ! array_key_exists( 'cas_port', $auth_multisite_settings ) ) {
1181 $auth_multisite_settings['cas_port'] = '';
1182 }
1183 if ( ! array_key_exists( 'cas_path', $auth_multisite_settings ) ) {
1184 $auth_multisite_settings['cas_path'] = '';
1185 }
1186 if ( ! array_key_exists( 'cas_method', $auth_multisite_settings ) ) {
1187 $auth_multisite_settings['cas_method'] = Options\External\Cas::get_instance()->sanitize_cas_method();
1188 }
1189 if ( ! array_key_exists( 'cas_version', $auth_multisite_settings ) ) {
1190 $auth_multisite_settings['cas_version'] = Options\External\Cas::get_instance()->sanitize_cas_version();
1191 }
1192 if ( ! array_key_exists( 'cas_attr_email', $auth_multisite_settings ) ) {
1193 $auth_multisite_settings['cas_attr_email'] = '';
1194 }
1195 if ( ! array_key_exists( 'cas_attr_first_name', $auth_multisite_settings ) ) {
1196 $auth_multisite_settings['cas_attr_first_name'] = '';
1197 }
1198 if ( ! array_key_exists( 'cas_attr_last_name', $auth_multisite_settings ) ) {
1199 $auth_multisite_settings['cas_attr_last_name'] = '';
1200 }
1201 if ( ! array_key_exists( 'cas_attr_update_on_login', $auth_multisite_settings ) ) {
1202 $auth_multisite_settings['cas_attr_update_on_login'] = '';
1203 }
1204 if ( ! array_key_exists( 'cas_link_on_username', $auth_multisite_settings ) ) {
1205 $auth_multisite_settings['cas_link_on_username'] = '';
1206 }
1207 if ( intval( $auth_multisite_settings['cas_num_servers'] ) > 1 ) {
1208 foreach ( range( 2, min( intval( $auth_multisite_settings['cas_num_servers'] ), 10 ) ) as $cas_num_server ) {
1209 if ( ! array_key_exists( 'cas_custom_label_' . $cas_num_server, $auth_multisite_settings ) ) {
1210 $auth_multisite_settings[ 'cas_custom_label_' . $cas_num_server ] = 'CAS';
1211 }
1212 if ( ! array_key_exists( 'cas_host_' . $cas_num_server, $auth_multisite_settings ) ) {
1213 $auth_multisite_settings[ 'cas_host_' . $cas_num_server ] = '';
1214 }
1215 if ( ! array_key_exists( 'cas_port_' . $cas_num_server, $auth_multisite_settings ) ) {
1216 $auth_multisite_settings[ 'cas_port_' . $cas_num_server ] = '';
1217 }
1218 if ( ! array_key_exists( 'cas_path_' . $cas_num_server, $auth_multisite_settings ) ) {
1219 $auth_multisite_settings[ 'cas_path_' . $cas_num_server ] = '';
1220 }
1221 if ( ! array_key_exists( 'cas_method_' . $cas_num_server, $auth_multisite_settings ) ) {
1222 $auth_multisite_settings[ 'cas_method_' . $cas_num_server ] = Options\External\Cas::get_instance()->sanitize_cas_method();
1223 }
1224 if ( ! array_key_exists( 'cas_version_' . $cas_num_server, $auth_multisite_settings ) ) {
1225 $auth_multisite_settings[ 'cas_version_' . $cas_num_server ] = Options\External\Cas::get_instance()->sanitize_cas_version();
1226 }
1227 if ( ! array_key_exists( 'cas_attr_email_' . $cas_num_server, $auth_multisite_settings ) ) {
1228 $auth_multisite_settings[ 'cas_attr_email_' . $cas_num_server ] = '';
1229 }
1230 if ( ! array_key_exists( 'cas_attr_first_name_' . $cas_num_server, $auth_multisite_settings ) ) {
1231 $auth_multisite_settings[ 'cas_attr_first_name_' . $cas_num_server ] = '';
1232 }
1233 if ( ! array_key_exists( 'cas_attr_last_name_' . $cas_num_server, $auth_multisite_settings ) ) {
1234 $auth_multisite_settings[ 'cas_attr_last_name_' . $cas_num_server ] = '';
1235 }
1236 if ( ! array_key_exists( 'cas_attr_update_on_login_' . $cas_num_server, $auth_multisite_settings ) ) {
1237 $auth_multisite_settings[ 'cas_attr_update_on_login_' . $cas_num_server ] = '';
1238 }
1239 if ( ! array_key_exists( 'cas_link_on_username_' . $cas_num_server, $auth_multisite_settings ) ) {
1240 $auth_multisite_settings[ 'cas_link_on_username_' . $cas_num_server ] = '';
1241 }
1242 }
1243 }
1244 if ( ! array_key_exists( 'ldap', $auth_multisite_settings ) ) {
1245 $auth_multisite_settings['ldap'] = '';
1246 }
1247 if ( ! array_key_exists( 'ldap_host', $auth_multisite_settings ) ) {
1248 $auth_multisite_settings['ldap_host'] = '';
1249 }
1250 if ( ! array_key_exists( 'ldap_port', $auth_multisite_settings ) ) {
1251 $auth_multisite_settings['ldap_port'] = '389';
1252 }
1253 if ( ! array_key_exists( 'ldap_tls', $auth_multisite_settings ) ) {
1254 $auth_multisite_settings['ldap_tls'] = '1';
1255 }
1256 if ( ! array_key_exists( 'ldap_search_base', $auth_multisite_settings ) ) {
1257 $auth_multisite_settings['ldap_search_base'] = '';
1258 }
1259 if ( ! array_key_exists( 'ldap_search_filter', $auth_multisite_settings ) ) {
1260 $auth_multisite_settings['ldap_search_filter'] = '';
1261 }
1262 if ( ! array_key_exists( 'ldap_uid', $auth_multisite_settings ) ) {
1263 $auth_multisite_settings['ldap_uid'] = 'uid';
1264 }
1265 if ( ! array_key_exists( 'ldap_attr_email', $auth_multisite_settings ) ) {
1266 $auth_multisite_settings['ldap_attr_email'] = '';
1267 }
1268 if ( ! array_key_exists( 'ldap_user', $auth_multisite_settings ) ) {
1269 $auth_multisite_settings['ldap_user'] = '';
1270 }
1271 if ( ! array_key_exists( 'ldap_password', $auth_multisite_settings ) ) {
1272 $auth_multisite_settings['ldap_password'] = '';
1273 }
1274 if ( ! array_key_exists( 'ldap_lostpassword_url', $auth_multisite_settings ) ) {
1275 $auth_multisite_settings['ldap_lostpassword_url'] = '';
1276 }
1277 if ( ! array_key_exists( 'ldap_attr_first_name', $auth_multisite_settings ) ) {
1278 $auth_multisite_settings['ldap_attr_first_name'] = '';
1279 }
1280 if ( ! array_key_exists( 'ldap_attr_last_name', $auth_multisite_settings ) ) {
1281 $auth_multisite_settings['ldap_attr_last_name'] = '';
1282 }
1283 if ( ! array_key_exists( 'ldap_attr_update_on_login', $auth_multisite_settings ) ) {
1284 $auth_multisite_settings['ldap_attr_update_on_login'] = '';
1285 }
1286 if ( ! array_key_exists( 'ldap_test_user', $auth_multisite_settings ) ) {
1287 $auth_multisite_settings['ldap_test_user'] = '';
1288 }
1289 // Advanced defaults.
1290 if ( ! array_key_exists( 'advanced_lockouts', $auth_multisite_settings ) ) {
1291 $auth_multisite_settings['advanced_lockouts'] = array(
1292 'attempts_1' => 10,
1293 'duration_1' => 1,
1294 'attempts_2' => 10,
1295 'duration_2' => 10,
1296 'reset_duration' => 120,
1297 );
1298 }
1299 if ( ! array_key_exists( 'advanced_hide_wp_login', $auth_multisite_settings ) ) {
1300 $auth_multisite_settings['advanced_hide_wp_login'] = '';
1301 }
1302 if ( ! array_key_exists( 'advanced_disable_wp_login', $auth_multisite_settings ) ) {
1303 $auth_multisite_settings['advanced_disable_wp_login'] = '';
1304 }
1305 if ( ! array_key_exists( 'advanced_disable_wp_login_bypass_usernames', $auth_multisite_settings ) ) {
1306 $auth_multisite_settings['advanced_disable_wp_login_bypass_usernames'] = '';
1307 }
1308 if ( ! array_key_exists( 'advanced_users_per_page', $auth_multisite_settings ) ) {
1309 $auth_multisite_settings['advanced_users_per_page'] = 20;
1310 }
1311 if ( ! array_key_exists( 'advanced_users_sort_by', $auth_multisite_settings ) ) {
1312 $auth_multisite_settings['advanced_users_sort_by'] = 'created';
1313 }
1314 if ( ! array_key_exists( 'advanced_users_sort_order', $auth_multisite_settings ) ) {
1315 $auth_multisite_settings['advanced_users_sort_order'] = 'asc';
1316 }
1317 if ( ! array_key_exists( 'advanced_show_usernames', $auth_multisite_settings ) ) {
1318 $auth_multisite_settings['advanced_show_usernames'] = '';
1319 }
1320 if ( ! array_key_exists( 'advanced_widget_enabled', $auth_multisite_settings ) ) {
1321 $auth_multisite_settings['advanced_widget_enabled'] = '1';
1322 }
1323 // Save default network options to database.
1324 update_blog_option( get_main_site_id( get_main_network_id() ), 'auth_multisite_settings', $auth_multisite_settings );
1325 update_blog_option( get_main_site_id( get_main_network_id() ), 'auth_multisite_settings_access_users_approved', $auth_multisite_settings_access_users_approved );
1326 }
1327
1328 return $auth_settings;
1329 }
1330
1331
1332 /**
1333 * List sanitizer.
1334 *
1335 * @param array $user_list Array of users to sanitize.
1336 * @return array Array of sanitized users.
1337 */
1338 public function sanitize_user_list( $user_list ) {
1339 // If it's not a list, make it so.
1340 if ( ! is_array( $user_list ) ) {
1341 $user_list = array();
1342 }
1343 foreach ( $user_list as $key => $user_info ) {
1344 if ( strlen( $user_info['email'] ) < 1 ) {
1345 // Make sure there are no empty entries in the list.
1346 unset( $user_list[ $key ] );
1347 }
1348 }
1349 return $user_list;
1350 }
1351
1352
1353 /**
1354 * Settings sanitizer callback.
1355 *
1356 * @param array $auth_settings Authorizer settings array.
1357 * @return array Sanitized Authorizer settings array.
1358 */
1359 public function sanitize_options( $auth_settings ) {
1360 // Default to "Approved Users" login access restriction.
1361 if ( ! in_array( $auth_settings['access_who_can_login'], array( 'external_users', 'approved_users' ), true ) ) {
1362 $auth_settings['access_who_can_login'] = 'approved_users';
1363 }
1364
1365 // Default to "Everyone" view access restriction.
1366 if ( ! in_array( $auth_settings['access_who_can_view'], array( 'everyone', 'logged_in_users' ), true ) ) {
1367 $auth_settings['access_who_can_view'] = 'everyone';
1368 }
1369
1370 // Make sure users receiving pending user notifications is an empty array if
1371 // it's empty.
1372 // Note: this option doesn't exist in multisite options, so we first
1373 // check to see if it exists.
1374 if ( array_key_exists( 'access_users_receive_pending_emails', $auth_settings ) && ! is_array( $auth_settings['access_users_receive_pending_emails'] ) ) {
1375 $auth_settings['access_users_receive_pending_emails'] = array();
1376 }
1377
1378 // Default to WordPress login access redirect.
1379 // Note: this option doesn't exist in multisite options, so we first
1380 // check to see if it exists.
1381 if ( array_key_exists( 'access_redirect', $auth_settings ) && ! in_array( $auth_settings['access_redirect'], array( 'login', 'page', 'message' ), true ) ) {
1382 $auth_settings['access_redirect'] = 'login';
1383 }
1384
1385 // Default to warning message for anonymous users on public pages.
1386 // Note: this option doesn't exist in multisite options, so we first
1387 // check to see if it exists.
1388 if ( array_key_exists( 'access_public_warning', $auth_settings ) && ! in_array( $auth_settings['access_public_warning'], array( 'no_warning', 'warning' ), true ) ) {
1389 $auth_settings['access_public_warning'] = 'no_warning';
1390 }
1391
1392 // Sanitize Send welcome email (checkbox: value can only be '1' or empty string).
1393 $auth_settings['access_should_email_approved_users'] = array_key_exists( 'access_should_email_approved_users', $auth_settings ) && strlen( $auth_settings['access_should_email_approved_users'] ) > 0 ? '1' : '';
1394
1395 // Sanitize Enable OAuth2 Logins (checkbox: value can only be '1' or empty string).
1396 $auth_settings['oauth2'] = array_key_exists( 'oauth2', $auth_settings ) && strlen( $auth_settings['oauth2'] ) > 0 ? '1' : '';
1397
1398 // Sanitize OAuth2 auto-login (select: value can be between '1' and '20' or empty string).
1399 if ( ! isset( $auth_settings['oauth2_auto_login'] ) || ! in_array( $auth_settings['oauth2_auto_login'], array( '', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20' ), true ) ) {
1400 $auth_settings['oauth2_auto_login'] = '';
1401 }
1402
1403 // Sanitize OAuth2 number of servers (range: value can only be '1' to '20').
1404 $auth_settings['oauth2_num_servers'] = filter_var( $auth_settings['oauth2_num_servers'], FILTER_SANITIZE_NUMBER_INT );
1405 $auth_settings['oauth2_num_servers'] = intval( $auth_settings['oauth2_num_servers'] ) < 1 || intval( $auth_settings['oauth2_num_servers'] ) > 20 ? '1' : $auth_settings['oauth2_num_servers'];
1406
1407 // Sanitize Oauth2 attribute update (select: value can only be 'update-if-empty', '1', or empty string).
1408 if ( ! isset( $auth_settings['oauth2_attr_update_on_login'] ) || ! in_array( $auth_settings['oauth2_attr_update_on_login'], array( '', '1', 'update-if-empty' ), true ) ) {
1409 $auth_settings['oauth2_attr_update_on_login'] = '';
1410 }
1411
1412 // Sanitize OAuth2 require verified email (checkbox: value can only be '1' or empty string).
1413 $auth_settings['oauth2_require_verified_email'] = array_key_exists( 'oauth2_require_verified_email', $auth_settings ) && strlen( $auth_settings['oauth2_require_verified_email'] ) > 0 ? '1' : '';
1414
1415 // Sanitize OAuth2 link on username (checkbox: value can only be '1' or empty string).
1416 $auth_settings['oauth2_link_on_username'] = array_key_exists( 'oauth2_link_on_username', $auth_settings ) && strlen( $auth_settings['oauth2_link_on_username'] ) > 0 ? '1' : '';
1417
1418 // Sanitize settings for any additional OAuth2 servers.
1419 if ( intval( $auth_settings['oauth2_num_servers'] ) > 1 ) {
1420 foreach ( range( 2, min( intval( $auth_settings['oauth2_num_servers'] ), 20 ) ) as $oauth2_num_server ) {
1421 // Sanitize Oauth2 attribute update (select: value can only be 'update-if-empty', '1', or empty string).
1422 if ( ! isset( $auth_settings[ 'oauth2_attr_update_on_login_' . $oauth2_num_server ] ) || ! in_array( $auth_settings[ 'oauth2_attr_update_on_login_' . $oauth2_num_server ], array( '', '1', 'update-if-empty' ), true ) ) {
1423 $auth_settings[ 'oauth2_attr_update_on_login_' . $oauth2_num_server ] = '';
1424 }
1425
1426 // Sanitize OAuth2 require verified email (checkbox: value can only be '1' or empty string).
1427 $auth_settings[ 'oauth2_require_verified_email_' . $oauth2_num_server ] = array_key_exists( 'oauth2_require_verified_email_' . $oauth2_num_server, $auth_settings ) && strlen( $auth_settings[ 'oauth2_require_verified_email_' . $oauth2_num_server ] ) > 0 ? '1' : '';
1428
1429 // Sanitize OAuth2 link on username (checkbox: value can only be '1' or empty string).
1430 $auth_settings[ 'oauth2_link_on_username_' . $oauth2_num_server ] = array_key_exists( 'oauth2_link_on_username_' . $oauth2_num_server, $auth_settings ) && strlen( $auth_settings[ 'oauth2_link_on_username_' . $oauth2_num_server ] ) > 0 ? '1' : '';
1431 }
1432 }
1433
1434 // Sanitize Enable Google Logins (checkbox: value can only be '1' or empty string).
1435 $auth_settings['google'] = array_key_exists( 'google', $auth_settings ) && strlen( $auth_settings['google'] ) > 0 ? '1' : '';
1436
1437 // Sanitize Enable CAS Logins (checkbox: value can only be '1' or empty string).
1438 $auth_settings['cas'] = array_key_exists( 'cas', $auth_settings ) && strlen( $auth_settings['cas'] ) > 0 ? '1' : '';
1439
1440 // Sanitize CAS auto-login (select: value can be between '1' and '10' or empty string).
1441 if ( ! isset( $auth_settings['cas_auto_login'] ) || ! in_array( $auth_settings['cas_auto_login'], array( '', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10' ), true ) ) {
1442 $auth_settings['cas_auto_login'] = '';
1443 }
1444
1445 // Sanitize CAS number of servers (range: value can only be '1' to '10').
1446 $auth_settings['cas_num_servers'] = filter_var( $auth_settings['cas_num_servers'], FILTER_SANITIZE_NUMBER_INT );
1447 $auth_settings['cas_num_servers'] = intval( $auth_settings['cas_num_servers'] ) < 1 || intval( $auth_settings['cas_num_servers'] ) > 10 ? '1' : $auth_settings['cas_num_servers'];
1448
1449 // Sanitize CAS Host setting.
1450 $auth_settings['cas_host'] = filter_var( $auth_settings['cas_host'], FILTER_SANITIZE_URL );
1451
1452 // Sanitize CAS Port (int).
1453 $auth_settings['cas_port'] = filter_var( $auth_settings['cas_port'], FILTER_SANITIZE_NUMBER_INT );
1454
1455 // Sanitize CAS attribute update (select: value can only be 'update-if-empty', '1', or empty string).
1456 if ( ! isset( $auth_settings['cas_attr_update_on_login'] ) || ! in_array( $auth_settings['cas_attr_update_on_login'], array( '', '1', 'update-if-empty' ), true ) ) {
1457 $auth_settings['cas_attr_update_on_login'] = '';
1458 }
1459
1460 // Sanitize CAS link on username (checkbox: value can only be '1' or empty string).
1461 $auth_settings['cas_link_on_username'] = array_key_exists( 'cas_link_on_username', $auth_settings ) && strlen( $auth_settings['cas_link_on_username'] ) > 0 ? '1' : '';
1462
1463 // Sanitize settings for any additional CAS servers.
1464 if ( intval( $auth_settings['cas_num_servers'] ) > 1 ) {
1465 foreach ( range( 2, min( intval( $auth_settings['cas_num_servers'] ), 10 ) ) as $cas_num_server ) {
1466 // Sanitize CAS Host setting.
1467 $auth_settings[ 'cas_host_' . $cas_num_server ] = filter_var( $auth_settings[ 'cas_host_' . $cas_num_server ] ?? '', FILTER_SANITIZE_URL );
1468
1469 // Sanitize CAS Port (int).
1470 $auth_settings[ 'cas_port_' . $cas_num_server ] = filter_var( $auth_settings[ 'cas_port_' . $cas_num_server ] ?? '', FILTER_SANITIZE_NUMBER_INT );
1471
1472 // Sanitize CAS attribute update (select: value can only be 'update-if-empty', '1', or empty string).
1473 if ( ! isset( $auth_settings[ 'cas_attr_update_on_login_' . $cas_num_server ] ) || ! in_array( $auth_settings[ 'cas_attr_update_on_login_' . $cas_num_server ], array( '', '1', 'update-if-empty' ), true ) ) {
1474 $auth_settings[ 'cas_attr_update_on_login_' . $cas_num_server ] = '';
1475 }
1476
1477 // Sanitize CAS link on username (checkbox: value can only be '1' or empty string).
1478 $auth_settings[ 'cas_link_on_username_' . $cas_num_server ] = array_key_exists( 'cas_link_on_username_' . $cas_num_server, $auth_settings ) && strlen( $auth_settings[ 'cas_link_on_username_' . $cas_num_server ] ) > 0 ? '1' : '';
1479 }
1480 }
1481
1482 // Sanitize Enable LDAP Logins (checkbox: value can only be '1' or empty string).
1483 $auth_settings['ldap'] = array_key_exists( 'ldap', $auth_settings ) && strlen( $auth_settings['ldap'] ) > 0 ? '1' : '';
1484
1485 // Sanitize LDAP Port (int).
1486 $auth_settings['ldap_port'] = filter_var( $auth_settings['ldap_port'], FILTER_SANITIZE_NUMBER_INT );
1487
1488 // Sanitize LDAP TLS (checkbox: value can only be '1' or empty string).
1489 $auth_settings['ldap_tls'] = array_key_exists( 'ldap_tls', $auth_settings ) && strlen( $auth_settings['ldap_tls'] ) > 0 ? '1' : '';
1490
1491 // Sanitize LDAP attributes (basically make sure they don't have any parentheses).
1492 $auth_settings['ldap_uid'] = filter_var( $auth_settings['ldap_uid'], FILTER_SANITIZE_EMAIL );
1493
1494 // Sanitize LDAP Lost Password URL.
1495 $auth_settings['ldap_lostpassword_url'] = filter_var( $auth_settings['ldap_lostpassword_url'], FILTER_SANITIZE_URL );
1496
1497 // Obfuscate LDAP directory user password.
1498 if ( isset( $auth_settings['ldap_password'] ) && strlen( $auth_settings['ldap_password'] ) > 0 ) {
1499 // encrypt the directory user password for some minor obfuscation in the database.
1500 $auth_settings['ldap_password'] = Helper::encrypt( $auth_settings['ldap_password'] );
1501 }
1502
1503 // Sanitize LDAP attribute update (select: value can only be 'update-if-empty', '1', or empty string).
1504 if ( ! isset( $auth_settings['ldap_attr_update_on_login'] ) || ! in_array( $auth_settings['ldap_attr_update_on_login'], array( '', '1', 'update-if-empty' ), true ) ) {
1505 $auth_settings['ldap_attr_update_on_login'] = '';
1506 }
1507
1508 // Sanitize Enable OIDC Logins (checkbox: value can only be '1' or empty string).
1509 $auth_settings['oidc'] = array_key_exists( 'oidc', $auth_settings ) && strlen( $auth_settings['oidc'] ) > 0 ? '1' : '';
1510
1511 // Sanitize OIDC Num Servers (int, 1-20).
1512 $auth_settings['oidc_num_servers'] = filter_var( $auth_settings['oidc_num_servers'] ?? '1', FILTER_SANITIZE_NUMBER_INT );
1513 $auth_settings['oidc_num_servers'] = intval( $auth_settings['oidc_num_servers'] ) < 1 || intval( $auth_settings['oidc_num_servers'] ) > 20 ? '1' : $auth_settings['oidc_num_servers'];
1514
1515 // Sanitize OIDC auto-login (select: value can be between '1' and '20' or empty string).
1516 if ( ! isset( $auth_settings['oidc_auto_login'] ) || ! in_array( $auth_settings['oidc_auto_login'], array( '', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20' ), true ) ) {
1517 $auth_settings['oidc_auto_login'] = '';
1518 }
1519
1520 // Sanitize OIDC Issuer URL.
1521 $auth_settings['oidc_issuer'] = filter_var( $auth_settings['oidc_issuer'] ?? '', FILTER_SANITIZE_URL );
1522
1523 // Sanitize OIDC attribute update (select: value can only be 'update-if-empty', '1', or empty string).
1524 if ( ! isset( $auth_settings['oidc_attr_update_on_login'] ) || ! in_array( $auth_settings['oidc_attr_update_on_login'], array( '', '1', 'update-if-empty' ), true ) ) {
1525 $auth_settings['oidc_attr_update_on_login'] = '';
1526 }
1527
1528 // Sanitize OIDC force auth method (select: value can only be 'client_secret_basic', 'client_secret_post', 'client_secret_jwt', 'private_key_jwt', or empty string).
1529 if ( ! isset( $auth_settings['oidc_force_auth_method'] ) || ! in_array( $auth_settings['oidc_force_auth_method'], array( '', 'client_secret_basic', 'client_secret_post', 'client_secret_jwt', 'private_key_jwt' ), true ) ) {
1530 $auth_settings['oidc_force_auth_method'] = '';
1531 }
1532
1533 // Sanitize OIDC require verified email (checkbox: value can only be '1' or empty string).
1534 $auth_settings['oidc_require_verified_email'] = array_key_exists( 'oidc_require_verified_email', $auth_settings ) && strlen( $auth_settings['oidc_require_verified_email'] ) > 0 ? '1' : '';
1535
1536 // Sanitize OIDC link on username (checkbox: value can only be '1' or empty string).
1537 $auth_settings['oidc_link_on_username'] = array_key_exists( 'oidc_link_on_username', $auth_settings ) && strlen( $auth_settings['oidc_link_on_username'] ) > 0 ? '1' : '';
1538
1539 // Sanitize settings for any additional OIDC servers.
1540 if ( intval( $auth_settings['oidc_num_servers'] ) > 1 ) {
1541 foreach ( range( 2, min( intval( $auth_settings['oidc_num_servers'] ), 20 ) ) as $oidc_num_server ) {
1542 // Sanitize OIDC Issuer URL.
1543 $auth_settings[ 'oidc_issuer_' . $oidc_num_server ] = filter_var( $auth_settings[ 'oidc_issuer_' . $oidc_num_server ] ?? '', FILTER_SANITIZE_URL );
1544
1545 // Sanitize OIDC attribute update (select: value can only be 'update-if-empty', '1', or empty string).
1546 if ( ! isset( $auth_settings[ 'oidc_attr_update_on_login_' . $oidc_num_server ] ) || ! in_array( $auth_settings[ 'oidc_attr_update_on_login_' . $oidc_num_server ], array( '', '1', 'update-if-empty' ), true ) ) {
1547 $auth_settings[ 'oidc_attr_update_on_login_' . $oidc_num_server ] = '';
1548 }
1549
1550 // Sanitize OIDC force auth method (select: value can only be 'client_secret_basic', 'client_secret_post', 'client_secret_jwt', 'private_key_jwt', or empty string).
1551 if ( ! isset( $auth_settings[ 'oidc_force_auth_method' . $oidc_num_server ] ) || ! in_array( $auth_settings[ 'oidc_force_auth_method' . $oidc_num_server ], array( '', 'client_secret_basic', 'client_secret_post', 'client_secret_jwt', 'private_key_jwt' ), true ) ) {
1552 $auth_settings[ 'oidc_force_auth_method' . $oidc_num_server ] = '';
1553 }
1554
1555 // Sanitize OIDC require verified email (checkbox: value can only be '1' or empty string).
1556 $auth_settings[ 'oidc_require_verified_email_' . $oidc_num_server ] = array_key_exists( 'oidc_require_verified_email_' . $oidc_num_server, $auth_settings ) && strlen( $auth_settings[ 'oidc_require_verified_email_' . $oidc_num_server ] ) > 0 ? '1' : '';
1557
1558 // Sanitize OIDC link on username (checkbox: value can only be '1' or empty string).
1559 $auth_settings[ 'oidc_link_on_username_' . $oidc_num_server ] = array_key_exists( 'oidc_link_on_username_' . $oidc_num_server, $auth_settings ) && strlen( $auth_settings[ 'oidc_link_on_username_' . $oidc_num_server ] ) > 0 ? '1' : '';
1560 }
1561 }
1562
1563 // Make sure public pages is an empty array if it's empty.
1564 // Note: this option doesn't exist in multisite options, so we first
1565 // check to see if it exists.
1566 if ( array_key_exists( 'access_public_pages', $auth_settings ) && ! is_array( $auth_settings['access_public_pages'] ) ) {
1567 $auth_settings['access_public_pages'] = array();
1568 }
1569
1570 // Make sure all lockout options are integers (attempts_1, duration_1,
1571 // attempts_2, duration_2, reset_duration). Default to 0 if not.
1572 foreach ( $auth_settings['advanced_lockouts'] as $key => $value ) {
1573 $auth_settings['advanced_lockouts'][ $key ] = filter_var( $value, FILTER_SANITIZE_NUMBER_INT );
1574 if ( empty( $auth_settings['advanced_lockouts'][ $key ] ) ) {
1575 $auth_settings['advanced_lockouts'][ $key ] = 0;
1576 }
1577 }
1578
1579 // Sanitize Hide WordPress logins (checkbox: value can only be '1' or empty string).
1580 $auth_settings['advanced_hide_wp_login'] = array_key_exists( 'advanced_hide_wp_login', $auth_settings ) && strlen( $auth_settings['advanced_hide_wp_login'] ) > 0 ? '1' : '';
1581
1582 // Sanitize Disable WordPress logins (checkbox: value can only be '1' or empty string).
1583 $auth_settings['advanced_disable_wp_login'] = array_key_exists( 'advanced_disable_wp_login', $auth_settings ) && strlen( $auth_settings['advanced_disable_wp_login'] ) > 0 ? '1' : '';
1584
1585 // Sanitize Users per page (text: value can only int from 1 to MAX_INT).
1586 $auth_settings['advanced_users_per_page'] = array_key_exists( 'advanced_users_per_page', $auth_settings ) && intval( $auth_settings['advanced_users_per_page'] ) > 0 ? intval( $auth_settings['advanced_users_per_page'] ) : 1;
1587
1588 // Sanitize Sort users by (select: value can be 'email', 'role', 'date_added', 'created').
1589 if ( ! isset( $auth_settings['advanced_users_sort_by'] ) || ! in_array( $auth_settings['advanced_users_sort_by'], array( 'email', 'role', 'date_added', 'created' ), true ) ) {
1590 $auth_settings['advanced_users_sort_by'] = 'created';
1591 }
1592
1593 // Sanitize Sort users order (select: value can be 'asc', 'desc').
1594 if ( ! isset( $auth_settings['advanced_users_sort_order'] ) || ! in_array( $auth_settings['advanced_users_sort_order'], array( 'asc', 'desc' ), true ) ) {
1595 $auth_settings['advanced_users_sort_order'] = 'asc';
1596 }
1597
1598 // Sanitize Show usernames in approved users list (checkbox: value can only be '1' or empty string).
1599 $auth_settings['advanced_show_usernames'] = array_key_exists( 'advanced_show_usernames', $auth_settings ) && strlen( $auth_settings['advanced_show_usernames'] ) > 0 ? '1' : '';
1600
1601 // Sanitize Show Dashboard Widget (checkbox: value can only be '1' or empty string).
1602 $auth_settings['advanced_widget_enabled'] = array_key_exists( 'advanced_widget_enabled', $auth_settings ) && strlen( $auth_settings['advanced_widget_enabled'] ) > 0 ? '1' : '';
1603
1604 // Sanitize Override multisite options (checkbox: value can only be '1' or empty string).
1605 $auth_settings['advanced_override_multisite'] = array_key_exists( 'advanced_override_multisite', $auth_settings ) && strlen( $auth_settings['advanced_override_multisite'] ) > 0 ? '1' : '';
1606
1607 return $auth_settings;
1608 }
1609
1610
1611 /**
1612 * Sanitizes an array of user update commands coming from the AJAX handler in Authorizer Settings.
1613 *
1614 * Example $users array:
1615 * array(
1616 * array(
1617 * edit_action: 'add' or 'remove' or 'change_role',
1618 * email: 'johndoe@example.com',
1619 * role: 'subscriber',
1620 * date_added: 'Jun 2014',
1621 * local_user: 'true' or 'false',
1622 * multisite_user: 'true' or 'false',
1623 * ),
1624 * ...
1625 * )
1626 *
1627 * @param array $users Users to edit.
1628 * @param array $args Options (e.g., 'allow_wildcard_email' => true).
1629 * @return array Sanitized users to edit.
1630 */
1631 public function sanitize_update_auth_users( $users = array(), $args = array() ) {
1632 if ( ! is_array( $users ) ) {
1633 $users = array();
1634 }
1635 if ( isset( $args['allow_wildcard_email'] ) && $args['allow_wildcard_email'] ) {
1636 $users = array_map( array( $this, 'sanitize_update_auth_user_allow_wildcard_email' ), $users );
1637 } else {
1638 $users = array_map( array( $this, 'sanitize_update_auth_user' ), $users );
1639 }
1640
1641 // Remove any entries that failed email address validation.
1642 $users = array_filter( $users, array( $this, 'remove_invalid_auth_users' ) );
1643
1644 return $users;
1645 }
1646
1647
1648 /**
1649 * Callback for array_map in sanitize_update_auth_users().
1650 *
1651 * @param array $user User data to sanitize.
1652 * @return array Sanitized user data.
1653 */
1654 public function sanitize_update_auth_user( $user ) {
1655 if ( array_key_exists( 'edit_action', $user ) ) {
1656 $user['edit_action'] = sanitize_text_field( $user['edit_action'] );
1657 }
1658 if ( isset( $user['email'] ) ) {
1659 $user['email'] = sanitize_email( $user['email'] );
1660 }
1661 if ( isset( $user['role'] ) ) {
1662 $user['role'] = sanitize_text_field( $user['role'] );
1663 }
1664 if ( isset( $user['date_added'] ) ) {
1665 $user['date_added'] = sanitize_text_field( $user['date_added'] );
1666 }
1667 if ( isset( $user['local_user'] ) ) {
1668 $user['local_user'] = 'true' === $user['local_user'] ? 'true' : 'false';
1669 }
1670 if ( isset( $user['multisite_user'] ) ) {
1671 $user['multisite_user'] = 'true' === $user['multisite_user'] ? 'true' : 'false';
1672 }
1673
1674 return $user;
1675 }
1676
1677
1678 /**
1679 * Settings print callback.
1680 *
1681 * @param array $args Args (e.g., multisite admin mode).
1682 * @return void
1683 */
1684 public function print_section_info_tabs( $args = '' ) {
1685 if ( Helper::NETWORK_CONTEXT === Helper::get_context( $args ) ) :
1686 ?>
1687 <h2 class="nav-tab-wrapper">
1688 <a class="nav-tab nav-tab-access_lists nav-tab-active" href="javascript:chooseTab('access_lists' );"><?php esc_html_e( 'Access Lists', 'authorizer' ); ?></a>
1689 <a class="nav-tab nav-tab-external" href="javascript:chooseTab('external' );"><?php esc_html_e( 'External Service', 'authorizer' ); ?></a>
1690 <a class="nav-tab nav-tab-external_oauth2" href="javascript:chooseTab('external_oauth2' );"><?php esc_html_e( 'OAuth2', 'authorizer' ); ?></a>
1691 <a class="nav-tab nav-tab-external_oidc" href="javascript:chooseTab('external_oidc' );"><?php esc_html_e( 'OIDC', 'authorizer' ); ?></a>
1692 <a class="nav-tab nav-tab-external_google" href="javascript:chooseTab('external_google' );"><?php esc_html_e( 'Google', 'authorizer' ); ?></a>
1693 <a class="nav-tab nav-tab-external_cas" href="javascript:chooseTab('external_cas' );"><?php esc_html_e( 'CAS', 'authorizer' ); ?></a>
1694 <a class="nav-tab nav-tab-external_ldap" href="javascript:chooseTab('external_ldap' );"><?php esc_html_e( 'LDAP', 'authorizer' ); ?></a>
1695 <a class="nav-tab nav-tab-advanced" href="javascript:chooseTab('advanced' );"><?php esc_html_e( 'Advanced', 'authorizer' ); ?></a>
1696 </h2>
1697 <?php else : ?>
1698 <h2 class="nav-tab-wrapper">
1699 <a class="nav-tab nav-tab-access_lists nav-tab-active" href="javascript:chooseTab('access_lists' );"><?php esc_html_e( 'Access Lists', 'authorizer' ); ?></a>
1700 <a class="nav-tab nav-tab-access_login" href="javascript:chooseTab('access_login' );"><?php esc_html_e( 'Login Access', 'authorizer' ); ?></a>
1701 <a class="nav-tab nav-tab-access_public" href="javascript:chooseTab('access_public' );"><?php esc_html_e( 'Public Access', 'authorizer' ); ?></a>
1702 <a class="nav-tab nav-tab-external" href="javascript:chooseTab('external' );"><?php esc_html_e( 'External Service', 'authorizer' ); ?></a>
1703 <a class="nav-tab nav-tab-external_oauth2" href="javascript:chooseTab('external_oauth2' );"><?php esc_html_e( 'OAuth2', 'authorizer' ); ?></a>
1704 <a class="nav-tab nav-tab-external_oidc" href="javascript:chooseTab('external_oidc' );"><?php esc_html_e( 'OIDC', 'authorizer' ); ?></a>
1705 <a class="nav-tab nav-tab-external_google" href="javascript:chooseTab('external_google' );"><?php esc_html_e( 'Google', 'authorizer' ); ?></a>
1706 <a class="nav-tab nav-tab-external_cas" href="javascript:chooseTab('external_cas' );"><?php esc_html_e( 'CAS', 'authorizer' ); ?></a>
1707 <a class="nav-tab nav-tab-external_ldap" href="javascript:chooseTab('external_ldap' );"><?php esc_html_e( 'LDAP', 'authorizer' ); ?></a>
1708 <a class="nav-tab nav-tab-advanced" href="javascript:chooseTab('advanced' );"><?php esc_html_e( 'Advanced', 'authorizer' ); ?></a>
1709 </h2>
1710 <?php
1711 endif;
1712 }
1713
1714
1715 /**
1716 * This array filter will remove any users who failed email address validation
1717 * (which would set their email to a blank string).
1718 *
1719 * @param array $user User data to check for a valid email.
1720 * @return bool Whether to filter out the user.
1721 */
1722 protected function remove_invalid_auth_users( $user ) {
1723 return isset( $user['email'] ) && strlen( $user['email'] ) > 0;
1724 }
1725
1726
1727 /**
1728 * Callback for array_map in sanitize_update_auth_users().
1729 *
1730 * @param array $user User data to sanitize.
1731 * @return array Sanitized user data.
1732 */
1733 protected function sanitize_update_auth_user_allow_wildcard_email( $user ) {
1734 if ( array_key_exists( 'edit_action', $user ) ) {
1735 $user['edit_action'] = sanitize_text_field( $user['edit_action'] );
1736 }
1737 if ( isset( $user['email'] ) ) {
1738 if ( strpos( $user['email'], '@' ) === 0 ) {
1739 $user['email'] = sanitize_text_field( $user['email'] );
1740 } else {
1741 $user['email'] = sanitize_email( $user['email'] );
1742 }
1743 }
1744 if ( isset( $user['role'] ) ) {
1745 $user['role'] = sanitize_text_field( $user['role'] );
1746 }
1747 if ( isset( $user['date_added'] ) ) {
1748 $user['date_added'] = sanitize_text_field( $user['date_added'] );
1749 }
1750 if ( isset( $user['local_user'] ) ) {
1751 $user['local_user'] = 'true' === $user['local_user'] ? 'true' : 'false';
1752 }
1753 if ( isset( $user['multisite_user'] ) ) {
1754 $user['multisite_user'] = 'true' === $user['multisite_user'] ? 'true' : 'false';
1755 }
1756
1757 return $user;
1758 }
1759 }
1760