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

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