PluginProbe
Authorizer / 2.3.6
Authorizer v2.3.6
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 / authorizer.php

authorizer.php in Authorizer 2.3.6, at authorizer.php

4,783 lines 232.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 Plugin Name: Authorizer
4 Plugin URI: https://github.com/figureone/authorizer
5 Description: Authorizer limits login attempts, restricts access to specified users, and authenticates against external sources (e.g., Google, LDAP, or CAS).
6 Version: 2.3.6
7 Author: Paul Ryan
8 Author URI: http://www.linkedin.com/in/paulrryan/
9 License: GPL2
10 */
11
12 /*
13 Copyright 2014 Paul Ryan (email: prar@hawaii.edu)
14
15 This program is free software; you can redistribute it and/or modify
16 it under the terms of the GNU General Public License, version 2, as
17 published by the Free Software Foundation.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
27 */
28
29 /*
30 Portions forked from Restricted Site Access plugin: http://wordpress.org/plugins/restricted-site-access/
31 Portions forked from wpCAS plugin: http://wordpress.org/extend/plugins/cas-authentication/
32 Portions forked from Limit Login Attempts: http://wordpress.org/plugins/limit-login-attempts/
33 */
34
35 // Add phpCAS library if it's not included.
36 // @see https://wiki.jasig.org/display/CASC/phpCAS+installation+guide
37 if ( ! defined( 'PHPCAS_VERSION' ) ) {
38 require_once dirname( __FILE__ ) . '/inc/CAS-1.3.3/CAS.php';
39 }
40
41 // Add Google API PHP Client if it's not included.
42 // @see https://github.com/google/google-api-php-client
43 if ( ! class_exists( 'Google_Client' ) ) {
44 set_include_path( get_include_path() . PATH_SEPARATOR . dirname( __FILE__ ) . '/inc/google-api-php-client/src' );
45 require_once dirname( __FILE__ ) . '/inc/google-api-php-client/src/Google/Client.php';
46 }
47
48 if ( ! class_exists( 'WP_Plugin_Authorizer' ) ) {
49 /**
50 * Define class for plugin: Authorizer.
51 *
52 * @category Authentication
53 * @package Authorizer
54 * @author Paul Ryan <prar@hawaii.edu>
55 * @license http://www.gnu.org/licenses/gpl-2.0.html GPL2
56 * @link http://hawaii.edu/coe/dcdc/wordpress/authorizer/doc/
57 */
58 class WP_Plugin_Authorizer {
59
60 /**
61 * Constructor.
62 */
63 public function __construct() {
64 // Installation and uninstallation hooks.
65 register_activation_hook( __FILE__, array( $this, 'activate' ) );
66 register_deactivation_hook( __FILE__, array( $this, 'deactivate' ) );
67
68 // Register filters.
69
70 // Custom wp authentication routine using external service.
71 add_filter( 'authenticate', array( $this, 'custom_authenticate' ), 1, 3 );
72
73 // Custom logout action using external service.
74 add_action( 'wp_logout', array( $this, 'custom_logout' ) );
75
76 // Removing this bypasses Wordpress authentication (so if external auth fails,
77 // no one can log in); with it enabled, it will run if external auth fails.
78 //remove_filter('authenticate', 'wp_authenticate_username_password', 20, 3);
79
80 // Create settings link on Plugins page
81 add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( $this, 'plugin_settings_link' ) );
82 add_filter( 'network_admin_plugin_action_links_' . plugin_basename( __FILE__ ), array( $this, 'network_admin_plugin_settings_link' ) );
83
84 // Modify login page with a custom password url (if option is set).
85 add_filter( 'lostpassword_url', array( $this, 'custom_lostpassword_url' ) );
86
87 // If we have a custom login error, add the filter to show it.
88 $error = get_option( 'auth_settings_advanced_login_error' );
89 if ( $error && strlen( $error ) > 0 ) {
90 add_filter( 'login_errors', array( $this, 'show_advanced_login_error' ) );
91 }
92
93 // Register actions.
94
95 // Perform plugin updates if newer version installed.
96 add_action( 'plugins_loaded', array( $this, 'auth_update_check' ) );
97
98 // Update the user meta with this user's failed login attempt.
99 add_action( 'wp_login_failed', array( $this, 'update_login_failed_count' ) );
100
101 // Create menu item in Settings
102 add_action( 'admin_menu', array( $this, 'add_plugin_page' ) );
103
104 // Create options page
105 add_action( 'admin_init', array( $this, 'page_init' ) );
106
107 // Update user role in approved list if it's changed in the WordPress edit user page.
108 add_action( 'edit_user_profile_update', array( $this, 'edit_user_profile_update_role' ) );
109
110 // Enqueue javascript and css on the plugin's options page, the
111 // dashboard (for the widget), and the network admin.
112 add_action( 'load-settings_page_authorizer', array( $this, 'load_options_page' ) );
113 add_action( 'admin_head-index.php', array( $this, 'load_options_page' ) );
114 add_action( 'load-toplevel_page_authorizer', array( $this, 'load_options_page' ) );
115
116 // Add custom css and js to wp-login.php
117 add_action( 'login_enqueue_scripts', array( $this, 'login_enqueue_scripts_and_styles' ) );
118 add_action( 'login_footer', array( $this, 'load_login_footer_js' ) );
119
120 // Modify login page with external auth links (if enabled; e.g., google or cas)
121 add_action( 'login_form', array( $this, 'login_form_add_external_service_links' ) );
122
123 // Verify current user has access to page they are visiting
124 add_action( 'parse_request', array( $this, 'restrict_access' ), 1 );
125
126 // ajax save options from dashboard widget
127 add_action( 'wp_ajax_update_auth_user', array( $this, 'ajax_update_auth_user' ) );
128
129 // ajax save options from multisite options page
130 add_action( 'wp_ajax_save_auth_multisite_settings', array( $this, 'ajax_save_auth_multisite_settings' ) );
131
132 // ajax save usermeta from options page
133 add_action( 'wp_ajax_update_auth_usermeta', array( $this, 'ajax_update_auth_usermeta' ) );
134
135 // ajax verify google login
136 add_action( 'wp_ajax_process_google_login', array( $this, 'ajax_process_google_login' ) );
137 add_action( 'wp_ajax_nopriv_process_google_login', array( $this, 'ajax_process_google_login' ) );
138
139 // Add dashboard widget so instructors can add/edit users with access.
140 // Hint: For Multisite Network Admin Dashboard use wp_network_dashboard_setup instead of wp_dashboard_setup.
141 add_action( 'wp_dashboard_setup', array( $this, 'add_dashboard_widgets' ) );
142
143 // If we have a custom admin message, add the action to show it.
144 $notice = get_option( 'auth_settings_advanced_admin_notice' );
145 if ( $notice && strlen( $notice ) > 0 ) {
146 add_action( 'admin_notices', array( $this, 'show_advanced_admin_notice' ) );
147 add_action( 'network_admin_notices', array( $this, 'show_advanced_admin_notice' ) );
148 }
149
150 // Load custom javascript for the main site (e.g., for displaying alerts).
151 add_action( 'wp_enqueue_scripts', array( $this, 'auth_public_scripts' ), 20 );
152
153 // If multisite, add network admin options page (global settings for all sites)
154 if ( is_multisite() ) {
155 add_action( 'network_admin_menu', array( $this, 'network_admin_menu' ) );
156 }
157
158 // Create login cookie (used by google login)
159 if ( ! isset( $_COOKIE['login_unique'] ) ) {
160 setcookie( 'login_unique', $this->get_cookie_value(), time()+1800, '/', defined( COOKIE_DOMAIN ) ? COOKIE_DOMAIN : '' );
161 }
162
163 } // END __construct()
164
165
166 /**
167 * Plugin activation hook.
168 * Will also activate the plugin for all sites/blogs if this is a "Network enable."
169 *
170 * @return void
171 */
172 public function activate() {
173 global $wpdb;
174
175 // If we're in a multisite environment, run the plugin activation for each site when network enabling
176 if ( is_multisite() && isset( $_GET['networkwide'] ) && $_GET['networkwide'] == 1 ) {
177 $old_blog = $wpdb->blogid;
178 // Get all blog ids
179 $blogs = wp_get_sites( array( 'limit' => 999999 ) );
180 foreach ( $blogs as $blog ) {
181 switch_to_blog( $blog['blog_id'] );
182 // Set meaningful defaults for other sites in the network.
183 $this->set_default_options();
184 // Add current WordPress users to the approved list.
185 $this->add_wp_users_to_approved_list();
186 }
187 switch_to_blog( $old_blog );
188 } else {
189 // Set meaningful defaults for this site.
190 $this->set_default_options();
191 // Add current WordPress users to the approved list.
192 $this->add_wp_users_to_approved_list();
193 }
194
195 } // END activate()
196
197 /**
198 * Adds all WordPress users in the current site to the approved list,
199 * unless they are already in the blocked list. Also removes them
200 * from the pending list if they are there.
201 *
202 * Runs in plugin activation hook.
203 *
204 * @return void
205 */
206 private function add_wp_users_to_approved_list() {
207 // Add current WordPress users to the approved list.
208 $auth_multisite_settings_access_users_approved = is_multisite() ? get_blog_option( BLOG_ID_CURRENT_SITE, 'auth_multisite_settings_access_users_approved', array() ) : array();
209 $auth_settings_access_users_pending = $this->get_plugin_option( 'access_users_pending', 'single admin' );
210 $auth_settings_access_users_approved = $this->get_plugin_option( 'access_users_approved', 'single admin' );
211 $auth_settings_access_users_blocked = $this->get_plugin_option( 'access_users_blocked', 'single admin' );
212 $default_role = $this->get_plugin_option( 'access_default_role', 'single admin', 'allow override' );
213 $updated = false;
214 foreach ( get_users() as $user ) {
215 // Skip if user is in blocked list.
216 if ( $this->in_multi_array( $user->user_email, $auth_settings_access_users_blocked ) ) {
217 continue;
218 }
219 // Skip if user is in multisite approved list.
220 if ( $this->in_multi_array( $user->user_email, $auth_multisite_settings_access_users_approved ) ) {
221 continue;
222 }
223 // Add to approved list if not there.
224 if ( ! $this->in_multi_array( $user->user_email, $auth_settings_access_users_approved ) ) {
225 $approved_user = array(
226 'email' => $user->user_email,
227 'role' => count( $user->roles ) > 0 ? $user->roles[0] : $default_role,
228 'date_added' => date( 'M Y', strtotime( $user->user_registered ) ),
229 'local_user' => true,
230 );
231 array_push( $auth_settings_access_users_approved, $approved_user );
232 $updated = true;
233 }
234 // Remove from pending list if there.
235 foreach ( $auth_settings_access_users_pending as $key => $pending_user ) {
236 if ( $pending_user['email'] == $user->user_email ) {
237 unset( $auth_settings_access_users_pending[$key] );
238 $updated = true;
239 }
240 }
241 }
242 if ( $updated ) {
243 update_option( 'auth_settings_access_users_pending', $auth_settings_access_users_pending );
244 update_option( 'auth_settings_access_users_approved', $auth_settings_access_users_approved );
245 }
246 }
247
248
249 /**
250 * Plugin deactivation.
251 *
252 * @return void
253 */
254 public function deactivate() {
255 // Do nothing.
256 } // END deactivate()
257
258
259
260 /**
261 * ***************************
262 * External Authentication
263 * ***************************
264 */
265
266
267
268 /**
269 * Authenticate against an external service.
270 *
271 * @param WP_User $user user to authenticate
272 * @param string $username optional username to authenticate.
273 * @param string $password optional password to authenticate.
274 *
275 * @return WP_User or WP_Error
276 */
277 public function custom_authenticate( $user, $username, $password ) {
278 // Pass through if already authenticated.
279 if ( is_a( $user, 'WP_User' ) ) {
280 return $user;
281 } else {
282 $user = null;
283 }
284
285 // If username and password are blank, this isn't a log in attempt
286 $is_login_attempt = strlen( $username ) > 0 && strlen( $password ) > 0;
287
288 // Check to make sure that $username is not locked out due to too
289 // many invalid login attempts. If it is, tell the user how much
290 // time remains until they can try again.
291 $unauthenticated_user = $is_login_attempt ? get_user_by( 'login', $username ) : false;
292 $unauthenticated_user_is_blocked = false;
293 if ( $is_login_attempt && $unauthenticated_user !== false ) {
294 $last_attempt = get_user_meta( $unauthenticated_user->ID, 'auth_settings_advanced_lockouts_time_last_failed', true );
295 $num_attempts = get_user_meta( $unauthenticated_user->ID, 'auth_settings_advanced_lockouts_failed_attempts', true );
296 // Also check the auth_blocked user_meta flag (users in blocked list will get this flag)
297 $unauthenticated_user_is_blocked = get_user_meta( $unauthenticated_user->ID, 'auth_blocked', true ) === 'yes';
298 } else {
299 $last_attempt = get_option( 'auth_settings_advanced_lockouts_time_last_failed' );
300 $num_attempts = get_option( 'auth_settings_advanced_lockouts_failed_attempts' );
301 }
302
303 // Inactive users should be treated like deleted users (we just
304 // do this to preserve any content they created, but here we should
305 // pretend they don't exist).
306 if ( $unauthenticated_user_is_blocked ) {
307 remove_filter( 'authenticate', 'wp_authenticate_username_password', 20, 3 );
308 return new WP_Error( 'empty_password', __( '<strong>ERROR</strong>: Incorrect username or password.' ) );
309 }
310
311 // Grab plugin settings.
312 $auth_settings = $this->get_plugin_options( 'single admin', 'allow override' );
313
314 // Make sure $last_attempt (time) and $num_attempts are positive integers.
315 // Note: this addresses resetting them if either is unset from above.
316 $last_attempt = abs( intval( $last_attempt ) );
317 $num_attempts = abs( intval( $num_attempts ) );
318
319 // Create semantic lockout variables.
320 $lockouts = $auth_settings['advanced_lockouts'];
321 $time_since_last_fail = time() - $last_attempt;
322 $reset_duration = $lockouts['reset_duration'] * 60; // minutes to seconds
323 $num_attempts_long_lockout = $lockouts['attempts_1'] + $lockouts['attempts_2'];
324 $num_attempts_short_lockout = $lockouts['attempts_1'];
325 $seconds_remaining_long_lockout = $lockouts['duration_2'] * 60 - $time_since_last_fail;
326 $seconds_remaining_short_lockout = $lockouts['duration_1'] * 60 - $time_since_last_fail;
327
328 // Check if we need to institute a lockout delay
329 if ( $is_login_attempt && $time_since_last_fail > $reset_duration ) {
330 // Enough time has passed since the last invalid attempt and
331 // now that we can reset the failed attempt count, and let this
332 // login attempt go through.
333 $num_attempts = 0; // This does nothing, but include it for semantic meaning.
334 } elseif ( $is_login_attempt && $num_attempts > $num_attempts_long_lockout && $seconds_remaining_long_lockout > 0 ) {
335 // Stronger lockout (1st/2nd round of invalid attempts reached)
336 // Note: set the error code to 'empty_password' so it doesn't
337 // trigger the wp_login_failed hook, which would continue to
338 // increment the failed attempt count.
339 remove_filter( 'authenticate', 'wp_authenticate_username_password', 20, 3 );
340 return new WP_Error( 'empty_password', sprintf( __( '<strong>ERROR</strong>: There have been too many invalid login attempts for the username <strong>%1$s</strong>. Please wait <strong id="seconds_remaining" data-seconds="%2$s">%3$s</strong> before trying again. <a href="%4$s" title="Password Lost and Found">Lost your password</a>?' ), $username, $seconds_remaining_long_lockout, $this->seconds_as_sentence( $seconds_remaining_long_lockout ), wp_lostpassword_url() ) );
341 } elseif ( $is_login_attempt && $num_attempts > $num_attempts_short_lockout && $seconds_remaining_short_lockout > 0 ) {
342 // Normal lockout (1st round of invalid attempts reached)
343 // Note: set the error code to 'empty_password' so it doesn't
344 // trigger the wp_login_failed hook, which would continue to
345 // increment the failed attempt count.
346 remove_filter( 'authenticate', 'wp_authenticate_username_password', 20, 3 );
347 return new WP_Error( 'empty_password', sprintf( __( '<strong>ERROR</strong>: There have been too many invalid login attempts for the username <strong>%1$s</strong>. Please wait <strong id="seconds_remaining" data-seconds="%2$s">%3$s</strong> before trying again. <a href="%4$s" title="Password Lost and Found">Lost your password</a>?' ), $username, $seconds_remaining_short_lockout, $this->seconds_as_sentence( $seconds_remaining_short_lockout ), wp_lostpassword_url() ) );
348 }
349
350 // Start external authentication.
351 $externally_authenticated_email = '';
352 $authenticated_by = '';
353
354 // Try Google authentication if it's enabled and we don't have a
355 // successful login yet.
356 if ( $auth_settings['google'] === '1' ) {
357 $result = $this->custom_authenticate_google( $auth_settings );
358 if ( ! is_wp_error( $result ) ) {
359 $externally_authenticated_email = $result['email'];
360 $authenticated_by = $result['authenticated_by'];
361 }
362 }
363
364 // Try CAS authentication if it's enabled and we don't have a
365 // successful login yet.
366 if ( $auth_settings['cas'] === '1' && strlen( $externally_authenticated_email ) === 0 ) {
367 $result = $this->custom_authenticate_cas( $auth_settings );
368 if ( ! is_wp_error( $result ) ) {
369 $externally_authenticated_email = $result['email'];
370 $authenticated_by = $result['authenticated_by'];
371 }
372 }
373
374 // Try LDAP authentication if it's enabled and we don't have an
375 // authenticated user yet.
376 if ( $auth_settings['ldap'] === '1' && strlen( $externally_authenticated_email ) === 0 ) {
377 $result = $this->custom_authenticate_ldap( $auth_settings, $username, $password );
378 if ( ! is_wp_error( $result ) ) {
379 $externally_authenticated_email = $result['email'];
380 $authenticated_by = $result['authenticated_by'];
381 }
382 }
383
384 // Skip to WordPress authentication if we don't have an externally
385 // authenticated user.
386 if ( strlen( $externally_authenticated_email ) < 1 ) {
387 return null;
388 }
389
390 // If we've made it this far, we should have an externally
391 // authenticated user. The following should be set:
392 // $externally_authenticated_email
393 // $authenticated_by
394
395 // Get the external user's WordPress account by email address.
396 $user = get_user_by( 'email', $externally_authenticated_email );
397
398 // Check this external user's access against the access lists
399 // (pending, approved, blocked)
400 $result = $this->check_user_access( $user, $externally_authenticated_email, $result );
401
402 // Fail with message if error.
403 if ( is_wp_error( $result ) ) {
404 return $result;
405 }
406
407 // If we created a new user in check_user_access(), log that user in.
408 if ( get_class( $result ) === 'WP_User' ) {
409 $user = $result;
410 }
411
412 // We'll track how this user was authenticated in user meta.
413 if ( $user ) {
414 update_user_meta( $user->ID, 'authenticated_by', $authenticated_by );
415 }
416
417 // If we haven't exited yet, we have a valid/approved user, so authenticate them.
418 return $user;
419 } // END custom_authenticate()
420
421
422 /**
423 * This function will fail with a wp_die() message to the user if they
424 * don't have access.
425 *
426 * @param WP_User $user User to check
427 * @param [type] $user_email User's plaintext email (in case current user doesn't have a WP account)
428 * @param [type] $user_data Array of keys for user email, first_name, last_name, and authenticated_by
429 * @return WP_Error if there was an error on user creation / adding user to blog
430 * wp_die() if user does not have access
431 * null if user has access (success)
432 */
433 private function check_user_access( $user, $user_email, $user_data = array() ) {
434 // Grab plugin settings.
435 $auth_settings = $this->get_plugin_options( 'single admin', 'allow override' );
436 $auth_settings_access_users_pending = $this->sanitize_user_list(
437 $this->get_plugin_option( 'access_users_pending', 'single admin' )
438 );
439 $auth_settings_access_users_approved = $this->sanitize_user_list(
440 array_merge(
441 $this->get_plugin_option( 'access_users_approved', 'single admin' ),
442 $this->get_plugin_option( 'access_users_approved', 'multisite admin' )
443 )
444 );
445
446 // Check our externally authenticated user against the block list.
447 // If they are blocked, set the relevant user meta field, and show
448 // them an error screen.
449 if ( $this->is_email_in_list( $user_email, 'blocked' ) ) {
450 // If the blocked external user has a WordPress account, change
451 // its password and mark it as blocked.
452 if ( $user ) {
453 // Mark user as blocked (enforce block in this->authenticate()).
454 update_user_meta( $user->ID, 'auth_blocked', 'yes' );
455 }
456
457 // Notify user about blocked status and return without authenticating them.
458 $redirect_to = ! empty( $_REQUEST['redirect_to'] ) ? $_REQUEST['redirect_to'] : home_url();
459 $page_title = get_bloginfo( 'name' ) . ' - Access Restricted';
460 $error_message = apply_filters( 'the_content', $auth_settings['access_blocked_redirect_to_message'] );
461 $error_message .= '<hr /><p style="text-align: center;"><a class="button" href="' . wp_logout_url( $redirect_to ) . '">Back</a></p>';
462 update_option( 'auth_settings_advanced_login_error', $error_message );
463 wp_die( $error_message, $page_title );
464 }
465
466 // If this externally authenticated user isn't in the approved list
467 // and login access is set to "All authenticated users," add them
468 // to the approved list (they'll get an account created below if
469 // they don't have one yet).
470 $is_newly_approved_user = false;
471 if ( ! $this->is_email_in_list( $user_email, 'approved' ) && $auth_settings['access_who_can_login'] === 'external_users' ) {
472 $is_newly_approved_user = true;
473
474 // If this user happens to be in the pending list (rare),
475 // remove them from pending before adding them to approved.
476 if ( $this->is_email_in_list( $user_email, 'pending' ) ) {
477 foreach ( $auth_settings_access_users_pending as $key => $pending_user ) {
478 if ( $pending_user['email'] === $user_email ) {
479 unset( $auth_settings_access_users_pending[ $key ] );
480 update_option( 'auth_settings_access_users_pending', $auth_settings_access_users_pending );
481 break;
482 }
483 }
484 }
485
486 // Add this user to the approved list.
487 $approved_role = $user && is_array( $user->roles ) && count( $user->roles ) > 0 ? $user->roles[0] : $auth_settings['access_default_role'];
488 $approved_user = array(
489 'email' => $user_email,
490 'role' => $approved_role,
491 'date_added' => date( "Y-m-d H:i:s" ),
492 );
493 array_push( $auth_settings_access_users_approved, $approved_user );
494 update_option( 'auth_settings_access_users_approved', $auth_settings_access_users_approved );
495 }
496
497 // Check our externally authenticated user against the approved
498 // list. If they are approved, log them in (and create their account
499 // if necessary)
500 if ( $is_newly_approved_user || $this->is_email_in_list( $user_email, 'approved' ) ) {
501 $user_info = $is_newly_approved_user ? $approved_user : $this->get_user_info_from_list( $user_email, $auth_settings_access_users_approved );
502
503 // If the approved external user does not have a WordPress account, create it
504 if ( ! $user ) {
505 // If there's already a user with this username (e.g.,
506 // johndoe/johndoe@gmail.com exists, and we're trying to add
507 // johndoe/johndoe@example.com), use the full email address
508 // as the username.
509 $username = explode( "@", $user_info['email'] );
510 $username = $username[0];
511 if ( get_user_by( 'login', $username ) !== false ) {
512 $username = $approved_user['email'];
513 }
514 $result = wp_insert_user(
515 array(
516 'user_login' => strtolower( $username ),
517 'user_pass' => wp_generate_password(), // random password
518 'first_name' => array_key_exists( 'first_name', $user_data ) ? $user_data['first_name'] : '',
519 'last_name' => array_key_exists( 'last_name', $user_data ) ? $user_data['last_name'] : '',
520 'user_email' => strtolower( $user_info['email'] ),
521 'user_registered' => date( 'Y-m-d H:i:s' ),
522 'role' => $user_info['role'],
523 )
524 );
525
526 // Fail with message if error.
527 if ( is_wp_error( $result ) || $result == 0 ) {
528 return $result;
529 }
530
531 // Authenticate as new user
532 $user = new WP_User( $result );
533
534 // Check if this new user has any preassigned usermeta
535 // values in their approved list entry, and apply them to
536 // their new WordPress account.
537 if ( array_key_exists( 'usermeta', $user_info ) && is_array( $user_info['usermeta'] ) ) {
538 $meta_key = $this->get_plugin_option( 'advanced_usermeta' );
539
540 if ( array_key_exists( 'meta_key', $user_info['usermeta'] ) && array_key_exists( 'meta_value', $user_info['usermeta'] ) ) {
541 // Only update the usermeta if the stored value matches
542 // the option set in authorizer settings (if they don't
543 // match it's probably old data).
544 if ( $meta_key === $user_info['usermeta']['meta_key'] ) {
545 // Update user's usermeta value for usermeta key stored in authorizer options.
546 if ( strpos( $meta_key, 'acf___' ) === 0 && class_exists( 'acf' ) ) {
547 // We have an ACF field value, so use the ACF function to update it.
548 update_field( str_replace('acf___', '', $meta_key ), $user_info['usermeta']['meta_value'], 'user_' . $user->ID );
549 } else {
550 // We have a normal usermeta value, so just update it via the WordPress function.
551 update_user_meta( $user->ID, $meta_key, $user_info['usermeta']['meta_value'] );
552 }
553 }
554 } elseif ( is_multisite() && count( $user_info['usermeta'] ) > 0 ) {
555 // Update usermeta for each multisite blog defined for this user.
556 foreach ( $user_info['usermeta'] as $blog_id => $usermeta ) {
557 if ( array_key_exists( 'meta_key', $usermeta ) && array_key_exists( 'meta_value', $usermeta ) ) {
558 // Add this new user to the blog before we create their user meta (this step typically happens below, but we need it to happen early so we can create user meta here).
559 if ( ! is_user_member_of_blog( $user->ID, $blog_id ) ) {
560 add_user_to_blog( $blog_id, $user->ID, $user_info['role'] );
561 }
562 switch_to_blog( $blog_id );
563 // Update user's usermeta value for usermeta key stored in authorizer options.
564 if ( strpos( $meta_key, 'acf___' ) === 0 && class_exists( 'acf' ) ) {
565 // We have an ACF field value, so use the ACF function to update it.
566 update_field( str_replace('acf___', '', $meta_key ), $usermeta['meta_value'], 'user_' . $user->ID );
567 } else {
568 // We have a normal usermeta value, so just update it via the WordPress function.
569 update_user_meta( $user->ID, $meta_key, $usermeta['meta_value'] );
570 }
571 restore_current_blog();
572 }
573 }
574 }
575 }
576 } else {
577 // Update first/last names of WordPress user from external
578 // service if that option is set.
579 if ( ( array_key_exists( 'authenticated_by', $user_data ) && $user_data['authenticated_by'] === 'cas' && array_key_exists( 'cas_attr_update_on_login', $auth_settings ) && $auth_settings['cas_attr_update_on_login'] == 1 ) || ( array_key_exists( 'authenticated_by', $user_data ) && $user_data['authenticated_by'] === 'ldap' && array_key_exists( 'ldap_attr_update_on_login', $auth_settings ) && $auth_settings['ldap_attr_update_on_login'] == 1 ) ) {
580 if ( array_key_exists( 'first_name', $user_data ) && strlen( $user_data['first_name'] ) > 0 ) {
581 wp_update_user( array(
582 'ID' => $user->ID,
583 'first_name' => $user_data['first_name'],
584 ));
585 }
586 if ( array_key_exists( 'last_name', $user_data ) && strlen( $user_data['last_name'] ) > 0 ) {
587 wp_update_user( array(
588 'ID' => $user->ID,
589 'last_name' => $user_data['last_name'],
590 ));
591 }
592 }
593 }
594
595 // If this is multisite, add new user to current blog.
596 if ( is_multisite() && ! is_user_member_of_blog( $user->ID ) ) {
597 $result = add_user_to_blog( get_current_blog_id(), $user->ID, $user_info['role'] );
598
599 // Fail with message if error.
600 if ( is_wp_error( $result ) ) {
601 return $result;
602 }
603 }
604
605 // Ensure user has the same role as their entry in the approved list.
606 // (This is just a precaution, the role should already be set when
607 // saving admin options in the sanitizing function.)
608 if ( $user_info && ! array_key_exists( $user_info['role'], $user->roles ) ) {
609 $user->set_role( $user_info['role'] );
610 }
611
612 return $user;
613
614 } elseif ( $user && in_array( 'administrator', $user->roles ) ) {
615 // User has a WordPress account, but is not in the blocked or approved
616 // list. If they are an administrator, let them in.
617 return;
618 } else {
619 // User isn't an admin, is not blocked, and is not approved.
620 // Add them to the pending list and notify them and their instructor.
621 if ( strlen( $user_email ) > 0 && ! $this->is_email_in_list( $user_email, 'pending' ) ) {
622 $pending_user = array();
623 $pending_user['email'] = $user_email;
624 $pending_user['role'] = $auth_settings['access_default_role'];
625 $pending_user['date_added'] = '';
626 array_push( $auth_settings_access_users_pending, $pending_user );
627 update_option( 'auth_settings_access_users_pending', $auth_settings_access_users_pending );
628
629 // Create strings used in the email notification.
630 $site_name = get_bloginfo( 'name' );
631 $site_url = get_bloginfo( 'url' );
632 $authorizer_options_url = $auth_settings['advanced_admin_menu'] === 'settings' ? admin_url( 'options-general.php?page=authorizer' ) : admin_url( '?page=authorizer' );
633
634 // Notify instructor about new pending user if that option is set.
635 foreach ( get_users( array( 'role' => $auth_settings['access_role_receive_pending_emails'] ) ) as $user_recipient ) {
636 wp_mail(
637 $user_recipient->user_email,
638 "Action required: Pending user {$pending_user['email']} at $site_name",
639 "A new user has tried to access the $site_name site you manage at:\n$site_url\n\n" .
640 "Please log in to approve or deny their request:\n$authorizer_options_url\n"
641 );
642 }
643 }
644
645 // Notify user about pending status and return without authenticating them.
646 $redirect_to = ! empty( $_REQUEST['redirect_to'] ) ? $_REQUEST['redirect_to'] : home_url();
647 $page_title = get_bloginfo( 'name' ) . ' - Access Pending';
648 $error_message = apply_filters( 'the_content', $auth_settings['access_pending_redirect_to_message'] );
649 $error_message .= '<hr /><p style="text-align: center;"><a class="button" href="' . wp_logout_url( $redirect_to ) . '">Back</a></p>';
650 update_option( 'auth_settings_advanced_login_error', $error_message );
651 wp_die( $error_message, $page_title );
652 }
653
654 } // END check_user_access()
655
656
657 /**
658 * Verify the Google login and set a session token.
659 *
660 * Flow: "Sign in with Google" button clicked; JS Google library
661 * called; JS function signInCallback() fired with results from Google;
662 * signInCallback() posts code and nonce (via AJAX) to this function;
663 * This function checks the token using the Google PHP library, and
664 * saves it to a session variable if it's authentic; control passes
665 * back to signInCallback(), which will reload the current page
666 * (wp-login.php) on success; wp-login.php reloads; custom_authenticate
667 * hooked into authenticate action fires again, and
668 * custom_authenticate_google() runs to verify the token; once verified
669 * custom_authenticate proceeds as normal with the google email address
670 * as a successfully authenticated external user.
671 *
672 * @return void, but die with the value to return to the success() function in AJAX call signInCallback()
673 */
674 function ajax_process_google_login() {
675 $nonce = array_key_exists( 'nonce', $_POST ) ? $_POST['nonce'] : '';
676 $code = array_key_exists( 'code', $_POST ) ? $_POST['code'] : null;
677
678 // Nonce check.
679 if ( ! wp_verify_nonce( $nonce, 'google_csrf_nonce' ) ) {
680 return '';
681 }
682
683 // Grab plugin settings.
684 $auth_settings = $this->get_plugin_options( 'single admin', 'allow override' );
685
686 // Build the Google Client.
687 $client = new Google_Client();
688 $client->setApplicationName( 'WordPress' );
689 $client->setClientId( $auth_settings['google_clientid'] );
690 $client->setClientSecret( $auth_settings['google_clientsecret'] );
691 $client->setRedirectUri( 'postmessage' );
692
693 // Get one time use token (if it doesn't exist, we'll create one below)
694 session_start();
695 $token = array_key_exists( 'token', $_SESSION ) ? json_decode( $_SESSION['token'] ) : null;
696
697 if ( empty( $token ) ) {
698 // Exchange the OAuth 2.0 authorization code for user credentials.
699 $client->authenticate( $code );
700 $token = json_decode( $client->getAccessToken() );
701
702 // Store the token in the session for later use.
703 $_SESSION['token'] = json_encode( $token );
704
705 $response = "Successfully authenticated.";
706 } else {
707 $client->setAccessToken( json_encode( $token ) );
708
709 $response = 'Already authenticated.';
710 }
711
712 die( $response );
713 } // END ajax_process_google_login()
714
715
716 /**
717 * Validate this user's credentials against Google.
718 *
719 * @param array $auth_settings Plugin settings
720 * @return [mixed] Array containing 'email' and 'authenticated_by'
721 * strings for the successfully authenticated
722 * user, or WP_Error() object on failure.
723 */
724 private function custom_authenticate_google( $auth_settings ) {
725 // Get one time use token
726 session_start();
727 $token = array_key_exists( 'token', $_SESSION ) ? json_decode( $_SESSION['token'] ) : null;
728
729 // No token, so this is not a succesful Google login.
730 if ( is_null( $token ) ) {
731 return new WP_Error( 'no_google_login', 'No Google credentials provided.' );
732 }
733
734 // Build the Google Client.
735 $client = new Google_Client();
736 $client->setApplicationName( 'WordPress' );
737 $client->setClientId( $auth_settings['google_clientid'] );
738 $client->setClientSecret( $auth_settings['google_clientsecret'] );
739 $client->setRedirectUri( 'postmessage' );
740
741 // Verify this is a successful Google authentication
742 $ticket = $client->verifyIdToken( $token->id_token, $auth_settings['google_clientid'] );
743
744 // Invalid ticket, so this in not a successful Google login.
745 if ( ! $ticket ) {
746 return new WP_Error( 'invalid_google_login', 'Invalid Google credentials provided.' );
747 }
748
749 // Get email address
750 $attributes = $ticket->getAttributes();
751 $email = $attributes['payload']['email'];
752
753 return array(
754 'email' => $email,
755 'first_name' => '',
756 'last_name' => '',
757 'authenticated_by' => 'google',
758 );
759 } // END custom_authenticate_google()
760
761
762 /**
763 * Validate this user's credentials against CAS.
764 *
765 * @param array $auth_settings Plugin settings
766 * @return [mixed] Array containing 'email' and 'authenticated_by'
767 * strings for the successfully authenticated
768 * user, or WP_Error() object on failure.
769 */
770 private function custom_authenticate_cas( $auth_settings ) {
771 // Move on if CAS hasn't been requested here.
772 if ( empty( $_GET['external'] ) || $_GET['external'] !== 'cas' ) {
773 return new WP_Error( 'cas_not_available', 'CAS is not enabled.' );
774 }
775
776 // Set the CAS client configuration
777 phpCAS::client( SAML_VERSION_1_1, $auth_settings['cas_host'], intval( $auth_settings['cas_port'] ), $auth_settings['cas_path'] );
778
779 // Update server certificate bundle if it doesn't exist or is older
780 // than 3 months, then use it to ensure CAS server is legitimate.
781 $cacert_path = plugin_dir_path( __FILE__ ) . 'inc/cacert.pem';
782 $time_90_days = 90 * 24 * 60 * 60; // days * hours * minutes * seconds
783 $time_90_days_ago = time() - $time_90_days;
784 if ( ! file_exists( $cacert_path ) || filemtime( $cacert_path ) < $time_90_days_ago ) {
785 $cacert_contents = file_get_contents( 'http://curl.haxx.se/ca/cacert.pem' );
786 if ( $cacert_contents !== false ) {
787 file_put_contents( $cacert_path, $cacert_contents );
788 } else {
789 return new WP_Error( 'cannot_update_cacert', 'Unable to update outdated server certificates from http://curl.haxx.se/ca/cacert.pem.' );
790 }
791 }
792 phpCAS::setCasServerCACert( $cacert_path );
793
794 // Authenticate against CAS
795 if ( ! phpCAS::isAuthenticated() ) {
796 phpCAS::forceAuthentication();
797 die();
798 }
799
800 // Get the TLD from the CAS host for use in matching email addresses
801 // For example: example.edu is the TLD for authn.example.edu, so user
802 // 'bob' will have the following email address: bob@example.edu.
803 $tld = preg_match( '/[^.]*\.[^.]*$/', $auth_settings['cas_host'], $matches ) === 1 ? $matches[0] : '';
804
805 // Get username that successfully authenticated against the external service (CAS).
806 $externally_authenticated_email = strtolower( phpCAS::getUser() ) . '@' . $tld;
807
808 // Retrieve the user attributes (e.g., email address, first name, last name) from the CAS server.
809 $cas_attributes = phpCAS::getAttributes();
810
811 // If a CAS attribute has been specified as containing the email address, use that instead.
812 if ( array_key_exists( 'cas_attr_email', $auth_settings ) && strlen( $auth_settings['cas_attr_email'] ) > 0 && array_key_exists( $auth_settings['cas_attr_email'], $cas_attributes ) && strlen( $cas_attributes[$auth_settings['cas_attr_email']] ) > 0 ) {
813 $externally_authenticated_email = $cas_attributes[$auth_settings['cas_attr_email']];
814 }
815
816 // Get user first name and last name.
817 $first_name = array_key_exists( 'cas_attr_first_name', $auth_settings ) && strlen( $auth_settings['cas_attr_first_name'] ) > 0 && array_key_exists( $auth_settings['cas_attr_first_name'], $cas_attributes ) && strlen( $cas_attributes[$auth_settings['cas_attr_first_name']] ) > 0 ? $cas_attributes[$auth_settings['cas_attr_first_name']] : '';
818 $last_name = array_key_exists( 'cas_attr_last_name', $auth_settings ) && strlen( $auth_settings['cas_attr_last_name'] ) > 0 && array_key_exists( $auth_settings['cas_attr_last_name'], $cas_attributes ) && strlen( $cas_attributes[$auth_settings['cas_attr_last_name']] ) > 0 ? $cas_attributes[$auth_settings['cas_attr_last_name']] : '';
819
820 return array(
821 'email' => $externally_authenticated_email,
822 'first_name' => $first_name,
823 'last_name' => $last_name,
824 'authenticated_by' => 'cas',
825 );
826 } // END custom_authenticate_cas()
827
828
829 /**
830 * Validate this user's credentials against LDAP.
831 *
832 * @param array $auth_settings Plugin settings
833 * @param string $username Attempted username from authenticate action
834 * @param string $password Attempted password from authenticate action
835 * @return [mixed] Array containing 'email' and 'authenticated_by'
836 * strings for the successfully authenticated
837 * user, or WP_Error() object on failure.
838 */
839 private function custom_authenticate_ldap( $auth_settings, $username, $password ) {
840 // Get the TLD from the LDAP host for use in matching email addresses
841 // For example: example.edu is the TLD for ldap.example.edu, so user
842 // 'bob' will have the following email address: bob@example.edu.
843 $tld = preg_match( '/[^.]*\.[^.]*$/', $auth_settings['ldap_host'], $matches ) === 1 ? $matches[0] : '';
844
845 // remove top level domain if it exists in the username (i.e., if user entered their email)
846 $username = str_replace( '@' . $tld, '', $username );
847
848 // Fail with error message if username or password is blank.
849 if ( empty( $username ) ) {
850 return null;
851 }
852 if ( empty( $password ) ) {
853 return new WP_Error( 'empty_password', 'You must provide a password.' );
854 }
855
856 // Make sure php5-ldap extension is installed on server.
857 if ( ! function_exists( 'ldap_connect' ) ) {
858 // Note: this error message won't get shown to the user because
859 // authenticate will fall back to WP auth when this fails.
860 return new WP_Error( 'ldap_not_installed', 'LDAP logins are disabled because this server does not support them.' );
861 }
862
863 // Authenticate against LDAP using options provided in plugin settings.
864 $result = false;
865 $ldap_user_dn = '';
866 $first_name = '';
867 $last_name = '';
868 $email = '';
869
870 // Establish LDAP connection.
871 $ldap = ldap_connect( $auth_settings['ldap_host'], $auth_settings['ldap_port'] );
872 ldap_set_option( $ldap, LDAP_OPT_PROTOCOL_VERSION, 3 );
873 if ( $auth_settings['ldap_tls'] == 1 ) {
874 ldap_start_tls( $ldap );
875 }
876
877 // Set bind credentials; attempt an anonymous bind if not provided.
878 $bind_rdn = NULL;
879 $bind_password = NULL;
880 if ( strlen( $auth_settings['ldap_user'] ) > 0 ) {
881 $bind_rdn = $auth_settings['ldap_user'];
882 $bind_password = $this->decrypt( base64_decode( $auth_settings['ldap_password'] ) );
883 }
884
885 // Attempt LDAP bind.
886 $result = @ldap_bind( $ldap, $bind_rdn, $bind_password );
887 if ( ! $result ) {
888 // Can't connect to LDAP, so fall back to WordPress authentication.
889 return new WP_Error( 'ldap_error', 'Could not authenticate using LDAP.' );
890 }
891 // Look up the bind DN (and first/last name) of the user trying to
892 // log in by performing an LDAP search for the login username in
893 // the field specified in the LDAP settings. This setup is common.
894 $ldap_attributes_to_retrieve = array( 'dn' );
895 if ( array_key_exists( 'ldap_attr_first_name', $auth_settings ) && strlen( $auth_settings['ldap_attr_first_name'] ) > 0 ) {
896 array_push( $ldap_attributes_to_retrieve, $auth_settings['ldap_attr_first_name'] );
897 }
898 if ( array_key_exists( 'ldap_attr_last_name', $auth_settings ) && strlen( $auth_settings['ldap_attr_last_name'] ) > 0 ) {
899 array_push( $ldap_attributes_to_retrieve, $auth_settings['ldap_attr_last_name'] );
900 }
901 if ( array_key_exists( 'ldap_attr_email', $auth_settings ) && strlen( $auth_settings['ldap_attr_email'] ) > 0 ) {
902 array_push( $ldap_attributes_to_retrieve, $auth_settings['ldap_attr_email'] );
903 }
904 $ldap_search = ldap_search(
905 $ldap,
906 $auth_settings['ldap_search_base'],
907 "(" . $auth_settings['ldap_uid'] . "=" . $username . ")",
908 $ldap_attributes_to_retrieve
909 );
910 $ldap_entries = ldap_get_entries( $ldap, $ldap_search );
911
912 // If we didn't find any users in ldap, exit with error (rely on default wordpress authentication)
913 if ( $ldap_entries['count'] < 1 ) {
914 return new WP_Error( 'no_ldap', 'No LDAP user found.' );
915 }
916
917 // Get the bind dn and first/last names; if there are multiple results returned, just get the last one.
918 for ( $i = 0; $i < $ldap_entries['count']; $i++ ) {
919 $ldap_user_dn = $ldap_entries[$i]['dn'];
920
921 // Get user first name and last name.
922 if ( array_key_exists( 'ldap_attr_first_name', $auth_settings ) && strlen( $auth_settings['ldap_attr_first_name'] ) > 0 && array_key_exists( $auth_settings['ldap_attr_first_name'], $ldap_entries[$i] ) && $ldap_entries[$i][$auth_settings['ldap_attr_first_name']]['count'] > 0 && strlen( $ldap_entries[$i][$auth_settings['ldap_attr_first_name']][0] ) > 0 ) {
923 $first_name = $ldap_entries[$i][$auth_settings['ldap_attr_first_name']][0];
924 }
925 if ( array_key_exists( 'ldap_attr_last_name', $auth_settings ) && strlen( $auth_settings['ldap_attr_last_name'] ) > 0 && array_key_exists( $auth_settings['ldap_attr_last_name'], $ldap_entries[$i] ) && $ldap_entries[$i][$auth_settings['ldap_attr_last_name']]['count'] > 0 && strlen( $ldap_entries[$i][$auth_settings['ldap_attr_last_name']][0] ) > 0 ) {
926 $last_name = $ldap_entries[$i][$auth_settings['ldap_attr_last_name']][0];
927 }
928 // Get user email if it is specified in another field.
929 if ( array_key_exists( 'ldap_attr_email', $auth_settings ) && strlen( $auth_settings['ldap_attr_email'] ) > 0 && array_key_exists( $auth_settings['ldap_attr_email'], $ldap_entries[$i] ) && $ldap_entries[$i][$auth_settings['ldap_attr_email']]['count'] > 0 && strlen( $ldap_entries[$i][$auth_settings['ldap_attr_email']][0] ) > 0 ) {
930 $email = $ldap_entries[$i][$auth_settings['ldap_attr_email']][0];
931 }
932 }
933
934 $result = @ldap_bind( $ldap, $ldap_user_dn, $password );
935 if ( ! $result ) {
936 // We have a real ldap user, but an invalid password. Pass
937 // through to wp authentication after failing LDAP (since
938 // this could be a local account that happens to be the
939 // same name as an LDAP user).
940 return new WP_Error( 'using_wp_authentication', 'Moving on to WordPress authentication...' );
941 }
942
943 // User successfully authenticated against LDAP, so set the relevant variables.
944 $externally_authenticated_email = $username . '@' . $tld;
945
946 // If an LDAP attribute has been specified as containing the email address, use that instead.
947 if ( strlen( $email ) > 0 ) {
948 $externally_authenticated_email = $email;
949 }
950
951 return array(
952 'email' => $externally_authenticated_email,
953 'first_name' => $first_name,
954 'last_name' => $last_name,
955 'authenticated_by' => 'ldap',
956 );
957 } // END custom_authenticate_ldap()
958
959
960 /**
961 * Log out of the attached external service.
962 *
963 * @return void
964 */
965 public function custom_logout() {
966 // Grab plugin settings.
967 $auth_settings = $this->get_plugin_options( 'single admin', 'allow override' );
968
969 // Reset option containing old error messages.
970 delete_option( 'auth_settings_advanced_login_error' );
971
972 if ( session_id() == '' ) {
973 session_start();
974 }
975
976 $current_user_authenticated_by = get_user_meta( get_current_user_id(), 'authenticated_by', true );
977
978 // If logged in to CAS, Log out of CAS.
979 if ( $current_user_authenticated_by === 'cas' && $auth_settings['cas'] === '1' ) {
980 if ( ! array_key_exists( 'PHPCAS_CLIENT', $GLOBALS ) || ! array_key_exists( 'phpCAS', $_SESSION ) ) {
981 // Set the CAS client configuration if it hasn't been set already.
982 phpCAS::client( SAML_VERSION_1_1, $auth_settings['cas_host'], intval( $auth_settings['cas_port'] ), $auth_settings['cas_path'] );
983 // Restrict logout request origin to the CAS server only (prevent DDOS).
984 phpCAS::handleLogoutRequests( true, array( $auth_settings['cas_host'] ) );
985 }
986 if ( phpCAS::isAuthenticated() ) {
987 phpCAS::logoutWithRedirectService( get_option( 'siteurl' ) );
988 }
989 }
990
991 // If session token set, log out of Google.
992 if ( $current_user_authenticated_by === 'google' && array_key_exists( 'token', $_SESSION ) ) {
993 $token = json_decode( $_SESSION['token'] )->access_token;
994
995 // Build the Google Client.
996 $client = new Google_Client();
997 $client->setApplicationName( 'WordPress' );
998 $client->setClientId( $auth_settings['google_clientid'] );
999 $client->setClientSecret( $auth_settings['google_clientsecret'] );
1000 $client->setRedirectUri( 'postmessage' );
1001
1002 // Revoke the token
1003 $client->revokeToken( $token );
1004
1005 // Remove the credentials from the user's session.
1006 $_SESSION['token'] = '';
1007 }
1008
1009 } // END custom_logout()
1010
1011
1012
1013 /**
1014 * ***************************
1015 * Access Restriction
1016 * ***************************
1017 */
1018
1019
1020
1021 /**
1022 * Restrict access to WordPress site based on settings (everyone, logged_in_users).
1023 * Hook: parse_request http://codex.wordpress.org/Plugin_API/Action_Reference/parse_request
1024 *
1025 * @param array $wp WordPress object.
1026 *
1027 * @return void
1028 */
1029 public function restrict_access( $wp ) {
1030 remove_action( 'parse_request', array( $this, 'restrict_access' ), 1 ); // only need it the first time
1031
1032 // Grab plugin settings.
1033 $auth_settings = $this->get_plugin_options( 'single admin', 'allow override' );
1034
1035 $has_access = (
1036 // Always allow access if WordPress is installing
1037 ( defined( 'WP_INSTALLING' ) && isset( $_GET['key'] ) ) ||
1038 // Always allow access to admins
1039 ( current_user_can( 'create_users' ) ) ||
1040 // Allow access if option is set to 'everyone'
1041 ( $auth_settings['access_who_can_view'] == 'everyone' ) ||
1042 // Allow access to approved external users and logged in users if option is set to 'logged_in_users'
1043 ( $auth_settings['access_who_can_view'] == 'logged_in_users' && $this->is_user_logged_in_and_blog_user() )
1044 );
1045
1046 /**
1047 * Developers can use the `authorizer_has_access` filter
1048 * to override restricted access on certain pages. Note that the
1049 * restriction checks happens before WordPress executes any queries, so
1050 * use the global `$wp` variable to investigate what the visitor is
1051 * trying to load.
1052 *
1053 * For example, to unblock an RSS feed, place the following PHP code in
1054 * the theme's functions.php file or in a simple plug-in:
1055 *
1056 * function my_rsa_feed_access_override( $has_access ) {
1057 * global $wp;
1058 * // check query variables to see if this is the feed
1059 * if ( ! empty( $wp->query_vars['feed'] ) )
1060 * $has_access = true;
1061 * return $has_access;
1062 * }
1063 * add_filter( 'authorizer_has_access', 'my_rsa_feed_access_override' );
1064 */
1065 if ( apply_filters( 'authorizer_has_access', $has_access, $wp ) === true ) {
1066 // Turn off the public notice about browsing anonymously
1067 update_option( 'auth_settings_advanced_public_notice', false );
1068
1069 // We've determined that the current user has access, so simply return to grant access.
1070 return;
1071 }
1072
1073 // We've determined that the current user doesn't have access, so we deal with them now.
1074
1075 // Fringe case: In a multisite, a user of a different blog can
1076 // successfully log in, but they aren't on the 'approved' whitelist
1077 // for this blog. Flag these users, and redirect them to their
1078 // profile page with a message (so we don't get into a redirect
1079 // loop on the wp-login.php page).
1080 if ( is_multisite() && is_user_logged_in() && ! $has_access ) {
1081 $current_user = wp_get_current_user();
1082
1083 // Check user access; block if not, add them to pending list if open, let them through otherwise.
1084 $result = $this->check_user_access( $current_user, $current_user->user_email );
1085 }
1086
1087 // Check to see if the requested page is public. If so, show it.
1088 $current_page_name = property_exists( $wp, 'query_vars' ) && array_key_exists( 'name', $wp->query_vars ) && strlen( $wp->query_vars['name'] ) > 0 ? $wp->query_vars['name'] : '';
1089 if ( ! $current_page_name ) {
1090 // Different WordPress versions store the page slug in different places; look for it elsewhere.
1091 if ( property_exists( $wp, 'query_vars' ) && array_key_exists( 'pagename', $wp->query_vars ) && strlen( $wp->query_vars['pagename'] ) > 0 ) {
1092 $current_page_name = $wp->query_vars['pagename'];
1093 }
1094 }
1095 $current_page_id = empty( $wp->request ) ? 'home' : $this->get_id_from_pagename( $current_page_name );
1096 if ( ! is_array( $auth_settings['access_public_pages'] ) ) {
1097 $auth_settings['access_public_pages'] = array();
1098 }
1099 if ( in_array( $current_page_id, $auth_settings['access_public_pages'] ) ) {
1100 if ( $auth_settings['access_public_warning'] === 'no_warning' ) {
1101 update_option( 'auth_settings_advanced_public_notice', false );
1102 } else {
1103 update_option( 'auth_settings_advanced_public_notice', true );
1104 }
1105 return;
1106 }
1107
1108 // Check to see if any category assigned to the requested page is public. If so, show it.
1109 $current_page_categories = wp_get_post_categories( $current_page_id, array( 'fields' => 'slugs' ) );
1110 foreach( $current_page_categories as $current_page_category ) {
1111 if ( in_array( 'cat_' . $current_page_category, $auth_settings['access_public_pages'] ) ) {
1112 if ( $auth_settings['access_public_warning'] === 'no_warning' ) {
1113 update_option( 'auth_settings_advanced_public_notice', false );
1114 } else {
1115 update_option( 'auth_settings_advanced_public_notice', true );
1116 }
1117 return;
1118 }
1119 }
1120
1121 $current_path = empty( $_SERVER['REQUEST_URI'] ) ? home_url() : $_SERVER['REQUEST_URI'];
1122 if ( $auth_settings['access_redirect'] === 'message' ) {
1123 $page_title = get_bloginfo( 'name' ) . ' - Access Restricted';
1124 $error_message = apply_filters( 'the_content', $auth_settings['access_redirect_to_message'] );
1125 $error_message .= '<hr /><p style="text-align:center;margin-bottom:-15px;"><a class="button" href="' . wp_login_url( $current_path ) . '">Log In</a></p>';
1126 wp_die( $error_message, $page_title );
1127 } else { // if ( $auth_settings['access_redirect'] === 'login' ) {
1128 wp_redirect( wp_login_url( $current_path ), 302 );
1129 exit;
1130 }
1131
1132 // Sanity check: we should never get here
1133 wp_die( '<p>Access denied.</p>', 'Site Access Restricted' );
1134 } // END restrict_access()
1135
1136
1137
1138 /**
1139 * ***************************
1140 * Login page (wp-login.php)
1141 * ***************************
1142 */
1143
1144
1145
1146 /**
1147 * Add custom error message to login screen.
1148 * Filter: login_errors
1149 */
1150 function show_advanced_login_error( $errors ) {
1151 $error = get_option( 'auth_settings_advanced_login_error' );
1152 delete_option( 'auth_settings_advanced_login_error' );
1153
1154 //$errors .= ' ' . $error . "<br />\n";
1155 $errors = ' ' . $error . "<br />\n";
1156 return $errors;
1157 } // END show_advance_login_error()
1158
1159
1160 /**
1161 * Load external resources for the public-facing site.
1162 */
1163 function auth_public_scripts() {
1164 // Load (and localize) public scripts
1165 $current_path = empty( $_SERVER['REQUEST_URI'] ) ? home_url() : $_SERVER['REQUEST_URI'];
1166 wp_enqueue_script( 'auth_public_scripts', plugins_url( '/js/authorizer-public.js', __FILE__ ), array(), '2.3.2' );
1167 $auth_localized = array(
1168 'wp_login_url' => wp_login_url( $current_path ),
1169 'public_warning' => get_option( 'auth_settings_advanced_public_notice' )
1170 );
1171 wp_localize_script( 'auth_public_scripts', 'auth', $auth_localized );
1172 //update_option( 'auth_settings_advanced_public_notice', false);
1173
1174 // Load public css
1175 wp_register_style( 'authorizer-public-css', plugins_url( 'css/authorizer-public.css', __FILE__ ), array(), '2.3.2' );
1176 wp_enqueue_style( 'authorizer-public-css' );
1177 } // END auth_public_scripts()
1178
1179
1180 /**
1181 * Enqueue JS scripts and CSS styles appearing on wp-login.php.
1182 *
1183 * @return void
1184 */
1185 function login_enqueue_scripts_and_styles() {
1186 // Grab plugin settings.
1187 $auth_settings = $this->get_plugin_options( 'single admin', 'allow override' );
1188
1189 // Enqueue scripts appearing on wp-login.php.
1190 wp_enqueue_script( 'auth_login_scripts', plugins_url( '/js/authorizer-login.js', __FILE__ ), array( 'jquery' ), '2.3.2' );
1191
1192 // Enqueue styles appearing on wp-login.php.
1193 wp_register_style( 'authorizer-login-css', plugins_url( '/css/authorizer-login.css', __FILE__ ), array(), '2.3.2' );
1194 wp_enqueue_style( 'authorizer-login-css' );
1195
1196 /**
1197 * Developers can use the `authorizer_add_branding_option` filter
1198 * to add a radio button for "Custom WordPress login branding"
1199 * under the "Advanced" tab in Authorizer options. Example:
1200 *
1201 * function my_authorizer_add_branding_option( $branding_options ) {
1202 * $new_branding_option = array(
1203 * 'value' => 'your_brand'
1204 * 'description' => 'Custom Your Brand Login Screen',
1205 * 'css_url' => 'http://url/to/your_brand.css',
1206 * 'js_url' => 'http://url/to/your_brand.js',
1207 * );
1208 * array_push( $branding_options, $new_branding_option );
1209 * return $branding_options;
1210 * }
1211 * add_filter( 'authorizer_add_branding_option', 'my_authorizer_add_branding_option' );
1212 */
1213 $branding_options = array();
1214 $branding_options = apply_filters( 'authorizer_add_branding_option', $branding_options );
1215 foreach ( $branding_options as $branding_option ) {
1216 // Make sure the custom brands have the required values
1217 if ( ! ( is_array( $branding_option ) && array_key_exists( 'value', $branding_option ) && array_key_exists( 'css_url', $branding_option ) && array_key_exists( 'js_url', $branding_option ) ) ) {
1218 continue;
1219 }
1220 if ( $auth_settings['advanced_branding'] === $branding_option['value'] ) {
1221 wp_enqueue_script( 'auth_login_custom_scripts-' . sanitize_title( $branding_option['value'] ), $branding_option['js_url'], array( 'jquery' ), '2.3.2' );
1222 wp_register_style( 'authorizer-login-custom-css-' . sanitize_title( $branding_option['value'] ), $branding_option['css_url'], array(), '2.3.2' );
1223 wp_enqueue_style( 'authorizer-login-custom-css-' . sanitize_title( $branding_option['value'] ) );
1224 }
1225 }
1226
1227 // If we're using Google logins, load those resources.
1228 if ( $auth_settings['google'] === '1' ) {
1229 wp_enqueue_script( 'authorizer-login-custom-google', plugins_url( '/js/authorizer-login-custom_google.js', __FILE__ ), array( 'jquery' ), '2.3.2' ); ?>
1230 <meta name="google-signin-clientid" content="<?php echo $auth_settings['google_clientid']; ?>" />
1231 <meta name="google-signin-scope" content="email" />
1232 <meta name="google-signin-cookiepolicy" content="single_host_origin" />
1233 <?php
1234 }
1235 } // END login_enqueue_scripts_and_styles()
1236
1237
1238 /**
1239 * Load external resources in the footer of the wp-login.php page.
1240 * Run on action hook: login_footer
1241 */
1242 function load_login_footer_js() {
1243 // Grab plugin settings.
1244 $auth_settings = $this->get_plugin_options( 'single admin', 'allow override' ); ?>
1245 <?php if ( $auth_settings['google'] === '1' ): ?>
1246 <script type="text/javascript">
1247 // Reload login page if reauth querystring param exists,
1248 // since reauth interrupts external logins (e.g., google).
1249 if ( location.search.indexOf( 'reauth=1' ) >= 0 ) {
1250 location.href = location.href.replace( 'reauth=1', '' );
1251 }
1252
1253 function signInCallback( authResult ) {
1254 var $ = jQuery;
1255 if ( authResult['status'] && authResult['status']['signed_in'] ) {
1256 // Hide the sign-in button now that the user is authorized, for example:
1257 $( '#googleplus_button' ).attr( 'style', 'display: none' );
1258
1259 // Send the code to the server
1260 var ajaxurl = '<?php echo admin_url( "admin-ajax.php" ); ?>';
1261 $.post(ajaxurl, {
1262 action: 'process_google_login',
1263 'code': authResult['code'],
1264 'nonce': $('#nonce_google_auth-<?php echo $this->get_cookie_value(); ?>').val(),
1265 }, function( response ) {
1266 // Handle or verify the server response if necessary.
1267 //console.log( response );
1268
1269 // Reload wp-login.php to continue the authentication process.
1270 location.reload();
1271 });
1272 } else {
1273 // Update the app to reflect a signed out user
1274 // Possible error values:
1275 // "user_signed_out" - User is signed-out
1276 // "access_denied" - User denied access to your app
1277 // "immediate_failed" - Could not automatically log in the user
1278 //console.log('Sign-in state: ' + authResult['error']);
1279 }
1280 }
1281 </script>
1282 <?php endif;
1283 } // END load_login_footer_js()
1284
1285
1286 /**
1287 * Create links for any external authentication services that are enabled.
1288 */
1289 function login_form_add_external_service_links() {
1290 // Grab plugin settings.
1291 $auth_settings = $this->get_plugin_options( 'single admin', 'allow override' );
1292
1293 $auth_url_cas = '';
1294 if ( $auth_settings['cas'] === '1' ) {
1295 $auth_url_cas = 'http' . ( isset( $_SERVER['HTTPS'] ) ? 's' : '' ) . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
1296 // Remove force reauth param if it exists so this
1297 // authentication attempt doesn't get stopped by WordPress.
1298 if ( strpos( $auth_url_cas, 'reauth=1' ) !== false ) {
1299 if ( strpos( $auth_url_cas, '&reauth=1' ) !== false ) {
1300 // There are parames before reauth, so just remove reauth
1301 $auth_url_cas = str_replace( '&reauth=1', '', $auth_url_cas );
1302 } elseif ( strpos( $auth_url_cas, '?reauth=1&' ) !== false ) {
1303 // Reauth is first param with others behind it, so remove it and next delimiter.
1304 $auth_url_cas = str_replace( 'reauth=1&', '', $auth_url_cas );
1305 } else {
1306 // Reauth is first and only param, so remove it and '?'
1307 $auth_url_cas = str_replace( '?reauth=1', '', $auth_url_cas );
1308 }
1309
1310 }
1311 // Add special param indicating this is CAS authentication attempt.
1312 if ( strpos( $auth_url_cas, 'external=cas' ) === false ) {
1313 $auth_url_cas .= strpos( $auth_url_cas, '?' ) !== false ? '&external=cas' : '?external=cas';
1314 }
1315 } ?>
1316 <div id="auth-external-service-login">
1317 <?php if ( $auth_settings['google'] === '1' ): ?>
1318 <p><a id="googleplus_button" class="button button-primary button-external button-google"><span class="dashicons dashicons-googleplus"></span><span class="label">Sign in with Google</span></a></p>
1319 <?php wp_nonce_field( 'google_csrf_nonce', 'nonce_google_auth-' . $this->get_cookie_value() ); ?>
1320 <?php endif; ?>
1321
1322 <?php if ( $auth_settings['cas'] === '1' ): ?>
1323 <p><a class="button button-primary button-external button-cas" href="<?php echo $auth_url_cas; ?>"><span class="dashicons dashicons-lock"></span><span class="label">Sign in with <?php echo $auth_settings['cas_custom_label']; ?></span></a></p>
1324 <?php endif; ?>
1325
1326 <?php if ( $auth_settings['advanced_hide_wp_login'] === '1' && strpos( $_SERVER['QUERY_STRING'], 'external=wordpress' ) === false ): ?>
1327 <style type="text/css">
1328 #loginform {
1329 padding-bottom: 8px;
1330 }
1331 #loginform p>label, #loginform p.forgetmenot, #loginform p.submit, p#nav {
1332 display: none;
1333 }
1334 </style>
1335 <?php elseif ( $auth_settings['cas'] === '1' || $auth_settings['google'] === '1' ): ?>
1336 <h3> &mdash; or &mdash; </h3>
1337 <?php endif; ?>
1338 </div>
1339 <?php
1340
1341 } // END login_form_add_external_service_links()
1342
1343
1344 /**
1345 * Implements hook: do_action( 'wp_login_failed', $username );
1346 * Update the user meta for the user that just failed logging in.
1347 * Keep track of time of last failed attempt and number of failed attempts.
1348 */
1349 function update_login_failed_count( $username ) {
1350 // Grab plugin settings.
1351 $auth_settings = $this->get_plugin_options( 'single admin', 'allow override' );
1352
1353 // Get user trying to log in.
1354 // If this isn't a real user, update the global failed attempt
1355 // variables. We'll use these global variables to institute the
1356 // lockouts on nonexistent accounts. We do this so an attacker
1357 // won't be able to determine which accounts are real by which
1358 // accounts get locked out on multiple invalid attempts.
1359 $user = get_user_by( 'login', $username );
1360
1361 if ( $user !== FALSE ) {
1362 $last_attempt = get_user_meta( $user->ID, 'auth_settings_advanced_lockouts_time_last_failed', true );
1363 $num_attempts = get_user_meta( $user->ID, 'auth_settings_advanced_lockouts_failed_attempts', true );
1364 } else {
1365 $last_attempt = get_option( 'auth_settings_advanced_lockouts_time_last_failed' );
1366 $num_attempts = get_option( 'auth_settings_advanced_lockouts_failed_attempts' );
1367 }
1368
1369 // Make sure $last_attempt (time) and $num_attempts are positive integers.
1370 // Note: this addresses resetting them if either is unset from above.
1371 $last_attempt = abs( intval( $last_attempt ) );
1372 $num_attempts = abs( intval( $num_attempts ) );
1373
1374 // Reset the failed attempt count if the time since the last
1375 // failed attempt is greater than the reset duration.
1376 $time_since_last_fail = time() - $last_attempt;
1377 $reset_duration = $auth_settings['advanced_lockouts']['reset_duration'] * 60; // minutes to seconds
1378 if ( $time_since_last_fail > $reset_duration ) {
1379 $num_attempts = 0;
1380 }
1381
1382 // Set last failed time to now and increment last failed count.
1383 if ( $user !== FALSE ) {
1384 update_user_meta( $user->ID, 'auth_settings_advanced_lockouts_time_last_failed', time() );
1385 update_user_meta( $user->ID, 'auth_settings_advanced_lockouts_failed_attempts', $num_attempts + 1 );
1386 } else {
1387 update_option( 'auth_settings_advanced_lockouts_time_last_failed', time() );
1388 update_option( 'auth_settings_advanced_lockouts_failed_attempts', $num_attempts + 1 );
1389 }
1390 } // END update_login_failed_count()
1391
1392 /**
1393 * Overwrite the URL for the lost password link on the login form.
1394 * If we're authenticating against an external service, standard
1395 * WordPress password resets won't work.
1396 */
1397 function custom_lostpassword_url( $lostpassword_url ) {
1398 // Grab plugin settings.
1399 $auth_settings = $this->get_plugin_options( 'single admin', 'allow override' );
1400
1401 if (
1402 array_key_exists( 'ldap_lostpassword_url', $auth_settings ) &&
1403 filter_var( $auth_settings['ldap_lostpassword_url'], FILTER_VALIDATE_URL )
1404 ) {
1405 $lostpassword_url = $auth_settings['ldap_lostpassword_url'];
1406 }
1407 return $lostpassword_url;
1408 } // END custom_lostpassword_url()
1409
1410
1411
1412 /**
1413 * ***************************
1414 * Options page
1415 * ***************************
1416 */
1417
1418
1419
1420 /**
1421 * Add a link to this plugin's settings page from the WordPress Plugins page.
1422 * Called from "plugin_action_links" filter in __construct() above.
1423 *
1424 * @param array $links array of links in the admin sidebar
1425 *
1426 * @return array of links to show in the admin sidebar.
1427 */
1428 public function plugin_settings_link( $links ) {
1429 $admin_menu = $this->get_plugin_option( 'advanced_admin_menu' );
1430 $settings_url = $admin_menu === 'settings' ? admin_url( 'options-general.php?page=authorizer' ) : admin_url( 'admin.php?page=authorizer' );
1431 array_unshift( $links, '<a href="' . $settings_url . '">Settings</a>' );
1432 return $links;
1433 } // END plugin_settings_link()
1434
1435
1436
1437 /**
1438 * Add a link to this plugin's network settings page from the WordPress Plugins page.
1439 * Called from "network_admin_plugin_action_links" filter in __construct() above.
1440 *
1441 * @param array $links array of links in the network admin sidebar
1442 *
1443 * @return array of links to show in the network admin sidebar.
1444 */
1445 public function network_admin_plugin_settings_link( $links ) {
1446 $settings_link = '<a href="admin.php?page=authorizer">Network Settings</a>';
1447 array_unshift( $links, $settings_link );
1448 return $links;
1449 } // END network_admin_plugin_settings_link()
1450
1451
1452
1453 /**
1454 * Create the options page under Dashboard > Settings
1455 * Run on action hook: admin_menu
1456 */
1457 public function add_plugin_page() {
1458 $admin_menu = $this->get_plugin_option( 'advanced_admin_menu' );
1459 if ( $admin_menu === 'settings' ) {
1460 // @see http://codex.wordpress.org/Function_Reference/add_options_page
1461 add_options_page(
1462 'Authorizer', // Page title
1463 'Authorizer', // Menu title
1464 'create_users', // Capability
1465 'authorizer', // Menu slug
1466 array( $this, 'create_admin_page' ) // function
1467 );
1468 } else {
1469 // @see http://codex.wordpress.org/Function_Reference/add_menu_page
1470 add_menu_page(
1471 'Authorizer', // Page title
1472 'Authorizer', // Menu title
1473 'create_users', // Capability
1474 'authorizer', // Menu slug
1475 array( $this, 'create_admin_page' ), // callback
1476 'dashicons-groups', // icon
1477 '99.0018465' // position (decimal is to make overlap with other plugins less likely)
1478 );
1479 }
1480 } // END add_plugin_page()
1481
1482
1483 /**
1484 * Output the HTML for the options page
1485 */
1486 public function create_admin_page() { ?>
1487 <div class="wrap">
1488 <h2>Authorizer Settings</h2>
1489 <form method="post" action="options.php" autocomplete="off"><?php
1490 // This prints out all hidden settings fields
1491 // @see http://codex.wordpress.org/Function_Reference/settings_fields
1492 settings_fields( 'auth_settings_group' );
1493 // This prints out all the sections
1494 // @see http://codex.wordpress.org/Function_Reference/do_settings_sections
1495 do_settings_sections( 'authorizer' );
1496 submit_button(); ?>
1497 </form>
1498 </div><?php
1499 } // END create_admin_page()
1500
1501
1502
1503 /**
1504 * Load external resources on this plugin's options page.
1505 * Run on action hooks: load-settings_page_authorizer, load-toplevel_page_authorizer, admin_head-index.php
1506 */
1507 public function load_options_page() {
1508 wp_enqueue_script(
1509 'authorizer',
1510 plugins_url( 'js/authorizer.js', __FILE__ ),
1511 array( 'jquery-effects-shake' ), '2.3.2', true
1512 );
1513 $js_auth_config = array( 'baseurl' => get_bloginfo( 'url' ) );
1514 wp_localize_script( 'authorizer', 'auth_config', $js_auth_config );
1515
1516 wp_enqueue_script(
1517 'jquery.multi-select',
1518 plugins_url( 'inc/jquery.multi-select/js/jquery.multi-select.js', __FILE__ ),
1519 array( 'jquery' ), '1.8', true
1520 );
1521
1522 wp_register_style( 'authorizer-css', plugins_url( 'css/authorizer.css', __FILE__ ), array(), '2.3.2' );
1523 wp_enqueue_style( 'authorizer-css' );
1524
1525 wp_register_style( 'jquery-multi-select-css', plugins_url( 'inc/jquery.multi-select/css/multi-select.css', __FILE__ ), array(), '1.8' );
1526 wp_enqueue_style( 'jquery-multi-select-css' );
1527
1528 add_action( 'admin_notices', array( $this, 'admin_notices' ) ); // Add any notices to the top of the options page.
1529 add_action( 'admin_head', array( $this, 'admin_head' ) ); // Add help documentation to the options page.
1530 } // END load_options_page()
1531
1532
1533
1534 /**
1535 * Show custom admin notice.
1536 * Filter: admin_notice
1537 */
1538 function show_advanced_admin_notice() {
1539 $notice = get_option( 'auth_settings_advanced_admin_notice' );
1540 delete_option( 'auth_settings_advanced_admin_notice' );
1541
1542 if ( $notice && strlen( $notice ) > 0 ) { ?>
1543 <div class="error">
1544 <p><?php _e( $notice ); ?></p>
1545 </div><?php
1546 }
1547 } // END show_advanced_admin_notice()
1548
1549
1550 /**
1551 * Add notices to the top of the options page.
1552 * Run on action hook chain: load-settings_page_authorizer > admin_notices
1553 * Description: Check for invalid settings combinations and show a warning message, e.g.:
1554 * if ( cas url inaccessible ) : ?>
1555 * <div class='updated settings-error'><p>Can't reach CAS server.</p></div>
1556 * <?php endif;
1557 */
1558 public function admin_notices() {
1559 // Grab plugin settings.
1560 $auth_settings = $this->get_plugin_options( 'single admin', 'allow override' );
1561
1562 if ( $auth_settings['cas'] === '1' ) :
1563 // Check if provided CAS URL is accessible.
1564 $protocol = $auth_settings['cas_port'] == '80' ? 'http' : 'https';
1565 if ( ! $this->url_is_accessible( $protocol . '://' . $auth_settings['cas_host'] . $auth_settings['cas_path'] ) ) :
1566 $authorizer_options_url = $auth_settings['advanced_admin_menu'] === 'settings' ? admin_url( 'options-general.php?page=authorizer' ) : admin_url( '?page=authorizer' );
1567 ?><div class='notice notice-warning is-dismissible'>
1568 <p>Can't reach CAS server. Please provide <a href='<?php echo $authorizer_options_url; ?>&tab=external'>accurate CAS settings</a> if you intend to use it.</p>
1569 </div><?php
1570 endif;
1571 endif;
1572 } // END admin_notices()
1573
1574
1575 /**
1576 * Create sections and options
1577 * Run on action hook: admin_init
1578 */
1579 public function page_init() {
1580 // Create one setting that holds all the options (array)
1581 // @see http://codex.wordpress.org/Function_Reference/register_setting
1582 // @see http://codex.wordpress.org/Function_Reference/add_settings_section
1583 // @see http://codex.wordpress.org/Function_Reference/add_settings_field
1584 register_setting(
1585 'auth_settings_group', // Option group
1586 'auth_settings', // Option name
1587 array( $this, 'sanitize_options' ) // Sanitize callback
1588 );
1589
1590 add_settings_section(
1591 'auth_settings_tabs', // HTML element ID
1592 '', // HTML element Title
1593 array( $this, 'print_section_info_tabs' ), // Callback (echos section content)
1594 'authorizer' // Page this section is shown on (slug)
1595 );
1596
1597 // Create Access Lists section
1598 add_settings_section(
1599 'auth_settings_lists', // HTML element ID
1600 '', // HTML element Title
1601 array( $this, 'print_section_info_access_lists' ), // Callback (echos section content)
1602 'authorizer' // Page this section is shown on (slug)
1603 );
1604
1605 // Create Login Access section
1606 add_settings_section(
1607 'auth_settings_access_login', // HTML element ID
1608 '', // HTML element Title
1609 array( $this, 'print_section_info_access_login' ), // Callback (echos section content)
1610 'authorizer' // Page this section is shown on (slug)
1611 );
1612 add_settings_field(
1613 'auth_settings_access_who_can_login', // HTML element ID
1614 'Who can log into the site?', // HTML element Title
1615 array( $this, 'print_radio_auth_access_who_can_login' ), // Callback (echos form element)
1616 'authorizer', // Page this setting is shown on (slug)
1617 'auth_settings_access_login' // Section this setting is shown on
1618 );
1619 add_settings_field(
1620 'auth_settings_access_role_receive_pending_emails', // HTML element ID
1621 'Which role should receive email notifications about pending users?', // HTML element Title
1622 array( $this, 'print_select_auth_access_role_receive_pending_emails' ), // Callback (echos form element)
1623 'authorizer', // Page this setting is shown on (slug)
1624 'auth_settings_access_login' // Section this setting is shown on
1625 );
1626 add_settings_field(
1627 'auth_settings_access_pending_redirect_to_message', // HTML element ID
1628 'What message should pending users see after attempting to log in?', // HTML element Title
1629 array( $this, 'print_wysiwyg_auth_access_pending_redirect_to_message' ), // Callback (echos form element)
1630 'authorizer', // Page this setting is shown on (slug)
1631 'auth_settings_access_login' // Section this setting is shown on
1632 );
1633 add_settings_field(
1634 'auth_settings_access_blocked_redirect_to_message', // HTML element ID
1635 'What message should blocked users see after attempting to log in?', // HTML element Title
1636 array( $this, 'print_wysiwyg_auth_access_blocked_redirect_to_message' ), // Callback (echos form element)
1637 'authorizer', // Page this setting is shown on (slug)
1638 'auth_settings_access_login' // Section this setting is shown on
1639 );
1640 add_settings_field(
1641 'auth_settings_access_should_email_approved_users', // HTML element ID
1642 'Send welcome email to new approved users?', // HTML element Title
1643 array( $this, 'print_checkbox_auth_access_should_email_approved_users' ), // Callback (echos form element)
1644 'authorizer', // Page this setting is shown on (slug)
1645 'auth_settings_access_login' // Section this setting is shown on
1646 );
1647 add_settings_field(
1648 'auth_settings_access_email_approved_users_subject', // HTML element ID
1649 'Welcome email subject', // HTML element Title
1650 array( $this, 'print_text_auth_access_email_approved_users_subject' ), // Callback (echos form element)
1651 'authorizer', // Page this setting is shown on (slug)
1652 'auth_settings_access_login' // Section this setting is shown on
1653 );
1654 add_settings_field(
1655 'auth_settings_access_email_approved_users_body', // HTML element ID
1656 'Welcome email body', // HTML element Title
1657 array( $this, 'print_wysiwyg_auth_access_email_approved_users_body' ), // Callback (echos form element)
1658 'authorizer', // Page this setting is shown on (slug)
1659 'auth_settings_access_login' // Section this setting is shown on
1660 );
1661
1662
1663 // Create Public Access section
1664 add_settings_section(
1665 'auth_settings_access_public', // HTML element ID
1666 '', // HTML element Title
1667 array( $this, 'print_section_info_access_public' ), // Callback (echos section content)
1668 'authorizer' // Page this section is shown on (slug)
1669 );
1670 add_settings_field(
1671 'auth_settings_access_who_can_view', // HTML element ID
1672 'Who can view the site?', // HTML element Title
1673 array( $this, 'print_radio_auth_access_who_can_view' ), // Callback (echos form element)
1674 'authorizer', // Page this setting is shown on (slug)
1675 'auth_settings_access_public' // Section this setting is shown on
1676 );
1677 add_settings_field(
1678 'auth_settings_access_public_pages', // HTML element ID
1679 'What pages (if any) should be available to everyone?', // HTML element Title
1680 array( $this, 'print_multiselect_auth_access_public_pages' ), // Callback (echos form element)
1681 'authorizer', // Page this setting is shown on (slug)
1682 'auth_settings_access_public' // Section this setting is shown on
1683 );
1684 add_settings_field(
1685 'auth_settings_access_redirect', // HTML element ID
1686 'What happens to people without access when they visit a private page?', // HTML element Title
1687 array( $this, 'print_radio_auth_access_redirect' ), // Callback (echos form element)
1688 'authorizer', // Page this setting is shown on (slug)
1689 'auth_settings_access_public' // Section this setting is shown on
1690 );
1691 add_settings_field(
1692 'auth_settings_access_public_warning', // HTML element ID
1693 'What happens to people without access when they visit a public page?', // HTML element Title
1694 array( $this, 'print_radio_auth_access_public_warning' ), // Callback (echos form element)
1695 'authorizer', // Page this setting is shown on (slug)
1696 'auth_settings_access_public' // Section this setting is shown on
1697 );
1698 add_settings_field(
1699 'auth_settings_access_redirect_to_message', // HTML element ID
1700 'What message should people without access see?', // HTML element Title
1701 array( $this, 'print_wysiwyg_auth_access_redirect_to_message' ), // Callback (echos form element)
1702 'authorizer', // Page this setting is shown on (slug)
1703 'auth_settings_access_public' // Section this setting is shown on
1704 );
1705
1706 // Create External Service Settings section
1707 add_settings_section(
1708 'auth_settings_external', // HTML element ID
1709 '', // HTML element Title
1710 array( $this, 'print_section_info_external' ), // Callback (echos section content)
1711 'authorizer' // Page this section is shown on (slug)
1712 );
1713 add_settings_field(
1714 'auth_settings_access_default_role', // HTML element ID
1715 'Default role for new users', // HTML element Title
1716 array( $this, 'print_select_auth_access_default_role' ), // Callback (echos form element)
1717 'authorizer', // Page this setting is shown on (slug)
1718 'auth_settings_external' // Section this setting is shown on
1719 );
1720 add_settings_field(
1721 'auth_settings_external_google', // HTML element ID
1722 'Google Logins', // HTML element Title
1723 array( $this, 'print_checkbox_auth_external_google' ), // Callback (echos form element)
1724 'authorizer', // Page this setting is shown on (slug)
1725 'auth_settings_external' // Section this setting is shown on
1726 );
1727 add_settings_field(
1728 'auth_settings_google_clientid', // HTML element ID
1729 'Google Client ID', // HTML element Title
1730 array( $this, 'print_text_google_clientid' ), // Callback (echos form element)
1731 'authorizer', // Page this setting is shown on (slug)
1732 'auth_settings_external' // Section this setting is shown on
1733 );
1734 add_settings_field(
1735 'auth_settings_google_clientsecret', // HTML element ID
1736 'Google Client Secret', // HTML element Title
1737 array( $this, 'print_text_google_clientsecret' ), // Callback (echos form element)
1738 'authorizer', // Page this setting is shown on (slug)
1739 'auth_settings_external' // Section this setting is shown on
1740 );
1741 add_settings_field(
1742 'auth_settings_external_cas', // HTML element ID
1743 'CAS Logins', // HTML element Title
1744 array( $this, 'print_checkbox_auth_external_cas' ), // Callback (echos form element)
1745 'authorizer', // Page this setting is shown on (slug)
1746 'auth_settings_external' // Section this setting is shown on
1747 );
1748 add_settings_field(
1749 'auth_settings_cas_custom_label', // HTML element ID
1750 'CAS custom label', // HTML element Title
1751 array( $this, 'print_text_cas_custom_label' ), // Callback (echos form element)
1752 'authorizer', // Page this setting is shown on (slug)
1753 'auth_settings_external' // Section this setting is shown on
1754 );
1755 add_settings_field(
1756 'auth_settings_cas_host', // HTML element ID
1757 'CAS server hostname', // HTML element Title
1758 array( $this, 'print_text_cas_host' ), // Callback (echos form element)
1759 'authorizer', // Page this setting is shown on (slug)
1760 'auth_settings_external' // Section this setting is shown on
1761 );
1762 add_settings_field(
1763 'auth_settings_cas_port', // HTML element ID
1764 'CAS server port', // HTML element Title
1765 array( $this, 'print_text_cas_port' ), // Callback (echos form element)
1766 'authorizer', // Page this setting is shown on (slug)
1767 'auth_settings_external' // Section this setting is shown on
1768 );
1769 add_settings_field(
1770 'auth_settings_cas_path', // HTML element ID
1771 'CAS server path/context', // HTML element Title
1772 array( $this, 'print_text_cas_path' ), // Callback (echos form element)
1773 'authorizer', // Page this setting is shown on (slug)
1774 'auth_settings_external' // Section this setting is shown on
1775 );
1776 add_settings_field(
1777 'auth_settings_cas_attr_email', // HTML element ID
1778 'CAS attribute containing email address', // HTML element Title
1779 array( $this, 'print_text_cas_attr_email' ), // Callback (echos form element)
1780 'authorizer', // Page this setting is shown on (slug)
1781 'auth_settings_external' // Section this setting is shown on
1782 );
1783 add_settings_field(
1784 'auth_settings_cas_attr_first_name', // HTML element ID
1785 'CAS attribute containing first name', // HTML element Title
1786 array( $this, 'print_text_cas_attr_first_name' ), // Callback (echos form element)
1787 'authorizer', // Page this setting is shown on (slug)
1788 'auth_settings_external' // Section this setting is shown on
1789 );
1790 add_settings_field(
1791 'auth_settings_cas_attr_last_name', // HTML element ID
1792 'CAS attribute containing last name', // HTML element Title
1793 array( $this, 'print_text_cas_attr_last_name' ), // Callback (echos form element)
1794 'authorizer', // Page this setting is shown on (slug)
1795 'auth_settings_external' // Section this setting is shown on
1796 );
1797 add_settings_field(
1798 'auth_settings_cas_attr_update_on_login', // HTML element ID
1799 'CAS attribute update', // HTML element Title
1800 array( $this, 'print_checkbox_cas_attr_update_on_login' ), // Callback (echos form element)
1801 'authorizer', // Page this setting is shown on (slug)
1802 'auth_settings_external' // Section this setting is shown on
1803 );
1804 add_settings_field(
1805 'auth_settings_external_ldap', // HTML element ID
1806 'LDAP Logins', // HTML element Title
1807 array( $this, 'print_checkbox_auth_external_ldap' ), // Callback (echos form element)
1808 'authorizer', // Page this setting is shown on (slug)
1809 'auth_settings_external' // Section this setting is shown on
1810 );
1811 add_settings_field(
1812 'auth_settings_ldap_host', // HTML element ID
1813 'LDAP Host', // HTML element Title
1814 array( $this, 'print_text_ldap_host' ), // Callback (echos form element)
1815 'authorizer', // Page this setting is shown on (slug)
1816 'auth_settings_external' // Section this setting is shown on
1817 );
1818 add_settings_field(
1819 'auth_settings_ldap_port', // HTML element ID
1820 'LDAP Port', // HTML element Title
1821 array( $this, 'print_text_ldap_port' ), // Callback (echos form element)
1822 'authorizer', // Page this setting is shown on (slug)
1823 'auth_settings_external' // Section this setting is shown on
1824 );
1825 add_settings_field(
1826 'auth_settings_ldap_search_base', // HTML element ID
1827 'LDAP Search Base', // HTML element Title
1828 array( $this, 'print_text_ldap_search_base' ), // Callback (echos form element)
1829 'authorizer', // Page this setting is shown on (slug)
1830 'auth_settings_external' // Section this setting is shown on
1831 );
1832 add_settings_field(
1833 'auth_settings_ldap_uid', // HTML element ID
1834 'LDAP attribute containing username', // HTML element Title
1835 array( $this, 'print_text_ldap_uid' ), // Callback (echos form element)
1836 'authorizer', // Page this setting is shown on (slug)
1837 'auth_settings_external' // Section this setting is shown on
1838 );
1839 add_settings_field(
1840 'auth_settings_ldap_attr_email', // HTML element ID
1841 'LDAP attribute containing email address', // HTML element Title
1842 array( $this, 'print_text_ldap_attr_email' ), // Callback (echos form element)
1843 'authorizer', // Page this setting is shown on (slug)
1844 'auth_settings_external' // Section this setting is shown on
1845 );
1846 add_settings_field(
1847 'auth_settings_ldap_user', // HTML element ID
1848 'LDAP Directory User', // HTML element Title
1849 array( $this, 'print_text_ldap_user' ), // Callback (echos form element)
1850 'authorizer', // Page this setting is shown on (slug)
1851 'auth_settings_external' // Section this setting is shown on
1852 );
1853 add_settings_field(
1854 'auth_settings_ldap_password', // HTML element ID
1855 'LDAP Directory User Password', // HTML element Title
1856 array( $this, 'print_password_ldap_password' ), // Callback (echos form element)
1857 'authorizer', // Page this setting is shown on (slug)
1858 'auth_settings_external' // Section this setting is shown on
1859 );
1860 add_settings_field(
1861 'auth_settings_ldap_tls', // HTML element ID
1862 'Secure Connection (TLS)', // HTML element Title
1863 array( $this, 'print_checkbox_ldap_tls' ), // Callback (echos form element)
1864 'authorizer', // Page this setting is shown on (slug)
1865 'auth_settings_external' // Section this setting is shown on
1866 );
1867 add_settings_field(
1868 'auth_settings_ldap_lostpassword_url', // HTML element ID
1869 'Custom lost password URL', // HTML element Title
1870 array( $this, 'print_text_ldap_lostpassword_url' ), // Callback (echos form element)
1871 'authorizer', // Page this setting is shown on (slug)
1872 'auth_settings_external' // Section this setting is shown on
1873 );
1874 add_settings_field(
1875 'auth_settings_ldap_attr_first_name', // HTML element ID
1876 'LDAP attribute containing first name', // HTML element Title
1877 array( $this, 'print_text_ldap_attr_first_name' ), // Callback (echos form element)
1878 'authorizer', // Page this setting is shown on (slug)
1879 'auth_settings_external' // Section this setting is shown on
1880 );
1881 add_settings_field(
1882 'auth_settings_ldap_attr_last_name', // HTML element ID
1883 'LDAP attribute containing last name', // HTML element Title
1884 array( $this, 'print_text_ldap_attr_last_name' ), // Callback (echos form element)
1885 'authorizer', // Page this setting is shown on (slug)
1886 'auth_settings_external' // Section this setting is shown on
1887 );
1888 add_settings_field(
1889 'auth_settings_ldap_attr_update_on_login', // HTML element ID
1890 'LDAP attribute update', // HTML element Title
1891 array( $this, 'print_checkbox_ldap_attr_update_on_login' ), // Callback (echos form element)
1892 'authorizer', // Page this setting is shown on (slug)
1893 'auth_settings_external' // Section this setting is shown on
1894 );
1895
1896 // Create Advanced Settings section
1897 add_settings_section(
1898 'auth_settings_advanced', // HTML element ID
1899 '', // HTML element Title
1900 array( $this, 'print_section_info_advanced' ), // Callback (echos section content)
1901 'authorizer' // Page this section is shown on (slug)
1902 );
1903 add_settings_field(
1904 'auth_settings_advanced_lockouts', // HTML element ID
1905 'Limit invalid login attempts', // HTML element Title
1906 array( $this, 'print_text_auth_advanced_lockouts' ), // Callback (echos form element)
1907 'authorizer', // Page this setting is shown on (slug)
1908 'auth_settings_advanced' // Section this setting is shown on
1909 );
1910 add_settings_field(
1911 'auth_settings_advanced_hide_wp_login', // HTML element ID
1912 'Hide WordPress Login', // HTML element Title
1913 array( $this, 'print_checkbox_auth_advanced_hide_wp_login' ), // Callback (echos form element)
1914 'authorizer', // Page this setting is shown on (slug)
1915 'auth_settings_advanced' // Section this setting is shown on
1916 );
1917 add_settings_field(
1918 'auth_settings_advanced_branding', // HTML element ID
1919 'Custom WordPress login branding', // HTML element Title
1920 array( $this, 'print_radio_auth_advanced_branding' ), // Callback (echos form element)
1921 'authorizer', // Page this setting is shown on (slug)
1922 'auth_settings_advanced' // Section this setting is shown on
1923 );
1924 add_settings_field(
1925 'auth_settings_advanced_admin_menu', // HTML element ID
1926 'Authorizer admin menu item location', // HTML element Title
1927 array( $this, 'print_radio_auth_advanced_admin_menu' ), // Callback (echos form element)
1928 'authorizer', // Page this setting is shown on (slug)
1929 'auth_settings_advanced' // Section this setting is shown on
1930 );
1931 add_settings_field(
1932 'auth_settings_advanced_usermeta', // HTML element ID
1933 'Show custom usermeta in user list', // HTML element Title
1934 array( $this, 'print_select_auth_advanced_usermeta' ), // Callback (echos form element)
1935 'authorizer', // Page this setting is shown on (slug)
1936 'auth_settings_advanced' // Section this setting is shown on
1937 );
1938 // On multisite installs, add an option to override all multisite settings on individual sites.
1939 if ( is_multisite() ) {
1940 add_settings_field(
1941 'auth_settings_advanced_override_multisite', // HTML element ID
1942 'Override multisite options', // HTML element Title
1943 array( $this, 'print_checkbox_auth_advanced_override_multisite' ), // Callback (echos form element)
1944 'authorizer', // Page this setting is shown on (slug)
1945 'auth_settings_advanced' // Section this setting is shown on
1946 );
1947 }
1948 } // END page_init()
1949
1950
1951 /**
1952 * Set meaningful defaults for the plugin options.
1953 * Note: This function is called on plugin activation.
1954 */
1955 function set_default_options() {
1956 global $wp_roles;
1957
1958 $auth_settings = get_option( 'auth_settings' );
1959 if ( $auth_settings === FALSE ) {
1960 $auth_settings = array();
1961 }
1962
1963 // Access Lists Defaults.
1964 $auth_settings_access_users_pending = get_option( 'auth_settings_access_users_pending' );
1965 if ( $auth_settings_access_users_pending === FALSE ) {
1966 $auth_settings_access_users_pending = array();
1967 }
1968 $auth_settings_access_users_approved = get_option( 'auth_settings_access_users_approved' );
1969 if ( $auth_settings_access_users_approved === FALSE ) {
1970 $auth_settings_access_users_approved = array();
1971 }
1972 $auth_settings_access_users_blocked = get_option( 'auth_settings_access_users_blocked' );
1973 if ( $auth_settings_access_users_blocked === FALSE ) {
1974 $auth_settings_access_users_blocked = array();
1975 }
1976
1977 // Login Access Defaults.
1978 if ( ! array_key_exists( 'access_who_can_login', $auth_settings ) ) {
1979 $auth_settings['access_who_can_login'] = 'approved_users';
1980 }
1981 if ( ! array_key_exists( 'access_role_receive_pending_emails', $auth_settings ) ) {
1982 $auth_settings['access_role_receive_pending_emails'] = '---';
1983 }
1984 if ( ! array_key_exists( 'access_pending_redirect_to_message', $auth_settings ) ) {
1985 $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.</p>';
1986 }
1987 if ( ! array_key_exists( 'access_blocked_redirect_to_message', $auth_settings ) ) {
1988 $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.</p>';
1989 }
1990 if ( ! array_key_exists( 'access_should_email_approved_users', $auth_settings ) ) {
1991 $auth_settings['access_should_email_approved_users'] = '';
1992 }
1993 if ( ! array_key_exists( 'access_email_approved_users_subject', $auth_settings ) ) {
1994 $auth_settings['access_email_approved_users_subject'] = 'Welcome to [site_name]!';
1995 }
1996 if ( ! array_key_exists( 'access_email_approved_users_body', $auth_settings ) ) {
1997 $auth_settings['access_email_approved_users_body'] =
1998 'Hello [user_email],' . PHP_EOL .
1999 'Welcome to [site_name]! You now have access to all content on the site. Please visit us here:' . PHP_EOL .
2000 '[site_url]';
2001 }
2002
2003 // Public Access to Private Page Defaults.
2004 if ( ! array_key_exists( 'access_who_can_view', $auth_settings ) ) {
2005 $auth_settings['access_who_can_view'] = 'everyone';
2006 }
2007 if ( ! array_key_exists( 'access_public_pages', $auth_settings ) ) {
2008 $auth_settings['access_public_pages'] = array();
2009 }
2010 if ( ! array_key_exists( 'access_redirect', $auth_settings ) ) {
2011 $auth_settings['access_redirect'] = 'login';
2012 }
2013 if ( ! array_key_exists( 'access_public_warning', $auth_settings ) ) {
2014 $auth_settings['access_public_warning'] = 'no_warning';
2015 }
2016 if ( ! array_key_exists( 'access_redirect_to_message', $auth_settings ) ) {
2017 $auth_settings['access_redirect_to_message'] = '<p><strong>Notice</strong>: You are browsing this site anonymously, and only have access to a portion of its content.</p>';
2018 }
2019
2020
2021 // External Service Defaults.
2022 if ( ! array_key_exists( 'access_default_role', $auth_settings ) ) {
2023 // Set default role to 'student' if that role exists, 'subscriber' otherwise.
2024 $all_roles = $wp_roles->roles;
2025 $editable_roles = apply_filters( 'editable_roles', $all_roles );
2026 if ( array_key_exists( 'student', $editable_roles ) ) {
2027 $auth_settings['access_default_role'] = 'student';
2028 } else {
2029 $auth_settings['access_default_role'] = 'subscriber';
2030 }
2031 }
2032
2033 if ( ! array_key_exists( 'google', $auth_settings ) ) {
2034 $auth_settings['google'] = '';
2035 }
2036 if ( ! array_key_exists( 'cas', $auth_settings ) ) {
2037 $auth_settings['cas'] = '';
2038 }
2039 if ( ! array_key_exists( 'ldap', $auth_settings ) ) {
2040 $auth_settings['ldap'] = '';
2041 }
2042
2043 if ( ! array_key_exists( 'google_clientid', $auth_settings ) ) {
2044 $auth_settings['google_clientid'] = '';
2045 }
2046 if ( ! array_key_exists( 'google_clientsecret', $auth_settings ) ) {
2047 $auth_settings['google_clientsecret'] = '';
2048 }
2049
2050 if ( ! array_key_exists( 'cas_custom_label', $auth_settings ) ) {
2051 $auth_settings['cas_custom_label'] = 'CAS';
2052 }
2053 if ( ! array_key_exists( 'cas_host', $auth_settings ) ) {
2054 $auth_settings['cas_host'] = '';
2055 }
2056 if ( ! array_key_exists( 'cas_port', $auth_settings ) ) {
2057 $auth_settings['cas_port'] = '';
2058 }
2059 if ( ! array_key_exists( 'cas_path', $auth_settings ) ) {
2060 $auth_settings['cas_path'] = '';
2061 }
2062 if ( ! array_key_exists( 'cas_attr_email', $auth_settings ) ) {
2063 $auth_settings['cas_attr_email'] = '';
2064 }
2065 if ( ! array_key_exists( 'cas_attr_first_name', $auth_settings ) ) {
2066 $auth_settings['cas_attr_first_name'] = '';
2067 }
2068 if ( ! array_key_exists( 'cas_attr_last_name', $auth_settings ) ) {
2069 $auth_settings['cas_attr_last_name'] = '';
2070 }
2071 if ( ! array_key_exists( 'cas_attr_update_on_login', $auth_settings ) ) {
2072 $auth_settings['cas_attr_update_on_login'] = '';
2073 }
2074
2075 if ( ! array_key_exists( 'ldap_host', $auth_settings ) ) {
2076 $auth_settings['ldap_host'] = '';
2077 }
2078 if ( ! array_key_exists( 'ldap_port', $auth_settings ) ) {
2079 $auth_settings['ldap_port'] = '';
2080 }
2081 if ( ! array_key_exists( 'ldap_search_base', $auth_settings ) ) {
2082 $auth_settings['ldap_search_base'] = '';
2083 }
2084 if ( ! array_key_exists( 'ldap_uid', $auth_settings ) ) {
2085 $auth_settings['ldap_uid'] = '';
2086 }
2087 if ( ! array_key_exists( 'ldap_attr_email', $auth_settings ) ) {
2088 $auth_settings['ldap_attr_email'] = '';
2089 }
2090 if ( ! array_key_exists( 'ldap_user', $auth_settings ) ) {
2091 $auth_settings['ldap_user'] = '';
2092 }
2093 if ( ! array_key_exists( 'ldap_password', $auth_settings ) ) {
2094 $auth_settings['ldap_password'] = '';
2095 }
2096 if ( ! array_key_exists( 'ldap_tls', $auth_settings ) ) {
2097 $auth_settings['ldap_tls'] = '1';
2098 }
2099 if ( ! array_key_exists( 'ldap_lostpassword_url', $auth_settings ) ) {
2100 $auth_settings['ldap_lostpassword_url'] = '';
2101 }
2102 if ( ! array_key_exists( 'ldap_attr_first_name', $auth_settings ) ) {
2103 $auth_settings['ldap_attr_first_name'] = '';
2104 }
2105 if ( ! array_key_exists( 'ldap_attr_last_name', $auth_settings ) ) {
2106 $auth_settings['ldap_attr_last_name'] = '';
2107 }
2108 if ( ! array_key_exists( 'ldap_attr_update_on_login', $auth_settings ) ) {
2109 $auth_settings['ldap_attr_update_on_login'] = '';
2110 }
2111
2112 // Advanced defaults.
2113 if ( ! array_key_exists( 'advanced_lockouts', $auth_settings ) ) {
2114 $auth_settings['advanced_lockouts'] = array(
2115 'attempts_1' => 10,
2116 'duration_1' => 1,
2117 'attempts_2' => 10,
2118 'duration_2' => 10,
2119 'reset_duration' => 120,
2120 );
2121 }
2122 if ( ! array_key_exists( 'advanced_hide_wp_login', $auth_settings ) ) {
2123 $auth_settings['advanced_hide_wp_login'] = '';
2124 }
2125 if ( ! array_key_exists( 'advanced_branding', $auth_settings ) ) {
2126 $auth_settings['advanced_branding'] = 'default';
2127 }
2128 if ( ! array_key_exists( 'advanced_admin_menu', $auth_settings ) ) {
2129 $auth_settings['advanced_admin_menu'] = 'top';
2130 }
2131 if ( ! array_key_exists( 'advanced_usermeta', $auth_settings ) ) {
2132 $auth_settings['advanced_usermeta'] = '';
2133 }
2134 if ( ! array_key_exists( 'advanced_override_multisite', $auth_settings ) ) {
2135 $auth_settings['advanced_override_multisite'] = '';
2136 }
2137
2138 // Save default options to database.
2139 update_option( 'auth_settings', $auth_settings );
2140 update_option( 'auth_settings_access_users_pending', $auth_settings_access_users_pending );
2141 update_option( 'auth_settings_access_users_approved', $auth_settings_access_users_approved );
2142 update_option( 'auth_settings_access_users_blocked', $auth_settings_access_users_blocked );
2143
2144 // Multisite defaults.
2145 if ( is_multisite() ) {
2146 $auth_multisite_settings = get_blog_option( BLOG_ID_CURRENT_SITE, 'auth_multisite_settings', array() );
2147
2148 if ( $auth_multisite_settings === FALSE ) {
2149 $auth_multisite_settings = array();
2150 }
2151 // Global switch for enabling multisite options.
2152 if ( ! array_key_exists( 'multisite_override', $auth_multisite_settings ) ) {
2153 $auth_multisite_settings['multisite_override'] = '';
2154 }
2155 // Access Lists Defaults.
2156 $auth_multisite_settings_access_users_approved = get_blog_option( BLOG_ID_CURRENT_SITE, 'auth_multisite_settings_access_users_approved' );
2157 if ( $auth_multisite_settings_access_users_approved === FALSE ) {
2158 $auth_multisite_settings_access_users_approved = array();
2159 }
2160 // Login Access Defaults.
2161 if ( ! array_key_exists( 'access_who_can_login', $auth_multisite_settings ) ) {
2162 $auth_multisite_settings['access_who_can_login'] = 'approved_users';
2163 }
2164 // View Access Defaults.
2165 if ( ! array_key_exists( 'access_who_can_view', $auth_multisite_settings ) ) {
2166 $auth_multisite_settings['access_who_can_view'] = 'everyone';
2167 }
2168 // External Service Defaults.
2169 if ( ! array_key_exists( 'access_default_role', $auth_multisite_settings ) ) {
2170 // Set default role to 'student' if that role exists, 'subscriber' otherwise.
2171 $all_roles = $wp_roles->roles;
2172 $editable_roles = apply_filters( 'editable_roles', $all_roles );
2173 if ( array_key_exists( 'student', $editable_roles ) ) {
2174 $auth_multisite_settings['access_default_role'] = 'student';
2175 } else {
2176 $auth_multisite_settings['access_default_role'] = 'subscriber';
2177 }
2178 }
2179 if ( ! array_key_exists( 'google', $auth_multisite_settings ) ) {
2180 $auth_multisite_settings['google'] = '';
2181 }
2182 if ( ! array_key_exists( 'cas', $auth_multisite_settings ) ) {
2183 $auth_multisite_settings['cas'] = '';
2184 }
2185 if ( ! array_key_exists( 'ldap', $auth_multisite_settings ) ) {
2186 $auth_multisite_settings['ldap'] = '';
2187 }
2188 if ( ! array_key_exists( 'google_clientid', $auth_multisite_settings ) ) {
2189 $auth_multisite_settings['google_clientid'] = '';
2190 }
2191 if ( ! array_key_exists( 'google_clientsecret', $auth_multisite_settings ) ) {
2192 $auth_multisite_settings['google_clientsecret'] = '';
2193 }
2194 if ( ! array_key_exists( 'cas_custom_label', $auth_multisite_settings ) ) {
2195 $auth_multisite_settings['cas_custom_label'] = 'CAS';
2196 }
2197 if ( ! array_key_exists( 'cas_host', $auth_multisite_settings ) ) {
2198 $auth_multisite_settings['cas_host'] = '';
2199 }
2200 if ( ! array_key_exists( 'cas_port', $auth_multisite_settings ) ) {
2201 $auth_multisite_settings['cas_port'] = '';
2202 }
2203 if ( ! array_key_exists( 'cas_path', $auth_multisite_settings ) ) {
2204 $auth_multisite_settings['cas_path'] = '';
2205 }
2206 if ( ! array_key_exists( 'cas_attr_email', $auth_multisite_settings ) ) {
2207 $auth_multisite_settings['cas_attr_email'] = '';
2208 }
2209 if ( ! array_key_exists( 'cas_attr_first_name', $auth_multisite_settings ) ) {
2210 $auth_multisite_settings['cas_attr_first_name'] = '';
2211 }
2212 if ( ! array_key_exists( 'cas_attr_last_name', $auth_multisite_settings ) ) {
2213 $auth_multisite_settings['cas_attr_last_name'] = '';
2214 }
2215 if ( ! array_key_exists( 'cas_attr_update_on_login', $auth_multisite_settings ) ) {
2216 $auth_multisite_settings['cas_attr_update_on_login'] = '';
2217 }
2218 if ( ! array_key_exists( 'ldap_host', $auth_multisite_settings ) ) {
2219 $auth_multisite_settings['ldap_host'] = '';
2220 }
2221 if ( ! array_key_exists( 'ldap_port', $auth_multisite_settings ) ) {
2222 $auth_multisite_settings['ldap_port'] = '';
2223 }
2224 if ( ! array_key_exists( 'ldap_search_base', $auth_multisite_settings ) ) {
2225 $auth_multisite_settings['ldap_search_base'] = '';
2226 }
2227 if ( ! array_key_exists( 'ldap_uid', $auth_multisite_settings ) ) {
2228 $auth_multisite_settings['ldap_uid'] = '';
2229 }
2230 if ( ! array_key_exists( 'ldap_attr_email', $auth_multisite_settings ) ) {
2231 $auth_multisite_settings['ldap_attr_email'] = '';
2232 }
2233 if ( ! array_key_exists( 'ldap_user', $auth_multisite_settings ) ) {
2234 $auth_multisite_settings['ldap_user'] = '';
2235 }
2236 if ( ! array_key_exists( 'ldap_password', $auth_multisite_settings ) ) {
2237 $auth_multisite_settings['ldap_password'] = '';
2238 }
2239 if ( ! array_key_exists( 'ldap_tls', $auth_multisite_settings ) ) {
2240 $auth_multisite_settings['ldap_tls'] = '1';
2241 }
2242 if ( ! array_key_exists( 'ldap_lostpassword_url', $auth_multisite_settings ) ) {
2243 $auth_multisite_settings['ldap_lostpassword_url'] = '';
2244 }
2245 if ( ! array_key_exists( 'ldap_attr_first_name', $auth_multisite_settings ) ) {
2246 $auth_multisite_settings['ldap_attr_first_name'] = '';
2247 }
2248 if ( ! array_key_exists( 'ldap_attr_last_name', $auth_multisite_settings ) ) {
2249 $auth_multisite_settings['ldap_attr_last_name'] = '';
2250 }
2251 if ( ! array_key_exists( 'ldap_attr_update_on_login', $auth_multisite_settings ) ) {
2252 $auth_multisite_settings['ldap_attr_update_on_login'] = '';
2253 }
2254 // Advanced defaults.
2255 if ( ! array_key_exists( 'advanced_lockouts', $auth_multisite_settings ) ) {
2256 $auth_multisite_settings['advanced_lockouts'] = array(
2257 'attempts_1' => 10,
2258 'duration_1' => 1,
2259 'attempts_2' => 10,
2260 'duration_2' => 10,
2261 'reset_duration' => 120,
2262 );
2263 }
2264 if ( ! array_key_exists( 'advanced_hide_wp_login', $auth_multisite_settings ) ) {
2265 $auth_multisite_settings['advanced_hide_wp_login'] = '';
2266 }
2267 // Save default network options to database.
2268 update_blog_option( BLOG_ID_CURRENT_SITE, 'auth_multisite_settings', $auth_multisite_settings );
2269 update_blog_option( BLOG_ID_CURRENT_SITE, 'auth_multisite_settings_access_users_approved', $auth_multisite_settings_access_users_approved );
2270 }
2271 } // END set_default_options()
2272
2273
2274 /**
2275 * List sanitizer.
2276 * $side_effect = 'none' or 'update roles' to make sure WP user roles match
2277 * $multisite_mode = 'single' or 'multisite' to indicate which user roles to change (this site or all sites)
2278 */
2279 function sanitize_user_list( $list, $side_effect = 'none', $multisite_mode = 'single' ) {
2280 // If it's not a list, make it so.
2281 if ( ! is_array( $list ) ) {
2282 $list = array();
2283 }
2284 foreach ( $list as $key => $user_info ) {
2285 if ( strlen( $user_info['email'] ) < 1 ) {
2286 // Make sure there are no empty entries in the list
2287 unset( $list[$key] );
2288 } elseif ( $side_effect === 'update roles' ) {
2289 // Make sure the WordPress user accounts have the same role
2290 // as that indicated in the list.
2291 $wp_user = get_user_by( 'email', $user_info['email'] );
2292 if ( $wp_user ) {
2293 if ( is_multisite() && $multisite_mode === 'multisite' ) {
2294 foreach ( get_blogs_of_user( $wp_user->ID ) as $blog ) {
2295 add_user_to_blog( $blog->userblog_id, $wp_user->ID, $user_info['role'] );
2296 }
2297 } else {
2298 $wp_user->set_role( $user_info['role'] );
2299 }
2300 }
2301 }
2302 }
2303 return $list;
2304 }
2305
2306 /**
2307 * Settings sanitizer callback
2308 */
2309 function sanitize_options( $auth_settings, $multisite_mode = 'single' ) {
2310 // Default to "Approved Users" login access restriction.
2311 if ( ! in_array( $auth_settings['access_who_can_login'], array( 'external_users', 'approved_users' ) ) ) {
2312 $auth_settings['access_who_can_login'] = 'approved_users';
2313 }
2314
2315 // Default to "Everyone" view access restriction.
2316 if ( ! in_array( $auth_settings['access_who_can_view'], array( 'everyone', 'logged_in_users' ) ) ) {
2317 $auth_settings['access_who_can_view'] = 'everyone';
2318 }
2319
2320 // Default to WordPress login access redirect.
2321 if ( ! in_array( $auth_settings['access_redirect'], array( 'login', 'page', 'message' ) ) ) {
2322 $auth_settings['access_redirect'] = 'login';
2323 }
2324
2325 // Default to warning message for anonymous users on public pages.
2326 if ( ! in_array( $auth_settings['access_public_warning'], array( 'no_warning', 'warning' ) ) ) {
2327 $auth_settings['access_public_warning'] = 'no_warning';
2328 }
2329
2330 // Sanitize Enable Google Logins (checkbox: value can only be '1' or empty string)
2331 if ( array_key_exists( 'google', $auth_settings ) && strlen( $auth_settings['google'] ) > 0 ) {
2332 $auth_settings['google'] = '1';
2333 }
2334
2335 // Sanitize Enable CAS Logins (checkbox: value can only be '1' or empty string)
2336 if ( array_key_exists( 'cas', $auth_settings ) && strlen( $auth_settings['cas'] ) > 0 ) {
2337 $auth_settings['cas'] = '1';
2338 }
2339
2340 // Sanitize Enable LDAP Logins (checkbox: value can only be '1' or empty string)
2341 if ( array_key_exists( 'ldap', $auth_settings ) && strlen( $auth_settings['ldap'] ) > 0 ) {
2342 $auth_settings['ldap'] = '1';
2343 }
2344
2345 // Sanitize CAS Host setting
2346 $auth_settings['cas_host'] = filter_var( $auth_settings['cas_host'], FILTER_SANITIZE_URL );
2347
2348 // Sanitize CAS Port (int)
2349 $auth_settings['cas_port'] = filter_var( $auth_settings['cas_port'], FILTER_SANITIZE_NUMBER_INT );
2350
2351 // Sanitize CAS attribute update (checkbox: value can only be '1' or empty string)
2352 if ( array_key_exists( 'cas_attr_update_on_login', $auth_settings ) && strlen( $auth_settings['cas_attr_update_on_login'] ) > 0 ) {
2353 $auth_settings['cas_attr_update_on_login'] = '1';
2354 }
2355
2356 // Sanitize LDAP Host setting
2357 $auth_settings['ldap_host'] = filter_var( $auth_settings['ldap_host'], FILTER_SANITIZE_URL );
2358
2359 // Sanitize LDAP Port (int)
2360 $auth_settings['ldap_port'] = filter_var( $auth_settings['ldap_port'], FILTER_SANITIZE_NUMBER_INT );
2361
2362 // Sanitize LDAP attributes (basically make sure they don't have any parantheses)
2363 $auth_settings['ldap_uid'] = filter_var( $auth_settings['ldap_uid'], FILTER_SANITIZE_EMAIL );
2364
2365 // Sanitize LDAP TLS (checkbox: value can only be '1' or empty string)
2366 if ( array_key_exists( 'ldap_tls', $auth_settings ) && strlen( $auth_settings['ldap_tls'] ) > 0 ) {
2367 $auth_settings['ldap_tls'] = '1';
2368 }
2369
2370 // Sanitize LDAP Lost Password URL
2371 $auth_settings['ldap_lostpassword_url'] = filter_var( $auth_settings['ldap_lostpassword_url'], FILTER_SANITIZE_URL );
2372
2373 // Obfuscate LDAP directory user password
2374 if ( strlen( $auth_settings['ldap_password'] ) > 0 ) {
2375 // encrypt the directory user password for some minor obfuscation in the database.
2376 $auth_settings['ldap_password'] = base64_encode( $this->encrypt( $auth_settings['ldap_password'] ) );
2377 }
2378
2379 // Sanitize LDAP attribute update (checkbox: value can only be '1' or empty string)
2380 if ( array_key_exists( 'ldap_attr_update_on_login', $auth_settings ) && strlen( $auth_settings['ldap_attr_update_on_login'] ) > 0 ) {
2381 $auth_settings['ldap_attr_update_on_login'] = '1';
2382 }
2383
2384 // Make sure public pages is an empty array if it's empty
2385 if ( ! is_array( $auth_settings['access_public_pages'] ) ) {
2386 $auth_settings['access_public_pages'] = array();
2387 }
2388
2389 // Make sure all lockout options are integers (attempts_1,
2390 // duration_1, attempts_2, duration_2, reset_duration).
2391 foreach ( $auth_settings['advanced_lockouts'] as $key => $value ) {
2392 $auth_settings['advanced_lockouts'][$key] = filter_var( $value, FILTER_SANITIZE_NUMBER_INT );
2393 }
2394
2395 // Sanitize Hide WordPress logins (checkbox: value can only be '1' or empty string)
2396 if ( array_key_exists( 'advanced_hide_wp_login', $auth_settings ) && strlen( $auth_settings['advanced_hide_wp_login'] ) > 0 ) {
2397 $auth_settings['advanced_hide_wp_login'] = '1';
2398 }
2399
2400 return $auth_settings;
2401 } // END sanitize_options()
2402
2403
2404 /**
2405 * Keep authorizer approved users' roles in sync with WordPress roles
2406 * if someone changes the role via the WordPress Edit User options page.
2407 *
2408 * @action edit_user_profile_update
2409 * @ref https://codex.wordpress.org/Plugin_API/Action_Reference/edit_user_profile_update
2410 * @param int $user_id The user ID of the user being edited
2411 */
2412 function edit_user_profile_update_role( $user_id ) {
2413 if ( ! current_user_can( 'edit_user', $user_id ) ) {
2414 return;
2415 }
2416
2417 // If user is in approved list, update his/her associated role.
2418 $wp_user = get_user_by( 'id', $user_id );
2419 if ( $this->is_email_in_list( $wp_user->get( 'user_email' ), 'approved' ) ) {
2420 $auth_settings_access_users_approved = $this->sanitize_user_list(
2421 $this->get_plugin_option( 'access_users_approved', 'single admin' )
2422 );
2423 // Find approved user and update their role.
2424 foreach ( $auth_settings_access_users_approved as $key => $user ) {
2425 if ( $user['email'] === $wp_user->get( 'user_email' ) ) {
2426 $auth_settings_access_users_approved[$key]['role'] = $_REQUEST['role'];
2427 }
2428 }
2429
2430 update_option( 'auth_settings_access_users_approved', $auth_settings_access_users_approved );
2431 }
2432 }
2433
2434 /**
2435 * Settings print callbacks
2436 */
2437 function print_section_info_tabs( $args = '' ) {
2438 if ( is_array( $args ) && array_key_exists( 'multisite_admin', $args ) && $args['multisite_admin'] === true ): ?>
2439 <h2 class="nav-tab-wrapper">
2440 <a class="nav-tab nav-tab-access_lists nav-tab-active" href="javascript:choose_tab('access_lists');">Access Lists</a>
2441 <a class="nav-tab nav-tab-external" href="javascript:choose_tab('external');">External Service</a>
2442 <a class="nav-tab nav-tab-advanced" href="javascript:choose_tab('advanced');">Advanced</a>
2443 </h2>
2444 <?php else: ?>
2445 <h2 class="nav-tab-wrapper">
2446 <a class="nav-tab nav-tab-access_lists nav-tab-active" href="javascript:choose_tab('access_lists');">Access Lists</a>
2447 <a class="nav-tab nav-tab-access_login" href="javascript:choose_tab('access_login');">Login Access</a>
2448 <a class="nav-tab nav-tab-access_public" href="javascript:choose_tab('access_public');">Public Access</a>
2449 <a class="nav-tab nav-tab-external" href="javascript:choose_tab('external');">External Service</a>
2450 <a class="nav-tab nav-tab-advanced" href="javascript:choose_tab('advanced');">Advanced</a>
2451 </h2>
2452 <?php endif;
2453 } // END print_section_info_tabs()
2454
2455
2456 function print_section_info_access_lists( $args = '' ) {
2457 ?><div id="section_info_access_lists" class="section_info">
2458 <p>Manage who has access to this site using these lists.</p>
2459 <ol>
2460 <li><strong>Pending</strong> users are users who have successfully logged in to the site, but who haven't yet been approved (or blocked) by you.</li>
2461 <li><strong>Approved</strong> users have access to the site once they successfully log in.</li>
2462 <li><strong>Blocked</strong> users will receive an error message when they try to visit the site after authenticating.</li>
2463 </ol>
2464 </div>
2465 <table class="form-table">
2466 <tbody>
2467 <tr>
2468 <th scope="row">Pending Users</th>
2469 <td><?php $this->print_combo_auth_access_users_pending(); ?></td>
2470 </tr>
2471 <tr>
2472 <th scope="row">Approved Users</th>
2473 <td><?php $this->print_combo_auth_access_users_approved(); ?></td>
2474 </tr>
2475 <tr>
2476 <th scope="row">Blocked Users</th>
2477 <td><?php $this->print_combo_auth_access_users_blocked(); ?></td>
2478 </tr>
2479 </tbody>
2480 </table>
2481 <?php
2482 } // END print_section_info_access_lists()
2483
2484 function print_combo_auth_access_users_pending( $args = '' ) {
2485 // Get plugin option.
2486 $option = 'access_users_pending';
2487 $auth_settings_option = $this->get_plugin_option( $option );
2488 $auth_settings_option = is_array( $auth_settings_option ) ? $auth_settings_option : array();
2489
2490 // Print option elements.
2491 ?><ul id="list_auth_settings_access_users_pending" style="margin:0;">
2492 <?php if ( count( $auth_settings_option ) > 0 ) : ?>
2493 <?php foreach ( $auth_settings_option as $key => $pending_user ): ?>
2494 <?php if ( empty( $pending_user ) || count( $pending_user ) < 1 ) continue; ?>
2495 <?php $pending_user['is_wp_user'] = false; ?>
2496 <li>
2497 <input type="text" id="auth_settings_<?php echo $option; ?>_<?php echo $key; ?>" value="<?php echo $pending_user['email']; ?>" readonly="true" class="auth-email" />
2498 <select id="auth_settings_<?php echo $option; ?>_<?php echo $key; ?>_role" class="auth-role">
2499 <?php $this->wp_dropdown_permitted_roles( $pending_user['role'] ); ?>
2500 </select>
2501 <a href="javascript:void(0);" class="button-primary" id="approve_user_<?php echo $key; ?>" onclick="auth_add_user( this, 'approved', false ); auth_ignore_user( this, 'pending' );"><span class="glyphicon glyphicon-ok"></span> Approve</a>
2502 <a href="javascript:void(0);" class="button-primary" id="block_user_<?php echo $key; ?>" onclick="auth_add_user( this, 'blocked', false ); auth_ignore_user( this, 'pending' );"><span class="glyphicon glyphicon-ban-circle"></span> Block</a>
2503 <a href="javascript:void(0);" class="button button-secondary" id="ignore_user_<?php echo $key; ?>" onclick="auth_ignore_user( this, 'pending' );" title="Remove user"><span class="glyphicon glyphicon-remove"></span> Ignore</a>
2504 </li>
2505 <?php endforeach; ?>
2506 <?php else: ?>
2507 <li class="auth-empty"><em>No pending users</em></li>
2508 <?php endif; ?>
2509 </ul>
2510 <?php
2511 } // END print_combo_auth_access_users_pending()
2512
2513 function print_combo_auth_access_users_approved( $args = '' ) {
2514 // Get plugin option.
2515 $option = 'access_users_approved';
2516 $admin_mode = ( is_array( $args ) && array_key_exists( 'multisite_admin', $args ) && $args['multisite_admin'] === true ) ? 'multisite admin' : 'single admin';
2517 $auth_settings_option = $this->get_plugin_option( $option, $admin_mode, 'no override' );
2518 $auth_settings_option = is_array( $auth_settings_option ) ? $auth_settings_option : array();
2519
2520 // Get multisite approved users (add them to top of list, greyed out).
2521 $auth_override_multisite = $this->get_plugin_option( 'advanced_override_multisite' );
2522 $auth_multisite_settings = $this->get_plugin_options( 'multisite admin' );
2523 $option_multisite = 'access_users_approved';
2524 $auth_settings_option_multisite = array();
2525 if (
2526 is_multisite() &&
2527 $auth_override_multisite != '1' &&
2528 array_key_exists( 'multisite_override', $auth_multisite_settings ) &&
2529 $auth_multisite_settings['multisite_override'] === '1'
2530 ) {
2531 $auth_settings_option_multisite = $this->get_plugin_option( $option, 'multisite admin', 'allow override' );
2532 $auth_settings_option_multisite = is_array( $auth_settings_option_multisite ) ? $auth_settings_option_multisite : array();
2533 }
2534
2535 // Get default role for new user dropdown.
2536 $access_default_role = $this->get_plugin_option( 'access_default_role', 'single admin', 'allow override' );
2537
2538 // Get custom usermeta field to show.
2539 $advanced_usermeta = $this->get_plugin_option( 'advanced_usermeta' );
2540
2541 // Adjust javascript function prefixes if multisite.
2542 $js_function_prefix = $admin_mode === 'multisite admin' ? 'auth_multisite_' : 'auth_';
2543 $multisite_admin_page = $admin_mode === 'multisite admin';
2544
2545 ?><ul id="list_auth_settings_access_users_approved" style="margin:0;">
2546 <?php if ( ! $multisite_admin_page ) :
2547 foreach ( $auth_settings_option_multisite as $key => $approved_user ) :
2548 if ( empty( $approved_user ) || count( $approved_user ) < 1 ) :
2549 continue;
2550 endif;
2551 $approved_wp_user = get_user_by( 'email', $approved_user['email'] );
2552 if ( $approved_wp_user ) :
2553 $approved_user['email'] = $approved_wp_user->user_email;
2554 $approved_user['role'] = $multisite_admin_page || count( $approved_wp_user->roles ) === 0 ? $approved_user['role'] : array_shift( $approved_wp_user->roles );
2555 $approved_user['date_added'] = $approved_wp_user->user_registered;
2556 // Get usermeta field from the WordPress user's real usermeta.
2557 if ( strlen( $advanced_usermeta ) > 0 ) :
2558 if ( strpos( $advanced_usermeta, 'acf___' ) === 0 && class_exists( 'acf' ) ) :
2559 // Get ACF Field value for the user
2560 $approved_user['usermeta'] = get_field( str_replace('acf___', '', $advanced_usermeta ), 'user_' . $approved_wp_user->ID );
2561 else :
2562 // Get regular usermeta value for the user.
2563 $approved_user['usermeta'] = get_user_meta( $approved_wp_user->ID, $advanced_usermeta, true );
2564 endif;
2565
2566 if ( is_array( $approved_user['usermeta'] ) || is_object( $approved_user['usermeta'] ) ) :
2567 $approved_user['usermeta'] = serialize( $approved_user['usermeta'] );
2568 endif;
2569 endif;
2570 endif;
2571 if ( ! array_key_exists( 'usermeta', $approved_user ) ) :
2572 $approved_user['usermeta'] = '';
2573 endif; ?>
2574 <li>
2575 <input type="text" id="auth_multisite_settings_<?php echo $option; ?>_<?php echo $key; ?>" value="<?php echo $approved_user['email']; ?>" readonly="true" class="auth-email auth-multisite-email" />
2576 <select id="auth_multisite_settings_<?php echo $option; ?>_<?php echo $key; ?>_role" class="auth-role auth-multisite-role" disabled="disabled">
2577 <?php $this->wp_dropdown_permitted_roles( $approved_user['role'] ); ?>
2578 </select>
2579 <input type="text" id="auth_multisite_settings_<?php echo $option; ?>_<?php echo $key; ?>_date_added" value="<?php echo date( 'M Y', strtotime( $approved_user['date_added'] ) ); ?>" readonly="true" class="auth-date-added auth-multisite-date-added" disabled="disabled" />
2580 <?php if ( strlen( $advanced_usermeta ) > 0 ) :
2581 $should_show_usermeta_in_text_field = true; // Fallback renderer for usermeta; try to use a select first.
2582 if ( strpos( $advanced_usermeta, 'acf___' ) === 0 && class_exists( 'acf' ) ) :
2583 $field_object = get_field_object( str_replace('acf___', '', $advanced_usermeta ) );
2584 if ( is_array( $field_object ) && array_key_exists( 'type', $field_object ) && $field_object['type'] === 'select' ) :
2585 $should_show_usermeta_in_text_field = false; ?>
2586 <select id="auth_settings_<?php echo $option; ?>_<?php echo $key; ?>_usermeta" class="auth-usermeta auth-multisite-usermeta" onchange="<?php echo $js_function_prefix; ?>update_usermeta( this );">
2587 <option value=""<?php if ( empty( $approved_user['usermeta'] ) ) echo ' selected="selected"'; ?>>-- None --</option>
2588 <?php foreach ( $field_object['choices'] as $key => $label ) : ?>
2589 <option value="<?php echo $key; ?>"<?php if ( $key === $approved_user['usermeta'] || ( is_array( $approved_user['usermeta'] ) && array_key_exists( get_current_blog_id(), $approved_user['usermeta'] ) && $key === $approved_user['usermeta'][get_current_blog_id()]['meta_value'] ) ) echo ' selected="selected"'; ?>><?php echo $label; ?></option>
2590 <?php endforeach; ?>
2591 </select>
2592 <?php endif; ?>
2593 <?php endif; ?>
2594 <?php if ( $should_show_usermeta_in_text_field ) : ?>
2595 <input type="text" id="auth_multisite_settings_<?php echo $option; ?>_<?php echo $key; ?>_usermeta" value="<?php echo htmlspecialchars( $approved_user['usermeta'], ENT_COMPAT ); ?>" class="auth-usermeta auth-multisite-usermeta" />
2596 <a class="button button-small button-primary update-usermeta" id="update_usermeta_<?php echo $key; ?>" onclick="<?php echo $js_function_prefix; ?>update_usermeta( this );" title="Update usermeta"><span class="glyphicon glyphicon-floppy-saved"></span></a>
2597 <?php endif; ?>
2598 <?php endif; ?>
2599 &nbsp;&nbsp;<a title="WordPress Multisite user" class="auth-multisite-user"><span class="glyphicon glyphicon-globe"></span></a>
2600 </li>
2601 <?php endforeach;
2602 endif;
2603 foreach ( $auth_settings_option as $key => $approved_user ):
2604 $is_current_user = false;
2605 $local_user_icon = array_key_exists( 'local_user', $approved_user ) && $approved_user['local_user'] === 'true' ? '&nbsp;<a title="Local WordPress user" class="auth-local-user"><span class="glyphicon glyphicon-user"></span></a>' : '';
2606 if ( empty( $approved_user ) || count( $approved_user ) < 1 ) :
2607 continue;
2608 endif;
2609 $approved_wp_user = get_user_by( 'email', $approved_user['email'] );
2610 if ( $approved_wp_user ) :
2611 $approved_user['email'] = $approved_wp_user->user_email;
2612 $approved_user['role'] = $multisite_admin_page || count( $approved_wp_user->roles ) === 0 ? $approved_user['role'] : array_shift( $approved_wp_user->roles );
2613 $approved_user['date_added'] = $approved_wp_user->user_registered;
2614 $approved_user['is_wp_user'] = true;
2615 $is_current_user = $approved_wp_user->ID === get_current_user_id();
2616 // Get usermeta field from the WordPress user's real usermeta.
2617 if ( strlen( $advanced_usermeta ) > 0 ) :
2618 if ( strpos( $advanced_usermeta, 'acf___' ) === 0 && class_exists( 'acf' ) ) :
2619 // Get ACF Field value for the user
2620 $approved_user['usermeta'] = get_field( str_replace('acf___', '', $advanced_usermeta ), 'user_' . $approved_wp_user->ID );
2621 else :
2622 // Get regular usermeta value for the user.
2623 $approved_user['usermeta'] = get_user_meta( $approved_wp_user->ID, $advanced_usermeta, true );
2624 endif;
2625
2626 if ( is_array( $approved_user['usermeta'] ) || is_object( $approved_user['usermeta'] ) ) :
2627 $approved_user['usermeta'] = serialize( $approved_user['usermeta'] );
2628 endif;
2629 endif;
2630 else :
2631 $approved_user['is_wp_user'] = false;
2632 endif;
2633 if ( ! array_key_exists( 'usermeta', $approved_user ) ) :
2634 $approved_user['usermeta'] = '';
2635 endif; ?>
2636 <li>
2637 <input type="text" id="auth_settings_<?php echo $option; ?>_<?php echo $key; ?>" value="<?php echo $approved_user['email']; ?>" readonly="true" class="auth-email" />
2638 <select id="auth_settings_<?php echo $option; ?>_<?php echo $key; ?>_role" class="auth-role" onchange="<?php echo $js_function_prefix; ?>change_role( this );">
2639 <?php $disable_input = $is_current_user ? 'disabled' : null; ?>
2640 <?php $this->wp_dropdown_permitted_roles( $approved_user['role'], $disable_input ); ?>
2641 </select>
2642 <input type="text" id="auth_settings_<?php echo $option; ?>_<?php echo $key; ?>_date_added" value="<?php echo date( 'M Y', strtotime( $approved_user['date_added'] ) ); ?>" readonly="true" class="auth-date-added" />
2643 <?php if ( strlen( $advanced_usermeta ) > 0 ) :
2644 $should_show_usermeta_in_text_field = true; // Fallback renderer for usermeta; try to use a select first.
2645 if ( strpos( $advanced_usermeta, 'acf___' ) === 0 && class_exists( 'acf' ) ) :
2646 $field_object = get_field_object( str_replace('acf___', '', $advanced_usermeta ) );
2647 if ( is_array( $field_object ) && array_key_exists( 'type', $field_object ) && $field_object['type'] === 'select' ) :
2648 $should_show_usermeta_in_text_field = false; ?>
2649 <select id="auth_settings_<?php echo $option; ?>_<?php echo $key; ?>_usermeta" class="auth-usermeta" onchange="<?php echo $js_function_prefix; ?>update_usermeta( this );" >
2650 <option value=""<?php if ( empty( $approved_user['usermeta'] ) ) echo ' selected="selected"'; ?>>-- None --</option>
2651 <?php foreach ( $field_object['choices'] as $key => $label ) : ?>
2652 <option value="<?php echo $key; ?>"<?php if ( $key === $approved_user['usermeta'] || ( is_array( $approved_user['usermeta'] ) && $key === $approved_user['usermeta']['meta_value'] ) ) echo ' selected="selected"'; ?>><?php echo $label; ?></option>
2653 <?php endforeach; ?>
2654 </select>
2655 <?php endif; ?>
2656 <?php endif; ?>
2657 <?php if ( $should_show_usermeta_in_text_field ) : ?>
2658 <input type="text" id="auth_settings_<?php echo $option; ?>_<?php echo $key; ?>_usermeta" value="<?php echo htmlspecialchars( $approved_user['usermeta'], ENT_COMPAT ); ?>" class="auth-usermeta" />
2659 <a class="button button-small button-primary update-usermeta" id="update_usermeta_<?php echo $key; ?>" onclick="<?php echo $js_function_prefix; ?>update_usermeta( this );" title="Update usermeta"><span class="glyphicon glyphicon-floppy-saved"></span></a>
2660 <?php endif; ?>
2661 <?php endif; ?>
2662 <?php if ( ! $is_current_user ): ?>
2663 <?php if ( ! $multisite_admin_page ) : ?>
2664 <a class="button" id="block_user_<?php echo $key; ?>" onclick="<?php echo $js_function_prefix; ?>add_user( this, 'blocked', false ); <?php echo $js_function_prefix; ?>ignore_user( this, 'approved' );" title="Block/Ban user"><span class="glyphicon glyphicon-ban-circle"></span></a>
2665 <?php endif; ?>
2666 <a class="button" id="ignore_user_<?php echo $key; ?>" onclick="<?php echo $js_function_prefix; ?>ignore_user(this, 'approved');" title="Remove user"><span class="glyphicon glyphicon-remove"></span></a>
2667 <?php endif; ?>
2668 <?php echo $local_user_icon; ?>
2669 </li>
2670 <?php endforeach; ?>
2671 </ul>
2672 <div id="new_auth_settings_<?php echo $option; ?>">
2673 <input type="text" id="new_approved_user_email" placeholder="email address" class="auth-email new" />
2674 <select id="new_approved_user_role" class="auth-role">
2675 <?php $this->wp_dropdown_permitted_roles( $access_default_role ); ?>
2676 </select>
2677 <div class="btn-group">
2678 <a href="javascript:void(0);" class="btn button-primary dropdown-toggle" id="approve_user_new" onclick="<?php echo $js_function_prefix; ?>add_user(this, 'approved');"><span class="glyphicon glyphicon-ok"></span> Approve</a>
2679 <button type="button" class="btn button-primary dropdown-toggle" data-toggle="dropdown">
2680 <span class="caret"></span>
2681 <span class="sr-only">Toggle Dropdown</span>
2682 </button>
2683 <ul class="dropdown-menu" role="menu">
2684 <li><a href="javascript:void(0);" onclick="<?php echo $js_function_prefix; ?>add_user( document.getElementById('approve_user_new'), 'approved', true);">Create a local WordPress <br />account instead, and email <br />the user their password.</a></li>
2685 </ul>
2686 </div>
2687 </div>
2688 <?php
2689 } // END print_combo_auth_access_users_approved()
2690
2691 function print_combo_auth_access_users_blocked( $args = '' ) {
2692 // Get plugin option.
2693 $option = 'access_users_blocked';
2694 $auth_settings_option = $this->get_plugin_option( $option );
2695 $auth_settings_option = is_array( $auth_settings_option ) ? $auth_settings_option : array();
2696
2697 // Get default role for new blocked user dropdown.
2698 $access_default_role = $this->get_plugin_option( 'access_default_role', 'single admin', 'allow override' );
2699
2700 // Print option elements.
2701 ?><ul id="list_auth_settings_<?php echo $option; ?>" style="margin:0;">
2702 <?php foreach ( $auth_settings_option as $key => $blocked_user ): ?>
2703 <?php if ( empty( $blocked_user ) || count( $blocked_user ) < 1 ) continue; ?>
2704 <?php if ( $blocked_wp_user = get_user_by( 'email', $blocked_user['email'] ) ): ?>
2705 <?php $blocked_user['email'] = $blocked_wp_user->user_email; ?>
2706 <?php $blocked_user['role'] = array_shift( $blocked_wp_user->roles ); ?>
2707 <?php $blocked_user['date_added'] = $blocked_wp_user->user_registered; ?>
2708 <?php $blocked_user['is_wp_user'] = true; ?>
2709 <?php else: ?>
2710 <?php $blocked_user['is_wp_user'] = false; ?>
2711 <?php endif; ?>
2712 <li>
2713 <input type="text" id="auth_settings_<?php echo $option; ?>_<?php echo $key; ?>" value="<?php echo $blocked_user['email']; ?>" readonly="true" class="auth-email" />
2714 <select id="auth_settings_<?php echo $option; ?>_<?php echo $key; ?>_role" class="auth-role">
2715 <?php $this->wp_dropdown_permitted_roles( $blocked_user['role'] ); ?>
2716 </select>
2717 <input type="text" id="auth_settings_<?php echo $option; ?>_<?php echo $key; ?>_date_added" value="<?php echo date( 'M Y', strtotime( $blocked_user['date_added'] ) ); ?>" readonly="true" class="auth-date-added" />
2718 <a class="button" id="ignore_user_<?php echo $key; ?>" onclick="auth_ignore_user(this, 'blocked');" title="Remove user"><span class="glyphicon glyphicon-remove"></span></a>
2719 </li>
2720 <?php endforeach; ?>
2721 </ul>
2722 <div id="new_auth_settings_<?php echo $option; ?>">
2723 <input type="text" id="new_blocked_user_email" placeholder="email address" class="auth-email new" />
2724 <select id="new_blocked_user_role" class="auth-role">
2725 <option value="<?php echo $access_default_role; ?>"><?php echo ucfirst( $access_default_role ); ?></option>
2726 </select>
2727 <a href="javascript:void(0);" class="button-primary" id="block_user_new" onclick="auth_add_user(this, 'blocked');"><span class="glyphicon glyphicon-ban-circle"></span> Block</a>
2728 </div>
2729 <?php
2730 } // END print_combo_auth_access_users_blocked()
2731
2732
2733 function print_section_info_access_login( $args = '' ) {
2734 ?><div id="section_info_access_login" class="section_info">
2735 <?php wp_nonce_field( 'save_auth_settings', 'nonce_save_auth_settings' ); ?>
2736 <p>Choose who is able to log into this site below.</p>
2737 </div><?php
2738 } // END print_section_info_access_login()
2739
2740 function print_radio_auth_access_who_can_login( $args = '' ) {
2741 // Get plugin option.
2742 $option = 'access_who_can_login';
2743 $admin_mode = ( is_array( $args ) && array_key_exists( 'multisite_admin', $args ) && $args['multisite_admin'] === true ) ? 'multisite admin' : 'single admin';
2744 $auth_settings_option = $this->get_plugin_option( $option, $admin_mode, 'allow override', 'print overlay' );
2745
2746 // If this site is configured independently of any multisite overrides, make sure we are not grabbing the multisite value; otherwise, grab the multisite value to show behind the disabled overlay.
2747 if ( is_multisite() && $this->get_plugin_option( 'advanced_override_multisite' ) == '1' ) {
2748 $auth_settings_option = $this->get_plugin_option( $option );
2749 } else if ( is_multisite() && $admin_mode === 'single admin' && $this->get_plugin_option( 'multisite_override', 'multisite admin' ) === '1' ) {
2750 // Workaround: javascript code hides/shows other settings based
2751 // on the selection in this option. If this option is overridden
2752 // by a multisite option, it should show that value in order to
2753 // correctly display the other appropriate options.
2754 // Side effect: this site option will be overwritten by the
2755 // multisite option on save. Since this is a 2-item radio, we
2756 // determined this was acceptable.
2757 $auth_settings_option = $this->get_plugin_option( $option, 'multisite admin' );
2758 }
2759
2760 // Print option elements.
2761 ?><input type="radio" id="radio_auth_settings_<?php echo $option; ?>_external_users" name="auth_settings[<?php echo $option; ?>]" value="external_users"<?php checked( 'external_users' == $auth_settings_option ); ?> /><label for="radio_auth_settings_<?php echo $option; ?>_external_users">All authenticated users (All external service users and all WordPress users)</label><br />
2762 <input type="radio" id="radio_auth_settings_<?php echo $option; ?>_approved_users" name="auth_settings[<?php echo $option; ?>]" value="approved_users"<?php checked( 'approved_users' == $auth_settings_option ); ?> /><label for="radio_auth_settings_<?php echo $option; ?>_approved_users">Only <a href="javascript:choose_tab('access_lists');" id="dashboard_link_approved_users">approved users</a> (Approved external users and all WordPress users)</label><br /><?php
2763 } // END print_radio_auth_access_who_can_login()
2764
2765 function print_select_auth_access_role_receive_pending_emails( $args = '' ) {
2766 // Get plugin option.
2767 $option = 'access_role_receive_pending_emails';
2768 $auth_settings_option = $this->get_plugin_option( $option );
2769
2770 // Print option elements.
2771 ?><select id="auth_settings_<?php echo $option; ?>" name="auth_settings[<?php echo $option; ?>]">
2772 <option value="---" <?php selected( $auth_settings_option, '---' ); ?>>None (Don't send notification emails)</option>
2773 <?php wp_dropdown_roles( $auth_settings_option ); ?>
2774 </select><?php
2775 } // END print_select_auth_access_role_receive_pending_emails()
2776
2777 function print_wysiwyg_auth_access_pending_redirect_to_message( $args = '' ) {
2778 // Get plugin option.
2779 $option = 'access_pending_redirect_to_message';
2780 $auth_settings_option = $this->get_plugin_option( $option );
2781
2782 // Print option elements.
2783 wp_editor(
2784 wpautop( $auth_settings_option ),
2785 "auth_settings_$option",
2786 array(
2787 'media_buttons' => false,
2788 'textarea_name' => "auth_settings[$option]",
2789 'textarea_rows' => 5,
2790 'tinymce' => true,
2791 'teeny' => true,
2792 'quicktags' => false,
2793 )
2794 );
2795 } // END print_wysiwyg_auth_access_pending_redirect_to_message()
2796
2797 function print_wysiwyg_auth_access_blocked_redirect_to_message( $args = '' ) {
2798 // Get plugin option.
2799 $option = 'access_blocked_redirect_to_message';
2800 $auth_settings_option = $this->get_plugin_option( $option );
2801
2802 // Print option elements.
2803 wp_editor(
2804 wpautop( $auth_settings_option ),
2805 "auth_settings_$option",
2806 array(
2807 'media_buttons' => false,
2808 'textarea_name' => "auth_settings[$option]",
2809 'textarea_rows' => 5,
2810 'tinymce' => true,
2811 'teeny' => true,
2812 'quicktags' => false,
2813 )
2814 );
2815 } // END print_wysiwyg_auth_access_blocked_redirect_to_message()
2816
2817 function print_checkbox_auth_access_should_email_approved_users( $args = '' ) {
2818 // Get plugin option.
2819 $option = 'access_should_email_approved_users';
2820 $auth_settings_option = $this->get_plugin_option( $option );
2821
2822 // Print option elements.
2823 ?><input type="checkbox" id="auth_settings_<?php echo $option; ?>" name="auth_settings[<?php echo $option; ?>]" value="1"<?php checked( 1 == $auth_settings_option ); ?> /><label for="auth_settings_<?php echo $option; ?>">Send a welcome email when approving a new user</label><?php
2824 } // END print_checkbox_auth_external_ldap()
2825
2826 function print_text_auth_access_email_approved_users_subject( $args = '' ) {
2827 // Get plugin option.
2828 $option = 'access_email_approved_users_subject';
2829 $auth_settings_option = $this->get_plugin_option( $option );
2830
2831 // Print option elements.
2832 ?><input type="text" id="auth_settings_<?php echo $option; ?>" name="auth_settings[<?php echo $option; ?>]" value="<?php echo $auth_settings_option; ?>" placeholder="Welcome to [site_name]!" style="width:320px;" /><br /><small>You can use the <b>[site_name]</b> shortcode.</small><?php
2833 } // END print_text_auth_access_email_approved_users_subject()
2834
2835 function print_wysiwyg_auth_access_email_approved_users_body( $args = '' ) {
2836 // Get plugin option.
2837 $option = 'access_email_approved_users_body';
2838 $auth_settings_option = $this->get_plugin_option( $option );
2839
2840 // Print option elements.
2841 wp_editor(
2842 wpautop( $auth_settings_option ),
2843 "auth_settings_$option",
2844 array(
2845 'media_buttons' => false,
2846 'textarea_name' => "auth_settings[$option]",
2847 'textarea_rows' => 9,
2848 'tinymce' => true,
2849 'teeny' => true,
2850 'quicktags' => false,
2851 )
2852 );
2853
2854 ?><small>You can use <b>[site_name]</b>, <b>[site_url]</b>, and <b>[user_email]</b> shortcodes.</small><?php
2855
2856 } // END print_wysiwyg_auth_access_email_approved_users_body()
2857
2858
2859 function print_section_info_access_public( $args = '' ) {
2860 ?><div id="section_info_access_public" class="section_info">
2861 <p>Choose your public access options here.</p>
2862 </div><?php
2863 } // END print_section_info_access_public()
2864
2865 function print_radio_auth_access_who_can_view( $args = '' ) {
2866 // Get plugin option.
2867 $option = 'access_who_can_view';
2868 $admin_mode = ( is_array( $args ) && array_key_exists( 'multisite_admin', $args ) && $args['multisite_admin'] === true ) ? 'multisite admin' : 'single admin';
2869 $auth_settings_option = $this->get_plugin_option( $option, $admin_mode, 'allow override', 'print overlay' );
2870
2871 // If this site is configured independently of any multisite overrides, make sure we are not grabbing the multisite value; otherwise, grab the multisite value to show behind the disabled overlay.
2872 if ( is_multisite() && $this->get_plugin_option( 'advanced_override_multisite' ) == '1' ) {
2873 $auth_settings_option = $this->get_plugin_option( $option );
2874 } else if ( is_multisite() && $admin_mode === 'single admin' && $this->get_plugin_option( 'multisite_override', 'multisite admin' ) === '1' ) {
2875 // Workaround: javascript code hides/shows other settings based
2876 // on the selection in this option. If this option is overridden
2877 // by a multisite option, it should show that value in order to
2878 // correctly display the other appropriate options.
2879 // Side effect: this site option will be overwritten by the
2880 // multisite option on save. Since this is a 2-item radio, we
2881 // determined this was acceptable.
2882 $auth_settings_option = $this->get_plugin_option( $option, 'multisite admin' );
2883 }
2884
2885 // Print option elements.
2886 ?><input type="radio" id="radio_auth_settings_<?php echo $option; ?>_everyone" name="auth_settings[<?php echo $option; ?>]" value="everyone"<?php checked( 'everyone' == $auth_settings_option ); ?> /><label for="radio_auth_settings_<?php echo $option; ?>_everyone">Everyone can see the site</label><br />
2887 <input type="radio" id="radio_auth_settings_<?php echo $option; ?>_logged_in_users" name="auth_settings[<?php echo $option; ?>]" value="logged_in_users"<?php checked( 'logged_in_users' == $auth_settings_option ); ?> /><label for="radio_auth_settings_<?php echo $option; ?>_logged_in_users">Only logged in users can see the site</label><br /><?php
2888 } // END print_radio_auth_access_who_can_view()
2889
2890 function print_radio_auth_access_redirect( $args = '' ) {
2891 // Get plugin option.
2892 $option = 'access_redirect';
2893 $auth_settings_option = $this->get_plugin_option( $option );
2894
2895 // Print option elements.
2896 ?><input type="radio" id="radio_auth_settings_<?php echo $option; ?>_to_login" name="auth_settings[<?php echo $option; ?>]" value="login"<?php checked( 'login' == $auth_settings_option ); ?> /><label for="radio_auth_settings_<?php echo $option; ?>_to_login">Send them to the login screen</label><br />
2897 <input type="radio" id="radio_auth_settings_<?php echo $option; ?>_to_message" name="auth_settings[<?php echo $option; ?>]" value="message"<?php checked( 'message' == $auth_settings_option ); ?> /><label for="radio_auth_settings_<?php echo $option; ?>_to_message">Show them the anonymous access message (below)</label><?php
2898 } // END print_radio_auth_access_redirect()
2899
2900 function print_radio_auth_access_public_warning( $args = '' ) {
2901 // Get plugin option.
2902 $option = 'access_public_warning';
2903 $auth_settings_option = $this->get_plugin_option( $option );
2904
2905 // Print option elements.
2906 ?><input type="radio" id="radio_auth_settings_<?php echo $option; ?>_no" name="auth_settings[<?php echo $option; ?>]" value="no_warning"<?php checked( 'no_warning' == $auth_settings_option ); ?> /><label for="radio_auth_settings_<?php echo $option; ?>_no">Show them the page <strong>without</strong> the anonymous access message</label><br />
2907 <input type="radio" id="radio_auth_settings_<?php echo $option; ?>" name="auth_settings[<?php echo $option; ?>]" value="warning"<?php checked( 'warning' == $auth_settings_option ); ?> /><label for="radio_auth_settings_<?php echo $option; ?>">Show them the page <strong>with</strong> the anonymous access message (marked up as a <a href="http://getbootstrap.com/components/#alerts-dismissible" target="_blank">Bootstrap Dismissible Alert</a>)</label><?php
2908 } // END print_radio_auth_access_public_warning()
2909
2910 function print_wysiwyg_auth_access_redirect_to_message( $args = '' ) {
2911 // Get plugin option.
2912 $option = 'access_redirect_to_message';
2913 $auth_settings_option = $this->get_plugin_option( $option );
2914
2915 // Print option elements.
2916 wp_editor(
2917 wpautop( $auth_settings_option ),
2918 "auth_settings_$option",
2919 array(
2920 'media_buttons' => false,
2921 'textarea_name' => "auth_settings[$option]",
2922 'textarea_rows' => 5,
2923 'tinymce' => true,
2924 'teeny' => true,
2925 'quicktags' => false,
2926 )
2927 );
2928 } // END print_wysiwyg_auth_access_redirect_to_message()
2929
2930 function print_multiselect_auth_access_public_pages( $args = '' ) {
2931 // Get plugin option.
2932 $option = 'access_public_pages';
2933 $auth_settings_option = $this->get_plugin_option( $option );
2934 $auth_settings_option = is_array( $auth_settings_option ) ? $auth_settings_option : array();
2935
2936 $post_types = array_merge( array( 'page', 'post' ), get_post_types( array( '_builtin' => false ), 'names' ) );
2937 $post_types = is_array( $post_types ) ? $post_types : array();
2938
2939 // Print option elements.
2940 ?><select id="auth_settings_<?php echo $option; ?>" multiple="multiple" name="auth_settings[<?php echo $option; ?>][]">
2941 <optgroup label="Home">
2942 <option value="home" <?php echo in_array( 'home', $auth_settings_option ) ? 'selected="selected"' : ''; ?>>Home Page</option>
2943 </optgroup>
2944 <?php foreach ( $post_types as $post_type ): ?>
2945 <optgroup label="<?php echo ucfirst( $post_type ); ?>">
2946 <?php $pages = get_posts( array( 'post_type' => $post_type, 'posts_per_page' => -1 ) ); ?>
2947 <?php $pages = is_array( $pages ) ? $pages : array(); ?>
2948 <?php foreach ( $pages as $page ): ?>
2949 <option value="<?php echo $page->ID; ?>" <?php echo in_array( $page->ID, $auth_settings_option ) ? 'selected="selected"' : ''; ?>><?php echo $page->post_title; ?></option>
2950 <?php endforeach; ?>
2951 </optgroup>
2952 <?php endforeach; ?>
2953 <optgroup label="Categories">
2954 <?php foreach ( get_categories() as $category ) : ?>
2955 <option value="<?php echo 'cat_' . $category->slug; ?>" <?php echo in_array( 'cat_' . $category->slug, $auth_settings_option ) ? 'selected="selected"' : ''; ?>><?php echo $category->name; ?></option>
2956 <?php endforeach; ?>
2957 </optgroup>
2958 </select><?php
2959 } // END print_multiselect_auth_access_public_pages()
2960
2961
2962 function print_section_info_external( $args = '' ) {
2963 ?><div id="section_info_external" class="section_info">
2964 <p>Enter your external server settings below.</p>
2965 </div><?php
2966 } // END print_section_info_external()
2967
2968 function print_select_auth_access_default_role( $args = '' ) {
2969 // Get plugin option.
2970 $option = 'access_default_role';
2971 $admin_mode = ( is_array( $args ) && array_key_exists( 'multisite_admin', $args ) && $args['multisite_admin'] === true ) ? 'multisite admin' : 'single admin';
2972 $auth_settings_option = $this->get_plugin_option( $option, $admin_mode, 'allow override', 'print overlay' );
2973
2974 // Print option elements.
2975 ?><select id="auth_settings_<?php echo $option; ?>" name="auth_settings[<?php echo $option; ?>]">
2976 <?php wp_dropdown_roles( $auth_settings_option ); ?>
2977 </select><?php
2978 } // END print_select_auth_access_default_role()
2979
2980 function print_checkbox_auth_external_google( $args = '' ) {
2981 // Get plugin option.
2982 $option = 'google';
2983 $admin_mode = ( is_array( $args ) && array_key_exists( 'multisite_admin', $args ) && $args['multisite_admin'] === true ) ? 'multisite admin' : 'single admin';
2984 $auth_settings_option = $this->get_plugin_option( $option, $admin_mode, 'allow override', 'print overlay' );
2985
2986 // Make sure php5-curl extension is installed on server.
2987 $curl_installed_message = ! function_exists( 'curl_init' ) ? '<span style="color: red;">(Warning: <a href="http://www.php.net//manual/en/curl.installation.php" target="_blank" style="color: red;">PHP CURL extension</a> is <strong>not</strong> installed)</span>' : '';
2988
2989 // Print option elements.
2990 ?><input type="checkbox" id="auth_settings_<?php echo $option; ?>" name="auth_settings[<?php echo $option; ?>]" value="1"<?php checked( 1 == $auth_settings_option ); ?> /><label for="auth_settings_<?php echo $option; ?>">Enable Google Logins</label> <?php echo $curl_installed_message; ?><?php
2991 } // END print_checkbox_auth_external_google()
2992
2993 function print_text_google_clientid( $args = '' ) {
2994 // Get plugin option.
2995 $option = 'google_clientid';
2996 $admin_mode = ( is_array( $args ) && array_key_exists( 'multisite_admin', $args ) && $args['multisite_admin'] === true ) ? 'multisite admin' : 'single admin';
2997 $auth_settings_option = $this->get_plugin_option( $option, $admin_mode, 'allow override', 'print overlay' );
2998
2999 // Print option elements.
3000 $site_url_parts = parse_url( get_site_url() );
3001 $site_url_host = $site_url_parts['scheme'] . '://' . $site_url_parts['host'] . '/';
3002 ?>If you don't have a Google Client ID and Secret, generate them by following these instructions:
3003 <ol>
3004 <li>Click <strong>Create a Project</strong> on the <a href="https://cloud.google.com/console" target="_blank">Google Developers Console</a>. You can name it whatever you want.</li>
3005 <li>Within the project, navigate to <em>APIs and Auth</em> &gt; <em>Credentials</em>, then click <strong>Create New Client ID</strong> under OAuth. Use these settings:
3006 <ul>
3007 <li>Application Type: <strong>Web application</strong></li>
3008 <li>Authorized Javascript Origins: <strong><?php echo $site_url_host; ?></strong></li>
3009 <li>Authorized Redirect URI: <em>none</em></li>
3010 </ul>
3011 </li>
3012 <li>Copy/paste your new Client ID/Secret pair into the fields below.</li>
3013 <li><strong>Note</strong>: Navigate to <em>APIs and Auth</em> &gt; <em>Consent screen</em> to change the way the Google consent screen appears after a user has successfully entered their password, but before they are redirected back to WordPress.</li>
3014 </ol>
3015 <input type="text" id="auth_settings_<?php echo $option; ?>" name="auth_settings[<?php echo $option; ?>]" value="<?php echo $auth_settings_option; ?>" placeholder="1234567890123-kdjr85yt6vjr6d8g7dhr8g7d6durjf7g.apps.googleusercontent.com" style="width:560px;" /><?php
3016 } // END print_text_google_clientid()
3017
3018 function print_text_google_clientsecret( $args = '' ) {
3019 // Get plugin option.
3020 $option = 'google_clientsecret';
3021 $admin_mode = ( is_array( $args ) && array_key_exists( 'multisite_admin', $args ) && $args['multisite_admin'] === true ) ? 'multisite admin' : 'single admin';
3022 $auth_settings_option = $this->get_plugin_option( $option, $admin_mode, 'allow override', 'print overlay' );
3023
3024 // Print option elements.
3025 ?><input type="text" id="auth_settings_<?php echo $option; ?>" name="auth_settings[<?php echo $option; ?>]" value="<?php echo $auth_settings_option; ?>" placeholder="sDNgX5_pr_5bly-frKmvp8jT" style="width:220px;" /><?php
3026 } // END print_text_google_clientsecret()
3027
3028 function print_checkbox_auth_external_cas( $args = '' ) {
3029 // Get plugin option.
3030 $option = 'cas';
3031 $admin_mode = ( is_array( $args ) && array_key_exists( 'multisite_admin', $args ) && $args['multisite_admin'] === true ) ? 'multisite admin' : 'single admin';
3032 $auth_settings_option = $this->get_plugin_option( $option, $admin_mode, 'allow override', 'print overlay' );
3033
3034 // Make sure php5-curl extension is installed on server.
3035 $curl_installed_message = ! function_exists( 'curl_init' ) ? '<span style="color: red;">(Warning: <a href="http://www.php.net//manual/en/curl.installation.php" target="_blank" style="color: red;">PHP CURL extension</a> is <strong>not</strong> installed)</span>' : '';
3036
3037 // Print option elements.
3038 ?><input type="checkbox" id="auth_settings_<?php echo $option; ?>" name="auth_settings[<?php echo $option; ?>]" value="1"<?php checked( 1 == $auth_settings_option ); ?> /><label for="auth_settings_<?php echo $option; ?>">Enable CAS Logins</label> <?php echo $curl_installed_message; ?><?php
3039 } // END print_checkbox_auth_external_cas()
3040
3041 function print_text_cas_custom_label( $args = '' ) {
3042 // Get plugin option.
3043 $option = 'cas_custom_label';
3044 $admin_mode = ( is_array( $args ) && array_key_exists( 'multisite_admin', $args ) && $args['multisite_admin'] === true ) ? 'multisite admin' : 'single admin';
3045 $auth_settings_option = $this->get_plugin_option( $option, $admin_mode, 'allow override', 'print overlay' );
3046
3047 // Print option elements.
3048 ?>The button on the login page will read:<p><a class="button-primary button-large" style="padding: 3px 16px; height: 36px;"><span class="dashicons dashicons-lock" style="margin: 4px 4px 0 0;"></span> <strong>Sign in with </strong><input type="text" id="auth_settings_<?php echo $option; ?>" name="auth_settings[<?php echo $option; ?>]" value="<?php echo $auth_settings_option; ?>" placeholder="CAS" style="width: 100px;" /></a></p><?php
3049 } // END print_text_cas_custom_label()
3050
3051 function print_text_cas_host( $args = '' ) {
3052 // Get plugin option.
3053 $option = 'cas_host';
3054 $admin_mode = ( is_array( $args ) && array_key_exists( 'multisite_admin', $args ) && $args['multisite_admin'] === true ) ? 'multisite admin' : 'single admin';
3055 $auth_settings_option = $this->get_plugin_option( $option, $admin_mode, 'allow override', 'print overlay' );
3056
3057 // Print option elements.
3058 ?><input type="text" id="auth_settings_<?php echo $option; ?>" name="auth_settings[<?php echo $option; ?>]" value="<?php echo $auth_settings_option; ?>" placeholder="authn.example.edu" /><?php
3059 } // END print_text_cas_host()
3060
3061 function print_text_cas_port( $args = '' ) {
3062 // Get plugin option.
3063 $option = 'cas_port';
3064 $admin_mode = ( is_array( $args ) && array_key_exists( 'multisite_admin', $args ) && $args['multisite_admin'] === true ) ? 'multisite admin' : 'single admin';
3065 $auth_settings_option = $this->get_plugin_option( $option, $admin_mode, 'allow override', 'print overlay' );
3066
3067 // Print option elements.
3068 ?><input type="text" id="auth_settings_<?php echo $option; ?>" name="auth_settings[<?php echo $option; ?>]" value="<?php echo $auth_settings_option; ?>" placeholder="443" style="width:50px;" /><?php
3069 } // END print_text_cas_port()
3070
3071 function print_text_cas_path( $args = '' ) {
3072 // Get plugin option.
3073 $option = 'cas_path';
3074 $admin_mode = ( is_array( $args ) && array_key_exists( 'multisite_admin', $args ) && $args['multisite_admin'] === true ) ? 'multisite admin' : 'single admin';
3075 $auth_settings_option = $this->get_plugin_option( $option, $admin_mode, 'allow override', 'print overlay' );
3076
3077 // Print option elements.
3078 ?><input type="text" id="auth_settings_<?php echo $option; ?>" name="auth_settings[<?php echo $option; ?>]" value="<?php echo $auth_settings_option; ?>" placeholder="/cas" /><?php
3079 } // END print_text_cas_path()
3080
3081 function print_text_cas_attr_email( $args = '' ) {
3082 // Get plugin option.
3083 $option = 'cas_attr_email';
3084 $admin_mode = ( is_array( $args ) && array_key_exists( 'multisite_admin', $args ) && $args['multisite_admin'] === true ) ? 'multisite admin' : 'single admin';
3085 $auth_settings_option = $this->get_plugin_option( $option, $admin_mode, 'allow override', 'print overlay' );
3086
3087 // Print option elements.
3088 ?><input type="text" id="auth_settings_<?php echo $option; ?>" name="auth_settings[<?php echo $option; ?>]" value="<?php echo $auth_settings_option; ?>" placeholder="mail" /><?php
3089 } // END print_text_cas_attr_email()
3090
3091 function print_text_cas_attr_first_name( $args = '' ) {
3092 // Get plugin option.
3093 $option = 'cas_attr_first_name';
3094 $admin_mode = ( is_array( $args ) && array_key_exists( 'multisite_admin', $args ) && $args['multisite_admin'] === true ) ? 'multisite admin' : 'single admin';
3095 $auth_settings_option = $this->get_plugin_option( $option, $admin_mode, 'allow override', 'print overlay' );
3096
3097 // Print option elements.
3098 ?><input type="text" id="auth_settings_<?php echo $option; ?>" name="auth_settings[<?php echo $option; ?>]" value="<?php echo $auth_settings_option; ?>" placeholder="givenName" /><?php
3099 } // END print_text_cas_attr_first_name()
3100
3101 function print_text_cas_attr_last_name( $args = '' ) {
3102 // Get plugin option.
3103 $option = 'cas_attr_last_name';
3104 $admin_mode = ( is_array( $args ) && array_key_exists( 'multisite_admin', $args ) && $args['multisite_admin'] === true ) ? 'multisite admin' : 'single admin';
3105 $auth_settings_option = $this->get_plugin_option( $option, $admin_mode, 'allow override', 'print overlay' );
3106
3107 // Print option elements.
3108 ?><input type="text" id="auth_settings_<?php echo $option; ?>" name="auth_settings[<?php echo $option; ?>]" value="<?php echo $auth_settings_option; ?>" placeholder="sn" /><?php
3109 } // END print_text_cas_attr_last_name()
3110
3111 function print_checkbox_cas_attr_update_on_login( $args = '' ) {
3112 // Get plugin option.
3113 $option = 'cas_attr_update_on_login';
3114 $admin_mode = ( is_array( $args ) && array_key_exists( 'multisite_admin', $args ) && $args['multisite_admin'] === true ) ? 'multisite admin' : 'single admin';
3115 $auth_settings_option = $this->get_plugin_option( $option, $admin_mode, 'allow override', 'print overlay' );
3116
3117 // Print option elements.
3118 ?><input type="checkbox" id="auth_settings_<?php echo $option; ?>" name="auth_settings[<?php echo $option; ?>]" value="1"<?php checked( 1 == $auth_settings_option ); ?> /><label for="auth_settings_<?php echo $option; ?>">Update first and last name fields on login (will overwrite any name the user has supplied in their profile)</label><?php
3119 } // END print_checkbox_cas_attr_update_on_login()
3120
3121
3122 function print_checkbox_auth_external_ldap( $args = '' ) {
3123 // Get plugin option.
3124 $option = 'ldap';
3125 $admin_mode = ( is_array( $args ) && array_key_exists( 'multisite_admin', $args ) && $args['multisite_admin'] === true ) ? 'multisite admin' : 'single admin';
3126 $auth_settings_option = $this->get_plugin_option( $option, $admin_mode, 'allow override', 'print overlay' );
3127
3128 // Make sure php5-ldap extension is installed on server.
3129 $ldap_installed_message = ! function_exists( 'ldap_connect' ) ? '<span style="color: red;">(Warning: <a href="http://www.php.net/manual/en/ldap.installation.php" target="_blank" style="color: red;">PHP LDAP extension</a> is <strong>not</strong> installed)</span>' : '';
3130
3131 // Print option elements.
3132 ?><input type="checkbox" id="auth_settings_<?php echo $option; ?>" name="auth_settings[<?php echo $option; ?>]" value="1"<?php checked( 1 == $auth_settings_option ); ?> /><label for="auth_settings_<?php echo $option; ?>">Enable LDAP Logins</label> <?php echo $ldap_installed_message; ?><?php
3133 } // END print_checkbox_auth_external_ldap()
3134
3135 function print_text_ldap_host( $args = '' ) {
3136 // Get plugin option.
3137 $option = 'ldap_host';
3138 $admin_mode = ( is_array( $args ) && array_key_exists( 'multisite_admin', $args ) && $args['multisite_admin'] === true ) ? 'multisite admin' : 'single admin';
3139 $auth_settings_option = $this->get_plugin_option( $option, $admin_mode, 'allow override', 'print overlay' );
3140
3141 // Print option elements.
3142 ?><input type="text" id="auth_settings_<?php echo $option; ?>" name="auth_settings[<?php echo $option; ?>]" value="<?php echo $auth_settings_option; ?>" placeholder="ldap.example.edu" /><?php
3143 } // END print_text_ldap_host()
3144
3145 function print_text_ldap_port( $args = '' ) {
3146 // Get plugin option.
3147 $option = 'ldap_port';
3148 $admin_mode = ( is_array( $args ) && array_key_exists( 'multisite_admin', $args ) && $args['multisite_admin'] === true ) ? 'multisite admin' : 'single admin';
3149 $auth_settings_option = $this->get_plugin_option( $option, $admin_mode, 'allow override', 'print overlay' );
3150
3151 // Print option elements.
3152 ?><input type="text" id="auth_settings_<?php echo $option; ?>" name="auth_settings[<?php echo $option; ?>]" value="<?php echo $auth_settings_option; ?>" placeholder="389" style="width:50px;" /><?php
3153 } // END print_text_ldap_port()
3154
3155 function print_text_ldap_search_base( $args = '' ) {
3156 // Get plugin option.
3157 $option = 'ldap_search_base';
3158 $admin_mode = ( is_array( $args ) && array_key_exists( 'multisite_admin', $args ) && $args['multisite_admin'] === true ) ? 'multisite admin' : 'single admin';
3159 $auth_settings_option = $this->get_plugin_option( $option, $admin_mode, 'allow override', 'print overlay' );
3160
3161 // Print option elements.
3162 ?><input type="text" id="auth_settings_<?php echo $option; ?>" name="auth_settings[<?php echo $option; ?>]" value="<?php echo $auth_settings_option; ?>" placeholder="ou=people,dc=example,dc=edu" style="width:225px;" /><?php
3163 } // END print_text_ldap_search_base()
3164
3165 function print_text_ldap_uid( $args = '' ) {
3166 // Get plugin option.
3167 $option = 'ldap_uid';
3168 $admin_mode = ( is_array( $args ) && array_key_exists( 'multisite_admin', $args ) && $args['multisite_admin'] === true ) ? 'multisite admin' : 'single admin';
3169 $auth_settings_option = $this->get_plugin_option( $option, $admin_mode, 'allow override', 'print overlay' );
3170
3171 // Print option elements.
3172 ?><input type="text" id="auth_settings_<?php echo $option; ?>" name="auth_settings[<?php echo $option; ?>]" value="<?php echo $auth_settings_option; ?>" placeholder="uid" style="width:80px;" /><?php
3173 } // END print_text_ldap_uid()
3174
3175 function print_text_ldap_attr_email( $args = '' ) {
3176 // Get plugin option.
3177 $option = 'ldap_attr_email';
3178 $admin_mode = ( is_array( $args ) && array_key_exists( 'multisite_admin', $args ) && $args['multisite_admin'] === true ) ? 'multisite admin' : 'single admin';
3179 $auth_settings_option = $this->get_plugin_option( $option, $admin_mode, 'allow override', 'print overlay' );
3180
3181 // Print option elements.
3182 ?><input type="text" id="auth_settings_<?php echo $option; ?>" name="auth_settings[<?php echo $option; ?>]" value="<?php echo $auth_settings_option; ?>" placeholder="mail" /><?php
3183 } // END print_text_ldap_attr_email()
3184
3185 function print_text_ldap_user( $args = '' ) {
3186 // Get plugin option.
3187 $option = 'ldap_user';
3188 $admin_mode = ( is_array( $args ) && array_key_exists( 'multisite_admin', $args ) && $args['multisite_admin'] === true ) ? 'multisite admin' : 'single admin';
3189 $auth_settings_option = $this->get_plugin_option( $option, $admin_mode, 'allow override', 'print overlay' );
3190
3191 // Print option elements.
3192 ?><input type="text" id="auth_settings_<?php echo $option; ?>" name="auth_settings[<?php echo $option; ?>]" value="<?php echo $auth_settings_option; ?>" placeholder="cn=directory-user,ou=specials,dc=example,dc=edu" style="width:330px;" /><?php
3193 } // END print_text_ldap_user()
3194
3195 function print_password_ldap_password( $args = '' ) {
3196 // Get plugin option.
3197 $option = 'ldap_password';
3198 $admin_mode = ( is_array( $args ) && array_key_exists( 'multisite_admin', $args ) && $args['multisite_admin'] === true ) ? 'multisite admin' : 'single admin';
3199 $auth_settings_option = $this->get_plugin_option( $option, $admin_mode, 'allow override', 'print overlay' );
3200
3201 // Print option elements.
3202 ?><input type="password" id="garbage_to_stop_autofill" name="garbage" value="" autocomplete="off" style="display:none;" />
3203 <input type="password" id="auth_settings_<?php echo $option; ?>" name="auth_settings[<?php echo $option; ?>]" value="<?php echo $this->decrypt( base64_decode( $auth_settings_option ) ); ?>" autocomplete="off" /><?php
3204 } // END print_password_ldap_password()
3205
3206 function print_checkbox_ldap_tls( $args = '' ) {
3207 // Get plugin option.
3208 $option = 'ldap_tls';
3209 $admin_mode = ( is_array( $args ) && array_key_exists( 'multisite_admin', $args ) && $args['multisite_admin'] === true ) ? 'multisite admin' : 'single admin';
3210 $auth_settings_option = $this->get_plugin_option( $option, $admin_mode, 'allow override', 'print overlay' );
3211
3212 // Print option elements.
3213 ?><input type="checkbox" id="auth_settings_<?php echo $option; ?>" name="auth_settings[<?php echo $option; ?>]" value="1"<?php checked( 1 == $auth_settings_option ); ?> /><label for="auth_settings_<?php echo $option; ?>">Use TLS</label><?php
3214 } // END print_checkbox_ldap_tls
3215
3216 function print_text_ldap_lostpassword_url( $args = '' ) {
3217 // Get plugin option.
3218 $option = 'ldap_lostpassword_url';
3219 $admin_mode = ( is_array( $args ) && array_key_exists( 'multisite_admin', $args ) && $args['multisite_admin'] === true ) ? 'multisite admin' : 'single admin';
3220 $auth_settings_option = $this->get_plugin_option( $option, $admin_mode, 'allow override', 'print overlay' );
3221
3222 // Print option elements.
3223 ?><input type="text" id="auth_settings_<?php echo $option; ?>" name="auth_settings[<?php echo $option; ?>]" value="<?php echo $auth_settings_option; ?>" placeholder="https://myschool.example.edu:8888/am-forgot-password" style="width: 400px;" /><?php
3224 } // END print_text_ldap_lostpassword_url()
3225
3226 function print_text_ldap_attr_first_name( $args = '' ) {
3227 // Get plugin option.
3228 $option = 'ldap_attr_first_name';
3229 $admin_mode = ( is_array( $args ) && array_key_exists( 'multisite_admin', $args ) && $args['multisite_admin'] === true ) ? 'multisite admin' : 'single admin';
3230 $auth_settings_option = $this->get_plugin_option( $option, $admin_mode, 'allow override', 'print overlay' );
3231
3232 // Print option elements.
3233 ?><input type="text" id="auth_settings_<?php echo $option; ?>" name="auth_settings[<?php echo $option; ?>]" value="<?php echo $auth_settings_option; ?>" placeholder="givenname" /><?php
3234 } // END print_text_ldap_attr_first_name()
3235
3236 function print_text_ldap_attr_last_name( $args = '' ) {
3237 // Get plugin option.
3238 $option = 'ldap_attr_last_name';
3239 $admin_mode = ( is_array( $args ) && array_key_exists( 'multisite_admin', $args ) && $args['multisite_admin'] === true ) ? 'multisite admin' : 'single admin';
3240 $auth_settings_option = $this->get_plugin_option( $option, $admin_mode, 'allow override', 'print overlay' );
3241
3242 // Print option elements.
3243 ?><input type="text" id="auth_settings_<?php echo $option; ?>" name="auth_settings[<?php echo $option; ?>]" value="<?php echo $auth_settings_option; ?>" placeholder="sn" /><?php
3244 } // END print_text_ldap_attr_last_name()
3245
3246 function print_checkbox_ldap_attr_update_on_login( $args = '' ) {
3247 // Get plugin option.
3248 $option = 'ldap_attr_update_on_login';
3249 $admin_mode = ( is_array( $args ) && array_key_exists( 'multisite_admin', $args ) && $args['multisite_admin'] === true ) ? 'multisite admin' : 'single admin';
3250 $auth_settings_option = $this->get_plugin_option( $option, $admin_mode, 'allow override', 'print overlay' );
3251
3252 // Print option elements.
3253 ?><input type="checkbox" id="auth_settings_<?php echo $option; ?>" name="auth_settings[<?php echo $option; ?>]" value="1"<?php checked( 1 == $auth_settings_option ); ?> /><label for="auth_settings_<?php echo $option; ?>">Update first and last name fields on login (will overwrite any name the user has supplied in their profile)</label><?php
3254 } // END print_checkbox_ldap_attr_update_on_login()
3255
3256
3257 function print_section_info_advanced( $args = '' ) {
3258 ?><div id="section_info_advanced" class="section_info">
3259 <p>You may optionally specify some advanced settings below.</p>
3260 </div><?php
3261 } // END print_section_info_advanced()
3262
3263 function print_text_auth_advanced_lockouts( $args = '' ) {
3264 // Get plugin option.
3265 $option = 'advanced_lockouts';
3266 $admin_mode = ( is_array( $args ) && array_key_exists( 'multisite_admin', $args ) && $args['multisite_admin'] === true ) ? 'multisite admin' : 'single admin';
3267 $auth_settings_option = $this->get_plugin_option( $option, $admin_mode, 'allow override', 'print overlay' );
3268
3269 // Print option elements.
3270 ?>After
3271 <input type="text" id="auth_settings_<?php echo $option; ?>_attempts_1" name="auth_settings[<?php echo $option; ?>][attempts_1]" value="<?php echo $auth_settings_option['attempts_1']; ?>" placeholder="10" style="width:30px;" />
3272 invalid password attempts, delay further attempts on that user for
3273 <input type="text" id="auth_settings_<?php echo $option; ?>_duration_1" name="auth_settings[<?php echo $option; ?>][duration_1]" value="<?php echo $auth_settings_option['duration_1']; ?>" placeholder="1" style="width:30px;" />
3274 minute(s).
3275 <br />
3276 After
3277 <input type="text" id="auth_settings_<?php echo $option; ?>_attempts_2" name="auth_settings[<?php echo $option; ?>][attempts_2]" value="<?php echo $auth_settings_option['attempts_2']; ?>" placeholder="10" style="width:30px;" />
3278 more invalid attempts, increase the delay to
3279 <input type="text" id="auth_settings_<?php echo $option; ?>_duration_2" name="auth_settings[<?php echo $option; ?>][duration_2]" value="<?php echo $auth_settings_option['duration_2']; ?>" placeholder="10" style="width:30px;" />
3280 minutes.
3281 <br />
3282 Reset the delays after
3283 <input type="text" id="auth_settings_<?php echo $option; ?>_reset_duration" name="auth_settings[<?php echo $option; ?>][reset_duration]" value="<?php echo $auth_settings_option['reset_duration']; ?>" placeholder="240" style="width:40px;" />
3284 minutes with no invalid attempts.<?php
3285 } // END print_text_auth_advanced_lockouts()
3286
3287 function print_checkbox_auth_advanced_hide_wp_login( $args = '' ) {
3288 // Get plugin option.
3289 $option = 'advanced_hide_wp_login';
3290 $admin_mode = ( is_array( $args ) && array_key_exists( 'multisite_admin', $args ) && $args['multisite_admin'] === true ) ? 'multisite admin' : 'single admin';
3291 $auth_settings_option = $this->get_plugin_option( $option, $admin_mode, 'allow override', 'print overlay' );
3292
3293 // Print option elements.
3294 ?><input type="checkbox" id="auth_settings_<?php echo $option; ?>" name="auth_settings[<?php echo $option; ?>]" value="1"<?php checked( 1 == $auth_settings_option ); ?> /><label for="auth_settings_<?php echo $option; ?>">Hide WordPress Logins</label>
3295 <p><small>Note: You can always access the WordPress logins by adding external=wordpress to the wp-login URL, like so:<br /><a href="<?php echo wp_login_url(); ?>?external=wordpress" target="_blank"><?php echo wp_login_url(); ?>?external=wordpress</a>.</p><?php
3296 } // END print_checkbox_auth_advanced_hide_wp_login()
3297
3298 function print_radio_auth_advanced_branding( $args = '' ) {
3299 // Get plugin option.
3300 $option = 'advanced_branding';
3301 $auth_settings_option = $this->get_plugin_option( $option );
3302
3303 // Print option elements.
3304 ?><input type="radio" id="radio_auth_settings_<?php echo $option; ?>_default" name="auth_settings[<?php echo $option; ?>]" value="default"<?php checked( 'default' == $auth_settings_option ); ?> /><label for="radio_auth_settings_<?php echo $option; ?>_default">Default WordPress login screen</label><br />
3305 <?php
3306
3307 /**
3308 * Developers can use the `authorizer_add_branding_option` filter
3309 * to add a radio button for "Custom WordPress login branding"
3310 * under the "Advanced" tab in Authorizer options. Example:
3311 *
3312 * function my_authorizer_add_branding_option( $branding_options ) {
3313 * $new_branding_option = array(
3314 * 'value' => 'your_brand'
3315 * 'description' => 'Custom Your Brand Login Screen',
3316 * 'css_url' => 'http://url/to/your_brand.css',
3317 * 'js_url' => 'http://url/to/your_brand.js',
3318 * );
3319 * array_push( $branding_options, $new_branding_option );
3320 * return $branding_options;
3321 * }
3322 * add_filter( 'authorizer_add_branding_option', 'my_authorizer_add_branding_option' );
3323 */
3324 $branding_options = array();
3325 $branding_options = apply_filters( 'authorizer_add_branding_option', $branding_options );
3326 foreach ( $branding_options as $branding_option ) {
3327 // Make sure the custom brands have the required values
3328 if ( ! ( is_array( $branding_option ) && array_key_exists( 'value', $branding_option ) && array_key_exists( 'description', $branding_option ) ) ) {
3329 continue;
3330 }
3331 ?><input type="radio" id="radio_auth_settings_<?php echo $option; ?>_<?php echo sanitize_title( $branding_option['value'] ); ?>" name="auth_settings[<?php echo $option; ?>]" value="<?php echo $branding_option['value']; ?>"<?php checked( $branding_option['value'] == $auth_settings_option ); ?> /><label for="radio_auth_settings_<?php echo $option; ?>_<?php echo sanitize_title( $branding_option['value'] ); ?>"><?php echo $branding_option['description']; ?></label><br /><?php
3332 }
3333
3334 // Print message about adding custom brands if there are none.
3335 if ( count( $branding_options ) === 0 ) {
3336 ?><p><em><strong>Note for theme developers</strong>: Add more options here by using the `authorizer_add_branding_option` filter in your theme. You can see an example theme that implements this filter in the plugin directory under sample-theme-add-branding.</em></p><?php
3337 }
3338 } // END print_radio_auth_advanced_branding()
3339
3340 function print_radio_auth_advanced_admin_menu( $args = '' ) {
3341 // Get plugin option.
3342 $option = 'advanced_admin_menu';
3343 $auth_settings_option = $this->get_plugin_option( $option );
3344
3345 // Print option elements.
3346 ?><input type="radio" id="radio_auth_settings_<?php echo $option; ?>_settings" name="auth_settings[<?php echo $option; ?>]" value="settings"<?php checked( 'settings' == $auth_settings_option ); ?> /><label for="radio_auth_settings_<?php echo $option; ?>_settings">Show in Settings menu</label><br />
3347 <input type="radio" id="radio_auth_settings_<?php echo $option; ?>_top" name="auth_settings[<?php echo $option; ?>]" value="top"<?php checked( 'top' == $auth_settings_option ); ?> /><label for="radio_auth_settings_<?php echo $option; ?>_top">Show in sidebar (top level)</label><br /><?php
3348
3349 } // END print_radio_auth_advanced_admin_menu()
3350
3351 function print_select_auth_advanced_usermeta( $args = '' ) {
3352 // Get plugin option.
3353 $option = 'advanced_usermeta';
3354 $auth_settings_option = $this->get_plugin_option( $option );
3355
3356 // Print option elements.
3357 ?><select id="auth_settings_<?php echo $option; ?>" name="auth_settings[<?php echo $option; ?>]">
3358 <option value="">-- None --</option>
3359 <?php if ( class_exists( 'acf' ) ) :
3360 // Get ACF 5 fields. Note: it would be much easier to use `get_field_objects()`
3361 // or `get_field_objects( 'user_' . get_current_user_id() )`, but neither will
3362 // list fields that have never been given values for users (i.e., new ACF
3363 // fields). Therefore we fall back on finding any ACF fields applied to users
3364 // (user_role or user_form location rules in the field group definition).
3365 $fields = array();
3366 $acf_field_group_ids = array();
3367 $acf_field_groups = new WP_Query( array(
3368 'post_type' => 'acf-field-group',
3369 ));
3370 while ( $acf_field_groups->have_posts() ) : $acf_field_groups->the_post();
3371 if ( strpos( get_the_content(), 's:5:"param";s:9:"user_role"' ) !== false || strpos( get_the_content(), 's:5:"param";s:9:"user_form"' ) !== false ) :
3372 array_push( $acf_field_group_ids, get_the_ID() );
3373 endif;
3374 endwhile; wp_reset_postdata();
3375 foreach ( $acf_field_group_ids as $acf_field_group_id ) :
3376 $acf_fields = new WP_Query( array(
3377 'post_type' => 'acf-field',
3378 'post_parent' => $acf_field_group_id,
3379 ));
3380 while ( $acf_fields->have_posts() ) : $acf_fields->the_post();
3381 global $post;
3382 $fields[$post->post_name] = get_field_object( $post->post_name );
3383 endwhile; wp_reset_postdata();
3384 endforeach;
3385 // Get ACF 4 fields.
3386 $acf4_field_groups = new WP_Query( array(
3387 'post_type' => 'acf',
3388 ));
3389 while ( $acf4_field_groups->have_posts() ) : $acf4_field_groups->the_post();
3390 $field_group_rules = get_post_meta( get_the_ID(), 'rule', true );
3391 if ( is_array( $field_group_rules ) && array_key_exists( 'param', $field_group_rules ) && $field_group_rules['param'] === 'ef_user' ) :
3392 $acf4_fields = get_post_custom( get_the_ID() );
3393 foreach ( $acf4_fields as $meta_key => $meta_value ) :
3394 if ( strpos( $meta_key, 'field_' ) === 0 ) :
3395 $meta_value = unserialize( $meta_value[0] );
3396 $fields[$meta_key] = $meta_value;
3397 endif;
3398 endforeach;
3399 endif;
3400 endwhile; wp_reset_postdata(); ?>
3401 <optgroup label="ACF User Fields:">
3402 <?php foreach ( (array)$fields as $field => $field_object ) : ?>
3403 <option value="acf___<?php echo $field_object['key']; ?>"<?php if ( $auth_settings_option === "acf___{$field_object['key']}" ) echo ' selected="selected"'; ?>><?php echo $field_object['label']; ?></option>
3404 <?php endforeach; ?>
3405 </optgroup>
3406 <?php endif; ?>
3407 <optgroup label="All Usermeta:">
3408 <?php foreach ( $this->get_all_usermeta_keys() as $meta_key ) : if ( substr( $meta_key, 0, 3 ) === 'wp_' ) continue; ?>
3409 <option value="<?php echo $meta_key; ?>"<?php if ( $auth_settings_option === $meta_key ) echo ' selected="selected"'; ?>><?php echo $meta_key; ?></option>
3410 <?php endforeach; ?>
3411 </optgroup>
3412 </select><?php
3413 } // END print_select_auth_advanced_usermeta()
3414
3415 function print_checkbox_auth_advanced_override_multisite( $args = '' ) {
3416 // Get plugin option.
3417 $option = 'advanced_override_multisite';
3418 $auth_settings_option = $this->get_plugin_option( $option );
3419
3420 // Print option elements.
3421 ?><input type="checkbox" id="auth_settings_<?php echo $option; ?>" name="auth_settings[<?php echo $option; ?>]" value="1"<?php checked( 1 == $auth_settings_option ); ?> /><label for="auth_settings_<?php echo $option; ?>">Configure this site independently (don't inherit any multisite settings)</label><?php
3422 } // END print_checkbox_auth_advanced_override_multisite()
3423
3424
3425
3426 /**
3427 * Add help documentation to the options page.
3428 * Run on action hook chain: load-settings_page_authorizer > admin_head
3429 */
3430 public function admin_head() {
3431 $screen = get_current_screen();
3432
3433 // Add help tab for Access Lists Settings
3434 $help_auth_settings_access_lists_content = '
3435 <p><strong>Pending Users</strong>: Pending users are users who have successfully logged in to the site, but who haven\'t yet been approved (or blocked) by you.</p>
3436 <p><strong>Approved Users</strong>: Approved users have access to the site once they successfully log in.</p>
3437 <p><strong>Blocked Users</strong>: Blocked users will receive an error message when they try to visit the site after authenticating.</p>
3438 <p>Users in the <strong>Pending</strong> list appear automatically after a new user tries to log in from the configured external authentication service. You can add users to the <strong>Approved</strong> or <strong>Blocked</strong> lists by typing them in manually, or by clicking the <em>Approve</em> or <em>Block</em> buttons next to a user in the <strong>Pending</strong> list.</p>
3439 ';
3440 $screen->add_help_tab(
3441 array(
3442 'id' => 'help_auth_settings_access_lists_content',
3443 'title' => 'Access Lists',
3444 'content' => $help_auth_settings_access_lists_content,
3445 )
3446 );
3447
3448 // Add help tab for Login Access Settings
3449 $help_auth_settings_access_login_content = '
3450 <p><strong>Who can log in to the site?</strong>: Choose the level of access restriction you\'d like to use on your site here. You can leave the site open to anyone with a WordPress account or an account on an external service like Google, CAS, or LDAP, or restrict it to WordPress users and only the external users that you specify via the <em>Access Lists</em>.</p>
3451 <p><strong>Which role should receive email notifications about pending users?</strong>: If you\'ve restricted access to <strong>approved users</strong>, you can determine which WordPress users will receive a notification email everytime a new external user successfully logs in and is added to the pending list. All users of the specified role will receive an email, and the external user will get a message (specified below) telling them their access is pending approval.</p>
3452 <p><strong>What message should pending users see after attempting to log in?</strong>: Here you can specify the exact message a new external user will see once they try to log in to the site for the first time.</p>
3453 ';
3454 $screen->add_help_tab(
3455 array(
3456 'id' => 'help_auth_settings_access_login_content',
3457 'title' => 'Login Access',
3458 'content' => $help_auth_settings_access_login_content,
3459 )
3460 );
3461
3462 // Add help tab for Public Access Settings
3463 $help_auth_settings_access_public_content = '
3464 <p><strong>Who can view the site?</strong>: You can restrict the site\'s visibility by only allowing logged in users to see pages. If you do so, you can customize the specifics about the site\'s privacy using the settings below.</p>
3465 <p><strong>What pages (if any) should be available to everyone?</strong>: If you\'d like to declare certain pages on your site as always public (such as the course syllabus, introduction, or calendar), specify those pages here. These pages will always be available no matter what access restrictions exist.</p>
3466 <p><strong>What happens to people without access when they visit a <em>private</em> page?</strong>: Choose the response anonymous users receive when visiting the site. You can choose between immediately taking them to the <strong>login screen</strong>, or simply showing them a <strong>message</strong>.</p>
3467 <p><strong>What happens to people without access when they visit a <em>public</em> page?</strong>: Choose the response anonymous users receive when visiting a page on the site marked as public. You can choose between showing them the page without any message, or showing them a the page with a message above the content.</p>
3468 <p><strong>What message should people without access see?</strong>: If you chose to show new users a <strong>message</strong> above, type that message here.</p>
3469 ';
3470 $screen->add_help_tab(
3471 array(
3472 'id' => 'help_auth_settings_access_public_content',
3473 'title' => 'Public Access',
3474 'content' => $help_auth_settings_access_public_content,
3475 )
3476 );
3477
3478 // Add help tab for External Service (CAS, LDAP) Settings
3479 $help_auth_settings_external_content = '
3480 <p><strong>Type of external service to authenticate against</strong>: Choose which authentication service type you will be using. You\'ll have to fill out different fields below depending on which service you choose.</p>
3481 <p><strong>Enable Google Logins</strong>: Choose if you want to allow users to log in with their Google Account credentials. You will need to enter your API Client ID and Secret to enable Google Logins.</p>
3482 <p><strong>Enable CAS Logins</strong>: Choose if you want to allow users to log in with via CAS (Central Authentication Service). You will need to enter details about your CAS server (host, port, and path) to enable CAS Logins.</p>
3483 <p><strong>Enable LDAP Logins</strong>: Choose if you want to allow users to log in with their LDAP (Lightweight Directory Access Protocol) credentials. You will need to enter details about your LDAP server (host, port, search base, uid attribute, directory user, directory user password, and whether to use TLS) to enable Google Logins.</p>
3484 <p><strong>Default role for new CAS users</strong>: Specify which role new external users will get by default. Be sure to choose a role with limited permissions!</p>
3485 <p><strong><em>If you enable Google logins:</em></strong></p>
3486 <ul>
3487 <li><strong>Google Client ID</strong>: You can generate this ID by creating a new Project in the <a href="https://cloud.google.com/console">Google Developers Console</a>. A Client ID typically looks something like this: 1234567890123-kdjr85yt6vjr6d8g7dhr8g7d6durjf7g.apps.googleusercontent.com</li>
3488 <li><strong>Google Client Secret</strong>: You can generate this secret by creating a new Project in the <a href="https://cloud.google.com/console">Google Developers Console</a>. A Client Secret typically looks something like this: sDNgX5_pr_5bly-frKmvp8jT</li>
3489 </ul>
3490 <p><strong><em>If you enable CAS logins:</em></strong></p>
3491 <ul>
3492 <li><strong>CAS server hostname</strong>: Enter the hostname of the CAS server you authenticate against (e.g., authn.example.edu).</li>
3493 <li><strong>CAS server port</strong>: Enter the port on the CAS server to connect to (e.g., 443).</li>
3494 <li><strong>CAS server path/context</strong>: Enter the path to the login endpoint on the CAS server (e.g., /cas).</li>
3495 <li><strong>CAS attribute containing first name</strong>: Enter the CAS attribute that has the user\'s first name. When this user first logs in, their WordPress account will have their first name retrieved from CAS and added to their WordPress profile.</li>
3496 <li><strong>CAS attribute containing last name</strong>: Enter the CAS attribute that has the user\'s last name. When this user first logs in, their WordPress account will have their last name retrieved from CAS and added to their WordPress profile.</li>
3497 <li><strong>CAS attribute update</strong>: Select whether the first and last names retrieved from CAS should overwrite any value the user has entered in the first and last name fields in their WordPress profile. If this is not set, this only happens the first time they log in.</li>
3498 </ul>
3499 <p><strong><em>If you enable LDAP logins:</em></strong></p>
3500 <ul>
3501 <li><strong>LDAP Host</strong>: Enter the URL of the LDAP server you authenticate against.</li>
3502 <li><strong>LDAP Port</strong>: Enter the port number that the LDAP server listens on.</li>
3503 <li><strong>LDAP Search Base</strong>: Enter the LDAP string that represents the search base, e.g., ou=people,dc=example,dc=edu</li>
3504 <li><strong>LDAP attribute containing username</strong>: Enter the name of the LDAP attribute that contains the usernames used by those attempting to log in. The plugin will search on this attribute to find the cn to bind against for login attempts.</li>
3505 <li><strong>LDAP Directory User</strong>: Enter the name of the LDAP user that has permissions to browse the directory.</li>
3506 <li><strong>LDAP Directory User Password</strong>: Enter the password for the LDAP user that has permission to browse the directory.</li>
3507 <li><strong>Secure Connection (TLS)</strong>: Select whether all communication with the LDAP server should be performed over a TLS-secured connection.</li>
3508 <li><strong>Custom lost password URL</strong>: The WordPress login page contains a link to recover a lost password. If you have external users who shouldn\'t change the password on their WordPress account, point them to the appropriate location to change the password on their external authentication service here.</li>
3509 <li><strong>LDAP attribute containing first name</strong>: Enter the LDAP attribute that has the user\'s first name. When this user first logs in, their WordPress account will have their first name retrieved from LDAP and added to their WordPress profile.</li>
3510 <li><strong>LDAP attribute containing last name</strong>: Enter the LDAP attribute that has the user\'s last name. When this user first logs in, their WordPress account will have their last name retrieved from LDAP and added to their WordPress profile.</li>
3511 <li><strong>LDAP attribute update</strong>: Select whether the first and last names retrieved from LDAP should overwrite any value the user has entered in the first and last name fields in their WordPress profile. If this is not set, this only happens the first time they log in.</li>
3512 </ul>';
3513 $screen->add_help_tab(
3514 array(
3515 'id' => 'help_auth_settings_external_content',
3516 'title' => 'External Service',
3517 'content' => $help_auth_settings_external_content,
3518 )
3519 );
3520
3521 // Add help tab for Advanced Settings
3522 $help_auth_settings_advanced_content = '
3523 <p><strong>Limit invalid login attempts</strong>: Choose how soon (and for how long) to restrict access to individuals (or bots) making repeated invalid login attempts. You may set a shorter delay first, and then a longer delay after repeated invalid attempts; you may also set how much time must pass before the delays will be reset to normal.</p>
3524 <p><strong>Hide WordPress Logins</strong>: If you want to hide the WordPress username and password fields and the Log In button on the wp-login screen, enable this option. Note: You can always access the WordPress logins by adding external=wordpress to the wp-login URL, like so: <a href="' . wp_login_url() . '?external=wordpress" target="_blank">' . wp_login_url() . '?external=wordpress</a>.</p>
3525 <p><strong>Custom WordPress login branding</strong>: If you\'d like to use custom branding on the WordPress login page, select that here. You will need to use the `authorizer_add_branding_option` filter in your theme to add it. You can see an example theme that implements this filter in the plugin directory under sample-theme-add-branding.</p>
3526 ';
3527 $screen->add_help_tab(
3528 array(
3529 'id' => 'help_auth_settings_advanced_content',
3530 'title' => 'Advanced',
3531 'content' => $help_auth_settings_advanced_content,
3532 )
3533 );
3534 } // END admin_head()
3535
3536
3537
3538 /**
3539 * ***************************
3540 * Multisite: Network Admin Options page
3541 * ***************************
3542 */
3543
3544
3545 /**
3546 * Network Admin menu item
3547 * Hook: network_admin_menu
3548 *
3549 * @param none
3550 * @return void
3551 */
3552 public function network_admin_menu() {
3553 // @see http://codex.wordpress.org/Function_Reference/add_menu_page
3554 add_menu_page(
3555 'Authorizer', // Page title
3556 'Authorizer', // Menu title
3557 'manage_network_options', // Capability
3558 'authorizer', // Menu slug
3559 array( $this, 'create_network_admin_page' ),
3560 'dashicons-groups', // Icon URL
3561 89 // Position
3562 );
3563 } // END network_admin_menu()
3564
3565 /**
3566 * Output the HTML for the options page
3567 */
3568 public function create_network_admin_page() {
3569 if ( ! current_user_can( 'manage_network_options' ) ) {
3570 wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
3571 }
3572 $auth_settings = get_blog_option( BLOG_ID_CURRENT_SITE, 'auth_multisite_settings', array() ); ?>
3573 <div class="wrap">
3574 <form method="post" action="" autocomplete="off">
3575 <h2>Authorizer Settings</h2>
3576 <p>Most <strong>Authorizer</strong> settings are set in the individual sites, but you can specify a few options here that apply to <strong>all sites in the network</strong>. These settings will override settings in the individual sites.</p>
3577
3578 <input type="checkbox" id="auth_settings_multisite_override" name="auth_settings[multisite_override]" value="1"<?php checked( 1 == $auth_settings['multisite_override'] ); ?> /><label for="auth_settings_multisite_override">Override individual site settings with the settings below</label>
3579
3580 <div id="auth_multisite_settings_disabled_overlay" style="display: none;"></div>
3581
3582 <div class="wrap" id="auth_multisite_settings">
3583 <?php $this->print_section_info_tabs( array( 'multisite_admin' => true ) ); ?>
3584
3585 <?php wp_nonce_field( 'save_auth_settings', 'nonce_save_auth_settings' ); ?>
3586
3587 <?php // Custom access lists (for network, we only really want approved list, not pending or blocked) ?>
3588 <div id="section_info_access_lists" class="section_info">
3589 <p>Manage who has access to all sites in the network.</p>
3590 </div>
3591 <table class="form-table"><tbody>
3592 <tr>
3593 <th scope="row">Who can log in to sites in this network?</th>
3594 <td><?php $this->print_radio_auth_access_who_can_login( array( 'multisite_admin' => true ) ); ?></td>
3595 </tr>
3596 <tr>
3597 <th scope="row">Who can view sites in this network?</th>
3598 <td><?php $this->print_radio_auth_access_who_can_view( array( 'multisite_admin' => true ) ); ?></td>
3599 </tr>
3600 <tr>
3601 <th scope="row">Approved Users (All Sites)<br /><small><em>Note: these users will <strong>not</strong> receive welcome emails when approved. Only users approved from individual sites can receive these messages.</em></small></th>
3602 <td><?php $this->print_combo_auth_access_users_approved( array( 'multisite_admin' => true ) ); ?></td>
3603 </tr>
3604 </tbody></table>
3605
3606 <?php $this->print_section_info_external(); ?>
3607 <table class="form-table"><tbody>
3608 <tr>
3609 <th scope="row">Default role for new users</th>
3610 <td><?php $this->print_select_auth_access_default_role( array( 'multisite_admin' => true ) ); ?></td>
3611 </tr>
3612 <tr>
3613 <th scope="row">Google Logins</th>
3614 <td><?php $this->print_checkbox_auth_external_google( array( 'multisite_admin' => true ) ); ?></td>
3615 </tr>
3616 <tr>
3617 <th scope="row">Google Client ID</th>
3618 <td><?php $this->print_text_google_clientid( array( 'multisite_admin' => true ) ); ?></td>
3619 </tr>
3620 <tr>
3621 <th scope="row">Google Client Secret</th>
3622 <td><?php $this->print_text_google_clientsecret( array( 'multisite_admin' => true ) ); ?></td>
3623 </tr>
3624 <tr>
3625 <th scope="row">CAS Logins</th>
3626 <td><?php $this->print_checkbox_auth_external_cas( array( 'multisite_admin' => true ) ); ?></td>
3627 </tr>
3628 <tr>
3629 <th scope="row">CAS Custom Label</th>
3630 <td><?php $this->print_text_cas_custom_label( array( 'multisite_admin' => true ) ); ?></td>
3631 </tr>
3632 <tr>
3633 <th scope="row">CAS server hostname</th>
3634 <td><?php $this->print_text_cas_host( array( 'multisite_admin' => true ) ); ?></td>
3635 </tr>
3636 <tr>
3637 <th scope="row">CAS server port</th>
3638 <td><?php $this->print_text_cas_port( array( 'multisite_admin' => true ) ); ?></td>
3639 </tr>
3640 <tr>
3641 <th scope="row">CAS server path/context</th>
3642 <td><?php $this->print_text_cas_path( array( 'multisite_admin' => true ) ); ?></td>
3643 </tr>
3644 <tr>
3645 <th scope="row">CAS attribute containing email</th>
3646 <td><?php $this->print_text_cas_attr_email( array( 'multisite_admin' => true ) ); ?></td>
3647 </tr>
3648 <tr>
3649 <th scope="row">CAS attribute containing first name</th>
3650 <td><?php $this->print_text_cas_attr_first_name( array( 'multisite_admin' => true ) ); ?></td>
3651 </tr>
3652 <tr>
3653 <th scope="row">CAS attribute containing last name</th>
3654 <td><?php $this->print_text_cas_attr_last_name( array( 'multisite_admin' => true ) ); ?></td>
3655 </tr>
3656 <tr>
3657 <th scope="row">CAS attribute update</th>
3658 <td><?php $this->print_checkbox_cas_attr_update_on_login( array( 'multisite_admin' => true ) ); ?></td>
3659 </tr>
3660 <tr>
3661 <th scope="row">LDAP Logins</th>
3662 <td><?php $this->print_checkbox_auth_external_ldap( array( 'multisite_admin' => true ) ); ?></td>
3663 </tr>
3664 <tr>
3665 <th scope="row">LDAP Host</th>
3666 <td><?php $this->print_text_ldap_host( array( 'multisite_admin' => true ) ); ?></td>
3667 </tr>
3668 <tr>
3669 <th scope="row">LDAP Port</th>
3670 <td><?php $this->print_text_ldap_port( array( 'multisite_admin' => true ) ); ?></td>
3671 </tr>
3672 <tr>
3673 <th scope="row">LDAP Search Base</th>
3674 <td><?php $this->print_text_ldap_search_base( array( 'multisite_admin' => true ) ); ?></td>
3675 </tr>
3676 <tr>
3677 <th scope="row">LDAP attribute containing username</th>
3678 <td><?php $this->print_text_ldap_uid( array( 'multisite_admin' => true ) ); ?></td>
3679 </tr>
3680 <tr>
3681 <th scope="row">LDAP attribute containing email</th>
3682 <td><?php $this->print_text_ldap_attr_email( array( 'multisite_admin' => true ) ); ?></td>
3683 </tr>
3684 <tr>
3685 <th scope="row">LDAP Directory User</th>
3686 <td><?php $this->print_text_ldap_user( array( 'multisite_admin' => true ) ); ?></td>
3687 </tr>
3688 <tr>
3689 <th scope="row">LDAP Directory User Password</th>
3690 <td><?php $this->print_password_ldap_password( array( 'multisite_admin' => true ) ); ?></td>
3691 </tr>
3692 <tr>
3693 <th scope="row">Secure Connection (TLS)</th>
3694 <td><?php $this->print_checkbox_ldap_tls( array( 'multisite_admin' => true ) ); ?></td>
3695 </tr>
3696 <tr>
3697 <th scope="row">Custom lost password URL</th>
3698 <td><?php $this->print_text_ldap_lostpassword_url( array( 'multisite_admin' => true ) ); ?></td>
3699 </tr>
3700 <tr>
3701 <th scope="row">LDAP attribute containing first name</th>
3702 <td><?php $this->print_text_ldap_attr_first_name( array( 'multisite_admin' => true ) ); ?></td>
3703 </tr>
3704 <tr>
3705 <th scope="row">LDAP attribute containing last name</th>
3706 <td><?php $this->print_text_ldap_attr_last_name( array( 'multisite_admin' => true ) ); ?></td>
3707 </tr>
3708 <tr>
3709 <th scope="row">LDAP attribute update</th>
3710 <td><?php $this->print_checkbox_ldap_attr_update_on_login( array( 'multisite_admin' => true ) ); ?></td>
3711 </tr>
3712 </tbody></table>
3713
3714 <?php $this->print_section_info_advanced(); ?>
3715 <table class="form-table"><tbody>
3716 <tr>
3717 <th scope="row">Limit invalid login attempts</th>
3718 <td><?php $this->print_text_auth_advanced_lockouts( array( 'multisite_admin' => true ) ); ?></td>
3719 </tr>
3720 <tr>
3721 <th scope="row">Hide WordPress Logins</th>
3722 <td><?php $this->print_checkbox_auth_advanced_hide_wp_login( array( 'multisite_admin' => true ) ); ?></td>
3723 </tr>
3724 </tbody></table>
3725
3726 <br class="clear" />
3727 </div>
3728 <input type="button" name="submit" id="submit" class="button button-primary" value="Save Changes" onclick="save_auth_multisite_settings(this);" />
3729 </form>
3730 </div>
3731 <?php
3732 } // END create_network_admin_page()
3733
3734 /**
3735 * Save multisite settings (ajax call).
3736 */
3737 function ajax_save_auth_multisite_settings() {
3738 // Fail silently if current user doesn't have permissions.
3739 if ( ! current_user_can( 'manage_network_options' ) ) {
3740 die( '' );
3741 }
3742
3743 // Make sure nonce exists.
3744 if ( empty( $_POST['nonce_save_auth_settings'] ) ) {
3745 die( '' );
3746 }
3747
3748 // Nonce check.
3749 if ( ! wp_verify_nonce( $_POST['nonce_save_auth_settings'], 'save_auth_settings' ) ) {
3750 die( '' );
3751 }
3752
3753 // Assert multisite.
3754 if ( ! is_multisite() ) {
3755 die( '' );
3756 }
3757
3758 // Get multisite settings.
3759 $auth_multisite_settings = get_blog_option( BLOG_ID_CURRENT_SITE, 'auth_multisite_settings', array() );
3760
3761 // Sanitize settings
3762 $auth_multisite_settings = $this->sanitize_options( $_POST, 'multisite' );
3763
3764 // Filter options to only the allowed values (multisite options are a subset of all options)
3765 $allowed = array(
3766 'multisite_override',
3767 'access_who_can_login',
3768 'access_who_can_view',
3769 'access_default_role',
3770 'google',
3771 'google_clientid',
3772 'google_clientsecret',
3773 'cas',
3774 'cas_custom_label',
3775 'cas_host',
3776 'cas_port',
3777 'cas_path',
3778 'cas_attr_email',
3779 'cas_attr_first_name',
3780 'cas_attr_last_name',
3781 'cas_attr_update_on_login',
3782 'ldap',
3783 'ldap_host',
3784 'ldap_port',
3785 'ldap_search_base',
3786 'ldap_uid',
3787 'ldap_attr_email',
3788 'ldap_user',
3789 'ldap_password',
3790 'ldap_tls',
3791 'ldap_lostpassword_url',
3792 'ldap_attr_first_name',
3793 'ldap_attr_last_name',
3794 'ldap_attr_update_on_login',
3795 'advanced_lockouts',
3796 'advanced_hide_wp_login',
3797 );
3798 $auth_multisite_settings = array_intersect_key( $auth_multisite_settings, array_flip( $allowed ) );
3799
3800 // Update multisite settings in database.
3801 update_blog_option( BLOG_ID_CURRENT_SITE, 'auth_multisite_settings', $auth_multisite_settings );
3802
3803 // Return 'success' value to AJAX call.
3804 die( 'success' );
3805 } // END ajax_save_auth_multisite_settings()
3806
3807
3808
3809 /**
3810 * ***************************
3811 * Dashboard widget
3812 * ***************************
3813 */
3814
3815
3816
3817 function add_dashboard_widgets() {
3818 // Only users who can edit can see the authorizer dashboard widget
3819 if ( current_user_can( 'create_users' ) ) {
3820 // Add dashboard widget for adding/editing users with access
3821 wp_add_dashboard_widget( 'auth_dashboard_widget', 'Authorizer Settings', array( $this, 'add_auth_dashboard_widget' ) );
3822 }
3823 } // END add_dashboard_widgets()
3824
3825
3826 function add_auth_dashboard_widget() {
3827 ?><form method="post" id="auth_settings_access_form" action="">
3828 <?php $this->print_section_info_access_login(); ?>
3829 <div>
3830 <h2>Pending Users</h2>
3831 <?php $this->print_combo_auth_access_users_pending(); ?>
3832 </div>
3833 <div>
3834 <h2>Approved Users</h2>
3835 <?php $this->print_combo_auth_access_users_approved(); ?>
3836 </div>
3837 <div>
3838 <h2>Blocked Users</h2>
3839 <?php $this->print_combo_auth_access_users_blocked(); ?>
3840 </div>
3841 <br class="clear" />
3842 </form><?php
3843 } // END add_auth_dashboard_widget()
3844
3845
3846 // Fired on a change event from the optional usermeta field in the
3847 // approved user list. Updates the selected usermeta value, or saves it
3848 // in the user's approved list entry if the user hasn't logged in yet
3849 // and created a WordPress account.
3850 function ajax_update_auth_usermeta() {
3851
3852 // Fail silently if current user doesn't have permissions.
3853 if ( ! current_user_can( 'create_users' ) ) {
3854 die( '' );
3855 }
3856
3857 // Nonce check.
3858 if ( empty( $_POST['nonce_save_auth_settings'] ) || ! wp_verify_nonce( $_POST['nonce_save_auth_settings'], 'save_auth_settings' ) ) {
3859 die( '' );
3860 }
3861
3862 // Fail if required post data doesn't exist.
3863 if ( ! array_key_exists( 'email', $_REQUEST ) || ! array_key_exists( 'usermeta', $_REQUEST ) ) {
3864 die( '' );
3865 }
3866
3867 // Get values to update from post data.
3868 $email = $_REQUEST['email'];
3869 $meta_value = $_REQUEST['usermeta'];
3870 $meta_key = $this->get_plugin_option( 'advanced_usermeta' );
3871
3872 // If user doesn't exist, save usermeta selection to authorizer
3873 // list. This value will get saved to usermeta when the user first
3874 // logs in (i.e., when their WordPress account is created).
3875 if ( ! ( $wp_user = get_user_by( 'email', $email ) ) ) {
3876 // Look through multisite approved users and add a usermeta
3877 // reference for the current blog if the user is found.
3878 $auth_multisite_settings_access_users_approved = is_multisite() ? get_blog_option( BLOG_ID_CURRENT_SITE, 'auth_multisite_settings_access_users_approved', array() ) : array();
3879 $should_update_auth_multisite_settings_access_users_approved = false;
3880 foreach ( $auth_multisite_settings_access_users_approved as $index => $approved_user ) {
3881 if ( $email === $approved_user['email'] ) {
3882 if ( ! is_array( $auth_multisite_settings_access_users_approved[$index]['usermeta'] ) ) {
3883 // Initialize the array of usermeta for each blog this user belongs to.
3884 $auth_multisite_settings_access_users_approved[$index]['usermeta'] = array();
3885 } else {
3886 // There is already usermeta associated with this
3887 // preapproved user; iterate through it and make
3888 // sure it's not for old meta_keys (delete it if
3889 // so). This can happen if someone changes the
3890 // usermeta key in authorizer options, and we don't
3891 // want to hang on to old data.
3892 foreach ( $auth_multisite_settings_access_users_approved[$index]['usermeta'] as $blog_id => $usermeta ) {
3893 if ( array_key_exists( 'meta_key', $usermeta ) && $usermeta['meta_key'] === $meta_key ) {
3894 continue;
3895 } else {
3896 unset( $auth_multisite_settings_access_users_approved[$index]['usermeta'][$blog_id] );
3897 }
3898 }
3899 }
3900 $auth_multisite_settings_access_users_approved[$index]['usermeta'][get_current_blog_id()] = array(
3901 'meta_key' => $meta_key,
3902 'meta_value' => $meta_value,
3903 );
3904 $should_update_auth_multisite_settings_access_users_approved = true;
3905 }
3906 }
3907 if ( $should_update_auth_multisite_settings_access_users_approved ) {
3908 update_blog_option( BLOG_ID_CURRENT_SITE, 'auth_multisite_settings_access_users_approved', $auth_multisite_settings_access_users_approved );
3909 }
3910
3911 // Look through the approved users (of the current blog in a
3912 // multisite install, or just of the single site) and add a
3913 // usermeta reference if the user is found.
3914 $auth_settings_access_users_approved = $this->get_plugin_option( 'access_users_approved', 'single admin' );
3915 $should_update_auth_settings_access_users_approved = false;
3916 foreach ( $auth_settings_access_users_approved as $index => $approved_user ) {
3917 if ( $email === $approved_user['email'] ) {
3918 $auth_settings_access_users_approved[$index]['usermeta'] = array(
3919 'meta_key' => $meta_key,
3920 'meta_value' => $meta_value,
3921 );
3922 $should_update_auth_settings_access_users_approved = true;
3923 }
3924 }
3925 if ( $should_update_auth_settings_access_users_approved ) {
3926 update_option( 'auth_settings_access_users_approved', $auth_settings_access_users_approved );
3927 }
3928
3929 } else {
3930 // Update user's usermeta value for usermeta key stored in authorizer options.
3931 if ( strpos( $meta_key, 'acf___' ) === 0 && class_exists( 'acf' ) ) {
3932 // We have an ACF field value, so use the ACF function to update it.
3933 update_field( str_replace('acf___', '', $meta_key ), $meta_value, 'user_' . $wp_user->ID );
3934 } else {
3935 // We have a normal usermeta value, so just update it via the WordPress function.
3936 update_user_meta( $wp_user->ID, $meta_key, $meta_value );
3937 }
3938
3939 }
3940
3941 // Return 'success' value to AJAX call.
3942 die( 'success' );
3943 } // END ajax_update_auth_usermeta()
3944
3945
3946 function ajax_update_auth_user() {
3947
3948 // Fail silently if current user doesn't have permissions.
3949 if ( ! current_user_can( 'create_users' ) ) {
3950 die( '' );
3951 }
3952
3953 // Nonce check.
3954 if ( empty( $_POST['nonce_save_auth_settings'] ) || ! wp_verify_nonce( $_POST['nonce_save_auth_settings'], 'save_auth_settings' ) ) {
3955 die( '' );
3956 }
3957
3958 // Fail if requesting a change to an invalid setting.
3959 if ( ! in_array( $_POST['setting'], array( 'access_users_pending', 'access_users_approved', 'access_users_blocked' ) ) ) {
3960 die( '' );
3961 }
3962
3963 // Editing a pending list entry.
3964 if ( $_POST['setting'] === 'access_users_pending' ) {
3965 // Initialize posted data if empty.
3966 if ( ! ( array_key_exists( 'access_users_pending', $_POST ) && is_array( $_POST['access_users_pending'] ) ) ) {
3967 $_POST['access_users_pending'] = array();
3968 }
3969
3970 // Deal with each modified user (add or remove).
3971 foreach ( $_POST['access_users_pending'] as $pending_user ) {
3972
3973 if ( $pending_user['edit_action'] === 'add' ) {
3974
3975 // Add new user to pending list and save (skip if it's
3976 // already there--someone else might have just done it).
3977 if ( ! $this->is_email_in_list( $pending_user['email'], 'pending' ) ) {
3978 $auth_settings_access_users_pending = $this->sanitize_user_list(
3979 $this->get_plugin_option( 'access_users_pending', 'single admin' )
3980 );
3981 array_push( $auth_settings_access_users_pending, $pending_user );
3982 update_option( 'auth_settings_access_users_pending', $auth_settings_access_users_pending );
3983 }
3984
3985 } elseif ( $pending_user['edit_action'] === 'remove' ) {
3986
3987 // Remove user from pending list and save
3988 if ( $this->is_email_in_list( $pending_user['email'], 'pending' ) ) {
3989 $auth_settings_access_users_pending = $this->sanitize_user_list(
3990 $this->get_plugin_option( 'access_users_pending', 'single admin' )
3991 );
3992 foreach ( $auth_settings_access_users_pending as $key => $existing_user ) {
3993 if ( $pending_user['email'] == $existing_user['email'] ) {
3994 unset( $auth_settings_access_users_pending[$key] );
3995 break;
3996 }
3997 }
3998 update_option( 'auth_settings_access_users_pending', $auth_settings_access_users_pending );
3999 }
4000
4001 }
4002 }
4003 }
4004
4005 // Editing an approved list entry.
4006 if ( $_POST['setting'] === 'access_users_approved' ) {
4007 // Initialize posted data if empty.
4008 if ( ! ( array_key_exists( 'access_users_approved', $_POST ) && is_array( $_POST['access_users_approved'] ) ) ) {
4009 $_POST['access_users_approved'] = array();
4010 }
4011
4012 // Deal with each modified user (add, remove, or change_role).
4013 foreach ( $_POST['access_users_approved'] as $approved_user ) {
4014 if ( $approved_user['edit_action'] === 'add' ) {
4015
4016 // New user (create user, or add existing user to current site in multisite).
4017 $new_user = get_user_by( 'email', $approved_user['email'] );
4018 if ( $new_user !== false ) {
4019 if ( is_multisite() ) {
4020 add_user_to_blog( get_current_blog_id(), $new_user->ID, $approved_user['role'] );
4021 }
4022 } elseif ( $approved_user['local_user'] === 'true' ) {
4023 // Create a WP account for this new *local* user and email the password.
4024 $plaintext_password = wp_generate_password(); // random password
4025 // If there's already a user with this username (e.g.,
4026 // johndoe/johndoe@gmail.com exists, and we're trying to add
4027 // johndoe/johndoe@example.com), use the full email address
4028 // as the username.
4029 $username = explode( "@", $approved_user['email'] );
4030 $username = $username[0];
4031 if ( get_user_by( 'login', $username ) !== false ) {
4032 $username = $approved_user['email'];
4033 }
4034 if ( $approved_user['multisite_user'] !== 'false' ) {
4035 $result = wpmu_create_user(
4036 strtolower( $username ),
4037 $plaintext_password,
4038 strtolower( $approved_user['email'] )
4039 );
4040 } else {
4041 $result = wp_insert_user(
4042 array(
4043 'user_login' => strtolower( $username ),
4044 'user_pass' => $plaintext_password,
4045 'first_name' => '',
4046 'last_name' => '',
4047 'user_email' => strtolower( $approved_user['email'] ),
4048 'user_registered' => date( 'Y-m-d H:i:s' ),
4049 'role' => $approved_user['role'],
4050 )
4051 );
4052 }
4053 if ( ! is_wp_error( $result ) ) {
4054 // Email password to new user
4055 wp_new_user_notification( $result, $plaintext_password );
4056 }
4057
4058 }
4059
4060 // Email new user welcome message if plugin option is set.
4061 $this->maybe_email_welcome_message( $approved_user['email'] );
4062
4063 // Add new user to approved list and save (skip if it's
4064 // already there--someone else might have just done it).
4065 if ( $approved_user['multisite_user'] !== 'false' ) {
4066 if ( ! $this->is_email_in_list( $approved_user['email'], 'approved', 'multisite' ) ) {
4067 $auth_multisite_settings_access_users_approved = $this->sanitize_user_list(
4068 $this->get_plugin_option( 'access_users_approved', 'multisite admin' )
4069 );
4070 $approved_user['date_added'] = date( 'M Y' );
4071 array_push( $auth_multisite_settings_access_users_approved, $approved_user );
4072 update_blog_option( BLOG_ID_CURRENT_SITE, 'auth_multisite_settings_access_users_approved', $auth_multisite_settings_access_users_approved );
4073 }
4074 } else {
4075 if ( ! $this->is_email_in_list( $approved_user['email'], 'approved' ) ) {
4076 $auth_settings_access_users_approved = $this->sanitize_user_list(
4077 $this->get_plugin_option( 'access_users_approved', 'single admin' )
4078 );
4079 $approved_user['date_added'] = date( 'M Y' );
4080 array_push( $auth_settings_access_users_approved, $approved_user );
4081 update_option( 'auth_settings_access_users_approved', $auth_settings_access_users_approved );
4082 }
4083 }
4084
4085 // If we've added a new multisite user, go through all pending/approved/blocked lists
4086 // on individual sites and remove this user from them (to prevent duplicate entries).
4087 if ( $approved_user['multisite_user'] !== 'false' && is_multisite() ) {
4088 $list_names = array( 'access_users_pending', 'access_users_approved', 'access_users_blocked' );
4089 foreach ( wp_get_sites( array( 'limit' => 999999 ) ) as $site ) {
4090 foreach ( $list_names as $list_name ) {
4091 $user_list = get_blog_option( $site['blog_id'], 'auth_settings_' . $list_name, array() );
4092 $list_changed = false;
4093 foreach ( $user_list as $key => $user ) {
4094 if ( $user['email'] == $approved_user['email'] ) {
4095 unset( $user_list[$key] );
4096 $list_changed = true;
4097 }
4098 }
4099 if ( $list_changed ) {
4100 update_blog_option( $site['blog_id'], 'auth_settings_' . $list_name, $user_list );
4101 }
4102 }
4103 }
4104 }
4105
4106 } elseif ( $approved_user['edit_action'] === 'remove' ) {
4107
4108 // Remove user from approved list and save
4109 if ( $approved_user['multisite_user'] !== 'false' ) {
4110 if ( $this->is_email_in_list( $approved_user['email'], 'approved', 'multisite' ) ) {
4111 $auth_multisite_settings_access_users_approved = $this->sanitize_user_list(
4112 $this->get_plugin_option( 'access_users_approved', 'multisite admin' )
4113 );
4114 foreach ( $auth_multisite_settings_access_users_approved as $key => $existing_user ) {
4115 if ( $approved_user['email'] == $existing_user['email'] ) {
4116 unset( $auth_multisite_settings_access_users_approved[$key] );
4117 break;
4118 }
4119 }
4120 update_blog_option( BLOG_ID_CURRENT_SITE, 'auth_multisite_settings_access_users_approved', $auth_multisite_settings_access_users_approved );
4121 }
4122 } else {
4123 if ( $this->is_email_in_list( $approved_user['email'], 'approved' ) ) {
4124 $auth_settings_access_users_approved = $this->sanitize_user_list(
4125 $this->get_plugin_option( 'access_users_approved', 'single admin' )
4126 );
4127 foreach ( $auth_settings_access_users_approved as $key => $existing_user ) {
4128 if ( $approved_user['email'] == $existing_user['email'] ) {
4129 unset( $auth_settings_access_users_approved[$key] );
4130 break;
4131 }
4132 }
4133 update_option( 'auth_settings_access_users_approved', $auth_settings_access_users_approved );
4134 }
4135 }
4136
4137 } elseif ( $approved_user['edit_action'] === 'change_role' ) {
4138
4139 // Update user's role in WordPress
4140 $changed_user = get_user_by( 'email', $approved_user['email'] );
4141 if ( $changed_user ) {
4142 if ( is_multisite() && $approved_user['multisite_user'] !== 'false' ) {
4143 foreach ( get_blogs_of_user( $changed_user->ID ) as $blog ) {
4144 add_user_to_blog( $blog->userblog_id, $changed_user->ID, $approved_user['role'] );
4145 }
4146 } else {
4147 $changed_user->set_role( $approved_user['role'] );
4148 }
4149 }
4150
4151 if ( $approved_user['multisite_user'] !== 'false' ) {
4152 if ( $this->is_email_in_list( $approved_user['email'], 'approved', 'multisite' ) ) {
4153 $auth_multisite_settings_access_users_approved = $this->sanitize_user_list(
4154 $this->get_plugin_option( 'access_users_approved', 'multisite admin' )
4155 );
4156 foreach ( $auth_multisite_settings_access_users_approved as $key => $existing_user ) {
4157 if ( $approved_user['email'] == $existing_user['email'] ) {
4158 $auth_multisite_settings_access_users_approved[$key]['role'] = $approved_user['role'];
4159 break;
4160 }
4161 }
4162 update_blog_option( BLOG_ID_CURRENT_SITE, 'auth_multisite_settings_access_users_approved', $auth_multisite_settings_access_users_approved );
4163 }
4164 } else {
4165 // Update user's role in approved list and save.
4166 if ( $this->is_email_in_list( $approved_user['email'], 'approved' ) ) {
4167 $auth_settings_access_users_approved = $this->sanitize_user_list(
4168 $this->get_plugin_option( 'access_users_approved', 'single admin' )
4169 );
4170 foreach ( $auth_settings_access_users_approved as $key => $existing_user ) {
4171 if ( $approved_user['email'] == $existing_user['email'] ) {
4172 $auth_settings_access_users_approved[$key]['role'] = $approved_user['role'];
4173 break;
4174 }
4175 }
4176 update_option( 'auth_settings_access_users_approved', $auth_settings_access_users_approved );
4177 }
4178 }
4179
4180 }
4181 }
4182 }
4183
4184 // Editing a blocked list entry.
4185 if ( $_POST['setting'] === 'access_users_blocked' ) {
4186 // Initialize posted data if empty.
4187 if ( ! ( array_key_exists( 'access_users_blocked', $_POST ) && is_array( $_POST['access_users_blocked'] ) ) ) {
4188 $_POST['access_users_blocked'] = array();
4189 }
4190
4191 // Deal with each modified user (add or remove).
4192 foreach ( $_POST['access_users_blocked'] as $blocked_user ) {
4193
4194 if ( $blocked_user['edit_action'] === 'add' ) {
4195
4196 // Add new user to blocked list and save (skip if it's
4197 // already there--someone else might have just done it).
4198 if ( ! $this->is_email_in_list( $blocked_user['email'], 'blocked' ) ) {
4199 $auth_settings_access_users_blocked = $this->sanitize_user_list(
4200 $this->get_plugin_option( 'access_users_blocked', 'single admin' )
4201 );
4202 $blocked_user['date_added'] = date( 'M Y' );
4203 array_push( $auth_settings_access_users_blocked, $blocked_user );
4204 update_option( 'auth_settings_access_users_blocked', $auth_settings_access_users_blocked );
4205 }
4206
4207 } elseif ( $blocked_user['edit_action'] === 'remove' ) {
4208
4209 // Remove auth_blocked usermeta for the user.
4210 $unblocked_user = get_user_by( 'email', $blocked_user['email'] );
4211 if ( $unblocked_user !== false ) {
4212 delete_user_meta( $unblocked_user->ID, 'auth_blocked', 'yes' );
4213 }
4214
4215 // Remove user from blocked list and save
4216 if ( $this->is_email_in_list( $blocked_user['email'], 'blocked' ) ) {
4217 $auth_settings_access_users_blocked = $this->sanitize_user_list(
4218 $this->get_plugin_option( 'access_users_blocked', 'single admin' )
4219 );
4220 foreach ( $auth_settings_access_users_blocked as $key => $existing_user ) {
4221 if ( $blocked_user['email'] == $existing_user['email'] ) {
4222 unset( $auth_settings_access_users_blocked[$key] );
4223 break;
4224 }
4225 }
4226 update_option( 'auth_settings_access_users_blocked', $auth_settings_access_users_blocked );
4227 }
4228
4229 }
4230 }
4231 }
4232
4233 // Return 'success' value to AJAX call.
4234 die( 'success' );
4235 } // END update_auth_user()
4236
4237
4238
4239 /**
4240 * ***************************
4241 * Helper functions
4242 * ***************************
4243 */
4244
4245
4246 /**
4247 * Retrieves a specific plugin option from db. Multisite enabled.
4248 *
4249 * @param string $option Option name
4250 * @param string $admin_mode 'multisite admin' will retrieve the multisite value
4251 * @param string $override_mode 'allow override' will retrieve the multisite value if it exists
4252 * @param string $print_mode 'print overlay' will output overlay that hides this option on the settings page
4253 * @return mixed Option value, or null on failure
4254 */
4255 private function get_plugin_option( $option, $admin_mode = 'single admin', $override_mode = 'no override', $print_mode = 'no overlay' ) {
4256
4257 // Special case for user lists (they are saved seperately to prevent concurrency issues).
4258 if ( in_array( $option, array( 'access_users_pending', 'access_users_approved', 'access_users_blocked' ) ) ) {
4259 $list = $admin_mode === 'multisite admin' ? array() : get_option( 'auth_settings_' . $option );
4260 if ( is_multisite() && $admin_mode === 'multisite admin' ) {
4261 $list = get_blog_option( BLOG_ID_CURRENT_SITE, 'auth_multisite_settings_' . $option, array() );
4262 }
4263 return $list;
4264 }
4265
4266 // Get all plugin options.
4267 $auth_settings = $this->get_plugin_options( $admin_mode, $override_mode );
4268
4269 // Set option to null if it wasn't found.
4270 if ( ! array_key_exists( $option, $auth_settings ) ) {
4271 return null;
4272 }
4273
4274 // If requested and appropriate, print the overlay hiding the
4275 // single site option that is overridden by a multisite option.
4276 if (
4277 $admin_mode !== 'multisite admin' &&
4278 $override_mode === 'allow override' &&
4279 $print_mode === 'print overlay' &&
4280 array_key_exists( 'multisite_override', $auth_settings ) &&
4281 $auth_settings['multisite_override'] === '1' &&
4282 ( ! array_key_exists( 'advanced_override_multisite', $auth_settings ) || $auth_settings['advanced_override_multisite'] != '1' )
4283 ) {
4284 // Get original plugin options (not overridden value). We'll
4285 // show this old value behind the disabled overlay.
4286 $auth_settings = $this->get_plugin_options( $admin_mode, 'no override' );
4287
4288 $name = "auth_settings[$option]";
4289 $id = "auth_settings_$option"; ?>
4290 <div id="overlay-hide-auth_settings_<?php echo $option; ?>" class="auth_multisite_override_overlay">
4291 <span class="overlay-note">
4292 This setting is overridden by a <a href="<?php echo network_admin_url( 'admin.php?page=authorizer&tab=external' ); ?>">multisite option</a>.
4293 </span>
4294 </div>
4295 <?php
4296 }
4297
4298 // If we're getting an option in a site that has overridden the multisite override, make
4299 // sure we are returning the option value from that site (not the multisite value).
4300 if ( array_key_exists( 'advanced_override_multisite', $auth_settings ) && $auth_settings['advanced_override_multisite'] == '1' ) {
4301 $auth_settings = $this->get_plugin_options( $admin_mode, 'no override' );
4302 }
4303
4304 return $auth_settings[$option];
4305 }
4306
4307 /**
4308 * Retrieves all plugin options from db. Multisite enabled.
4309 *
4310 * @param string $admin_mode 'multisite admin' will retrieve the multisite value
4311 * @param string $override_mode 'allow override' will retrieve the multisite value if it exists
4312 * @return mixed Option value, or null on failure
4313 */
4314 private function get_plugin_options( $admin_mode = 'single admin', $override_mode = 'no override' ) {
4315 // Grab plugin settings (skip if in multisite admin mode).
4316 $auth_settings = $admin_mode === 'multisite admin' ? array() : get_option( 'auth_settings' );
4317
4318 // Initialize to empty array if the plugin option doesn't exist.
4319 if ( $auth_settings === FALSE ) {
4320 $auth_settings = array();
4321 }
4322
4323 // Merge multisite options if we're in a network and the current site hasn't overridden multisite settings.
4324 if ( is_multisite() && ( ! array_key_exists( 'advanced_override_multisite', $auth_settings ) || $auth_settings['advanced_override_multisite'] != '1' ) ) {
4325 // Get multisite options.
4326 $auth_multisite_settings = get_blog_option( BLOG_ID_CURRENT_SITE, 'auth_multisite_settings', array() );
4327
4328 // Return the multisite options if we're viewing the network admin options page.
4329 // Otherwise override options with their multisite equivalents.
4330 if ( $admin_mode === 'multisite admin' ) {
4331 $auth_settings = $auth_multisite_settings;
4332 } elseif (
4333 $override_mode === 'allow override' &&
4334 array_key_exists( 'multisite_override', $auth_multisite_settings ) &&
4335 $auth_multisite_settings['multisite_override'] === '1'
4336 ) {
4337 // Keep track of the multisite override selection.
4338 $auth_settings['multisite_override'] = $auth_multisite_settings['multisite_override'];
4339
4340 // Note: the options below should be the complete list of
4341 // overridden options. It is *not* the complete list of all
4342 // options (some options don't have a multisite equivalent)
4343
4344 // Note: access_users_approved, access_users_pending, and
4345 // access_users_blocked do not get overridden. However,
4346 // since access_users_approved has a multisite equivalent,
4347 // you must retrieve them both seperately. This is done
4348 // because the two lists should be treated differently.
4349 // $approved_users = $this->get_plugin_option( 'access_users_approved', 'single admin' );
4350 // $ms_approved_users = $this->get_plugin_option( 'access_users_approved', 'multisite admin' );
4351
4352 // Override external services (google, cas, or ldap) and associated options
4353 $auth_settings['google'] = $auth_multisite_settings['google'];
4354 $auth_settings['google_clientid'] = $auth_multisite_settings['google_clientid'];
4355 $auth_settings['google_clientsecret'] = $auth_multisite_settings['google_clientsecret'];
4356 $auth_settings['cas'] = $auth_multisite_settings['cas'];
4357 $auth_settings['cas_custom_label'] = $auth_multisite_settings['cas_custom_label'];
4358 $auth_settings['cas_host'] = $auth_multisite_settings['cas_host'];
4359 $auth_settings['cas_port'] = $auth_multisite_settings['cas_port'];
4360 $auth_settings['cas_path'] = $auth_multisite_settings['cas_path'];
4361 $auth_settings['cas_attr_email'] = $auth_multisite_settings['cas_attr_email'];
4362 $auth_settings['cas_attr_first_name'] = $auth_multisite_settings['cas_attr_first_name'];
4363 $auth_settings['cas_attr_last_name'] = $auth_multisite_settings['cas_attr_last_name'];
4364 $auth_settings['cas_attr_update_on_login'] = $auth_multisite_settings['cas_attr_update_on_login'];
4365 $auth_settings['ldap'] = $auth_multisite_settings['ldap'];
4366 $auth_settings['ldap_host'] = $auth_multisite_settings['ldap_host'];
4367 $auth_settings['ldap_port'] = $auth_multisite_settings['ldap_port'];
4368 $auth_settings['ldap_search_base'] = $auth_multisite_settings['ldap_search_base'];
4369 $auth_settings['ldap_uid'] = $auth_multisite_settings['ldap_uid'];
4370 $auth_settings['ldap_attr_email'] = $auth_multisite_settings['ldap_attr_email'];
4371 $auth_settings['ldap_user'] = $auth_multisite_settings['ldap_user'];
4372 $auth_settings['ldap_password'] = $auth_multisite_settings['ldap_password'];
4373 $auth_settings['ldap_tls'] = $auth_multisite_settings['ldap_tls'];
4374 $auth_settings['ldap_lostpassword_url'] = $auth_multisite_settings['ldap_lostpassword_url'];
4375 $auth_settings['ldap_attr_first_name'] = $auth_multisite_settings['ldap_attr_first_name'];
4376 $auth_settings['ldap_attr_last_name'] = $auth_multisite_settings['ldap_attr_last_name'];
4377 $auth_settings['ldap_attr_update_on_login'] = $auth_multisite_settings['ldap_attr_update_on_login'];
4378
4379 // Override access_who_can_login and access_who_can_view
4380 $auth_settings['access_who_can_login'] = $auth_multisite_settings['access_who_can_login'];
4381 $auth_settings['access_who_can_view'] = $auth_multisite_settings['access_who_can_view'];
4382
4383 // Override access_default_role
4384 $auth_settings['access_default_role'] = $auth_multisite_settings['access_default_role'];
4385
4386 // Override lockouts
4387 $auth_settings['advanced_lockouts'] = $auth_multisite_settings['advanced_lockouts'];
4388
4389 // Override Hide WordPress login
4390 $auth_settings['advanced_hide_wp_login'] = $auth_multisite_settings['advanced_hide_wp_login'];
4391 }
4392 }
4393 return $auth_settings;
4394 }
4395
4396
4397 private function maybe_email_welcome_message( $email ) {
4398 // Get option for whether to email welcome messages.
4399 $should_email_new_approved_users = $this->get_plugin_option( 'access_should_email_approved_users' );
4400
4401 // Do not send welcome email if option not enabled.
4402 if ( $should_email_new_approved_users !== '1' ) {
4403 return false;
4404 }
4405
4406 // Make sure we didn't just email this user (can happen with
4407 // multiple admins saving at the same time, or by clicking
4408 // Approve button too rapidly).
4409 $recently_sent_emails = get_option( 'auth_settings_recently_sent_emails' );
4410 if ( $recently_sent_emails === FALSE ) {
4411 $recently_sent_emails = array();
4412 }
4413 foreach ( $recently_sent_emails as $key => $recently_sent_email ) {
4414 if ( $recently_sent_email['time'] < strtotime( 'now -1 minutes' ) ) {
4415 // Remove emails sent more than 1 minute ago.
4416 unset( $recently_sent_emails[$key] );
4417 } elseif ( $recently_sent_email['email'] === $email ) {
4418 // Sent an email to this user within the last 1 minute, so
4419 // quit without sending.
4420 return false;
4421 }
4422 }
4423 // Add the email we're about to send to the list.
4424 $recently_sent_emails[] = array(
4425 'email' => $email,
4426 'time' => time(),
4427 );
4428 update_option( 'auth_settings_recently_sent_emails', $recently_sent_emails );
4429
4430 // Get welcome email subject and body text
4431 $subject = $this->get_plugin_option( 'access_email_approved_users_subject' );
4432 $body = apply_filters( 'the_content', $this->get_plugin_option( 'access_email_approved_users_body' ) );
4433
4434 // Fail if the subject/body options don't exist or are empty.
4435 if ( is_null( $subject ) || is_null( $body ) || strlen( $subject ) === 0 || strlen( $body ) === 0 ) {
4436 return false;
4437 }
4438
4439 // Replace approved shortcode patterns in subject and body.
4440 $site_name = get_bloginfo( 'name' );
4441 $site_url = get_site_url();
4442 $subject = str_replace( '[site_name]', $site_name, $subject );
4443 $body = str_replace( '[site_name]', $site_name, $body );
4444 $body = str_replace( '[site_url]', $site_url, $body );
4445 $body = str_replace( '[user_email]', $email, $body );
4446 $headers = 'Content-type: text/html' . "\r\n";
4447
4448 // Send email.
4449 wp_mail( $email, $subject, $body, $headers );
4450
4451 // Indicate mail was sent.
4452 return true;
4453 }
4454
4455 /**
4456 * Generate a unique cookie to add to nonces to prevent CSRF.
4457 */
4458 protected $cookie_value = null;
4459 function get_cookie_value() {
4460 if ( ! $this->cookie_value ) {
4461 if ( isset( $_COOKIE['login_unique'] ) ) {
4462 $this->cookie_value = $_COOKIE['login_unique'];
4463 } else {
4464 $this->cookie_value = md5( rand() );
4465 }
4466 }
4467 return $this->cookie_value;
4468 } // END get_cookie_value()
4469
4470 /**
4471 * Basic encryption using a public (not secret!) key. Used for general
4472 * database obfuscation of passwords.
4473 */
4474 private static $key = '8QxnrvjdtweisvCBKEY!+0';
4475 function encrypt( $text ) {
4476 $result = '';
4477
4478 // Use mcrypt library (better) if php5-mcrypt extension is enabled.
4479 if ( function_exists( 'mcrypt_encrypt' ) ) {
4480 $result = mcrypt_encrypt( MCRYPT_RIJNDAEL_256, self::$key, $text, MCRYPT_MODE_ECB, 'abcdefghijklmnopqrstuvwxyz012345' );
4481 } else {
4482 for ( $i = 0; $i < strlen( $text ); $i++ ) {
4483 $char = substr( $text, $i, 1 );
4484 $keychar = substr( self::$key, ( $i % strlen( self::$key ) ) - 1, 1 );
4485 $char = chr( ord( $char ) + ord( $keychar ) );
4486 $result .= $char;
4487 }
4488 $result = base64_encode( $result );
4489 }
4490
4491 return $result;
4492 } // END encrypt()
4493
4494 function decrypt( $secret ) {
4495 $result = '';
4496
4497 // Use mcrypt library (better) if php5-mcrypt extension is enabled.
4498 if ( function_exists( 'mcrypt_decrypt' ) ) {
4499 $result = rtrim( mcrypt_decrypt( MCRYPT_RIJNDAEL_256, self::$key, $secret, MCRYPT_MODE_ECB, 'abcdefghijklmnopqrstuvwxyz012345' ), "\0$result" );
4500 } else {
4501 $secret = base64_decode( $secret );
4502 for ( $i = 0; $i < strlen( $secret ); $i++ ) {
4503 $char = substr( $secret, $i, 1 );
4504 $keychar = substr( self::$key, ( $i % strlen( self::$key ) ) - 1, 1 );
4505 $char = chr( ord( $char ) - ord( $keychar ) );
4506 $result .= $char;
4507 }
4508 }
4509
4510 return $result;
4511 } // END decrypt()
4512
4513 /**
4514 * In a multisite environment, returns true if the current user is logged
4515 * in and a user of the current blog. In single site mode, simply returns
4516 * true if the current user is logged in.
4517 */
4518 function is_user_logged_in_and_blog_user() {
4519 $is_user_logged_in_and_blog_user = false;
4520 if ( is_multisite() ) {
4521 $is_user_logged_in_and_blog_user = is_user_logged_in() && is_user_member_of_blog( get_current_user_id() );
4522 } else {
4523 $is_user_logged_in_and_blog_user = is_user_logged_in();
4524 }
4525 return $is_user_logged_in_and_blog_user;
4526 } // END is_user_logged_in_and_blog_user()
4527
4528 /**
4529 * Helper function to determine whether a given email is in one of
4530 * the lists (pending, approved, blocked). Defaults to the list of
4531 * approved users.
4532 */
4533 function is_email_in_list( $email = '', $list = 'approved', $multisite_mode = 'single' ) {
4534 if ( empty( $email ) )
4535 return false;
4536
4537 switch ( $list ) {
4538 case 'pending':
4539 $auth_settings_access_users_pending = $this->get_plugin_option( 'access_users_pending', 'single admin' );
4540 return $this->in_multi_array( $email, $auth_settings_access_users_pending );
4541 break;
4542 case 'blocked':
4543 $auth_settings_access_users_blocked = $this->get_plugin_option( 'access_users_blocked', 'single admin' );
4544 return $this->in_multi_array( $email, $auth_settings_access_users_blocked );
4545 break;
4546 case 'approved':
4547 default:
4548 if ( $multisite_mode !== 'single' ) {
4549 // Get multisite users only.
4550 $auth_settings_access_users_approved = $this->get_plugin_option( 'access_users_approved', 'multisite admin' );
4551 } else if ( is_multisite() && $this->get_plugin_option( 'advanced_override_multisite' ) == '1' ) {
4552 // This site has overridden any multisite settings, so only get its users.
4553 $auth_settings_access_users_approved = $this->get_plugin_option( 'access_users_approved', 'single admin' );
4554 } else {
4555 // Get all site users and all multisite users.
4556 $auth_settings_access_users_approved = array_merge(
4557 $this->get_plugin_option( 'access_users_approved', 'single admin' ),
4558 $this->get_plugin_option( 'access_users_approved', 'multisite admin' )
4559 );
4560 }
4561 return $this->in_multi_array( $email, $auth_settings_access_users_approved );
4562 break;
4563 }
4564 } // END is_email_in_list
4565
4566 /**
4567 * Helper function to search a multidimensional array for a value.
4568 */
4569 function in_multi_array( $needle = '', $haystack = array(), $strict_mode = 'not strict', $case_sensitivity = 'case insensitive' ) {
4570 if ( ! is_array( $haystack ) ) {
4571 return false;
4572 }
4573 if ( $case_sensitivity === 'case insensitive' ) {
4574 $needle = strtolower( $needle );
4575 }
4576 foreach ( $haystack as $item ) {
4577 if ( $case_sensitivity === 'case insensitive' && ! is_array( $item ) ) {
4578 $item = strtolower( $item );
4579 }
4580 if ( ( $strict_mode === 'strict' ? $item === $needle : $item == $needle ) || ( is_array( $item ) && $this->in_multi_array( $needle, $item, $strict_mode, $case_sensitivity ) ) ) {
4581 return true;
4582 }
4583 }
4584 return false;
4585 } // END in_multi_array()
4586
4587 /**
4588 * Helper function to get a WordPress page ID from the pagename.
4589 *
4590 * @param string $pagename Page Slug
4591 * @return int Page/Post ID
4592 */
4593 function get_id_from_pagename( $pagename = '' ) {
4594 global $wpdb;
4595 $page_id = $wpdb->get_var( "SELECT ID FROM $wpdb->posts WHERE post_name = '" . sanitize_title_for_query( $pagename ) . "'" );
4596 return $page_id;
4597 } // END get_id_from_pagename()
4598
4599 /**
4600 * Helper function to determine if an URL is accessible.
4601 *
4602 * @param string $url URL that should be publicly reachable
4603 * @return boolean Whether the URL is publicly reachable
4604 */
4605 function url_is_accessible( $url ) {
4606 // Make sure php5-curl extension is installed on server.
4607 if ( ! function_exists( 'curl_init' ) ) {
4608 // Note: This will silently fail, saying url is not accessible.
4609 // Warn user elsewhere that they should install curl.
4610 return false;
4611 }
4612
4613 // Use curl to retrieve the URL.
4614 $handle = curl_init( $url );
4615 curl_setopt( $handle, CURLOPT_RETURNTRANSFER, TRUE );
4616 curl_setopt( $handle, CURLOPT_SSL_VERIFYPEER, FALSE );
4617 $response = curl_exec( $handle );
4618 $http_code = curl_getinfo( $handle, CURLINFO_HTTP_CODE );
4619 curl_close( $handle );
4620
4621 // Return true if the document has loaded successfully without any redirection or error
4622 return $http_code >= 200 && $http_code < 400;
4623 } // END url_is_accessible()
4624
4625 // Helper function that builds option tags for a select element for all
4626 // roles the current user has permission to assign.
4627 function wp_dropdown_permitted_roles( $selected_role = 'subscriber', $disable_input = 'not disabled' ) {
4628 $roles = get_editable_roles();
4629 $current_user = wp_get_current_user();
4630
4631 // Make sure we have a selected role (default to subscriber).
4632 if ( strlen( $selected_role ) < 1 ) {
4633 $selected_role = 'subscriber';
4634 }
4635
4636 // If the currently selected role is not in the list of roles, it
4637 // either doesn't exist or the current user is not permitted to
4638 // assign it.
4639 if ( ! array_key_exists( $selected_role, $roles ) ) {
4640 ?><option value="<?php echo $selected_role; ?>"><?php echo ucfirst( $selected_role ); ?></option><?php
4641
4642 // If the role exists, that means the user isn't permitted to
4643 // assign it, so assume they can't edit that user's role at
4644 // all. Return only the one role for the dropdown list.
4645 if ( ! is_null( get_role( $selected_role ) ) ) {
4646 return;
4647 }
4648 }
4649
4650 // Print an option element for each permitted role.
4651 foreach ( $roles as $name => $role ) {
4652 $selected = $selected_role === $name ? ' selected="selected"' : '';
4653
4654 // Don't let a user change their own role
4655 $disabled = $selected_role !== $name && $disable_input === 'disabled' ? ' disabled="disabled"' : '';
4656
4657 // But network admins can always change their role.
4658 if ( is_multisite() && current_user_can( 'manage_network' ) ) {
4659 $disabled = '';
4660 }
4661
4662 ?><option value="<?php echo $name; ?>"<?php echo $selected . $disabled; ?>><?php echo $role['name']; ?></option><?php
4663 }
4664 } // END wp_dropdown_permitted_roles()
4665
4666 // Helper function to get a single user info array from one of the
4667 // access control lists (pending, approved, or blocked).
4668 // Returns: false if not found; otherwise
4669 // array( 'email' => '', 'role' => '', 'date_added' => '', ['usermeta' => [''|array()]] );
4670 function get_user_info_from_list( $email, $list ) {
4671 foreach ( $list as $user_info ) {
4672 if ( $user_info['email'] === $email ) {
4673 return $user_info;
4674 }
4675 }
4676 return false;
4677 } // END get_user_info_from_list()
4678
4679 // Helper function to convert seconds to human readable text.
4680 // Source: http://csl.name/php-secs-to-human-text/
4681 function seconds_as_sentence( $secs ) {
4682 $units = array(
4683 "week" => 7 * 24 * 3600,
4684 "day" => 24 * 3600,
4685 "hour" => 3600,
4686 "minute" => 60,
4687 "second" => 1,
4688 );
4689
4690 // specifically handle zero
4691 if ( $secs == 0 ) return "0 seconds";
4692
4693 $s = "";
4694
4695 foreach ( $units as $name => $divisor ) {
4696 if ( $quot = intval( $secs / $divisor ) ) {
4697 $s .= "$quot $name";
4698 $s .= ( abs( $quot ) > 1 ? "s" : "" ) . ", ";
4699 $secs -= $quot * $divisor;
4700 }
4701 }
4702
4703 return substr( $s, 0, -2 );
4704 } // END seconds_as_sentence()
4705
4706 // Helper function to get all available usermeta keys as an array.
4707 function get_all_usermeta_keys() {
4708 global $wpdb;
4709 $usermeta_keys = $wpdb->get_col( "SELECT DISTINCT $wpdb->usermeta.meta_key FROM $wpdb->usermeta" );
4710 return $usermeta_keys;
4711 }
4712
4713
4714 /**
4715 * Plugin Update Routines.
4716 */
4717 function auth_update_check() {
4718 // Update: migrate user lists to own options (addresses concurrency
4719 // when saving plugin options, since user lists are changed often
4720 // and we don't want to overwrite changes to the lists when an
4721 // admin saves all of the plugin options.)
4722 // Note: Pending user list is changed whenever a new user tries to
4723 // log in; approved and blocked lists are changed whenever an admin
4724 // changes them from the multisite panel, the dashboard widget, or
4725 // the plugin options page.
4726 $update_if_older_than = 20140709;
4727 $auth_version = get_option( 'auth_version' );
4728 if ( $auth_version === false || intval( $auth_version ) < $update_if_older_than ) {
4729 // Copy single site user lists to new options (if they exist).
4730 $auth_settings = get_option( 'auth_settings' );
4731 if ( is_array( $auth_settings ) && array_key_exists( 'access_users_pending', $auth_settings ) ) {
4732 update_option( 'auth_settings_access_users_pending', $auth_settings['access_users_pending'] );
4733 unset( $auth_settings['access_users_pending'] );
4734 update_option( 'auth_settings', $auth_settings );
4735 }
4736 if ( is_array( $auth_settings ) && array_key_exists( 'access_users_approved', $auth_settings ) ) {
4737 update_option( 'auth_settings_access_users_approved', $auth_settings['access_users_approved'] );
4738 unset( $auth_settings['access_users_approved'] );
4739 update_option( 'auth_settings', $auth_settings );
4740 }
4741 if ( is_array( $auth_settings ) && array_key_exists( 'access_users_blocked', $auth_settings ) ) {
4742 update_option( 'auth_settings_access_users_blocked', $auth_settings['access_users_blocked'] );
4743 unset( $auth_settings['access_users_blocked'] );
4744 update_option( 'auth_settings', $auth_settings );
4745 }
4746 // Copy multisite user lists to new options (if they exist).
4747 if ( is_multisite() ) {
4748 $auth_multisite_settings = get_blog_option( BLOG_ID_CURRENT_SITE, 'auth_multisite_settings', array() );
4749 if ( is_array( $auth_multisite_settings ) && array_key_exists( 'access_users_pending', $auth_multisite_settings ) ) {
4750 update_blog_option( BLOG_ID_CURRENT_SITE, 'auth_multisite_settings_access_users_pending', $auth_multisite_settings['access_users_pending'] );
4751 unset( $auth_multisite_settings['access_users_pending'] );
4752 update_blog_option( BLOG_ID_CURRENT_SITE, 'auth_multisite_settings', $auth_multisite_settings );
4753 }
4754 if ( is_array( $auth_multisite_settings ) && array_key_exists( 'access_users_approved', $auth_multisite_settings ) ) {
4755 update_blog_option( BLOG_ID_CURRENT_SITE, 'auth_multisite_settings_access_users_approved', $auth_multisite_settings['access_users_approved'] );
4756 unset( $auth_multisite_settings['access_users_approved'] );
4757 update_blog_option( BLOG_ID_CURRENT_SITE, 'auth_multisite_settings', $auth_multisite_settings );
4758 }
4759 if ( is_array( $auth_multisite_settings ) && array_key_exists( 'access_users_blocked', $auth_multisite_settings ) ) {
4760 update_blog_option( BLOG_ID_CURRENT_SITE, 'auth_multisite_settings_access_users_blocked', $auth_multisite_settings['access_users_blocked'] );
4761 unset( $auth_multisite_settings['access_users_blocked'] );
4762 update_blog_option( BLOG_ID_CURRENT_SITE, 'auth_multisite_settings', $auth_multisite_settings );
4763 }
4764 }
4765 // Update version to reflect this change has been made.
4766 update_option( 'auth_version', $update_if_older_than );
4767 }
4768
4769 // // Update: TEMPLATE
4770 // $update_if_older_than = YYYYMMDD;
4771 // $auth_version = get_option( 'auth_version' );
4772 // if ( $auth_version === false || intval( $auth_version ) < $update_if_older_than ) {
4773 // UPDATE CODE HERE
4774 // update_option( 'auth_version', $update_if_older_than );
4775 // }
4776 }
4777
4778 } // END class WP_Plugin_Authorizer
4779 }
4780
4781 // Instantiate the plugin class.
4782 $wp_plugin_authorizer = new WP_Plugin_Authorizer();
4783