PluginProbe
Authorizer / 2.3.7
Authorizer v2.3.7
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.7, at authorizer.php

4,820 lines 234.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.7
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 $admin_mode = ( is_array( $args ) && array_key_exists( 'multisite_admin', $args ) && $args['multisite_admin'] === true ) ? 'multisite admin' : 'single admin';
2458 ?><div id="section_info_access_lists" class="section_info">
2459 <p>Manage who has access to this site using these lists.</p>
2460 <ol>
2461 <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>
2462 <li><strong>Approved</strong> users have access to the site once they successfully log in.</li>
2463 <li><strong>Blocked</strong> users will receive an error message when they try to visit the site after authenticating.</li>
2464 </ol>
2465 </div>
2466 <table class="form-table">
2467 <tbody>
2468 <tr>
2469 <th scope="row">Pending Users <em>(<?php echo $this->get_user_count_from_list( 'pending', $admin_mode ); ?>)</em></th>
2470 <td><?php $this->print_combo_auth_access_users_pending(); ?></td>
2471 </tr>
2472 <tr>
2473 <th scope="row">Approved Users <em>(<?php echo $this->get_user_count_from_list( 'approved', $admin_mode ); ?>)</em></th>
2474 <td><?php $this->print_combo_auth_access_users_approved(); ?></td>
2475 </tr>
2476 <tr>
2477 <th scope="row">Blocked Users <em>(<?php echo $this->get_user_count_from_list( 'blocked', $admin_mode ); ?>)</em></th>
2478 <td><?php $this->print_combo_auth_access_users_blocked(); ?></td>
2479 </tr>
2480 </tbody>
2481 </table>
2482 <?php
2483 } // END print_section_info_access_lists()
2484
2485 function print_combo_auth_access_users_pending( $args = '' ) {
2486 // Get plugin option.
2487 $option = 'access_users_pending';
2488 $auth_settings_option = $this->get_plugin_option( $option );
2489 $auth_settings_option = is_array( $auth_settings_option ) ? $auth_settings_option : array();
2490
2491 // Print option elements.
2492 ?><ul id="list_auth_settings_access_users_pending" style="margin:0;">
2493 <?php if ( count( $auth_settings_option ) > 0 ) : ?>
2494 <?php foreach ( $auth_settings_option as $key => $pending_user ): ?>
2495 <?php if ( empty( $pending_user ) || count( $pending_user ) < 1 ) continue; ?>
2496 <?php $pending_user['is_wp_user'] = false; ?>
2497 <li>
2498 <input type="text" id="auth_settings_<?php echo $option; ?>_<?php echo $key; ?>" value="<?php echo $pending_user['email']; ?>" readonly="true" class="auth-email" />
2499 <select id="auth_settings_<?php echo $option; ?>_<?php echo $key; ?>_role" class="auth-role">
2500 <?php $this->wp_dropdown_permitted_roles( $pending_user['role'] ); ?>
2501 </select>
2502 <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>
2503 <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>
2504 <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>
2505 </li>
2506 <?php endforeach; ?>
2507 <?php else: ?>
2508 <li class="auth-empty"><em>No pending users</em></li>
2509 <?php endif; ?>
2510 </ul>
2511 <?php
2512 } // END print_combo_auth_access_users_pending()
2513
2514 function print_combo_auth_access_users_approved( $args = '' ) {
2515 // Get plugin option.
2516 $option = 'access_users_approved';
2517 $admin_mode = ( is_array( $args ) && array_key_exists( 'multisite_admin', $args ) && $args['multisite_admin'] === true ) ? 'multisite admin' : 'single admin';
2518 $auth_settings_option = $this->get_plugin_option( $option, $admin_mode, 'no override' );
2519 $auth_settings_option = is_array( $auth_settings_option ) ? $auth_settings_option : array();
2520
2521 // Get multisite approved users (add them to top of list, greyed out).
2522 $auth_override_multisite = $this->get_plugin_option( 'advanced_override_multisite' );
2523 $auth_multisite_settings = $this->get_plugin_options( 'multisite admin' );
2524 $option_multisite = 'access_users_approved';
2525 $auth_settings_option_multisite = array();
2526 if (
2527 is_multisite() &&
2528 $auth_override_multisite != '1' &&
2529 array_key_exists( 'multisite_override', $auth_multisite_settings ) &&
2530 $auth_multisite_settings['multisite_override'] === '1'
2531 ) {
2532 $auth_settings_option_multisite = $this->get_plugin_option( $option, 'multisite admin', 'allow override' );
2533 $auth_settings_option_multisite = is_array( $auth_settings_option_multisite ) ? $auth_settings_option_multisite : array();
2534 }
2535
2536 // Get default role for new user dropdown.
2537 $access_default_role = $this->get_plugin_option( 'access_default_role', 'single admin', 'allow override' );
2538
2539 // Get custom usermeta field to show.
2540 $advanced_usermeta = $this->get_plugin_option( 'advanced_usermeta' );
2541
2542 // Adjust javascript function prefixes if multisite.
2543 $js_function_prefix = $admin_mode === 'multisite admin' ? 'auth_multisite_' : 'auth_';
2544 $multisite_admin_page = $admin_mode === 'multisite admin';
2545
2546 ?><ul id="list_auth_settings_access_users_approved" style="margin:0;">
2547 <?php if ( ! $multisite_admin_page ) :
2548 foreach ( $auth_settings_option_multisite as $key => $approved_user ) :
2549 if ( empty( $approved_user ) || count( $approved_user ) < 1 ) :
2550 continue;
2551 endif;
2552 $approved_wp_user = get_user_by( 'email', $approved_user['email'] );
2553 if ( $approved_wp_user ) :
2554 $approved_user['email'] = $approved_wp_user->user_email;
2555 $approved_user['role'] = $multisite_admin_page || count( $approved_wp_user->roles ) === 0 ? $approved_user['role'] : array_shift( $approved_wp_user->roles );
2556 $approved_user['date_added'] = $approved_wp_user->user_registered;
2557 // Get usermeta field from the WordPress user's real usermeta.
2558 if ( strlen( $advanced_usermeta ) > 0 ) :
2559 if ( strpos( $advanced_usermeta, 'acf___' ) === 0 && class_exists( 'acf' ) ) :
2560 // Get ACF Field value for the user
2561 $approved_user['usermeta'] = get_field( str_replace('acf___', '', $advanced_usermeta ), 'user_' . $approved_wp_user->ID );
2562 else :
2563 // Get regular usermeta value for the user.
2564 $approved_user['usermeta'] = get_user_meta( $approved_wp_user->ID, $advanced_usermeta, true );
2565 endif;
2566
2567 if ( is_array( $approved_user['usermeta'] ) || is_object( $approved_user['usermeta'] ) ) :
2568 $approved_user['usermeta'] = serialize( $approved_user['usermeta'] );
2569 endif;
2570 endif;
2571 endif;
2572 if ( ! array_key_exists( 'usermeta', $approved_user ) ) :
2573 $approved_user['usermeta'] = '';
2574 endif; ?>
2575 <li>
2576 <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" />
2577 <select id="auth_multisite_settings_<?php echo $option; ?>_<?php echo $key; ?>_role" class="auth-role auth-multisite-role" disabled="disabled">
2578 <?php $this->wp_dropdown_permitted_roles( $approved_user['role'] ); ?>
2579 </select>
2580 <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" />
2581 <?php if ( strlen( $advanced_usermeta ) > 0 ) :
2582 $should_show_usermeta_in_text_field = true; // Fallback renderer for usermeta; try to use a select first.
2583 if ( strpos( $advanced_usermeta, 'acf___' ) === 0 && class_exists( 'acf' ) ) :
2584 $field_object = get_field_object( str_replace('acf___', '', $advanced_usermeta ) );
2585 if ( is_array( $field_object ) && array_key_exists( 'type', $field_object ) && $field_object['type'] === 'select' ) :
2586 $should_show_usermeta_in_text_field = false; ?>
2587 <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 );">
2588 <option value=""<?php if ( empty( $approved_user['usermeta'] ) ) echo ' selected="selected"'; ?>>-- None --</option>
2589 <?php foreach ( $field_object['choices'] as $key => $label ) : ?>
2590 <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>
2591 <?php endforeach; ?>
2592 </select>
2593 <?php endif; ?>
2594 <?php endif; ?>
2595 <?php if ( $should_show_usermeta_in_text_field ) : ?>
2596 <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" />
2597 <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>
2598 <?php endif; ?>
2599 <?php endif; ?>
2600 &nbsp;&nbsp;<a title="WordPress Multisite user" class="auth-multisite-user"><span class="glyphicon glyphicon-globe"></span></a>
2601 </li>
2602 <?php endforeach;
2603 endif;
2604 foreach ( $auth_settings_option as $key => $approved_user ):
2605 $is_current_user = false;
2606 $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>' : '';
2607 if ( empty( $approved_user ) || count( $approved_user ) < 1 ) :
2608 continue;
2609 endif;
2610 $approved_wp_user = get_user_by( 'email', $approved_user['email'] );
2611 if ( $approved_wp_user ) :
2612 $approved_user['email'] = $approved_wp_user->user_email;
2613 $approved_user['role'] = $multisite_admin_page || count( $approved_wp_user->roles ) === 0 ? $approved_user['role'] : array_shift( $approved_wp_user->roles );
2614 $approved_user['date_added'] = $approved_wp_user->user_registered;
2615 $approved_user['is_wp_user'] = true;
2616 $is_current_user = $approved_wp_user->ID === get_current_user_id();
2617 // Get usermeta field from the WordPress user's real usermeta.
2618 if ( strlen( $advanced_usermeta ) > 0 ) :
2619 if ( strpos( $advanced_usermeta, 'acf___' ) === 0 && class_exists( 'acf' ) ) :
2620 // Get ACF Field value for the user
2621 $approved_user['usermeta'] = get_field( str_replace('acf___', '', $advanced_usermeta ), 'user_' . $approved_wp_user->ID );
2622 else :
2623 // Get regular usermeta value for the user.
2624 $approved_user['usermeta'] = get_user_meta( $approved_wp_user->ID, $advanced_usermeta, true );
2625 endif;
2626
2627 if ( is_array( $approved_user['usermeta'] ) || is_object( $approved_user['usermeta'] ) ) :
2628 $approved_user['usermeta'] = serialize( $approved_user['usermeta'] );
2629 endif;
2630 endif;
2631 else :
2632 $approved_user['is_wp_user'] = false;
2633 endif;
2634 if ( ! array_key_exists( 'usermeta', $approved_user ) ) :
2635 $approved_user['usermeta'] = '';
2636 endif; ?>
2637 <li>
2638 <input type="text" id="auth_settings_<?php echo $option; ?>_<?php echo $key; ?>" value="<?php echo $approved_user['email']; ?>" readonly="true" class="auth-email" />
2639 <select id="auth_settings_<?php echo $option; ?>_<?php echo $key; ?>_role" class="auth-role" onchange="<?php echo $js_function_prefix; ?>change_role( this );">
2640 <?php $disable_input = $is_current_user ? 'disabled' : null; ?>
2641 <?php $this->wp_dropdown_permitted_roles( $approved_user['role'], $disable_input ); ?>
2642 </select>
2643 <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" />
2644 <?php if ( strlen( $advanced_usermeta ) > 0 ) :
2645 $should_show_usermeta_in_text_field = true; // Fallback renderer for usermeta; try to use a select first.
2646 if ( strpos( $advanced_usermeta, 'acf___' ) === 0 && class_exists( 'acf' ) ) :
2647 $field_object = get_field_object( str_replace('acf___', '', $advanced_usermeta ) );
2648 if ( is_array( $field_object ) && array_key_exists( 'type', $field_object ) && $field_object['type'] === 'select' ) :
2649 $should_show_usermeta_in_text_field = false; ?>
2650 <select id="auth_settings_<?php echo $option; ?>_<?php echo $key; ?>_usermeta" class="auth-usermeta" onchange="<?php echo $js_function_prefix; ?>update_usermeta( this );" >
2651 <option value=""<?php if ( empty( $approved_user['usermeta'] ) ) echo ' selected="selected"'; ?>>-- None --</option>
2652 <?php foreach ( $field_object['choices'] as $key => $label ) : ?>
2653 <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>
2654 <?php endforeach; ?>
2655 </select>
2656 <?php endif; ?>
2657 <?php endif; ?>
2658 <?php if ( $should_show_usermeta_in_text_field ) : ?>
2659 <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" />
2660 <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>
2661 <?php endif; ?>
2662 <?php endif; ?>
2663 <?php if ( ! $is_current_user ): ?>
2664 <?php if ( ! $multisite_admin_page ) : ?>
2665 <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>
2666 <?php endif; ?>
2667 <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>
2668 <?php endif; ?>
2669 <?php echo $local_user_icon; ?>
2670 </li>
2671 <?php endforeach; ?>
2672 </ul>
2673 <div id="new_auth_settings_<?php echo $option; ?>">
2674 <input type="text" id="new_approved_user_email" placeholder="email address" class="auth-email new" />
2675 <select id="new_approved_user_role" class="auth-role">
2676 <?php $this->wp_dropdown_permitted_roles( $access_default_role ); ?>
2677 </select>
2678 <div class="btn-group">
2679 <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>
2680 <button type="button" class="btn button-primary dropdown-toggle" data-toggle="dropdown">
2681 <span class="caret"></span>
2682 <span class="sr-only">Toggle Dropdown</span>
2683 </button>
2684 <ul class="dropdown-menu" role="menu">
2685 <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>
2686 </ul>
2687 </div>
2688 </div>
2689 <?php
2690 } // END print_combo_auth_access_users_approved()
2691
2692 function print_combo_auth_access_users_blocked( $args = '' ) {
2693 // Get plugin option.
2694 $option = 'access_users_blocked';
2695 $auth_settings_option = $this->get_plugin_option( $option );
2696 $auth_settings_option = is_array( $auth_settings_option ) ? $auth_settings_option : array();
2697
2698 // Get default role for new blocked user dropdown.
2699 $access_default_role = $this->get_plugin_option( 'access_default_role', 'single admin', 'allow override' );
2700
2701 // Print option elements.
2702 ?><ul id="list_auth_settings_<?php echo $option; ?>" style="margin:0;">
2703 <?php foreach ( $auth_settings_option as $key => $blocked_user ): ?>
2704 <?php if ( empty( $blocked_user ) || count( $blocked_user ) < 1 ) continue; ?>
2705 <?php if ( $blocked_wp_user = get_user_by( 'email', $blocked_user['email'] ) ): ?>
2706 <?php $blocked_user['email'] = $blocked_wp_user->user_email; ?>
2707 <?php $blocked_user['role'] = array_shift( $blocked_wp_user->roles ); ?>
2708 <?php $blocked_user['date_added'] = $blocked_wp_user->user_registered; ?>
2709 <?php $blocked_user['is_wp_user'] = true; ?>
2710 <?php else: ?>
2711 <?php $blocked_user['is_wp_user'] = false; ?>
2712 <?php endif; ?>
2713 <li>
2714 <input type="text" id="auth_settings_<?php echo $option; ?>_<?php echo $key; ?>" value="<?php echo $blocked_user['email']; ?>" readonly="true" class="auth-email" />
2715 <select id="auth_settings_<?php echo $option; ?>_<?php echo $key; ?>_role" class="auth-role">
2716 <?php $this->wp_dropdown_permitted_roles( $blocked_user['role'] ); ?>
2717 </select>
2718 <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" />
2719 <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>
2720 </li>
2721 <?php endforeach; ?>
2722 </ul>
2723 <div id="new_auth_settings_<?php echo $option; ?>">
2724 <input type="text" id="new_blocked_user_email" placeholder="email address" class="auth-email new" />
2725 <select id="new_blocked_user_role" class="auth-role">
2726 <option value="<?php echo $access_default_role; ?>"><?php echo ucfirst( $access_default_role ); ?></option>
2727 </select>
2728 <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>
2729 </div>
2730 <?php
2731 } // END print_combo_auth_access_users_blocked()
2732
2733
2734 function print_section_info_access_login( $args = '' ) {
2735 ?><div id="section_info_access_login" class="section_info">
2736 <?php wp_nonce_field( 'save_auth_settings', 'nonce_save_auth_settings' ); ?>
2737 <p>Choose who is able to log into this site below.</p>
2738 </div><?php
2739 } // END print_section_info_access_login()
2740
2741 function print_radio_auth_access_who_can_login( $args = '' ) {
2742 // Get plugin option.
2743 $option = 'access_who_can_login';
2744 $admin_mode = ( is_array( $args ) && array_key_exists( 'multisite_admin', $args ) && $args['multisite_admin'] === true ) ? 'multisite admin' : 'single admin';
2745 $auth_settings_option = $this->get_plugin_option( $option, $admin_mode, 'allow override', 'print overlay' );
2746
2747 // 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.
2748 if ( is_multisite() && $this->get_plugin_option( 'advanced_override_multisite' ) == '1' ) {
2749 $auth_settings_option = $this->get_plugin_option( $option );
2750 } else if ( is_multisite() && $admin_mode === 'single admin' && $this->get_plugin_option( 'multisite_override', 'multisite admin' ) === '1' ) {
2751 // Workaround: javascript code hides/shows other settings based
2752 // on the selection in this option. If this option is overridden
2753 // by a multisite option, it should show that value in order to
2754 // correctly display the other appropriate options.
2755 // Side effect: this site option will be overwritten by the
2756 // multisite option on save. Since this is a 2-item radio, we
2757 // determined this was acceptable.
2758 $auth_settings_option = $this->get_plugin_option( $option, 'multisite admin' );
2759 }
2760
2761 // Print option elements.
2762 ?><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 />
2763 <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
2764 } // END print_radio_auth_access_who_can_login()
2765
2766 function print_select_auth_access_role_receive_pending_emails( $args = '' ) {
2767 // Get plugin option.
2768 $option = 'access_role_receive_pending_emails';
2769 $auth_settings_option = $this->get_plugin_option( $option );
2770
2771 // Print option elements.
2772 ?><select id="auth_settings_<?php echo $option; ?>" name="auth_settings[<?php echo $option; ?>]">
2773 <option value="---" <?php selected( $auth_settings_option, '---' ); ?>>None (Don't send notification emails)</option>
2774 <?php wp_dropdown_roles( $auth_settings_option ); ?>
2775 </select><?php
2776 } // END print_select_auth_access_role_receive_pending_emails()
2777
2778 function print_wysiwyg_auth_access_pending_redirect_to_message( $args = '' ) {
2779 // Get plugin option.
2780 $option = 'access_pending_redirect_to_message';
2781 $auth_settings_option = $this->get_plugin_option( $option );
2782
2783 // Print option elements.
2784 wp_editor(
2785 wpautop( $auth_settings_option ),
2786 "auth_settings_$option",
2787 array(
2788 'media_buttons' => false,
2789 'textarea_name' => "auth_settings[$option]",
2790 'textarea_rows' => 5,
2791 'tinymce' => true,
2792 'teeny' => true,
2793 'quicktags' => false,
2794 )
2795 );
2796 } // END print_wysiwyg_auth_access_pending_redirect_to_message()
2797
2798 function print_wysiwyg_auth_access_blocked_redirect_to_message( $args = '' ) {
2799 // Get plugin option.
2800 $option = 'access_blocked_redirect_to_message';
2801 $auth_settings_option = $this->get_plugin_option( $option );
2802
2803 // Print option elements.
2804 wp_editor(
2805 wpautop( $auth_settings_option ),
2806 "auth_settings_$option",
2807 array(
2808 'media_buttons' => false,
2809 'textarea_name' => "auth_settings[$option]",
2810 'textarea_rows' => 5,
2811 'tinymce' => true,
2812 'teeny' => true,
2813 'quicktags' => false,
2814 )
2815 );
2816 } // END print_wysiwyg_auth_access_blocked_redirect_to_message()
2817
2818 function print_checkbox_auth_access_should_email_approved_users( $args = '' ) {
2819 // Get plugin option.
2820 $option = 'access_should_email_approved_users';
2821 $auth_settings_option = $this->get_plugin_option( $option );
2822
2823 // Print option elements.
2824 ?><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
2825 } // END print_checkbox_auth_external_ldap()
2826
2827 function print_text_auth_access_email_approved_users_subject( $args = '' ) {
2828 // Get plugin option.
2829 $option = 'access_email_approved_users_subject';
2830 $auth_settings_option = $this->get_plugin_option( $option );
2831
2832 // Print option elements.
2833 ?><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
2834 } // END print_text_auth_access_email_approved_users_subject()
2835
2836 function print_wysiwyg_auth_access_email_approved_users_body( $args = '' ) {
2837 // Get plugin option.
2838 $option = 'access_email_approved_users_body';
2839 $auth_settings_option = $this->get_plugin_option( $option );
2840
2841 // Print option elements.
2842 wp_editor(
2843 wpautop( $auth_settings_option ),
2844 "auth_settings_$option",
2845 array(
2846 'media_buttons' => false,
2847 'textarea_name' => "auth_settings[$option]",
2848 'textarea_rows' => 9,
2849 'tinymce' => true,
2850 'teeny' => true,
2851 'quicktags' => false,
2852 )
2853 );
2854
2855 ?><small>You can use <b>[site_name]</b>, <b>[site_url]</b>, and <b>[user_email]</b> shortcodes.</small><?php
2856
2857 } // END print_wysiwyg_auth_access_email_approved_users_body()
2858
2859
2860 function print_section_info_access_public( $args = '' ) {
2861 ?><div id="section_info_access_public" class="section_info">
2862 <p>Choose your public access options here.</p>
2863 </div><?php
2864 } // END print_section_info_access_public()
2865
2866 function print_radio_auth_access_who_can_view( $args = '' ) {
2867 // Get plugin option.
2868 $option = 'access_who_can_view';
2869 $admin_mode = ( is_array( $args ) && array_key_exists( 'multisite_admin', $args ) && $args['multisite_admin'] === true ) ? 'multisite admin' : 'single admin';
2870 $auth_settings_option = $this->get_plugin_option( $option, $admin_mode, 'allow override', 'print overlay' );
2871
2872 // 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.
2873 if ( is_multisite() && $this->get_plugin_option( 'advanced_override_multisite' ) == '1' ) {
2874 $auth_settings_option = $this->get_plugin_option( $option );
2875 } else if ( is_multisite() && $admin_mode === 'single admin' && $this->get_plugin_option( 'multisite_override', 'multisite admin' ) === '1' ) {
2876 // Workaround: javascript code hides/shows other settings based
2877 // on the selection in this option. If this option is overridden
2878 // by a multisite option, it should show that value in order to
2879 // correctly display the other appropriate options.
2880 // Side effect: this site option will be overwritten by the
2881 // multisite option on save. Since this is a 2-item radio, we
2882 // determined this was acceptable.
2883 $auth_settings_option = $this->get_plugin_option( $option, 'multisite admin' );
2884 }
2885
2886 // Print option elements.
2887 ?><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 />
2888 <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
2889 } // END print_radio_auth_access_who_can_view()
2890
2891 function print_radio_auth_access_redirect( $args = '' ) {
2892 // Get plugin option.
2893 $option = 'access_redirect';
2894 $auth_settings_option = $this->get_plugin_option( $option );
2895
2896 // Print option elements.
2897 ?><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 />
2898 <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
2899 } // END print_radio_auth_access_redirect()
2900
2901 function print_radio_auth_access_public_warning( $args = '' ) {
2902 // Get plugin option.
2903 $option = 'access_public_warning';
2904 $auth_settings_option = $this->get_plugin_option( $option );
2905
2906 // Print option elements.
2907 ?><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 />
2908 <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
2909 } // END print_radio_auth_access_public_warning()
2910
2911 function print_wysiwyg_auth_access_redirect_to_message( $args = '' ) {
2912 // Get plugin option.
2913 $option = 'access_redirect_to_message';
2914 $auth_settings_option = $this->get_plugin_option( $option );
2915
2916 // Print option elements.
2917 wp_editor(
2918 wpautop( $auth_settings_option ),
2919 "auth_settings_$option",
2920 array(
2921 'media_buttons' => false,
2922 'textarea_name' => "auth_settings[$option]",
2923 'textarea_rows' => 5,
2924 'tinymce' => true,
2925 'teeny' => true,
2926 'quicktags' => false,
2927 )
2928 );
2929 } // END print_wysiwyg_auth_access_redirect_to_message()
2930
2931 function print_multiselect_auth_access_public_pages( $args = '' ) {
2932 // Get plugin option.
2933 $option = 'access_public_pages';
2934 $auth_settings_option = $this->get_plugin_option( $option );
2935 $auth_settings_option = is_array( $auth_settings_option ) ? $auth_settings_option : array();
2936
2937 $post_types = array_merge( array( 'page', 'post' ), get_post_types( array( '_builtin' => false ), 'names' ) );
2938 $post_types = is_array( $post_types ) ? $post_types : array();
2939
2940 // Print option elements.
2941 ?><select id="auth_settings_<?php echo $option; ?>" multiple="multiple" name="auth_settings[<?php echo $option; ?>][]">
2942 <optgroup label="Home">
2943 <option value="home" <?php echo in_array( 'home', $auth_settings_option ) ? 'selected="selected"' : ''; ?>>Home Page</option>
2944 </optgroup>
2945 <?php foreach ( $post_types as $post_type ): ?>
2946 <optgroup label="<?php echo ucfirst( $post_type ); ?>">
2947 <?php $pages = get_posts( array( 'post_type' => $post_type, 'posts_per_page' => -1 ) ); ?>
2948 <?php $pages = is_array( $pages ) ? $pages : array(); ?>
2949 <?php foreach ( $pages as $page ): ?>
2950 <option value="<?php echo $page->ID; ?>" <?php echo in_array( $page->ID, $auth_settings_option ) ? 'selected="selected"' : ''; ?>><?php echo $page->post_title; ?></option>
2951 <?php endforeach; ?>
2952 </optgroup>
2953 <?php endforeach; ?>
2954 <optgroup label="Categories">
2955 <?php foreach ( get_categories() as $category ) : ?>
2956 <option value="<?php echo 'cat_' . $category->slug; ?>" <?php echo in_array( 'cat_' . $category->slug, $auth_settings_option ) ? 'selected="selected"' : ''; ?>><?php echo $category->name; ?></option>
2957 <?php endforeach; ?>
2958 </optgroup>
2959 </select><?php
2960 } // END print_multiselect_auth_access_public_pages()
2961
2962
2963 function print_section_info_external( $args = '' ) {
2964 ?><div id="section_info_external" class="section_info">
2965 <p>Enter your external server settings below.</p>
2966 </div><?php
2967 } // END print_section_info_external()
2968
2969 function print_select_auth_access_default_role( $args = '' ) {
2970 // Get plugin option.
2971 $option = 'access_default_role';
2972 $admin_mode = ( is_array( $args ) && array_key_exists( 'multisite_admin', $args ) && $args['multisite_admin'] === true ) ? 'multisite admin' : 'single admin';
2973 $auth_settings_option = $this->get_plugin_option( $option, $admin_mode, 'allow override', 'print overlay' );
2974
2975 // Print option elements.
2976 ?><select id="auth_settings_<?php echo $option; ?>" name="auth_settings[<?php echo $option; ?>]">
2977 <?php wp_dropdown_roles( $auth_settings_option ); ?>
2978 </select><?php
2979 } // END print_select_auth_access_default_role()
2980
2981 function print_checkbox_auth_external_google( $args = '' ) {
2982 // Get plugin option.
2983 $option = 'google';
2984 $admin_mode = ( is_array( $args ) && array_key_exists( 'multisite_admin', $args ) && $args['multisite_admin'] === true ) ? 'multisite admin' : 'single admin';
2985 $auth_settings_option = $this->get_plugin_option( $option, $admin_mode, 'allow override', 'print overlay' );
2986
2987 // Make sure php5-curl extension is installed on server.
2988 $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>' : '';
2989
2990 // Print option elements.
2991 ?><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
2992 } // END print_checkbox_auth_external_google()
2993
2994 function print_text_google_clientid( $args = '' ) {
2995 // Get plugin option.
2996 $option = 'google_clientid';
2997 $admin_mode = ( is_array( $args ) && array_key_exists( 'multisite_admin', $args ) && $args['multisite_admin'] === true ) ? 'multisite admin' : 'single admin';
2998 $auth_settings_option = $this->get_plugin_option( $option, $admin_mode, 'allow override', 'print overlay' );
2999
3000 // Print option elements.
3001 $site_url_parts = parse_url( get_site_url() );
3002 $site_url_host = $site_url_parts['scheme'] . '://' . $site_url_parts['host'] . '/';
3003 ?>If you don't have a Google Client ID and Secret, generate them by following these instructions:
3004 <ol>
3005 <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>
3006 <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:
3007 <ul>
3008 <li>Application Type: <strong>Web application</strong></li>
3009 <li>Authorized Javascript Origins: <strong><?php echo $site_url_host; ?></strong></li>
3010 <li>Authorized Redirect URI: <em>none</em></li>
3011 </ul>
3012 </li>
3013 <li>Copy/paste your new Client ID/Secret pair into the fields below.</li>
3014 <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>
3015 </ol>
3016 <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
3017 } // END print_text_google_clientid()
3018
3019 function print_text_google_clientsecret( $args = '' ) {
3020 // Get plugin option.
3021 $option = 'google_clientsecret';
3022 $admin_mode = ( is_array( $args ) && array_key_exists( 'multisite_admin', $args ) && $args['multisite_admin'] === true ) ? 'multisite admin' : 'single admin';
3023 $auth_settings_option = $this->get_plugin_option( $option, $admin_mode, 'allow override', 'print overlay' );
3024
3025 // Print option elements.
3026 ?><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
3027 } // END print_text_google_clientsecret()
3028
3029 function print_checkbox_auth_external_cas( $args = '' ) {
3030 // Get plugin option.
3031 $option = 'cas';
3032 $admin_mode = ( is_array( $args ) && array_key_exists( 'multisite_admin', $args ) && $args['multisite_admin'] === true ) ? 'multisite admin' : 'single admin';
3033 $auth_settings_option = $this->get_plugin_option( $option, $admin_mode, 'allow override', 'print overlay' );
3034
3035 // Make sure php5-curl extension is installed on server.
3036 $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>' : '';
3037
3038 // Print option elements.
3039 ?><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
3040 } // END print_checkbox_auth_external_cas()
3041
3042 function print_text_cas_custom_label( $args = '' ) {
3043 // Get plugin option.
3044 $option = 'cas_custom_label';
3045 $admin_mode = ( is_array( $args ) && array_key_exists( 'multisite_admin', $args ) && $args['multisite_admin'] === true ) ? 'multisite admin' : 'single admin';
3046 $auth_settings_option = $this->get_plugin_option( $option, $admin_mode, 'allow override', 'print overlay' );
3047
3048 // Print option elements.
3049 ?>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
3050 } // END print_text_cas_custom_label()
3051
3052 function print_text_cas_host( $args = '' ) {
3053 // Get plugin option.
3054 $option = 'cas_host';
3055 $admin_mode = ( is_array( $args ) && array_key_exists( 'multisite_admin', $args ) && $args['multisite_admin'] === true ) ? 'multisite admin' : 'single admin';
3056 $auth_settings_option = $this->get_plugin_option( $option, $admin_mode, 'allow override', 'print overlay' );
3057
3058 // Print option elements.
3059 ?><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
3060 } // END print_text_cas_host()
3061
3062 function print_text_cas_port( $args = '' ) {
3063 // Get plugin option.
3064 $option = 'cas_port';
3065 $admin_mode = ( is_array( $args ) && array_key_exists( 'multisite_admin', $args ) && $args['multisite_admin'] === true ) ? 'multisite admin' : 'single admin';
3066 $auth_settings_option = $this->get_plugin_option( $option, $admin_mode, 'allow override', 'print overlay' );
3067
3068 // Print option elements.
3069 ?><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
3070 } // END print_text_cas_port()
3071
3072 function print_text_cas_path( $args = '' ) {
3073 // Get plugin option.
3074 $option = 'cas_path';
3075 $admin_mode = ( is_array( $args ) && array_key_exists( 'multisite_admin', $args ) && $args['multisite_admin'] === true ) ? 'multisite admin' : 'single admin';
3076 $auth_settings_option = $this->get_plugin_option( $option, $admin_mode, 'allow override', 'print overlay' );
3077
3078 // Print option elements.
3079 ?><input type="text" id="auth_settings_<?php echo $option; ?>" name="auth_settings[<?php echo $option; ?>]" value="<?php echo $auth_settings_option; ?>" placeholder="/cas" /><?php
3080 } // END print_text_cas_path()
3081
3082 function print_text_cas_attr_email( $args = '' ) {
3083 // Get plugin option.
3084 $option = 'cas_attr_email';
3085 $admin_mode = ( is_array( $args ) && array_key_exists( 'multisite_admin', $args ) && $args['multisite_admin'] === true ) ? 'multisite admin' : 'single admin';
3086 $auth_settings_option = $this->get_plugin_option( $option, $admin_mode, 'allow override', 'print overlay' );
3087
3088 // Print option elements.
3089 ?><input type="text" id="auth_settings_<?php echo $option; ?>" name="auth_settings[<?php echo $option; ?>]" value="<?php echo $auth_settings_option; ?>" placeholder="mail" /><?php
3090 } // END print_text_cas_attr_email()
3091
3092 function print_text_cas_attr_first_name( $args = '' ) {
3093 // Get plugin option.
3094 $option = 'cas_attr_first_name';
3095 $admin_mode = ( is_array( $args ) && array_key_exists( 'multisite_admin', $args ) && $args['multisite_admin'] === true ) ? 'multisite admin' : 'single admin';
3096 $auth_settings_option = $this->get_plugin_option( $option, $admin_mode, 'allow override', 'print overlay' );
3097
3098 // Print option elements.
3099 ?><input type="text" id="auth_settings_<?php echo $option; ?>" name="auth_settings[<?php echo $option; ?>]" value="<?php echo $auth_settings_option; ?>" placeholder="givenName" /><?php
3100 } // END print_text_cas_attr_first_name()
3101
3102 function print_text_cas_attr_last_name( $args = '' ) {
3103 // Get plugin option.
3104 $option = 'cas_attr_last_name';
3105 $admin_mode = ( is_array( $args ) && array_key_exists( 'multisite_admin', $args ) && $args['multisite_admin'] === true ) ? 'multisite admin' : 'single admin';
3106 $auth_settings_option = $this->get_plugin_option( $option, $admin_mode, 'allow override', 'print overlay' );
3107
3108 // Print option elements.
3109 ?><input type="text" id="auth_settings_<?php echo $option; ?>" name="auth_settings[<?php echo $option; ?>]" value="<?php echo $auth_settings_option; ?>" placeholder="sn" /><?php
3110 } // END print_text_cas_attr_last_name()
3111
3112 function print_checkbox_cas_attr_update_on_login( $args = '' ) {
3113 // Get plugin option.
3114 $option = 'cas_attr_update_on_login';
3115 $admin_mode = ( is_array( $args ) && array_key_exists( 'multisite_admin', $args ) && $args['multisite_admin'] === true ) ? 'multisite admin' : 'single admin';
3116 $auth_settings_option = $this->get_plugin_option( $option, $admin_mode, 'allow override', 'print overlay' );
3117
3118 // Print option elements.
3119 ?><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
3120 } // END print_checkbox_cas_attr_update_on_login()
3121
3122
3123 function print_checkbox_auth_external_ldap( $args = '' ) {
3124 // Get plugin option.
3125 $option = 'ldap';
3126 $admin_mode = ( is_array( $args ) && array_key_exists( 'multisite_admin', $args ) && $args['multisite_admin'] === true ) ? 'multisite admin' : 'single admin';
3127 $auth_settings_option = $this->get_plugin_option( $option, $admin_mode, 'allow override', 'print overlay' );
3128
3129 // Make sure php5-ldap extension is installed on server.
3130 $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>' : '';
3131
3132 // Print option elements.
3133 ?><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
3134 } // END print_checkbox_auth_external_ldap()
3135
3136 function print_text_ldap_host( $args = '' ) {
3137 // Get plugin option.
3138 $option = 'ldap_host';
3139 $admin_mode = ( is_array( $args ) && array_key_exists( 'multisite_admin', $args ) && $args['multisite_admin'] === true ) ? 'multisite admin' : 'single admin';
3140 $auth_settings_option = $this->get_plugin_option( $option, $admin_mode, 'allow override', 'print overlay' );
3141
3142 // Print option elements.
3143 ?><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
3144 } // END print_text_ldap_host()
3145
3146 function print_text_ldap_port( $args = '' ) {
3147 // Get plugin option.
3148 $option = 'ldap_port';
3149 $admin_mode = ( is_array( $args ) && array_key_exists( 'multisite_admin', $args ) && $args['multisite_admin'] === true ) ? 'multisite admin' : 'single admin';
3150 $auth_settings_option = $this->get_plugin_option( $option, $admin_mode, 'allow override', 'print overlay' );
3151
3152 // Print option elements.
3153 ?><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
3154 } // END print_text_ldap_port()
3155
3156 function print_text_ldap_search_base( $args = '' ) {
3157 // Get plugin option.
3158 $option = 'ldap_search_base';
3159 $admin_mode = ( is_array( $args ) && array_key_exists( 'multisite_admin', $args ) && $args['multisite_admin'] === true ) ? 'multisite admin' : 'single admin';
3160 $auth_settings_option = $this->get_plugin_option( $option, $admin_mode, 'allow override', 'print overlay' );
3161
3162 // Print option elements.
3163 ?><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
3164 } // END print_text_ldap_search_base()
3165
3166 function print_text_ldap_uid( $args = '' ) {
3167 // Get plugin option.
3168 $option = 'ldap_uid';
3169 $admin_mode = ( is_array( $args ) && array_key_exists( 'multisite_admin', $args ) && $args['multisite_admin'] === true ) ? 'multisite admin' : 'single admin';
3170 $auth_settings_option = $this->get_plugin_option( $option, $admin_mode, 'allow override', 'print overlay' );
3171
3172 // Print option elements.
3173 ?><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
3174 } // END print_text_ldap_uid()
3175
3176 function print_text_ldap_attr_email( $args = '' ) {
3177 // Get plugin option.
3178 $option = 'ldap_attr_email';
3179 $admin_mode = ( is_array( $args ) && array_key_exists( 'multisite_admin', $args ) && $args['multisite_admin'] === true ) ? 'multisite admin' : 'single admin';
3180 $auth_settings_option = $this->get_plugin_option( $option, $admin_mode, 'allow override', 'print overlay' );
3181
3182 // Print option elements.
3183 ?><input type="text" id="auth_settings_<?php echo $option; ?>" name="auth_settings[<?php echo $option; ?>]" value="<?php echo $auth_settings_option; ?>" placeholder="mail" /><?php
3184 } // END print_text_ldap_attr_email()
3185
3186 function print_text_ldap_user( $args = '' ) {
3187 // Get plugin option.
3188 $option = 'ldap_user';
3189 $admin_mode = ( is_array( $args ) && array_key_exists( 'multisite_admin', $args ) && $args['multisite_admin'] === true ) ? 'multisite admin' : 'single admin';
3190 $auth_settings_option = $this->get_plugin_option( $option, $admin_mode, 'allow override', 'print overlay' );
3191
3192 // Print option elements.
3193 ?><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
3194 } // END print_text_ldap_user()
3195
3196 function print_password_ldap_password( $args = '' ) {
3197 // Get plugin option.
3198 $option = 'ldap_password';
3199 $admin_mode = ( is_array( $args ) && array_key_exists( 'multisite_admin', $args ) && $args['multisite_admin'] === true ) ? 'multisite admin' : 'single admin';
3200 $auth_settings_option = $this->get_plugin_option( $option, $admin_mode, 'allow override', 'print overlay' );
3201
3202 // Print option elements.
3203 ?><input type="password" id="garbage_to_stop_autofill" name="garbage" value="" autocomplete="off" style="display:none;" />
3204 <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
3205 } // END print_password_ldap_password()
3206
3207 function print_checkbox_ldap_tls( $args = '' ) {
3208 // Get plugin option.
3209 $option = 'ldap_tls';
3210 $admin_mode = ( is_array( $args ) && array_key_exists( 'multisite_admin', $args ) && $args['multisite_admin'] === true ) ? 'multisite admin' : 'single admin';
3211 $auth_settings_option = $this->get_plugin_option( $option, $admin_mode, 'allow override', 'print overlay' );
3212
3213 // Print option elements.
3214 ?><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
3215 } // END print_checkbox_ldap_tls
3216
3217 function print_text_ldap_lostpassword_url( $args = '' ) {
3218 // Get plugin option.
3219 $option = 'ldap_lostpassword_url';
3220 $admin_mode = ( is_array( $args ) && array_key_exists( 'multisite_admin', $args ) && $args['multisite_admin'] === true ) ? 'multisite admin' : 'single admin';
3221 $auth_settings_option = $this->get_plugin_option( $option, $admin_mode, 'allow override', 'print overlay' );
3222
3223 // Print option elements.
3224 ?><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
3225 } // END print_text_ldap_lostpassword_url()
3226
3227 function print_text_ldap_attr_first_name( $args = '' ) {
3228 // Get plugin option.
3229 $option = 'ldap_attr_first_name';
3230 $admin_mode = ( is_array( $args ) && array_key_exists( 'multisite_admin', $args ) && $args['multisite_admin'] === true ) ? 'multisite admin' : 'single admin';
3231 $auth_settings_option = $this->get_plugin_option( $option, $admin_mode, 'allow override', 'print overlay' );
3232
3233 // Print option elements.
3234 ?><input type="text" id="auth_settings_<?php echo $option; ?>" name="auth_settings[<?php echo $option; ?>]" value="<?php echo $auth_settings_option; ?>" placeholder="givenname" /><?php
3235 } // END print_text_ldap_attr_first_name()
3236
3237 function print_text_ldap_attr_last_name( $args = '' ) {
3238 // Get plugin option.
3239 $option = 'ldap_attr_last_name';
3240 $admin_mode = ( is_array( $args ) && array_key_exists( 'multisite_admin', $args ) && $args['multisite_admin'] === true ) ? 'multisite admin' : 'single admin';
3241 $auth_settings_option = $this->get_plugin_option( $option, $admin_mode, 'allow override', 'print overlay' );
3242
3243 // Print option elements.
3244 ?><input type="text" id="auth_settings_<?php echo $option; ?>" name="auth_settings[<?php echo $option; ?>]" value="<?php echo $auth_settings_option; ?>" placeholder="sn" /><?php
3245 } // END print_text_ldap_attr_last_name()
3246
3247 function print_checkbox_ldap_attr_update_on_login( $args = '' ) {
3248 // Get plugin option.
3249 $option = 'ldap_attr_update_on_login';
3250 $admin_mode = ( is_array( $args ) && array_key_exists( 'multisite_admin', $args ) && $args['multisite_admin'] === true ) ? 'multisite admin' : 'single admin';
3251 $auth_settings_option = $this->get_plugin_option( $option, $admin_mode, 'allow override', 'print overlay' );
3252
3253 // Print option elements.
3254 ?><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
3255 } // END print_checkbox_ldap_attr_update_on_login()
3256
3257
3258 function print_section_info_advanced( $args = '' ) {
3259 ?><div id="section_info_advanced" class="section_info">
3260 <p>You may optionally specify some advanced settings below.</p>
3261 </div><?php
3262 } // END print_section_info_advanced()
3263
3264 function print_text_auth_advanced_lockouts( $args = '' ) {
3265 // Get plugin option.
3266 $option = 'advanced_lockouts';
3267 $admin_mode = ( is_array( $args ) && array_key_exists( 'multisite_admin', $args ) && $args['multisite_admin'] === true ) ? 'multisite admin' : 'single admin';
3268 $auth_settings_option = $this->get_plugin_option( $option, $admin_mode, 'allow override', 'print overlay' );
3269
3270 // Print option elements.
3271 ?>After
3272 <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;" />
3273 invalid password attempts, delay further attempts on that user for
3274 <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;" />
3275 minute(s).
3276 <br />
3277 After
3278 <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;" />
3279 more invalid attempts, increase the delay to
3280 <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;" />
3281 minutes.
3282 <br />
3283 Reset the delays after
3284 <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;" />
3285 minutes with no invalid attempts.<?php
3286 } // END print_text_auth_advanced_lockouts()
3287
3288 function print_checkbox_auth_advanced_hide_wp_login( $args = '' ) {
3289 // Get plugin option.
3290 $option = 'advanced_hide_wp_login';
3291 $admin_mode = ( is_array( $args ) && array_key_exists( 'multisite_admin', $args ) && $args['multisite_admin'] === true ) ? 'multisite admin' : 'single admin';
3292 $auth_settings_option = $this->get_plugin_option( $option, $admin_mode, 'allow override', 'print overlay' );
3293
3294 // Print option elements.
3295 ?><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>
3296 <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
3297 } // END print_checkbox_auth_advanced_hide_wp_login()
3298
3299 function print_radio_auth_advanced_branding( $args = '' ) {
3300 // Get plugin option.
3301 $option = 'advanced_branding';
3302 $auth_settings_option = $this->get_plugin_option( $option );
3303
3304 // Print option elements.
3305 ?><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 />
3306 <?php
3307
3308 /**
3309 * Developers can use the `authorizer_add_branding_option` filter
3310 * to add a radio button for "Custom WordPress login branding"
3311 * under the "Advanced" tab in Authorizer options. Example:
3312 *
3313 * function my_authorizer_add_branding_option( $branding_options ) {
3314 * $new_branding_option = array(
3315 * 'value' => 'your_brand'
3316 * 'description' => 'Custom Your Brand Login Screen',
3317 * 'css_url' => 'http://url/to/your_brand.css',
3318 * 'js_url' => 'http://url/to/your_brand.js',
3319 * );
3320 * array_push( $branding_options, $new_branding_option );
3321 * return $branding_options;
3322 * }
3323 * add_filter( 'authorizer_add_branding_option', 'my_authorizer_add_branding_option' );
3324 */
3325 $branding_options = array();
3326 $branding_options = apply_filters( 'authorizer_add_branding_option', $branding_options );
3327 foreach ( $branding_options as $branding_option ) {
3328 // Make sure the custom brands have the required values
3329 if ( ! ( is_array( $branding_option ) && array_key_exists( 'value', $branding_option ) && array_key_exists( 'description', $branding_option ) ) ) {
3330 continue;
3331 }
3332 ?><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
3333 }
3334
3335 // Print message about adding custom brands if there are none.
3336 if ( count( $branding_options ) === 0 ) {
3337 ?><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
3338 }
3339 } // END print_radio_auth_advanced_branding()
3340
3341 function print_radio_auth_advanced_admin_menu( $args = '' ) {
3342 // Get plugin option.
3343 $option = 'advanced_admin_menu';
3344 $auth_settings_option = $this->get_plugin_option( $option );
3345
3346 // Print option elements.
3347 ?><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 />
3348 <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
3349
3350 } // END print_radio_auth_advanced_admin_menu()
3351
3352 function print_select_auth_advanced_usermeta( $args = '' ) {
3353 // Get plugin option.
3354 $option = 'advanced_usermeta';
3355 $auth_settings_option = $this->get_plugin_option( $option );
3356
3357 // Print option elements.
3358 ?><select id="auth_settings_<?php echo $option; ?>" name="auth_settings[<?php echo $option; ?>]">
3359 <option value="">-- None --</option>
3360 <?php if ( class_exists( 'acf' ) ) :
3361 // Get ACF 5 fields. Note: it would be much easier to use `get_field_objects()`
3362 // or `get_field_objects( 'user_' . get_current_user_id() )`, but neither will
3363 // list fields that have never been given values for users (i.e., new ACF
3364 // fields). Therefore we fall back on finding any ACF fields applied to users
3365 // (user_role or user_form location rules in the field group definition).
3366 $fields = array();
3367 $acf_field_group_ids = array();
3368 $acf_field_groups = new WP_Query( array(
3369 'post_type' => 'acf-field-group',
3370 ));
3371 while ( $acf_field_groups->have_posts() ) : $acf_field_groups->the_post();
3372 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 ) :
3373 array_push( $acf_field_group_ids, get_the_ID() );
3374 endif;
3375 endwhile; wp_reset_postdata();
3376 foreach ( $acf_field_group_ids as $acf_field_group_id ) :
3377 $acf_fields = new WP_Query( array(
3378 'post_type' => 'acf-field',
3379 'post_parent' => $acf_field_group_id,
3380 ));
3381 while ( $acf_fields->have_posts() ) : $acf_fields->the_post();
3382 global $post;
3383 $fields[$post->post_name] = get_field_object( $post->post_name );
3384 endwhile; wp_reset_postdata();
3385 endforeach;
3386 // Get ACF 4 fields.
3387 $acf4_field_groups = new WP_Query( array(
3388 'post_type' => 'acf',
3389 ));
3390 while ( $acf4_field_groups->have_posts() ) : $acf4_field_groups->the_post();
3391 $field_group_rules = get_post_meta( get_the_ID(), 'rule', true );
3392 if ( is_array( $field_group_rules ) && array_key_exists( 'param', $field_group_rules ) && $field_group_rules['param'] === 'ef_user' ) :
3393 $acf4_fields = get_post_custom( get_the_ID() );
3394 foreach ( $acf4_fields as $meta_key => $meta_value ) :
3395 if ( strpos( $meta_key, 'field_' ) === 0 ) :
3396 $meta_value = unserialize( $meta_value[0] );
3397 $fields[$meta_key] = $meta_value;
3398 endif;
3399 endforeach;
3400 endif;
3401 endwhile; wp_reset_postdata(); ?>
3402 <optgroup label="ACF User Fields:">
3403 <?php foreach ( (array)$fields as $field => $field_object ) : ?>
3404 <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>
3405 <?php endforeach; ?>
3406 </optgroup>
3407 <?php endif; ?>
3408 <optgroup label="All Usermeta:">
3409 <?php foreach ( $this->get_all_usermeta_keys() as $meta_key ) : if ( substr( $meta_key, 0, 3 ) === 'wp_' ) continue; ?>
3410 <option value="<?php echo $meta_key; ?>"<?php if ( $auth_settings_option === $meta_key ) echo ' selected="selected"'; ?>><?php echo $meta_key; ?></option>
3411 <?php endforeach; ?>
3412 </optgroup>
3413 </select><?php
3414 } // END print_select_auth_advanced_usermeta()
3415
3416 function print_checkbox_auth_advanced_override_multisite( $args = '' ) {
3417 // Get plugin option.
3418 $option = 'advanced_override_multisite';
3419 $auth_settings_option = $this->get_plugin_option( $option );
3420
3421 // Print option elements.
3422 ?><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
3423 } // END print_checkbox_auth_advanced_override_multisite()
3424
3425
3426
3427 /**
3428 * Add help documentation to the options page.
3429 * Run on action hook chain: load-settings_page_authorizer > admin_head
3430 */
3431 public function admin_head() {
3432 $screen = get_current_screen();
3433
3434 // Add help tab for Access Lists Settings
3435 $help_auth_settings_access_lists_content = '
3436 <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>
3437 <p><strong>Approved Users</strong>: Approved users have access to the site once they successfully log in.</p>
3438 <p><strong>Blocked Users</strong>: Blocked users will receive an error message when they try to visit the site after authenticating.</p>
3439 <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>
3440 ';
3441 $screen->add_help_tab(
3442 array(
3443 'id' => 'help_auth_settings_access_lists_content',
3444 'title' => 'Access Lists',
3445 'content' => $help_auth_settings_access_lists_content,
3446 )
3447 );
3448
3449 // Add help tab for Login Access Settings
3450 $help_auth_settings_access_login_content = '
3451 <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>
3452 <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>
3453 <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>
3454 ';
3455 $screen->add_help_tab(
3456 array(
3457 'id' => 'help_auth_settings_access_login_content',
3458 'title' => 'Login Access',
3459 'content' => $help_auth_settings_access_login_content,
3460 )
3461 );
3462
3463 // Add help tab for Public Access Settings
3464 $help_auth_settings_access_public_content = '
3465 <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>
3466 <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>
3467 <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>
3468 <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>
3469 <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>
3470 ';
3471 $screen->add_help_tab(
3472 array(
3473 'id' => 'help_auth_settings_access_public_content',
3474 'title' => 'Public Access',
3475 'content' => $help_auth_settings_access_public_content,
3476 )
3477 );
3478
3479 // Add help tab for External Service (CAS, LDAP) Settings
3480 $help_auth_settings_external_content = '
3481 <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>
3482 <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>
3483 <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>
3484 <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>
3485 <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>
3486 <p><strong><em>If you enable Google logins:</em></strong></p>
3487 <ul>
3488 <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>
3489 <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>
3490 </ul>
3491 <p><strong><em>If you enable CAS logins:</em></strong></p>
3492 <ul>
3493 <li><strong>CAS server hostname</strong>: Enter the hostname of the CAS server you authenticate against (e.g., authn.example.edu).</li>
3494 <li><strong>CAS server port</strong>: Enter the port on the CAS server to connect to (e.g., 443).</li>
3495 <li><strong>CAS server path/context</strong>: Enter the path to the login endpoint on the CAS server (e.g., /cas).</li>
3496 <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>
3497 <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>
3498 <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>
3499 </ul>
3500 <p><strong><em>If you enable LDAP logins:</em></strong></p>
3501 <ul>
3502 <li><strong>LDAP Host</strong>: Enter the URL of the LDAP server you authenticate against.</li>
3503 <li><strong>LDAP Port</strong>: Enter the port number that the LDAP server listens on.</li>
3504 <li><strong>LDAP Search Base</strong>: Enter the LDAP string that represents the search base, e.g., ou=people,dc=example,dc=edu</li>
3505 <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>
3506 <li><strong>LDAP Directory User</strong>: Enter the name of the LDAP user that has permissions to browse the directory.</li>
3507 <li><strong>LDAP Directory User Password</strong>: Enter the password for the LDAP user that has permission to browse the directory.</li>
3508 <li><strong>Secure Connection (TLS)</strong>: Select whether all communication with the LDAP server should be performed over a TLS-secured connection.</li>
3509 <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>
3510 <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>
3511 <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>
3512 <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>
3513 </ul>';
3514 $screen->add_help_tab(
3515 array(
3516 'id' => 'help_auth_settings_external_content',
3517 'title' => 'External Service',
3518 'content' => $help_auth_settings_external_content,
3519 )
3520 );
3521
3522 // Add help tab for Advanced Settings
3523 $help_auth_settings_advanced_content = '
3524 <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>
3525 <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>
3526 <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>
3527 ';
3528 $screen->add_help_tab(
3529 array(
3530 'id' => 'help_auth_settings_advanced_content',
3531 'title' => 'Advanced',
3532 'content' => $help_auth_settings_advanced_content,
3533 )
3534 );
3535 } // END admin_head()
3536
3537
3538
3539 /**
3540 * ***************************
3541 * Multisite: Network Admin Options page
3542 * ***************************
3543 */
3544
3545
3546 /**
3547 * Network Admin menu item
3548 * Hook: network_admin_menu
3549 *
3550 * @param none
3551 * @return void
3552 */
3553 public function network_admin_menu() {
3554 // @see http://codex.wordpress.org/Function_Reference/add_menu_page
3555 add_menu_page(
3556 'Authorizer', // Page title
3557 'Authorizer', // Menu title
3558 'manage_network_options', // Capability
3559 'authorizer', // Menu slug
3560 array( $this, 'create_network_admin_page' ),
3561 'dashicons-groups', // Icon URL
3562 89 // Position
3563 );
3564 } // END network_admin_menu()
3565
3566 /**
3567 * Output the HTML for the options page
3568 */
3569 public function create_network_admin_page() {
3570 if ( ! current_user_can( 'manage_network_options' ) ) {
3571 wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
3572 }
3573 $auth_settings = get_blog_option( BLOG_ID_CURRENT_SITE, 'auth_multisite_settings', array() ); ?>
3574 <div class="wrap">
3575 <form method="post" action="" autocomplete="off">
3576 <h2>Authorizer Settings</h2>
3577 <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>
3578
3579 <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>
3580
3581 <div id="auth_multisite_settings_disabled_overlay" style="display: none;"></div>
3582
3583 <div class="wrap" id="auth_multisite_settings">
3584 <?php $this->print_section_info_tabs( array( 'multisite_admin' => true ) ); ?>
3585
3586 <?php wp_nonce_field( 'save_auth_settings', 'nonce_save_auth_settings' ); ?>
3587
3588 <?php // Custom access lists (for network, we only really want approved list, not pending or blocked) ?>
3589 <div id="section_info_access_lists" class="section_info">
3590 <p>Manage who has access to all sites in the network.</p>
3591 </div>
3592 <table class="form-table"><tbody>
3593 <tr>
3594 <th scope="row">Who can log in to sites in this network?</th>
3595 <td><?php $this->print_radio_auth_access_who_can_login( array( 'multisite_admin' => true ) ); ?></td>
3596 </tr>
3597 <tr>
3598 <th scope="row">Who can view sites in this network?</th>
3599 <td><?php $this->print_radio_auth_access_who_can_view( array( 'multisite_admin' => true ) ); ?></td>
3600 </tr>
3601 <tr>
3602 <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>
3603 <td><?php $this->print_combo_auth_access_users_approved( array( 'multisite_admin' => true ) ); ?></td>
3604 </tr>
3605 </tbody></table>
3606
3607 <?php $this->print_section_info_external(); ?>
3608 <table class="form-table"><tbody>
3609 <tr>
3610 <th scope="row">Default role for new users</th>
3611 <td><?php $this->print_select_auth_access_default_role( array( 'multisite_admin' => true ) ); ?></td>
3612 </tr>
3613 <tr>
3614 <th scope="row">Google Logins</th>
3615 <td><?php $this->print_checkbox_auth_external_google( array( 'multisite_admin' => true ) ); ?></td>
3616 </tr>
3617 <tr>
3618 <th scope="row">Google Client ID</th>
3619 <td><?php $this->print_text_google_clientid( array( 'multisite_admin' => true ) ); ?></td>
3620 </tr>
3621 <tr>
3622 <th scope="row">Google Client Secret</th>
3623 <td><?php $this->print_text_google_clientsecret( array( 'multisite_admin' => true ) ); ?></td>
3624 </tr>
3625 <tr>
3626 <th scope="row">CAS Logins</th>
3627 <td><?php $this->print_checkbox_auth_external_cas( array( 'multisite_admin' => true ) ); ?></td>
3628 </tr>
3629 <tr>
3630 <th scope="row">CAS Custom Label</th>
3631 <td><?php $this->print_text_cas_custom_label( array( 'multisite_admin' => true ) ); ?></td>
3632 </tr>
3633 <tr>
3634 <th scope="row">CAS server hostname</th>
3635 <td><?php $this->print_text_cas_host( array( 'multisite_admin' => true ) ); ?></td>
3636 </tr>
3637 <tr>
3638 <th scope="row">CAS server port</th>
3639 <td><?php $this->print_text_cas_port( array( 'multisite_admin' => true ) ); ?></td>
3640 </tr>
3641 <tr>
3642 <th scope="row">CAS server path/context</th>
3643 <td><?php $this->print_text_cas_path( array( 'multisite_admin' => true ) ); ?></td>
3644 </tr>
3645 <tr>
3646 <th scope="row">CAS attribute containing email</th>
3647 <td><?php $this->print_text_cas_attr_email( array( 'multisite_admin' => true ) ); ?></td>
3648 </tr>
3649 <tr>
3650 <th scope="row">CAS attribute containing first name</th>
3651 <td><?php $this->print_text_cas_attr_first_name( array( 'multisite_admin' => true ) ); ?></td>
3652 </tr>
3653 <tr>
3654 <th scope="row">CAS attribute containing last name</th>
3655 <td><?php $this->print_text_cas_attr_last_name( array( 'multisite_admin' => true ) ); ?></td>
3656 </tr>
3657 <tr>
3658 <th scope="row">CAS attribute update</th>
3659 <td><?php $this->print_checkbox_cas_attr_update_on_login( array( 'multisite_admin' => true ) ); ?></td>
3660 </tr>
3661 <tr>
3662 <th scope="row">LDAP Logins</th>
3663 <td><?php $this->print_checkbox_auth_external_ldap( array( 'multisite_admin' => true ) ); ?></td>
3664 </tr>
3665 <tr>
3666 <th scope="row">LDAP Host</th>
3667 <td><?php $this->print_text_ldap_host( array( 'multisite_admin' => true ) ); ?></td>
3668 </tr>
3669 <tr>
3670 <th scope="row">LDAP Port</th>
3671 <td><?php $this->print_text_ldap_port( array( 'multisite_admin' => true ) ); ?></td>
3672 </tr>
3673 <tr>
3674 <th scope="row">LDAP Search Base</th>
3675 <td><?php $this->print_text_ldap_search_base( array( 'multisite_admin' => true ) ); ?></td>
3676 </tr>
3677 <tr>
3678 <th scope="row">LDAP attribute containing username</th>
3679 <td><?php $this->print_text_ldap_uid( array( 'multisite_admin' => true ) ); ?></td>
3680 </tr>
3681 <tr>
3682 <th scope="row">LDAP attribute containing email</th>
3683 <td><?php $this->print_text_ldap_attr_email( array( 'multisite_admin' => true ) ); ?></td>
3684 </tr>
3685 <tr>
3686 <th scope="row">LDAP Directory User</th>
3687 <td><?php $this->print_text_ldap_user( array( 'multisite_admin' => true ) ); ?></td>
3688 </tr>
3689 <tr>
3690 <th scope="row">LDAP Directory User Password</th>
3691 <td><?php $this->print_password_ldap_password( array( 'multisite_admin' => true ) ); ?></td>
3692 </tr>
3693 <tr>
3694 <th scope="row">Secure Connection (TLS)</th>
3695 <td><?php $this->print_checkbox_ldap_tls( array( 'multisite_admin' => true ) ); ?></td>
3696 </tr>
3697 <tr>
3698 <th scope="row">Custom lost password URL</th>
3699 <td><?php $this->print_text_ldap_lostpassword_url( array( 'multisite_admin' => true ) ); ?></td>
3700 </tr>
3701 <tr>
3702 <th scope="row">LDAP attribute containing first name</th>
3703 <td><?php $this->print_text_ldap_attr_first_name( array( 'multisite_admin' => true ) ); ?></td>
3704 </tr>
3705 <tr>
3706 <th scope="row">LDAP attribute containing last name</th>
3707 <td><?php $this->print_text_ldap_attr_last_name( array( 'multisite_admin' => true ) ); ?></td>
3708 </tr>
3709 <tr>
3710 <th scope="row">LDAP attribute update</th>
3711 <td><?php $this->print_checkbox_ldap_attr_update_on_login( array( 'multisite_admin' => true ) ); ?></td>
3712 </tr>
3713 </tbody></table>
3714
3715 <?php $this->print_section_info_advanced(); ?>
3716 <table class="form-table"><tbody>
3717 <tr>
3718 <th scope="row">Limit invalid login attempts</th>
3719 <td><?php $this->print_text_auth_advanced_lockouts( array( 'multisite_admin' => true ) ); ?></td>
3720 </tr>
3721 <tr>
3722 <th scope="row">Hide WordPress Logins</th>
3723 <td><?php $this->print_checkbox_auth_advanced_hide_wp_login( array( 'multisite_admin' => true ) ); ?></td>
3724 </tr>
3725 </tbody></table>
3726
3727 <br class="clear" />
3728 </div>
3729 <input type="button" name="submit" id="submit" class="button button-primary" value="Save Changes" onclick="save_auth_multisite_settings(this);" />
3730 </form>
3731 </div>
3732 <?php
3733 } // END create_network_admin_page()
3734
3735 /**
3736 * Save multisite settings (ajax call).
3737 */
3738 function ajax_save_auth_multisite_settings() {
3739 // Fail silently if current user doesn't have permissions.
3740 if ( ! current_user_can( 'manage_network_options' ) ) {
3741 die( '' );
3742 }
3743
3744 // Make sure nonce exists.
3745 if ( empty( $_POST['nonce_save_auth_settings'] ) ) {
3746 die( '' );
3747 }
3748
3749 // Nonce check.
3750 if ( ! wp_verify_nonce( $_POST['nonce_save_auth_settings'], 'save_auth_settings' ) ) {
3751 die( '' );
3752 }
3753
3754 // Assert multisite.
3755 if ( ! is_multisite() ) {
3756 die( '' );
3757 }
3758
3759 // Get multisite settings.
3760 $auth_multisite_settings = get_blog_option( BLOG_ID_CURRENT_SITE, 'auth_multisite_settings', array() );
3761
3762 // Sanitize settings
3763 $auth_multisite_settings = $this->sanitize_options( $_POST, 'multisite' );
3764
3765 // Filter options to only the allowed values (multisite options are a subset of all options)
3766 $allowed = array(
3767 'multisite_override',
3768 'access_who_can_login',
3769 'access_who_can_view',
3770 'access_default_role',
3771 'google',
3772 'google_clientid',
3773 'google_clientsecret',
3774 'cas',
3775 'cas_custom_label',
3776 'cas_host',
3777 'cas_port',
3778 'cas_path',
3779 'cas_attr_email',
3780 'cas_attr_first_name',
3781 'cas_attr_last_name',
3782 'cas_attr_update_on_login',
3783 'ldap',
3784 'ldap_host',
3785 'ldap_port',
3786 'ldap_search_base',
3787 'ldap_uid',
3788 'ldap_attr_email',
3789 'ldap_user',
3790 'ldap_password',
3791 'ldap_tls',
3792 'ldap_lostpassword_url',
3793 'ldap_attr_first_name',
3794 'ldap_attr_last_name',
3795 'ldap_attr_update_on_login',
3796 'advanced_lockouts',
3797 'advanced_hide_wp_login',
3798 );
3799 $auth_multisite_settings = array_intersect_key( $auth_multisite_settings, array_flip( $allowed ) );
3800
3801 // Update multisite settings in database.
3802 update_blog_option( BLOG_ID_CURRENT_SITE, 'auth_multisite_settings', $auth_multisite_settings );
3803
3804 // Return 'success' value to AJAX call.
3805 die( 'success' );
3806 } // END ajax_save_auth_multisite_settings()
3807
3808
3809
3810 /**
3811 * ***************************
3812 * Dashboard widget
3813 * ***************************
3814 */
3815
3816
3817
3818 function add_dashboard_widgets() {
3819 // Only users who can edit can see the authorizer dashboard widget
3820 if ( current_user_can( 'create_users' ) ) {
3821 // Add dashboard widget for adding/editing users with access
3822 wp_add_dashboard_widget( 'auth_dashboard_widget', 'Authorizer Settings', array( $this, 'add_auth_dashboard_widget' ) );
3823 }
3824 } // END add_dashboard_widgets()
3825
3826
3827 function add_auth_dashboard_widget() {
3828 ?><form method="post" id="auth_settings_access_form" action="">
3829 <?php $this->print_section_info_access_login(); ?>
3830 <div>
3831 <h2>Pending Users</h2>
3832 <?php $this->print_combo_auth_access_users_pending(); ?>
3833 </div>
3834 <div>
3835 <h2>Approved Users</h2>
3836 <?php $this->print_combo_auth_access_users_approved(); ?>
3837 </div>
3838 <div>
3839 <h2>Blocked Users</h2>
3840 <?php $this->print_combo_auth_access_users_blocked(); ?>
3841 </div>
3842 <br class="clear" />
3843 </form><?php
3844 } // END add_auth_dashboard_widget()
3845
3846
3847 // Fired on a change event from the optional usermeta field in the
3848 // approved user list. Updates the selected usermeta value, or saves it
3849 // in the user's approved list entry if the user hasn't logged in yet
3850 // and created a WordPress account.
3851 function ajax_update_auth_usermeta() {
3852
3853 // Fail silently if current user doesn't have permissions.
3854 if ( ! current_user_can( 'create_users' ) ) {
3855 die( '' );
3856 }
3857
3858 // Nonce check.
3859 if ( empty( $_POST['nonce_save_auth_settings'] ) || ! wp_verify_nonce( $_POST['nonce_save_auth_settings'], 'save_auth_settings' ) ) {
3860 die( '' );
3861 }
3862
3863 // Fail if required post data doesn't exist.
3864 if ( ! array_key_exists( 'email', $_REQUEST ) || ! array_key_exists( 'usermeta', $_REQUEST ) ) {
3865 die( '' );
3866 }
3867
3868 // Get values to update from post data.
3869 $email = $_REQUEST['email'];
3870 $meta_value = $_REQUEST['usermeta'];
3871 $meta_key = $this->get_plugin_option( 'advanced_usermeta' );
3872
3873 // If user doesn't exist, save usermeta selection to authorizer
3874 // list. This value will get saved to usermeta when the user first
3875 // logs in (i.e., when their WordPress account is created).
3876 if ( ! ( $wp_user = get_user_by( 'email', $email ) ) ) {
3877 // Look through multisite approved users and add a usermeta
3878 // reference for the current blog if the user is found.
3879 $auth_multisite_settings_access_users_approved = is_multisite() ? get_blog_option( BLOG_ID_CURRENT_SITE, 'auth_multisite_settings_access_users_approved', array() ) : array();
3880 $should_update_auth_multisite_settings_access_users_approved = false;
3881 foreach ( $auth_multisite_settings_access_users_approved as $index => $approved_user ) {
3882 if ( $email === $approved_user['email'] ) {
3883 if ( ! is_array( $auth_multisite_settings_access_users_approved[$index]['usermeta'] ) ) {
3884 // Initialize the array of usermeta for each blog this user belongs to.
3885 $auth_multisite_settings_access_users_approved[$index]['usermeta'] = array();
3886 } else {
3887 // There is already usermeta associated with this
3888 // preapproved user; iterate through it and make
3889 // sure it's not for old meta_keys (delete it if
3890 // so). This can happen if someone changes the
3891 // usermeta key in authorizer options, and we don't
3892 // want to hang on to old data.
3893 foreach ( $auth_multisite_settings_access_users_approved[$index]['usermeta'] as $blog_id => $usermeta ) {
3894 if ( array_key_exists( 'meta_key', $usermeta ) && $usermeta['meta_key'] === $meta_key ) {
3895 continue;
3896 } else {
3897 unset( $auth_multisite_settings_access_users_approved[$index]['usermeta'][$blog_id] );
3898 }
3899 }
3900 }
3901 $auth_multisite_settings_access_users_approved[$index]['usermeta'][get_current_blog_id()] = array(
3902 'meta_key' => $meta_key,
3903 'meta_value' => $meta_value,
3904 );
3905 $should_update_auth_multisite_settings_access_users_approved = true;
3906 }
3907 }
3908 if ( $should_update_auth_multisite_settings_access_users_approved ) {
3909 update_blog_option( BLOG_ID_CURRENT_SITE, 'auth_multisite_settings_access_users_approved', $auth_multisite_settings_access_users_approved );
3910 }
3911
3912 // Look through the approved users (of the current blog in a
3913 // multisite install, or just of the single site) and add a
3914 // usermeta reference if the user is found.
3915 $auth_settings_access_users_approved = $this->get_plugin_option( 'access_users_approved', 'single admin' );
3916 $should_update_auth_settings_access_users_approved = false;
3917 foreach ( $auth_settings_access_users_approved as $index => $approved_user ) {
3918 if ( $email === $approved_user['email'] ) {
3919 $auth_settings_access_users_approved[$index]['usermeta'] = array(
3920 'meta_key' => $meta_key,
3921 'meta_value' => $meta_value,
3922 );
3923 $should_update_auth_settings_access_users_approved = true;
3924 }
3925 }
3926 if ( $should_update_auth_settings_access_users_approved ) {
3927 update_option( 'auth_settings_access_users_approved', $auth_settings_access_users_approved );
3928 }
3929
3930 } else {
3931 // Update user's usermeta value for usermeta key stored in authorizer options.
3932 if ( strpos( $meta_key, 'acf___' ) === 0 && class_exists( 'acf' ) ) {
3933 // We have an ACF field value, so use the ACF function to update it.
3934 update_field( str_replace('acf___', '', $meta_key ), $meta_value, 'user_' . $wp_user->ID );
3935 } else {
3936 // We have a normal usermeta value, so just update it via the WordPress function.
3937 update_user_meta( $wp_user->ID, $meta_key, $meta_value );
3938 }
3939
3940 }
3941
3942 // Return 'success' value to AJAX call.
3943 die( 'success' );
3944 } // END ajax_update_auth_usermeta()
3945
3946
3947 function ajax_update_auth_user() {
3948
3949 // Fail silently if current user doesn't have permissions.
3950 if ( ! current_user_can( 'create_users' ) ) {
3951 die( '' );
3952 }
3953
3954 // Nonce check.
3955 if ( empty( $_POST['nonce_save_auth_settings'] ) || ! wp_verify_nonce( $_POST['nonce_save_auth_settings'], 'save_auth_settings' ) ) {
3956 die( '' );
3957 }
3958
3959 // Fail if requesting a change to an invalid setting.
3960 if ( ! in_array( $_POST['setting'], array( 'access_users_pending', 'access_users_approved', 'access_users_blocked' ) ) ) {
3961 die( '' );
3962 }
3963
3964 // Editing a pending list entry.
3965 if ( $_POST['setting'] === 'access_users_pending' ) {
3966 // Initialize posted data if empty.
3967 if ( ! ( array_key_exists( 'access_users_pending', $_POST ) && is_array( $_POST['access_users_pending'] ) ) ) {
3968 $_POST['access_users_pending'] = array();
3969 }
3970
3971 // Deal with each modified user (add or remove).
3972 foreach ( $_POST['access_users_pending'] as $pending_user ) {
3973
3974 if ( $pending_user['edit_action'] === 'add' ) {
3975
3976 // Add new user to pending list and save (skip if it's
3977 // already there--someone else might have just done it).
3978 if ( ! $this->is_email_in_list( $pending_user['email'], 'pending' ) ) {
3979 $auth_settings_access_users_pending = $this->sanitize_user_list(
3980 $this->get_plugin_option( 'access_users_pending', 'single admin' )
3981 );
3982 array_push( $auth_settings_access_users_pending, $pending_user );
3983 update_option( 'auth_settings_access_users_pending', $auth_settings_access_users_pending );
3984 }
3985
3986 } elseif ( $pending_user['edit_action'] === 'remove' ) {
3987
3988 // Remove user from pending list and save
3989 if ( $this->is_email_in_list( $pending_user['email'], 'pending' ) ) {
3990 $auth_settings_access_users_pending = $this->sanitize_user_list(
3991 $this->get_plugin_option( 'access_users_pending', 'single admin' )
3992 );
3993 foreach ( $auth_settings_access_users_pending as $key => $existing_user ) {
3994 if ( $pending_user['email'] == $existing_user['email'] ) {
3995 unset( $auth_settings_access_users_pending[$key] );
3996 break;
3997 }
3998 }
3999 update_option( 'auth_settings_access_users_pending', $auth_settings_access_users_pending );
4000 }
4001
4002 }
4003 }
4004 }
4005
4006 // Editing an approved list entry.
4007 if ( $_POST['setting'] === 'access_users_approved' ) {
4008 // Initialize posted data if empty.
4009 if ( ! ( array_key_exists( 'access_users_approved', $_POST ) && is_array( $_POST['access_users_approved'] ) ) ) {
4010 $_POST['access_users_approved'] = array();
4011 }
4012
4013 // Deal with each modified user (add, remove, or change_role).
4014 foreach ( $_POST['access_users_approved'] as $approved_user ) {
4015 if ( $approved_user['edit_action'] === 'add' ) {
4016
4017 // New user (create user, or add existing user to current site in multisite).
4018 $new_user = get_user_by( 'email', $approved_user['email'] );
4019 if ( $new_user !== false ) {
4020 if ( is_multisite() ) {
4021 add_user_to_blog( get_current_blog_id(), $new_user->ID, $approved_user['role'] );
4022 }
4023 } elseif ( $approved_user['local_user'] === 'true' ) {
4024 // Create a WP account for this new *local* user and email the password.
4025 $plaintext_password = wp_generate_password(); // random password
4026 // If there's already a user with this username (e.g.,
4027 // johndoe/johndoe@gmail.com exists, and we're trying to add
4028 // johndoe/johndoe@example.com), use the full email address
4029 // as the username.
4030 $username = explode( "@", $approved_user['email'] );
4031 $username = $username[0];
4032 if ( get_user_by( 'login', $username ) !== false ) {
4033 $username = $approved_user['email'];
4034 }
4035 if ( $approved_user['multisite_user'] !== 'false' ) {
4036 $result = wpmu_create_user(
4037 strtolower( $username ),
4038 $plaintext_password,
4039 strtolower( $approved_user['email'] )
4040 );
4041 } else {
4042 $result = wp_insert_user(
4043 array(
4044 'user_login' => strtolower( $username ),
4045 'user_pass' => $plaintext_password,
4046 'first_name' => '',
4047 'last_name' => '',
4048 'user_email' => strtolower( $approved_user['email'] ),
4049 'user_registered' => date( 'Y-m-d H:i:s' ),
4050 'role' => $approved_user['role'],
4051 )
4052 );
4053 }
4054 if ( ! is_wp_error( $result ) ) {
4055 // Email password to new user
4056 wp_new_user_notification( $result, $plaintext_password );
4057 }
4058
4059 }
4060
4061 // Email new user welcome message if plugin option is set.
4062 $this->maybe_email_welcome_message( $approved_user['email'] );
4063
4064 // Add new user to approved list and save (skip if it's
4065 // already there--someone else might have just done it).
4066 if ( $approved_user['multisite_user'] !== 'false' ) {
4067 if ( ! $this->is_email_in_list( $approved_user['email'], 'approved', 'multisite' ) ) {
4068 $auth_multisite_settings_access_users_approved = $this->sanitize_user_list(
4069 $this->get_plugin_option( 'access_users_approved', 'multisite admin' )
4070 );
4071 $approved_user['date_added'] = date( 'M Y' );
4072 array_push( $auth_multisite_settings_access_users_approved, $approved_user );
4073 update_blog_option( BLOG_ID_CURRENT_SITE, 'auth_multisite_settings_access_users_approved', $auth_multisite_settings_access_users_approved );
4074 }
4075 } else {
4076 if ( ! $this->is_email_in_list( $approved_user['email'], 'approved' ) ) {
4077 $auth_settings_access_users_approved = $this->sanitize_user_list(
4078 $this->get_plugin_option( 'access_users_approved', 'single admin' )
4079 );
4080 $approved_user['date_added'] = date( 'M Y' );
4081 array_push( $auth_settings_access_users_approved, $approved_user );
4082 update_option( 'auth_settings_access_users_approved', $auth_settings_access_users_approved );
4083 }
4084 }
4085
4086 // If we've added a new multisite user, go through all pending/approved/blocked lists
4087 // on individual sites and remove this user from them (to prevent duplicate entries).
4088 if ( $approved_user['multisite_user'] !== 'false' && is_multisite() ) {
4089 $list_names = array( 'access_users_pending', 'access_users_approved', 'access_users_blocked' );
4090 foreach ( wp_get_sites( array( 'limit' => 999999 ) ) as $site ) {
4091 foreach ( $list_names as $list_name ) {
4092 $user_list = get_blog_option( $site['blog_id'], 'auth_settings_' . $list_name, array() );
4093 $list_changed = false;
4094 foreach ( $user_list as $key => $user ) {
4095 if ( $user['email'] == $approved_user['email'] ) {
4096 unset( $user_list[$key] );
4097 $list_changed = true;
4098 }
4099 }
4100 if ( $list_changed ) {
4101 update_blog_option( $site['blog_id'], 'auth_settings_' . $list_name, $user_list );
4102 }
4103 }
4104 }
4105 }
4106
4107 } elseif ( $approved_user['edit_action'] === 'remove' ) {
4108
4109 // Remove user from approved list and save
4110 if ( $approved_user['multisite_user'] !== 'false' ) {
4111 if ( $this->is_email_in_list( $approved_user['email'], 'approved', 'multisite' ) ) {
4112 $auth_multisite_settings_access_users_approved = $this->sanitize_user_list(
4113 $this->get_plugin_option( 'access_users_approved', 'multisite admin' )
4114 );
4115 foreach ( $auth_multisite_settings_access_users_approved as $key => $existing_user ) {
4116 if ( $approved_user['email'] == $existing_user['email'] ) {
4117 unset( $auth_multisite_settings_access_users_approved[$key] );
4118 break;
4119 }
4120 }
4121 update_blog_option( BLOG_ID_CURRENT_SITE, 'auth_multisite_settings_access_users_approved', $auth_multisite_settings_access_users_approved );
4122 }
4123 } else {
4124 if ( $this->is_email_in_list( $approved_user['email'], 'approved' ) ) {
4125 $auth_settings_access_users_approved = $this->sanitize_user_list(
4126 $this->get_plugin_option( 'access_users_approved', 'single admin' )
4127 );
4128 foreach ( $auth_settings_access_users_approved as $key => $existing_user ) {
4129 if ( $approved_user['email'] == $existing_user['email'] ) {
4130 unset( $auth_settings_access_users_approved[$key] );
4131 break;
4132 }
4133 }
4134 update_option( 'auth_settings_access_users_approved', $auth_settings_access_users_approved );
4135 }
4136 }
4137
4138 } elseif ( $approved_user['edit_action'] === 'change_role' ) {
4139
4140 // Update user's role in WordPress
4141 $changed_user = get_user_by( 'email', $approved_user['email'] );
4142 if ( $changed_user ) {
4143 if ( is_multisite() && $approved_user['multisite_user'] !== 'false' ) {
4144 foreach ( get_blogs_of_user( $changed_user->ID ) as $blog ) {
4145 add_user_to_blog( $blog->userblog_id, $changed_user->ID, $approved_user['role'] );
4146 }
4147 } else {
4148 $changed_user->set_role( $approved_user['role'] );
4149 }
4150 }
4151
4152 if ( $approved_user['multisite_user'] !== 'false' ) {
4153 if ( $this->is_email_in_list( $approved_user['email'], 'approved', 'multisite' ) ) {
4154 $auth_multisite_settings_access_users_approved = $this->sanitize_user_list(
4155 $this->get_plugin_option( 'access_users_approved', 'multisite admin' )
4156 );
4157 foreach ( $auth_multisite_settings_access_users_approved as $key => $existing_user ) {
4158 if ( $approved_user['email'] == $existing_user['email'] ) {
4159 $auth_multisite_settings_access_users_approved[$key]['role'] = $approved_user['role'];
4160 break;
4161 }
4162 }
4163 update_blog_option( BLOG_ID_CURRENT_SITE, 'auth_multisite_settings_access_users_approved', $auth_multisite_settings_access_users_approved );
4164 }
4165 } else {
4166 // Update user's role in approved list and save.
4167 if ( $this->is_email_in_list( $approved_user['email'], 'approved' ) ) {
4168 $auth_settings_access_users_approved = $this->sanitize_user_list(
4169 $this->get_plugin_option( 'access_users_approved', 'single admin' )
4170 );
4171 foreach ( $auth_settings_access_users_approved as $key => $existing_user ) {
4172 if ( $approved_user['email'] == $existing_user['email'] ) {
4173 $auth_settings_access_users_approved[$key]['role'] = $approved_user['role'];
4174 break;
4175 }
4176 }
4177 update_option( 'auth_settings_access_users_approved', $auth_settings_access_users_approved );
4178 }
4179 }
4180
4181 }
4182 }
4183 }
4184
4185 // Editing a blocked list entry.
4186 if ( $_POST['setting'] === 'access_users_blocked' ) {
4187 // Initialize posted data if empty.
4188 if ( ! ( array_key_exists( 'access_users_blocked', $_POST ) && is_array( $_POST['access_users_blocked'] ) ) ) {
4189 $_POST['access_users_blocked'] = array();
4190 }
4191
4192 // Deal with each modified user (add or remove).
4193 foreach ( $_POST['access_users_blocked'] as $blocked_user ) {
4194
4195 if ( $blocked_user['edit_action'] === 'add' ) {
4196
4197 // Add new user to blocked list and save (skip if it's
4198 // already there--someone else might have just done it).
4199 if ( ! $this->is_email_in_list( $blocked_user['email'], 'blocked' ) ) {
4200 $auth_settings_access_users_blocked = $this->sanitize_user_list(
4201 $this->get_plugin_option( 'access_users_blocked', 'single admin' )
4202 );
4203 $blocked_user['date_added'] = date( 'M Y' );
4204 array_push( $auth_settings_access_users_blocked, $blocked_user );
4205 update_option( 'auth_settings_access_users_blocked', $auth_settings_access_users_blocked );
4206 }
4207
4208 } elseif ( $blocked_user['edit_action'] === 'remove' ) {
4209
4210 // Remove auth_blocked usermeta for the user.
4211 $unblocked_user = get_user_by( 'email', $blocked_user['email'] );
4212 if ( $unblocked_user !== false ) {
4213 delete_user_meta( $unblocked_user->ID, 'auth_blocked', 'yes' );
4214 }
4215
4216 // Remove user from blocked list and save
4217 if ( $this->is_email_in_list( $blocked_user['email'], 'blocked' ) ) {
4218 $auth_settings_access_users_blocked = $this->sanitize_user_list(
4219 $this->get_plugin_option( 'access_users_blocked', 'single admin' )
4220 );
4221 foreach ( $auth_settings_access_users_blocked as $key => $existing_user ) {
4222 if ( $blocked_user['email'] == $existing_user['email'] ) {
4223 unset( $auth_settings_access_users_blocked[$key] );
4224 break;
4225 }
4226 }
4227 update_option( 'auth_settings_access_users_blocked', $auth_settings_access_users_blocked );
4228 }
4229
4230 }
4231 }
4232 }
4233
4234 // Return 'success' value to AJAX call.
4235 die( 'success' );
4236 } // END update_auth_user()
4237
4238
4239
4240 /**
4241 * ***************************
4242 * Helper functions
4243 * ***************************
4244 */
4245
4246
4247 /**
4248 * Retrieves a specific plugin option from db. Multisite enabled.
4249 *
4250 * @param string $option Option name
4251 * @param string $admin_mode 'multisite admin' will retrieve the multisite value
4252 * @param string $override_mode 'allow override' will retrieve the multisite value if it exists
4253 * @param string $print_mode 'print overlay' will output overlay that hides this option on the settings page
4254 * @return mixed Option value, or null on failure
4255 */
4256 private function get_plugin_option( $option, $admin_mode = 'single admin', $override_mode = 'no override', $print_mode = 'no overlay' ) {
4257
4258 // Special case for user lists (they are saved seperately to prevent concurrency issues).
4259 if ( in_array( $option, array( 'access_users_pending', 'access_users_approved', 'access_users_blocked' ) ) ) {
4260 $list = $admin_mode === 'multisite admin' ? array() : get_option( 'auth_settings_' . $option );
4261 if ( is_multisite() && $admin_mode === 'multisite admin' ) {
4262 $list = get_blog_option( BLOG_ID_CURRENT_SITE, 'auth_multisite_settings_' . $option, array() );
4263 }
4264 return $list;
4265 }
4266
4267 // Get all plugin options.
4268 $auth_settings = $this->get_plugin_options( $admin_mode, $override_mode );
4269
4270 // Set option to null if it wasn't found.
4271 if ( ! array_key_exists( $option, $auth_settings ) ) {
4272 return null;
4273 }
4274
4275 // If requested and appropriate, print the overlay hiding the
4276 // single site option that is overridden by a multisite option.
4277 if (
4278 $admin_mode !== 'multisite admin' &&
4279 $override_mode === 'allow override' &&
4280 $print_mode === 'print overlay' &&
4281 array_key_exists( 'multisite_override', $auth_settings ) &&
4282 $auth_settings['multisite_override'] === '1' &&
4283 ( ! array_key_exists( 'advanced_override_multisite', $auth_settings ) || $auth_settings['advanced_override_multisite'] != '1' )
4284 ) {
4285 // Get original plugin options (not overridden value). We'll
4286 // show this old value behind the disabled overlay.
4287 $auth_settings = $this->get_plugin_options( $admin_mode, 'no override' );
4288
4289 $name = "auth_settings[$option]";
4290 $id = "auth_settings_$option"; ?>
4291 <div id="overlay-hide-auth_settings_<?php echo $option; ?>" class="auth_multisite_override_overlay">
4292 <span class="overlay-note">
4293 This setting is overridden by a <a href="<?php echo network_admin_url( 'admin.php?page=authorizer&tab=external' ); ?>">multisite option</a>.
4294 </span>
4295 </div>
4296 <?php
4297 }
4298
4299 // If we're getting an option in a site that has overridden the multisite override, make
4300 // sure we are returning the option value from that site (not the multisite value).
4301 if ( array_key_exists( 'advanced_override_multisite', $auth_settings ) && $auth_settings['advanced_override_multisite'] == '1' ) {
4302 $auth_settings = $this->get_plugin_options( $admin_mode, 'no override' );
4303 }
4304
4305 return $auth_settings[$option];
4306 }
4307
4308 /**
4309 * Retrieves all plugin options from db. Multisite enabled.
4310 *
4311 * @param string $admin_mode 'multisite admin' will retrieve the multisite value
4312 * @param string $override_mode 'allow override' will retrieve the multisite value if it exists
4313 * @return mixed Option value, or null on failure
4314 */
4315 private function get_plugin_options( $admin_mode = 'single admin', $override_mode = 'no override' ) {
4316 // Grab plugin settings (skip if in multisite admin mode).
4317 $auth_settings = $admin_mode === 'multisite admin' ? array() : get_option( 'auth_settings' );
4318
4319 // Initialize to empty array if the plugin option doesn't exist.
4320 if ( $auth_settings === FALSE ) {
4321 $auth_settings = array();
4322 }
4323
4324 // Merge multisite options if we're in a network and the current site hasn't overridden multisite settings.
4325 if ( is_multisite() && ( ! array_key_exists( 'advanced_override_multisite', $auth_settings ) || $auth_settings['advanced_override_multisite'] != '1' ) ) {
4326 // Get multisite options.
4327 $auth_multisite_settings = get_blog_option( BLOG_ID_CURRENT_SITE, 'auth_multisite_settings', array() );
4328
4329 // Return the multisite options if we're viewing the network admin options page.
4330 // Otherwise override options with their multisite equivalents.
4331 if ( $admin_mode === 'multisite admin' ) {
4332 $auth_settings = $auth_multisite_settings;
4333 } elseif (
4334 $override_mode === 'allow override' &&
4335 array_key_exists( 'multisite_override', $auth_multisite_settings ) &&
4336 $auth_multisite_settings['multisite_override'] === '1'
4337 ) {
4338 // Keep track of the multisite override selection.
4339 $auth_settings['multisite_override'] = $auth_multisite_settings['multisite_override'];
4340
4341 // Note: the options below should be the complete list of
4342 // overridden options. It is *not* the complete list of all
4343 // options (some options don't have a multisite equivalent)
4344
4345 // Note: access_users_approved, access_users_pending, and
4346 // access_users_blocked do not get overridden. However,
4347 // since access_users_approved has a multisite equivalent,
4348 // you must retrieve them both seperately. This is done
4349 // because the two lists should be treated differently.
4350 // $approved_users = $this->get_plugin_option( 'access_users_approved', 'single admin' );
4351 // $ms_approved_users = $this->get_plugin_option( 'access_users_approved', 'multisite admin' );
4352
4353 // Override external services (google, cas, or ldap) and associated options
4354 $auth_settings['google'] = $auth_multisite_settings['google'];
4355 $auth_settings['google_clientid'] = $auth_multisite_settings['google_clientid'];
4356 $auth_settings['google_clientsecret'] = $auth_multisite_settings['google_clientsecret'];
4357 $auth_settings['cas'] = $auth_multisite_settings['cas'];
4358 $auth_settings['cas_custom_label'] = $auth_multisite_settings['cas_custom_label'];
4359 $auth_settings['cas_host'] = $auth_multisite_settings['cas_host'];
4360 $auth_settings['cas_port'] = $auth_multisite_settings['cas_port'];
4361 $auth_settings['cas_path'] = $auth_multisite_settings['cas_path'];
4362 $auth_settings['cas_attr_email'] = $auth_multisite_settings['cas_attr_email'];
4363 $auth_settings['cas_attr_first_name'] = $auth_multisite_settings['cas_attr_first_name'];
4364 $auth_settings['cas_attr_last_name'] = $auth_multisite_settings['cas_attr_last_name'];
4365 $auth_settings['cas_attr_update_on_login'] = $auth_multisite_settings['cas_attr_update_on_login'];
4366 $auth_settings['ldap'] = $auth_multisite_settings['ldap'];
4367 $auth_settings['ldap_host'] = $auth_multisite_settings['ldap_host'];
4368 $auth_settings['ldap_port'] = $auth_multisite_settings['ldap_port'];
4369 $auth_settings['ldap_search_base'] = $auth_multisite_settings['ldap_search_base'];
4370 $auth_settings['ldap_uid'] = $auth_multisite_settings['ldap_uid'];
4371 $auth_settings['ldap_attr_email'] = $auth_multisite_settings['ldap_attr_email'];
4372 $auth_settings['ldap_user'] = $auth_multisite_settings['ldap_user'];
4373 $auth_settings['ldap_password'] = $auth_multisite_settings['ldap_password'];
4374 $auth_settings['ldap_tls'] = $auth_multisite_settings['ldap_tls'];
4375 $auth_settings['ldap_lostpassword_url'] = $auth_multisite_settings['ldap_lostpassword_url'];
4376 $auth_settings['ldap_attr_first_name'] = $auth_multisite_settings['ldap_attr_first_name'];
4377 $auth_settings['ldap_attr_last_name'] = $auth_multisite_settings['ldap_attr_last_name'];
4378 $auth_settings['ldap_attr_update_on_login'] = $auth_multisite_settings['ldap_attr_update_on_login'];
4379
4380 // Override access_who_can_login and access_who_can_view
4381 $auth_settings['access_who_can_login'] = $auth_multisite_settings['access_who_can_login'];
4382 $auth_settings['access_who_can_view'] = $auth_multisite_settings['access_who_can_view'];
4383
4384 // Override access_default_role
4385 $auth_settings['access_default_role'] = $auth_multisite_settings['access_default_role'];
4386
4387 // Override lockouts
4388 $auth_settings['advanced_lockouts'] = $auth_multisite_settings['advanced_lockouts'];
4389
4390 // Override Hide WordPress login
4391 $auth_settings['advanced_hide_wp_login'] = $auth_multisite_settings['advanced_hide_wp_login'];
4392 }
4393 }
4394 return $auth_settings;
4395 }
4396
4397
4398 private function maybe_email_welcome_message( $email ) {
4399 // Get option for whether to email welcome messages.
4400 $should_email_new_approved_users = $this->get_plugin_option( 'access_should_email_approved_users' );
4401
4402 // Do not send welcome email if option not enabled.
4403 if ( $should_email_new_approved_users !== '1' ) {
4404 return false;
4405 }
4406
4407 // Make sure we didn't just email this user (can happen with
4408 // multiple admins saving at the same time, or by clicking
4409 // Approve button too rapidly).
4410 $recently_sent_emails = get_option( 'auth_settings_recently_sent_emails' );
4411 if ( $recently_sent_emails === FALSE ) {
4412 $recently_sent_emails = array();
4413 }
4414 foreach ( $recently_sent_emails as $key => $recently_sent_email ) {
4415 if ( $recently_sent_email['time'] < strtotime( 'now -1 minutes' ) ) {
4416 // Remove emails sent more than 1 minute ago.
4417 unset( $recently_sent_emails[$key] );
4418 } elseif ( $recently_sent_email['email'] === $email ) {
4419 // Sent an email to this user within the last 1 minute, so
4420 // quit without sending.
4421 return false;
4422 }
4423 }
4424 // Add the email we're about to send to the list.
4425 $recently_sent_emails[] = array(
4426 'email' => $email,
4427 'time' => time(),
4428 );
4429 update_option( 'auth_settings_recently_sent_emails', $recently_sent_emails );
4430
4431 // Get welcome email subject and body text
4432 $subject = $this->get_plugin_option( 'access_email_approved_users_subject' );
4433 $body = apply_filters( 'the_content', $this->get_plugin_option( 'access_email_approved_users_body' ) );
4434
4435 // Fail if the subject/body options don't exist or are empty.
4436 if ( is_null( $subject ) || is_null( $body ) || strlen( $subject ) === 0 || strlen( $body ) === 0 ) {
4437 return false;
4438 }
4439
4440 // Replace approved shortcode patterns in subject and body.
4441 $site_name = get_bloginfo( 'name' );
4442 $site_url = get_site_url();
4443 $subject = str_replace( '[site_name]', $site_name, $subject );
4444 $body = str_replace( '[site_name]', $site_name, $body );
4445 $body = str_replace( '[site_url]', $site_url, $body );
4446 $body = str_replace( '[user_email]', $email, $body );
4447 $headers = 'Content-type: text/html' . "\r\n";
4448
4449 // Send email.
4450 wp_mail( $email, $subject, $body, $headers );
4451
4452 // Indicate mail was sent.
4453 return true;
4454 }
4455
4456 /**
4457 * Generate a unique cookie to add to nonces to prevent CSRF.
4458 */
4459 protected $cookie_value = null;
4460 function get_cookie_value() {
4461 if ( ! $this->cookie_value ) {
4462 if ( isset( $_COOKIE['login_unique'] ) ) {
4463 $this->cookie_value = $_COOKIE['login_unique'];
4464 } else {
4465 $this->cookie_value = md5( rand() );
4466 }
4467 }
4468 return $this->cookie_value;
4469 } // END get_cookie_value()
4470
4471 /**
4472 * Basic encryption using a public (not secret!) key. Used for general
4473 * database obfuscation of passwords.
4474 */
4475 private static $key = '8QxnrvjdtweisvCBKEY!+0';
4476 function encrypt( $text ) {
4477 $result = '';
4478
4479 // Use mcrypt library (better) if php5-mcrypt extension is enabled.
4480 if ( function_exists( 'mcrypt_encrypt' ) ) {
4481 $result = mcrypt_encrypt( MCRYPT_RIJNDAEL_256, self::$key, $text, MCRYPT_MODE_ECB, 'abcdefghijklmnopqrstuvwxyz012345' );
4482 } else {
4483 for ( $i = 0; $i < strlen( $text ); $i++ ) {
4484 $char = substr( $text, $i, 1 );
4485 $keychar = substr( self::$key, ( $i % strlen( self::$key ) ) - 1, 1 );
4486 $char = chr( ord( $char ) + ord( $keychar ) );
4487 $result .= $char;
4488 }
4489 $result = base64_encode( $result );
4490 }
4491
4492 return $result;
4493 } // END encrypt()
4494
4495 function decrypt( $secret ) {
4496 $result = '';
4497
4498 // Use mcrypt library (better) if php5-mcrypt extension is enabled.
4499 if ( function_exists( 'mcrypt_decrypt' ) ) {
4500 $result = rtrim( mcrypt_decrypt( MCRYPT_RIJNDAEL_256, self::$key, $secret, MCRYPT_MODE_ECB, 'abcdefghijklmnopqrstuvwxyz012345' ), "\0$result" );
4501 } else {
4502 $secret = base64_decode( $secret );
4503 for ( $i = 0; $i < strlen( $secret ); $i++ ) {
4504 $char = substr( $secret, $i, 1 );
4505 $keychar = substr( self::$key, ( $i % strlen( self::$key ) ) - 1, 1 );
4506 $char = chr( ord( $char ) - ord( $keychar ) );
4507 $result .= $char;
4508 }
4509 }
4510
4511 return $result;
4512 } // END decrypt()
4513
4514 /**
4515 * In a multisite environment, returns true if the current user is logged
4516 * in and a user of the current blog. In single site mode, simply returns
4517 * true if the current user is logged in.
4518 */
4519 function is_user_logged_in_and_blog_user() {
4520 $is_user_logged_in_and_blog_user = false;
4521 if ( is_multisite() ) {
4522 $is_user_logged_in_and_blog_user = is_user_logged_in() && is_user_member_of_blog( get_current_user_id() );
4523 } else {
4524 $is_user_logged_in_and_blog_user = is_user_logged_in();
4525 }
4526 return $is_user_logged_in_and_blog_user;
4527 } // END is_user_logged_in_and_blog_user()
4528
4529 /**
4530 * Helper function to determine whether a given email is in one of
4531 * the lists (pending, approved, blocked). Defaults to the list of
4532 * approved users.
4533 */
4534 function is_email_in_list( $email = '', $list = 'approved', $multisite_mode = 'single' ) {
4535 if ( empty( $email ) )
4536 return false;
4537
4538 switch ( $list ) {
4539 case 'pending':
4540 $auth_settings_access_users_pending = $this->get_plugin_option( 'access_users_pending', 'single admin' );
4541 return $this->in_multi_array( $email, $auth_settings_access_users_pending );
4542 break;
4543 case 'blocked':
4544 $auth_settings_access_users_blocked = $this->get_plugin_option( 'access_users_blocked', 'single admin' );
4545 return $this->in_multi_array( $email, $auth_settings_access_users_blocked );
4546 break;
4547 case 'approved':
4548 default:
4549 if ( $multisite_mode !== 'single' ) {
4550 // Get multisite users only.
4551 $auth_settings_access_users_approved = $this->get_plugin_option( 'access_users_approved', 'multisite admin' );
4552 } else if ( is_multisite() && $this->get_plugin_option( 'advanced_override_multisite' ) == '1' ) {
4553 // This site has overridden any multisite settings, so only get its users.
4554 $auth_settings_access_users_approved = $this->get_plugin_option( 'access_users_approved', 'single admin' );
4555 } else {
4556 // Get all site users and all multisite users.
4557 $auth_settings_access_users_approved = array_merge(
4558 $this->get_plugin_option( 'access_users_approved', 'single admin' ),
4559 $this->get_plugin_option( 'access_users_approved', 'multisite admin' )
4560 );
4561 }
4562 return $this->in_multi_array( $email, $auth_settings_access_users_approved );
4563 break;
4564 }
4565 } // END is_email_in_list
4566
4567 /**
4568 * Helper function to get number of users (including multisite users)
4569 * in a given list (pending, approved, or blocked).
4570 * @param string $list
4571 * @param string $admin_mode 'single admin' or 'multisite admin' determines whether to include multisite users
4572 * @return int number of users in list
4573 */
4574 function get_user_count_from_list( $list, $admin_mode = 'single admin' ) {
4575 $auth_settings_access_users = array();
4576
4577 switch ( $list ) {
4578 case 'pending':
4579 $auth_settings_access_users = $this->get_plugin_option( 'access_users_pending', 'single admin' );
4580 break;
4581 case 'blocked':
4582 $auth_settings_access_users = $this->get_plugin_option( 'access_users_blocked', 'single admin' );
4583 break;
4584 case 'approved':
4585 if ( $admin_mode !== 'single admin' ) {
4586 // Get multisite users only.
4587 $auth_settings_access_users = $this->get_plugin_option( 'access_users_approved', 'multisite admin' );
4588 } else if ( is_multisite() && $this->get_plugin_option( 'advanced_override_multisite' ) == '1' ) {
4589 // This site has overridden any multisite settings, so only get its users.
4590 $auth_settings_access_users = $this->get_plugin_option( 'access_users_approved', 'single admin' );
4591 } else {
4592 // Get all site users and all multisite users.
4593 $auth_settings_access_users = array_merge(
4594 $this->get_plugin_option( 'access_users_approved', 'single admin' ),
4595 $this->get_plugin_option( 'access_users_approved', 'multisite admin' )
4596 );
4597 }
4598 }
4599
4600 return count( $auth_settings_access_users );
4601 }
4602
4603 /**
4604 * Helper function to search a multidimensional array for a value.
4605 */
4606 function in_multi_array( $needle = '', $haystack = array(), $strict_mode = 'not strict', $case_sensitivity = 'case insensitive' ) {
4607 if ( ! is_array( $haystack ) ) {
4608 return false;
4609 }
4610 if ( $case_sensitivity === 'case insensitive' ) {
4611 $needle = strtolower( $needle );
4612 }
4613 foreach ( $haystack as $item ) {
4614 if ( $case_sensitivity === 'case insensitive' && ! is_array( $item ) ) {
4615 $item = strtolower( $item );
4616 }
4617 if ( ( $strict_mode === 'strict' ? $item === $needle : $item == $needle ) || ( is_array( $item ) && $this->in_multi_array( $needle, $item, $strict_mode, $case_sensitivity ) ) ) {
4618 return true;
4619 }
4620 }
4621 return false;
4622 } // END in_multi_array()
4623
4624 /**
4625 * Helper function to get a WordPress page ID from the pagename.
4626 *
4627 * @param string $pagename Page Slug
4628 * @return int Page/Post ID
4629 */
4630 function get_id_from_pagename( $pagename = '' ) {
4631 global $wpdb;
4632 $page_id = $wpdb->get_var( "SELECT ID FROM $wpdb->posts WHERE post_name = '" . sanitize_title_for_query( $pagename ) . "'" );
4633 return $page_id;
4634 } // END get_id_from_pagename()
4635
4636 /**
4637 * Helper function to determine if an URL is accessible.
4638 *
4639 * @param string $url URL that should be publicly reachable
4640 * @return boolean Whether the URL is publicly reachable
4641 */
4642 function url_is_accessible( $url ) {
4643 // Make sure php5-curl extension is installed on server.
4644 if ( ! function_exists( 'curl_init' ) ) {
4645 // Note: This will silently fail, saying url is not accessible.
4646 // Warn user elsewhere that they should install curl.
4647 return false;
4648 }
4649
4650 // Use curl to retrieve the URL.
4651 $handle = curl_init( $url );
4652 curl_setopt( $handle, CURLOPT_RETURNTRANSFER, TRUE );
4653 curl_setopt( $handle, CURLOPT_SSL_VERIFYPEER, FALSE );
4654 $response = curl_exec( $handle );
4655 $http_code = curl_getinfo( $handle, CURLINFO_HTTP_CODE );
4656 curl_close( $handle );
4657
4658 // Return true if the document has loaded successfully without any redirection or error
4659 return $http_code >= 200 && $http_code < 400;
4660 } // END url_is_accessible()
4661
4662 // Helper function that builds option tags for a select element for all
4663 // roles the current user has permission to assign.
4664 function wp_dropdown_permitted_roles( $selected_role = 'subscriber', $disable_input = 'not disabled' ) {
4665 $roles = get_editable_roles();
4666 $current_user = wp_get_current_user();
4667
4668 // Make sure we have a selected role (default to subscriber).
4669 if ( strlen( $selected_role ) < 1 ) {
4670 $selected_role = 'subscriber';
4671 }
4672
4673 // If the currently selected role is not in the list of roles, it
4674 // either doesn't exist or the current user is not permitted to
4675 // assign it.
4676 if ( ! array_key_exists( $selected_role, $roles ) ) {
4677 ?><option value="<?php echo $selected_role; ?>"><?php echo ucfirst( $selected_role ); ?></option><?php
4678
4679 // If the role exists, that means the user isn't permitted to
4680 // assign it, so assume they can't edit that user's role at
4681 // all. Return only the one role for the dropdown list.
4682 if ( ! is_null( get_role( $selected_role ) ) ) {
4683 return;
4684 }
4685 }
4686
4687 // Print an option element for each permitted role.
4688 foreach ( $roles as $name => $role ) {
4689 $selected = $selected_role === $name ? ' selected="selected"' : '';
4690
4691 // Don't let a user change their own role
4692 $disabled = $selected_role !== $name && $disable_input === 'disabled' ? ' disabled="disabled"' : '';
4693
4694 // But network admins can always change their role.
4695 if ( is_multisite() && current_user_can( 'manage_network' ) ) {
4696 $disabled = '';
4697 }
4698
4699 ?><option value="<?php echo $name; ?>"<?php echo $selected . $disabled; ?>><?php echo $role['name']; ?></option><?php
4700 }
4701 } // END wp_dropdown_permitted_roles()
4702
4703 // Helper function to get a single user info array from one of the
4704 // access control lists (pending, approved, or blocked).
4705 // Returns: false if not found; otherwise
4706 // array( 'email' => '', 'role' => '', 'date_added' => '', ['usermeta' => [''|array()]] );
4707 function get_user_info_from_list( $email, $list ) {
4708 foreach ( $list as $user_info ) {
4709 if ( $user_info['email'] === $email ) {
4710 return $user_info;
4711 }
4712 }
4713 return false;
4714 } // END get_user_info_from_list()
4715
4716 // Helper function to convert seconds to human readable text.
4717 // Source: http://csl.name/php-secs-to-human-text/
4718 function seconds_as_sentence( $secs ) {
4719 $units = array(
4720 "week" => 7 * 24 * 3600,
4721 "day" => 24 * 3600,
4722 "hour" => 3600,
4723 "minute" => 60,
4724 "second" => 1,
4725 );
4726
4727 // specifically handle zero
4728 if ( $secs == 0 ) return "0 seconds";
4729
4730 $s = "";
4731
4732 foreach ( $units as $name => $divisor ) {
4733 if ( $quot = intval( $secs / $divisor ) ) {
4734 $s .= "$quot $name";
4735 $s .= ( abs( $quot ) > 1 ? "s" : "" ) . ", ";
4736 $secs -= $quot * $divisor;
4737 }
4738 }
4739
4740 return substr( $s, 0, -2 );
4741 } // END seconds_as_sentence()
4742
4743 // Helper function to get all available usermeta keys as an array.
4744 function get_all_usermeta_keys() {
4745 global $wpdb;
4746 $usermeta_keys = $wpdb->get_col( "SELECT DISTINCT $wpdb->usermeta.meta_key FROM $wpdb->usermeta" );
4747 return $usermeta_keys;
4748 }
4749
4750
4751 /**
4752 * Plugin Update Routines.
4753 */
4754 function auth_update_check() {
4755 // Update: migrate user lists to own options (addresses concurrency
4756 // when saving plugin options, since user lists are changed often
4757 // and we don't want to overwrite changes to the lists when an
4758 // admin saves all of the plugin options.)
4759 // Note: Pending user list is changed whenever a new user tries to
4760 // log in; approved and blocked lists are changed whenever an admin
4761 // changes them from the multisite panel, the dashboard widget, or
4762 // the plugin options page.
4763 $update_if_older_than = 20140709;
4764 $auth_version = get_option( 'auth_version' );
4765 if ( $auth_version === false || intval( $auth_version ) < $update_if_older_than ) {
4766 // Copy single site user lists to new options (if they exist).
4767 $auth_settings = get_option( 'auth_settings' );
4768 if ( is_array( $auth_settings ) && array_key_exists( 'access_users_pending', $auth_settings ) ) {
4769 update_option( 'auth_settings_access_users_pending', $auth_settings['access_users_pending'] );
4770 unset( $auth_settings['access_users_pending'] );
4771 update_option( 'auth_settings', $auth_settings );
4772 }
4773 if ( is_array( $auth_settings ) && array_key_exists( 'access_users_approved', $auth_settings ) ) {
4774 update_option( 'auth_settings_access_users_approved', $auth_settings['access_users_approved'] );
4775 unset( $auth_settings['access_users_approved'] );
4776 update_option( 'auth_settings', $auth_settings );
4777 }
4778 if ( is_array( $auth_settings ) && array_key_exists( 'access_users_blocked', $auth_settings ) ) {
4779 update_option( 'auth_settings_access_users_blocked', $auth_settings['access_users_blocked'] );
4780 unset( $auth_settings['access_users_blocked'] );
4781 update_option( 'auth_settings', $auth_settings );
4782 }
4783 // Copy multisite user lists to new options (if they exist).
4784 if ( is_multisite() ) {
4785 $auth_multisite_settings = get_blog_option( BLOG_ID_CURRENT_SITE, 'auth_multisite_settings', array() );
4786 if ( is_array( $auth_multisite_settings ) && array_key_exists( 'access_users_pending', $auth_multisite_settings ) ) {
4787 update_blog_option( BLOG_ID_CURRENT_SITE, 'auth_multisite_settings_access_users_pending', $auth_multisite_settings['access_users_pending'] );
4788 unset( $auth_multisite_settings['access_users_pending'] );
4789 update_blog_option( BLOG_ID_CURRENT_SITE, 'auth_multisite_settings', $auth_multisite_settings );
4790 }
4791 if ( is_array( $auth_multisite_settings ) && array_key_exists( 'access_users_approved', $auth_multisite_settings ) ) {
4792 update_blog_option( BLOG_ID_CURRENT_SITE, 'auth_multisite_settings_access_users_approved', $auth_multisite_settings['access_users_approved'] );
4793 unset( $auth_multisite_settings['access_users_approved'] );
4794 update_blog_option( BLOG_ID_CURRENT_SITE, 'auth_multisite_settings', $auth_multisite_settings );
4795 }
4796 if ( is_array( $auth_multisite_settings ) && array_key_exists( 'access_users_blocked', $auth_multisite_settings ) ) {
4797 update_blog_option( BLOG_ID_CURRENT_SITE, 'auth_multisite_settings_access_users_blocked', $auth_multisite_settings['access_users_blocked'] );
4798 unset( $auth_multisite_settings['access_users_blocked'] );
4799 update_blog_option( BLOG_ID_CURRENT_SITE, 'auth_multisite_settings', $auth_multisite_settings );
4800 }
4801 }
4802 // Update version to reflect this change has been made.
4803 update_option( 'auth_version', $update_if_older_than );
4804 }
4805
4806 // // Update: TEMPLATE
4807 // $update_if_older_than = YYYYMMDD;
4808 // $auth_version = get_option( 'auth_version' );
4809 // if ( $auth_version === false || intval( $auth_version ) < $update_if_older_than ) {
4810 // UPDATE CODE HERE
4811 // update_option( 'auth_version', $update_if_older_than );
4812 // }
4813 }
4814
4815 } // END class WP_Plugin_Authorizer
4816 }
4817
4818 // Instantiate the plugin class.
4819 $wp_plugin_authorizer = new WP_Plugin_Authorizer();
4820