PluginProbe
Authorizer / 2.6.12
Authorizer v2.6.12
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.12, at authorizer.php

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