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

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