| 1 |
<?php |
| 2 |
// @codingStandardsIgnoreFile |
| 3 |
/** |
| 4 |
* User securities. |
| 5 |
* |
| 6 |
* Copyright: © 2009-2011 |
| 7 |
* {@link http://websharks-inc.com/ WebSharks, Inc.} |
| 8 |
* (coded in the USA) |
| 9 |
* |
| 10 |
* Released under the terms of the GNU General Public License. |
| 11 |
* You should have received a copy of the GNU General Public License, |
| 12 |
* along with this software. In the main directory, see: /licensing/ |
| 13 |
* If not, see: {@link http://www.gnu.org/licenses/}. |
| 14 |
* |
| 15 |
* @package s2Member\User_Securities |
| 16 |
* @since 3.5 |
| 17 |
*/ |
| 18 |
if(!defined('WPINC')) // MUST have WordPress. |
| 19 |
exit ('Do not access this file directly.'); |
| 20 |
|
| 21 |
if(!class_exists('c_ws_plugin__s2member_user_securities')) |
| 22 |
{ |
| 23 |
/** |
| 24 |
* User securities. |
| 25 |
* |
| 26 |
* @package s2Member\User_Securities |
| 27 |
* @since 3.5 |
| 28 |
*/ |
| 29 |
class c_ws_plugin__s2member_user_securities |
| 30 |
{ |
| 31 |
/** |
| 32 |
* Initializes Filter for `user_has_cap`. |
| 33 |
* |
| 34 |
* It's very important that this is NOT attached before WordPress creates `$current_user` via `$wp->init()`. |
| 35 |
* This prevents crashes when other plugins attempt to call upon `current_user_can()` before WordPress is initialized. |
| 36 |
* For instance, some plugins attempt to use `current_user_can()` on the `plugins_loaded` Hook, which they should not do. |
| 37 |
* |
| 38 |
* @package s2Member\User_Securities |
| 39 |
* @since 3.5 |
| 40 |
* |
| 41 |
* @attaches-to ``add_action('init');`` |
| 42 |
*/ |
| 43 |
public static function initialize() // Initializes the Filter for `user_has_cap`. |
| 44 |
{ |
| 45 |
add_filter('user_has_cap', 'c_ws_plugin__s2member_user_securities::user_capabilities', 10, 3); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Deals w/ bbPress-specific issues on a Multisite Network. |
| 50 |
* |
| 51 |
* @package s2Member\User_Securities |
| 52 |
* @since 150224 |
| 53 |
* |
| 54 |
* @attaches-to ``add_action('after_setup_theme');`` |
| 55 |
*/ |
| 56 |
public static function set_current_user() |
| 57 |
{ |
| 58 |
if(is_multisite() && is_user_logged_in() && !current_user_can('read')) |
| 59 |
remove_action('bbp_setup_current_user', 'bbp_set_current_user_default_role'); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Alters `WP_User->has_cap()` in special cases for Administrators. |
| 64 |
* |
| 65 |
* @package s2Member\User_Securities |
| 66 |
* @since 110815 |
| 67 |
* |
| 68 |
* @attaches-to ``add_filter('user_has_cap');`` |
| 69 |
* |
| 70 |
* @param array $capabilities Expects an array of Capabilities passed in by the Filter. |
| 71 |
* This array contains all of the Capabilities that the User has *( i.e., ``$user->allcaps`` )*. |
| 72 |
* @param array $caps_map An array of Capabilities mapped out by the ``map_meta_cap`` function. |
| 73 |
* @param array $args Array of arguments originally passed through the ``has_cap()`` function. |
| 74 |
* However, WordPress modifies this array of arguments in the following way. |
| 75 |
* Argument `[0]` is the Capability test string itself *(this is normal)*. |
| 76 |
* Argument `[1]` is added by WordPress; it's the ID of the User. |
| 77 |
* Other arguments starting from array index `[2]` are normal. |
| 78 |
* |
| 79 |
* @return array An array of Capabilities. |
| 80 |
*/ |
| 81 |
public static function user_capabilities($capabilities, $caps_map, $args) |
| 82 |
{ |
| 83 |
$_hook = 'ws_plugin__s2member_before_user_capabilities'; if (isset($GLOBALS['wp_filter'][$_hook]) || isset($GLOBALS['wp_filter']['all'])) |
| 84 |
{ foreach(array_keys(get_defined_vars()) as $__v) $__refs[$__v] =& $$__v; do_action($_hook, get_defined_vars()); } unset($_hook, $__refs, $__v); //260901 Vars by reference. |
| 85 |
|
| 86 |
if(!empty($capabilities['access_s2member_ccap_all_ccaps']) && !empty($args[0]) && preg_match('/^access_s2member_ccap_/i', $args[0]) && apply_filters('ws_plugin__s2member_all_ccaps_enable', TRUE, get_defined_vars())) |
| 87 |
$capabilities = array_merge((array)$capabilities, array($args[0] => 1)); |
| 88 |
|
| 89 |
else if(!is_multisite() && !empty($capabilities['administrator']) && !empty($args[0]) && preg_match('/^access_s2member_ccap_/i', $args[0]) && apply_filters('ws_plugin__s2member_admins_have_all_ccaps', TRUE, get_defined_vars())) |
| 90 |
$capabilities = array_merge((array)$capabilities, array($args[0] => 1)); |
| 91 |
|
| 92 |
else if(is_multisite() && c_ws_plugin__s2member_utils_conds::is_multisite_farm() && (is_super_admin() || !empty($capabilities['administrator'])) && !empty($args[0]) && ($args[0] === 'edit_user' || $args[0] === 'edit_users')) |
| 93 |
if($args[0] === 'edit_users' || ($args[0] === 'edit_user' && !empty($args[2]) && ((!empty($args[1]) && (int)$args[1] === (int)$args[2]) || is_user_member_of_blog($args[2])))) |
| 94 |
$capabilities = array_merge((array)$capabilities, array('edit_users' => 1)); |
| 95 |
|
| 96 |
$_hook = 'ws_plugin__s2member_user_capabilities'; if (isset($GLOBALS['wp_filter'][$_hook]) || isset($GLOBALS['wp_filter']['all'])) |
| 97 |
$capabilities = apply_filters($_hook, $capabilities, get_defined_vars()); unset($_hook); //260901 Defined vars. |
| 98 |
return $capabilities; |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Alters this Filter inside `/wp-admin/user-edit.php`. |
| 103 |
* |
| 104 |
* @package s2Member\User_Securities |
| 105 |
* @since 3.5 |
| 106 |
* |
| 107 |
* @attaches-to ``add_filter('enable_edit_any_user_configuration');`` |
| 108 |
* |
| 109 |
* @param bool $allow Expects boolean value passed through by the Filter. |
| 110 |
* |
| 111 |
* @return bool True if the current User is allowed to edit any User, else existing value. |
| 112 |
*/ |
| 113 |
public static function ms_allow_edits($allow = FALSE) |
| 114 |
{ |
| 115 |
global $user_id; // Available inside `/wp-admin/user-edit.php`. |
| 116 |
|
| 117 |
foreach(array_keys(get_defined_vars()) as $__v) $__refs[$__v] =& $$__v; |
| 118 |
do_action('ws_plugin__s2member_before_ms_allow_edits', get_defined_vars()); |
| 119 |
unset($__refs, $__v); |
| 120 |
|
| 121 |
if(is_multisite() && c_ws_plugin__s2member_utils_conds::is_multisite_farm()) |
| 122 |
if(is_super_admin() || (current_user_can('administrator') && $user_id && is_user_member_of_blog($user_id))) |
| 123 |
$allow = TRUE; // Yes, allow Administrators to edit User Profiles. |
| 124 |
|
| 125 |
return apply_filters('ws_plugin__s2member_ms_allow_edits', $allow, get_defined_vars()); |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Hides Password fields for Demo Users; and deals with Password fields on Multisite Blog Farms. |
| 130 |
* |
| 131 |
* Demo accounts *( where the Username MUST be 'demo' )*, will NOT be allowed to change their Password. |
| 132 |
* Any other restrictions you need to impose must be done through custom programming, using s2Member's Conditionals. |
| 133 |
* See `s2Member → API Scripting`. |
| 134 |
* |
| 135 |
* @package s2Member\User_Securities |
| 136 |
* @since 3.5 |
| 137 |
* |
| 138 |
* @attaches-to ``add_filter('show_password_fields');`` |
| 139 |
* |
| 140 |
* @param bool $show Expects boolean value passed through by the Filter. |
| 141 |
* @param WP_User $user Expects a `WP_User` object passed through by the Filter. |
| 142 |
* If this is NOT passed (it isn't always), then we assume the current user. |
| 143 |
* |
| 144 |
* @return bool False if the Password is locked for this User, else existing value. |
| 145 |
*/ |
| 146 |
public static function hide_password_fields($show, $user = NULL) |
| 147 |
{ |
| 148 |
foreach(array_keys(get_defined_vars()) as $__v) $__refs[$__v] =& $$__v; |
| 149 |
do_action('ws_plugin__s2member_before_hide_password_fields', get_defined_vars()); |
| 150 |
unset($__refs, $__v); |
| 151 |
|
| 152 |
if($show && is_multisite() && c_ws_plugin__s2member_utils_conds::is_multisite_farm()) |
| 153 |
if(!is_super_admin() && is_object($user) && !empty($user->ID) && is_object($current_user = wp_get_current_user()) && !empty($current_user->ID)) |
| 154 |
if($user->ID !== $current_user->ID) |
| 155 |
$show = FALSE; |
| 156 |
|
| 157 |
if($show && is_object($user) && !empty($user->ID) && $user->user_login === 'demo') |
| 158 |
$show = FALSE; // Lock Password on Demos. |
| 159 |
|
| 160 |
return apply_filters('ws_plugin__s2member_hide_password_fields', $show, get_defined_vars()); |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* Acquires password minimum length. |
| 165 |
* |
| 166 |
* @package s2Member\User_Securities |
| 167 |
* @since 150717 |
| 168 |
* |
| 169 |
* @param string $password The password to score. |
| 170 |
* |
| 171 |
* @return integer Password minimum length. |
| 172 |
*/ |
| 173 |
public static function min_password_length() |
| 174 |
{ |
| 175 |
$min = (int)$GLOBALS['WS_PLUGIN__']['s2member']['o']['custom_reg_password_min_length']; |
| 176 |
return max(6, (int)apply_filters('ws_plugin__s2member_min_password_length', $min > 0 ? $min : 0)); |
| 177 |
} |
| 178 |
|
| 179 |
/** |
| 180 |
* Acquires minimum password strength code. |
| 181 |
* |
| 182 |
* @package s2Member\User_Securities |
| 183 |
* @since 150717 |
| 184 |
* |
| 185 |
* @return string Minimum password strength code. |
| 186 |
*/ |
| 187 |
public static function min_password_strength_code() |
| 188 |
{ |
| 189 |
$code = $GLOBALS['WS_PLUGIN__']['s2member']['o']['custom_reg_password_min_strength']; |
| 190 |
return apply_filters('ws_plugin__s2member_min_password_strength_code', trim($code)); |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* Acquires minimum password strength label. |
| 195 |
* |
| 196 |
* @package s2Member\User_Securities |
| 197 |
* @since 150717 |
| 198 |
* |
| 199 |
* @return string Minimum password strength label. |
| 200 |
*/ |
| 201 |
public static function min_password_strength_label() |
| 202 |
{ |
| 203 |
switch(self::min_password_strength_code()) |
| 204 |
{ |
| 205 |
case 'weak': return _x('`weak`, `good`, or `strong`', 's2member-front', 's2member'); |
| 206 |
case 'good': return _x('`good` or `strong` (i.e., use numbers, letters, and mixed caSe)', 's2member-front', 's2member'); |
| 207 |
case 'strong': return _x('`strong` (i.e., use numbers, letters, mixed caSe, and punctuation)', 's2member-front', 's2member'); |
| 208 |
} |
| 209 |
return ''; // Default behavior. |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* Acquires minimum password strength score. |
| 214 |
* |
| 215 |
* @package s2Member\User_Securities |
| 216 |
* @since 150717 |
| 217 |
* |
| 218 |
* @return integer Minimum password strength score. |
| 219 |
*/ |
| 220 |
public static function min_password_strength_score() |
| 221 |
{ |
| 222 |
$score = 0; // Default behavior. |
| 223 |
|
| 224 |
switch(self::min_password_strength_code()) |
| 225 |
{ |
| 226 |
case 'n/a': $score = 0; break; |
| 227 |
case 'weak': $score = 10; break; |
| 228 |
case 'good': $score = 30; break; |
| 229 |
case 'strong': $score = 50; break; |
| 230 |
} |
| 231 |
return apply_filters('ws_plugin__s2member_min_password_strength_score', $score > 0 ? $score : 0); |
| 232 |
} |
| 233 |
|
| 234 |
/** |
| 235 |
* Acquires password strength score. |
| 236 |
* |
| 237 |
* @package s2Member\User_Securities |
| 238 |
* @since 150717 |
| 239 |
* |
| 240 |
* @param string $password The password to score. |
| 241 |
* |
| 242 |
* @return integer Password strength score. |
| 243 |
*/ |
| 244 |
public static function password_strength_score($password) |
| 245 |
{ |
| 246 |
$score = 0; // Initialize score. |
| 247 |
|
| 248 |
if(strlen($password) < 1) |
| 249 |
return $score; |
| 250 |
|
| 251 |
else if(strlen($password) < self::min_password_length()) |
| 252 |
return $score; |
| 253 |
|
| 254 |
if(preg_match('/[0-9]/', $password)) |
| 255 |
$score += 10; |
| 256 |
|
| 257 |
if(preg_match('/[a-z]/', $password)) |
| 258 |
$score += 10; |
| 259 |
|
| 260 |
if(preg_match('/[A-Z]/', $password)) |
| 261 |
$score += 10; |
| 262 |
|
| 263 |
if(preg_match('/[^0-9a-zA-Z]/', $password)) |
| 264 |
$score += $score === 30 ? 20 : 10; |
| 265 |
|
| 266 |
return apply_filters('ws_plugin__s2member_password_strength_score', $score > 0 ? $score : 0); |
| 267 |
} |
| 268 |
} |
| 269 |
} |
| 270 |
|