PluginProbe
Authorizer / 2.6.20
Authorizer v2.6.20
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.20, at authorizer.php

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