PluginProbe
s2Member – Excellent for All Kinds of Memberships, Content Restriction Paywalls & Member Access Subscriptions / 260805
s2Member – Excellent for All Kinds of Memberships, Content Restriction Paywalls & Member Access Subscriptions v260805
260917 260913 260909 260829 260814 260805 110710 110731 110812 110815 110912 110913 110915 110926 110927 111002 111003 111011 111017 111029 111105 111206 111216 111220 120213 All 189 releases
s2member / src / includes / classes / user-securities.inc.php

user-securities.inc.php in s2Member – Excellent for All Kinds of Memberships, Content Restriction Paywalls & Member Access Subscriptions 260805, at src/includes/classes/user-securities.inc.php

269 lines 9.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 foreach(array_keys(get_defined_vars()) as $__v) $__refs[$__v] =& $$__v;
84 do_action('ws_plugin__s2member_before_user_capabilities', get_defined_vars());
85 unset($__refs, $__v);
86
87 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()))
88 $capabilities = array_merge((array)$capabilities, array($args[0] => 1));
89
90 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()))
91 $capabilities = array_merge((array)$capabilities, array($args[0] => 1));
92
93 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'))
94 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]))))
95 $capabilities = array_merge((array)$capabilities, array('edit_users' => 1));
96
97 return apply_filters('ws_plugin__s2member_user_capabilities', $capabilities, get_defined_vars());
98 }
99
100 /**
101 * Alters this Filter inside `/wp-admin/user-edit.php`.
102 *
103 * @package s2Member\User_Securities
104 * @since 3.5
105 *
106 * @attaches-to ``add_filter('enable_edit_any_user_configuration');``
107 *
108 * @param bool $allow Expects boolean value passed through by the Filter.
109 *
110 * @return bool True if the current User is allowed to edit any User, else existing value.
111 */
112 public static function ms_allow_edits($allow = FALSE)
113 {
114 global $user_id; // Available inside `/wp-admin/user-edit.php`.
115
116 foreach(array_keys(get_defined_vars()) as $__v) $__refs[$__v] =& $$__v;
117 do_action('ws_plugin__s2member_before_ms_allow_edits', get_defined_vars());
118 unset($__refs, $__v);
119
120 if(is_multisite() && c_ws_plugin__s2member_utils_conds::is_multisite_farm())
121 if(is_super_admin() || (current_user_can('administrator') && $user_id && is_user_member_of_blog($user_id)))
122 $allow = TRUE; // Yes, allow Administrators to edit User Profiles.
123
124 return apply_filters('ws_plugin__s2member_ms_allow_edits', $allow, get_defined_vars());
125 }
126
127 /**
128 * Hides Password fields for Demo Users; and deals with Password fields on Multisite Blog Farms.
129 *
130 * Demo accounts *( where the Username MUST be 'demo' )*, will NOT be allowed to change their Password.
131 * Any other restrictions you need to impose must be done through custom programming, using s2Member's Conditionals.
132 * See `s2Member → API Scripting`.
133 *
134 * @package s2Member\User_Securities
135 * @since 3.5
136 *
137 * @attaches-to ``add_filter('show_password_fields');``
138 *
139 * @param bool $show Expects boolean value passed through by the Filter.
140 * @param WP_User $user Expects a `WP_User` object passed through by the Filter.
141 * If this is NOT passed (it isn't always), then we assume the current user.
142 *
143 * @return bool False if the Password is locked for this User, else existing value.
144 */
145 public static function hide_password_fields($show, $user = NULL)
146 {
147 foreach(array_keys(get_defined_vars()) as $__v) $__refs[$__v] =& $$__v;
148 do_action('ws_plugin__s2member_before_hide_password_fields', get_defined_vars());
149 unset($__refs, $__v);
150
151 if($show && is_multisite() && c_ws_plugin__s2member_utils_conds::is_multisite_farm())
152 if(!is_super_admin() && is_object($user) && !empty($user->ID) && is_object($current_user = wp_get_current_user()) && !empty($current_user->ID))
153 if($user->ID !== $current_user->ID)
154 $show = FALSE;
155
156 if($show && is_object($user) && !empty($user->ID) && $user->user_login === 'demo')
157 $show = FALSE; // Lock Password on Demos.
158
159 return apply_filters('ws_plugin__s2member_hide_password_fields', $show, get_defined_vars());
160 }
161
162 /**
163 * Acquires password minimum length.
164 *
165 * @package s2Member\User_Securities
166 * @since 150717
167 *
168 * @param string $password The password to score.
169 *
170 * @return integer Password minimum length.
171 */
172 public static function min_password_length()
173 {
174 $min = (integer)$GLOBALS['WS_PLUGIN__']['s2member']['o']['custom_reg_password_min_length'];
175 return max(6, (integer)apply_filters('ws_plugin__s2member_min_password_length', $min > 0 ? $min : 0));
176 }
177
178 /**
179 * Acquires minimum password strength code.
180 *
181 * @package s2Member\User_Securities
182 * @since 150717
183 *
184 * @return string Minimum password strength code.
185 */
186 public static function min_password_strength_code()
187 {
188 $code = $GLOBALS['WS_PLUGIN__']['s2member']['o']['custom_reg_password_min_strength'];
189 return apply_filters('ws_plugin__s2member_min_password_strength_code', trim($code));
190 }
191
192 /**
193 * Acquires minimum password strength label.
194 *
195 * @package s2Member\User_Securities
196 * @since 150717
197 *
198 * @return string Minimum password strength label.
199 */
200 public static function min_password_strength_label()
201 {
202 switch(self::min_password_strength_code())
203 {
204 case 'weak': return _x('`weak`, `good`, or `strong`', 's2member-front', 's2member');
205 case 'good': return _x('`good` or `strong` (i.e., use numbers, letters, and mixed caSe)', 's2member-front', 's2member');
206 case 'strong': return _x('`strong` (i.e., use numbers, letters, mixed caSe, and punctuation)', 's2member-front', 's2member');
207 }
208 return ''; // Default behavior.
209 }
210
211 /**
212 * Acquires minimum password strength score.
213 *
214 * @package s2Member\User_Securities
215 * @since 150717
216 *
217 * @return integer Minimum password strength score.
218 */
219 public static function min_password_strength_score()
220 {
221 $score = 0; // Default behavior.
222
223 switch(self::min_password_strength_code())
224 {
225 case 'n/a': $score = 0; break;
226 case 'weak': $score = 10; break;
227 case 'good': $score = 30; break;
228 case 'strong': $score = 50; break;
229 }
230 return apply_filters('ws_plugin__s2member_min_password_strength_score', $score > 0 ? $score : 0);
231 }
232
233 /**
234 * Acquires password strength score.
235 *
236 * @package s2Member\User_Securities
237 * @since 150717
238 *
239 * @param string $password The password to score.
240 *
241 * @return integer Password strength score.
242 */
243 public static function password_strength_score($password)
244 {
245 $score = 0; // Initialize score.
246
247 if(strlen($password) < 1)
248 return $score;
249
250 else if(strlen($password) < self::min_password_length())
251 return $score;
252
253 if(preg_match('/[0-9]/', $password))
254 $score += 10;
255
256 if(preg_match('/[a-z]/', $password))
257 $score += 10;
258
259 if(preg_match('/[A-Z]/', $password))
260 $score += 10;
261
262 if(preg_match('/[^0-9a-zA-Z]/', $password))
263 $score += $score === 30 ? 20 : 10;
264
265 return apply_filters('ws_plugin__s2member_password_strength_score', $score > 0 ? $score : 0);
266 }
267 }
268 }
269