| 1 |
<?php |
| 2 |
/** |
| 3 |
* Authorizer |
| 4 |
* |
| 5 |
* @license GPL-2.0+ |
| 6 |
* @link https://github.com/uhm-coe/authorizer |
| 7 |
* @package authorizer |
| 8 |
*/ |
| 9 |
|
| 10 |
namespace Authorizer; |
| 11 |
|
| 12 |
use Authorizer\Helper; |
| 13 |
use Authorizer\Options; |
| 14 |
|
| 15 |
/** |
| 16 |
* Run any database migrations or plugin updates when installing a new version. |
| 17 |
*/ |
| 18 |
class Updates extends Static_Instance { |
| 19 |
|
| 20 |
/** |
| 21 |
* Plugin Update Routines. |
| 22 |
* |
| 23 |
* Action: plugins_loaded |
| 24 |
*/ |
| 25 |
public function auth_update_check() { |
| 26 |
$options = Options::get_instance(); |
| 27 |
|
| 28 |
// Get current version. |
| 29 |
$needs_updating = false; |
| 30 |
if ( is_multisite() ) { |
| 31 |
$auth_version = get_blog_option( get_network()->blog_id, 'auth_version' ); |
| 32 |
} else { |
| 33 |
$auth_version = get_option( 'auth_version' ); |
| 34 |
} |
| 35 |
|
| 36 |
// Update: migrate user lists to own options (addresses concurrency |
| 37 |
// when saving plugin options, since user lists are changed often |
| 38 |
// and we don't want to overwrite changes to the lists when an |
| 39 |
// admin saves all of the plugin options.) |
| 40 |
// Note: Pending user list is changed whenever a new user tries to |
| 41 |
// log in; approved and blocked lists are changed whenever an admin |
| 42 |
// changes them from the multisite panel, the dashboard widget, or |
| 43 |
// the plugin options page. |
| 44 |
$update_if_older_than = 20140709; |
| 45 |
if ( false === $auth_version || intval( $auth_version ) < $update_if_older_than ) { |
| 46 |
// Copy single site user lists to new options (if they exist). |
| 47 |
$auth_settings = get_option( 'auth_settings' ); |
| 48 |
if ( is_array( $auth_settings ) && array_key_exists( 'access_users_pending', $auth_settings ) ) { |
| 49 |
update_option( 'auth_settings_access_users_pending', $auth_settings['access_users_pending'] ); |
| 50 |
unset( $auth_settings['access_users_pending'] ); |
| 51 |
update_option( 'auth_settings', $auth_settings ); |
| 52 |
} |
| 53 |
if ( is_array( $auth_settings ) && array_key_exists( 'access_users_approved', $auth_settings ) ) { |
| 54 |
update_option( 'auth_settings_access_users_approved', $auth_settings['access_users_approved'] ); |
| 55 |
unset( $auth_settings['access_users_approved'] ); |
| 56 |
update_option( 'auth_settings', $auth_settings ); |
| 57 |
} |
| 58 |
if ( is_array( $auth_settings ) && array_key_exists( 'access_users_blocked', $auth_settings ) ) { |
| 59 |
update_option( 'auth_settings_access_users_blocked', $auth_settings['access_users_blocked'] ); |
| 60 |
unset( $auth_settings['access_users_blocked'] ); |
| 61 |
update_option( 'auth_settings', $auth_settings ); |
| 62 |
} |
| 63 |
// Copy multisite user lists to new options (if they exist). |
| 64 |
if ( is_multisite() ) { |
| 65 |
$auth_multisite_settings = get_blog_option( get_network()->blog_id, 'auth_multisite_settings', array() ); |
| 66 |
if ( is_array( $auth_multisite_settings ) && array_key_exists( 'access_users_pending', $auth_multisite_settings ) ) { |
| 67 |
update_blog_option( get_network()->blog_id, 'auth_multisite_settings_access_users_pending', $auth_multisite_settings['access_users_pending'] ); |
| 68 |
unset( $auth_multisite_settings['access_users_pending'] ); |
| 69 |
update_blog_option( get_network()->blog_id, 'auth_multisite_settings', $auth_multisite_settings ); |
| 70 |
} |
| 71 |
if ( is_array( $auth_multisite_settings ) && array_key_exists( 'access_users_approved', $auth_multisite_settings ) ) { |
| 72 |
update_blog_option( get_network()->blog_id, 'auth_multisite_settings_access_users_approved', $auth_multisite_settings['access_users_approved'] ); |
| 73 |
unset( $auth_multisite_settings['access_users_approved'] ); |
| 74 |
update_blog_option( get_network()->blog_id, 'auth_multisite_settings', $auth_multisite_settings ); |
| 75 |
} |
| 76 |
if ( is_array( $auth_multisite_settings ) && array_key_exists( 'access_users_blocked', $auth_multisite_settings ) ) { |
| 77 |
update_blog_option( get_network()->blog_id, 'auth_multisite_settings_access_users_blocked', $auth_multisite_settings['access_users_blocked'] ); |
| 78 |
unset( $auth_multisite_settings['access_users_blocked'] ); |
| 79 |
update_blog_option( get_network()->blog_id, 'auth_multisite_settings', $auth_multisite_settings ); |
| 80 |
} |
| 81 |
} |
| 82 |
// Update version to reflect this change has been made. |
| 83 |
$auth_version = $update_if_older_than; |
| 84 |
$needs_updating = true; |
| 85 |
} |
| 86 |
|
| 87 |
// Update: Set default values for newly added options (forgot to do |
| 88 |
// this, so some users are getting debug log notices about undefined |
| 89 |
// indexes in $auth_settings). |
| 90 |
$update_if_older_than = 20160831; |
| 91 |
if ( false === $auth_version || intval( $auth_version ) < $update_if_older_than ) { |
| 92 |
// Provide default values for any $auth_settings options that don't exist. |
| 93 |
if ( is_multisite() ) { |
| 94 |
// Get all blog ids. |
| 95 |
// phpcs:ignore WordPress.WP.DeprecatedFunctions.wp_get_sitesFound |
| 96 |
$sites = function_exists( 'get_sites' ) ? get_sites() : wp_get_sites( array( 'limit' => PHP_INT_MAX ) ); |
| 97 |
foreach ( $sites as $site ) { |
| 98 |
$blog_id = function_exists( 'get_sites' ) ? $site->blog_id : $site['blog_id']; |
| 99 |
switch_to_blog( $blog_id ); |
| 100 |
// Set meaningful defaults for other sites in the network. |
| 101 |
$options->set_default_options(); |
| 102 |
// Switch back to original blog. |
| 103 |
restore_current_blog(); |
| 104 |
} |
| 105 |
} else { |
| 106 |
// Set meaningful defaults for this site. |
| 107 |
$options->set_default_options(); |
| 108 |
} |
| 109 |
// Update version to reflect this change has been made. |
| 110 |
$auth_version = $update_if_older_than; |
| 111 |
$needs_updating = true; |
| 112 |
} |
| 113 |
|
| 114 |
// Update: Migrate LDAP passwords encrypted with mcrypt since mcrypt is |
| 115 |
// deprecated as of PHP 7.1. Use openssl library instead. |
| 116 |
$update_if_older_than = 20170510; |
| 117 |
if ( false === $auth_version || intval( $auth_version ) < $update_if_older_than ) { |
| 118 |
if ( is_multisite() ) { |
| 119 |
// Reencrypt LDAP passwords in each site in the network. |
| 120 |
// phpcs:ignore WordPress.WP.DeprecatedFunctions.wp_get_sitesFound |
| 121 |
$sites = function_exists( 'get_sites' ) ? get_sites() : wp_get_sites( array( 'limit' => PHP_INT_MAX ) ); |
| 122 |
foreach ( $sites as $site ) { |
| 123 |
$blog_id = function_exists( 'get_sites' ) ? $site->blog_id : $site['blog_id']; |
| 124 |
$auth_settings = get_blog_option( $blog_id, 'auth_settings', array() ); |
| 125 |
if ( array_key_exists( 'ldap_password', $auth_settings ) && strlen( $auth_settings['ldap_password'] ) > 0 ) { |
| 126 |
$plaintext_ldap_password = Helper::decrypt( $auth_settings['ldap_password'], 'mcrypt' ); |
| 127 |
$auth_settings['ldap_password'] = Helper::encrypt( $plaintext_ldap_password ); |
| 128 |
update_blog_option( $blog_id, 'auth_settings', $auth_settings ); |
| 129 |
} |
| 130 |
} |
| 131 |
} else { |
| 132 |
// Reencrypt LDAP password on this single-site install. |
| 133 |
$auth_settings = get_option( 'auth_settings', array() ); |
| 134 |
if ( array_key_exists( 'ldap_password', $auth_settings ) && strlen( $auth_settings['ldap_password'] ) > 0 ) { |
| 135 |
$plaintext_ldap_password = Helper::decrypt( $auth_settings['ldap_password'], 'mcrypt' ); |
| 136 |
$auth_settings['ldap_password'] = Helper::encrypt( $plaintext_ldap_password ); |
| 137 |
update_option( 'auth_settings', $auth_settings ); |
| 138 |
} |
| 139 |
} |
| 140 |
// Update version to reflect this change has been made. |
| 141 |
$auth_version = $update_if_older_than; |
| 142 |
$needs_updating = true; |
| 143 |
} |
| 144 |
|
| 145 |
// Update: Migrate LDAP passwords encrypted with mcrypt since mcrypt is |
| 146 |
// deprecated as of PHP 7.1. Use openssl library instead. |
| 147 |
// Note: Forgot to update the auth_multisite_settings ldap password! Do it here. |
| 148 |
$update_if_older_than = 20170511; |
| 149 |
if ( false === $auth_version || intval( $auth_version ) < $update_if_older_than ) { |
| 150 |
if ( is_multisite() ) { |
| 151 |
// Reencrypt LDAP password in network (multisite) options. |
| 152 |
$auth_multisite_settings = get_blog_option( get_network()->blog_id, 'auth_multisite_settings', array() ); |
| 153 |
if ( array_key_exists( 'ldap_password', $auth_multisite_settings ) && strlen( $auth_multisite_settings['ldap_password'] ) > 0 ) { |
| 154 |
$plaintext_ldap_password = Helper::decrypt( $auth_multisite_settings['ldap_password'], 'mcrypt' ); |
| 155 |
$auth_multisite_settings['ldap_password'] = Helper::encrypt( $plaintext_ldap_password ); |
| 156 |
update_blog_option( get_network()->blog_id, 'auth_multisite_settings', $auth_multisite_settings ); |
| 157 |
} |
| 158 |
} |
| 159 |
// Update version to reflect this change has been made. |
| 160 |
$auth_version = $update_if_older_than; |
| 161 |
$needs_updating = true; |
| 162 |
} |
| 163 |
|
| 164 |
// Update: Remove duplicates from approved list caused by authorizer_automatically_approve_login |
| 165 |
// filter not respecting users who are already in the approved list |
| 166 |
// (causing them to get re-added each time they logged in). |
| 167 |
$update_if_older_than = 20170711; |
| 168 |
if ( false === $auth_version || intval( $auth_version ) < $update_if_older_than ) { |
| 169 |
// Remove duplicates from approved user lists. |
| 170 |
if ( is_multisite() ) { |
| 171 |
// Remove duplicates from each site in the multisite. |
| 172 |
// phpcs:ignore WordPress.WP.DeprecatedFunctions.wp_get_sitesFound |
| 173 |
$sites = function_exists( 'get_sites' ) ? get_sites() : wp_get_sites( array( 'limit' => PHP_INT_MAX ) ); |
| 174 |
foreach ( $sites as $site ) { |
| 175 |
$blog_id = function_exists( 'get_sites' ) ? $site->blog_id : $site['blog_id']; |
| 176 |
$auth_settings_access_users_approved = get_blog_option( $blog_id, 'auth_settings_access_users_approved', array() ); |
| 177 |
if ( is_array( $auth_settings_access_users_approved ) ) { |
| 178 |
$should_update = false; |
| 179 |
$distinct_emails = array(); |
| 180 |
foreach ( $auth_settings_access_users_approved as $key => $user ) { |
| 181 |
if ( in_array( $user['email'], $distinct_emails, true ) ) { |
| 182 |
$should_update = true; |
| 183 |
unset( $auth_settings_access_users_approved[ $key ] ); |
| 184 |
} else { |
| 185 |
$distinct_emails[] = $user['email']; |
| 186 |
} |
| 187 |
} |
| 188 |
if ( $should_update ) { |
| 189 |
update_blog_option( $blog_id, 'auth_settings_access_users_approved', $auth_settings_access_users_approved ); |
| 190 |
} |
| 191 |
} |
| 192 |
} |
| 193 |
// Remove duplicates from multisite approved user list. |
| 194 |
$auth_multisite_settings_access_users_approved = get_blog_option( get_network()->blog_id, 'auth_multisite_settings_access_users_approved', array() ); |
| 195 |
if ( is_array( $auth_multisite_settings_access_users_approved ) ) { |
| 196 |
$should_update = false; |
| 197 |
$distinct_emails = array(); |
| 198 |
foreach ( $auth_multisite_settings_access_users_approved as $key => $user ) { |
| 199 |
if ( in_array( $user['email'], $distinct_emails, true ) ) { |
| 200 |
$should_update = true; |
| 201 |
unset( $auth_multisite_settings_access_users_approved[ $key ] ); |
| 202 |
} else { |
| 203 |
$distinct_emails[] = $user['email']; |
| 204 |
} |
| 205 |
} |
| 206 |
if ( $should_update ) { |
| 207 |
update_blog_option( get_network()->blog_id, 'auth_multisite_settings_access_users_approved', $auth_multisite_settings_access_users_approved ); |
| 208 |
} |
| 209 |
} |
| 210 |
} else { |
| 211 |
// Remove duplicates from single site approved user list. |
| 212 |
$auth_settings_access_users_approved = get_option( 'auth_settings_access_users_approved' ); |
| 213 |
if ( is_array( $auth_settings_access_users_approved ) ) { |
| 214 |
$should_update = false; |
| 215 |
$distinct_emails = array(); |
| 216 |
foreach ( $auth_settings_access_users_approved as $key => $user ) { |
| 217 |
if ( in_array( $user['email'], $distinct_emails, true ) ) { |
| 218 |
$should_update = true; |
| 219 |
unset( $auth_settings_access_users_approved[ $key ] ); |
| 220 |
} else { |
| 221 |
$distinct_emails[] = $user['email']; |
| 222 |
} |
| 223 |
} |
| 224 |
if ( $should_update ) { |
| 225 |
update_option( 'auth_settings_access_users_approved', $auth_settings_access_users_approved ); |
| 226 |
} |
| 227 |
} |
| 228 |
} |
| 229 |
// Update version to reflect this change has been made. |
| 230 |
$auth_version = $update_if_older_than; |
| 231 |
$needs_updating = true; |
| 232 |
} |
| 233 |
|
| 234 |
// Update: Set default value for newly added option advanced_widget_enabled. |
| 235 |
$update_if_older_than = 20171023; |
| 236 |
if ( false === $auth_version || intval( $auth_version ) < $update_if_older_than ) { |
| 237 |
// Provide default values for any $auth_settings options that don't exist. |
| 238 |
if ( is_multisite() ) { |
| 239 |
// phpcs:ignore WordPress.WP.DeprecatedFunctions.wp_get_sitesFound |
| 240 |
$sites = function_exists( 'get_sites' ) ? get_sites() : wp_get_sites( array( 'limit' => PHP_INT_MAX ) ); |
| 241 |
foreach ( $sites as $site ) { |
| 242 |
$blog_id = function_exists( 'get_sites' ) ? $site->blog_id : $site['blog_id']; |
| 243 |
switch_to_blog( $blog_id ); |
| 244 |
$options->set_default_options(); |
| 245 |
restore_current_blog(); |
| 246 |
} |
| 247 |
} else { |
| 248 |
$options->set_default_options(); |
| 249 |
} |
| 250 |
// Update version to reflect this change has been made. |
| 251 |
$auth_version = $update_if_older_than; |
| 252 |
$needs_updating = true; |
| 253 |
} |
| 254 |
|
| 255 |
// Update: Set default value for newly added option advanced_users_per_page. |
| 256 |
$update_if_older_than = 20171215; |
| 257 |
if ( false === $auth_version || intval( $auth_version ) < $update_if_older_than ) { |
| 258 |
// Provide default values for any $auth_settings options that don't exist. |
| 259 |
if ( is_multisite() ) { |
| 260 |
// phpcs:ignore WordPress.WP.DeprecatedFunctions.wp_get_sitesFound |
| 261 |
$sites = function_exists( 'get_sites' ) ? get_sites() : wp_get_sites( array( 'limit' => PHP_INT_MAX ) ); |
| 262 |
foreach ( $sites as $site ) { |
| 263 |
$blog_id = function_exists( 'get_sites' ) ? $site->blog_id : $site['blog_id']; |
| 264 |
switch_to_blog( $blog_id ); |
| 265 |
$options->set_default_options(); |
| 266 |
restore_current_blog(); |
| 267 |
} |
| 268 |
} else { |
| 269 |
$options->set_default_options(); |
| 270 |
} |
| 271 |
// Update version to reflect this change has been made. |
| 272 |
$auth_version = $update_if_older_than; |
| 273 |
$needs_updating = true; |
| 274 |
} |
| 275 |
|
| 276 |
// Update: Set default value for newly added options advanced_users_sort_by and advanced_users_sort_order. |
| 277 |
$update_if_older_than = 20171219; |
| 278 |
if ( false === $auth_version || intval( $auth_version ) < $update_if_older_than ) { |
| 279 |
// Provide default values for any $auth_settings options that don't exist. |
| 280 |
if ( is_multisite() ) { |
| 281 |
// phpcs:ignore WordPress.WP.DeprecatedFunctions.wp_get_sitesFound |
| 282 |
$sites = function_exists( 'get_sites' ) ? get_sites() : wp_get_sites( array( 'limit' => PHP_INT_MAX ) ); |
| 283 |
foreach ( $sites as $site ) { |
| 284 |
$blog_id = function_exists( 'get_sites' ) ? $site->blog_id : $site['blog_id']; |
| 285 |
switch_to_blog( $blog_id ); |
| 286 |
$options->set_default_options(); |
| 287 |
restore_current_blog(); |
| 288 |
} |
| 289 |
} else { |
| 290 |
$options->set_default_options(); |
| 291 |
} |
| 292 |
// Update version to reflect this change has been made. |
| 293 |
$auth_version = $update_if_older_than; |
| 294 |
$needs_updating = true; |
| 295 |
} |
| 296 |
|
| 297 |
// Update: Set default value for newly added option cas_link_on_username. |
| 298 |
$update_if_older_than = 20190227; |
| 299 |
if ( false === $auth_version || intval( $auth_version ) < $update_if_older_than ) { |
| 300 |
// Provide default values for any $auth_settings options that don't exist. |
| 301 |
if ( is_multisite() ) { |
| 302 |
// phpcs:ignore WordPress.WP.DeprecatedFunctions.wp_get_sitesFound |
| 303 |
$sites = function_exists( 'get_sites' ) ? get_sites() : wp_get_sites( array( 'limit' => PHP_INT_MAX ) ); |
| 304 |
foreach ( $sites as $site ) { |
| 305 |
$blog_id = function_exists( 'get_sites' ) ? $site->blog_id : $site['blog_id']; |
| 306 |
switch_to_blog( $blog_id ); |
| 307 |
$options->set_default_options(); |
| 308 |
restore_current_blog(); |
| 309 |
} |
| 310 |
} else { |
| 311 |
$options->set_default_options(); |
| 312 |
} |
| 313 |
// Update version to reflect this change has been made. |
| 314 |
$auth_version = $update_if_older_than; |
| 315 |
$needs_updating = true; |
| 316 |
} |
| 317 |
|
| 318 |
/* phpcs:ignore Squiz.PHP.CommentedOutCode.Found |
| 319 |
// Update: TEMPLATE |
| 320 |
$update_if_older_than = YYYYMMDD; |
| 321 |
if ( $auth_version === false || intval( $auth_version ) < $update_if_older_than ) { |
| 322 |
////// PLACE UPDATE CODE HERE |
| 323 |
// Update version to reflect this change has been made. |
| 324 |
$auth_version = $update_if_older_than; |
| 325 |
$needs_updating = true; |
| 326 |
} |
| 327 |
*/ |
| 328 |
|
| 329 |
// Save new version number if we performed any updates. |
| 330 |
if ( $needs_updating ) { |
| 331 |
if ( is_multisite() ) { |
| 332 |
// phpcs:ignore WordPress.WP.DeprecatedFunctions.wp_get_sitesFound |
| 333 |
$sites = function_exists( 'get_sites' ) ? get_sites() : wp_get_sites( array( 'limit' => PHP_INT_MAX ) ); |
| 334 |
foreach ( $sites as $site ) { |
| 335 |
$blog_id = function_exists( 'get_sites' ) ? $site->blog_id : $site['blog_id']; |
| 336 |
update_blog_option( $blog_id, 'auth_version', $auth_version ); |
| 337 |
} |
| 338 |
} else { |
| 339 |
update_option( 'auth_version', $auth_version ); |
| 340 |
} |
| 341 |
} |
| 342 |
} |
| 343 |
|
| 344 |
} |
| 345 |
|