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