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

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

302 lines 17.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 // @codingStandardsIgnoreFile
3 /**
4 * IP Restrictions.
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\IP_Restrictions
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_ip_restrictions"))
22 {
23 /**
24 * IP Restrictions.
25 *
26 * @package s2Member\IP_Restrictions
27 * @since 3.5
28 */
29 class c_ws_plugin__s2member_ip_restrictions
30 {
31 /**
32 * Handles IP Restrictions.
33 *
34 * IP address details are stored as Transients.
35 *
36 * @package s2Member\IP_Restrictions
37 * @since 3.5
38 *
39 * @param string $ip IP Address.
40 * @param string $restriction Unique IP Restriction name/identifier. Such as a Username, or a unique access code.
41 * @return bool True if IP Restrictions are OK, otherwise this function will exit script execution after issuing a warning.
42 */
43 public static function ip_restrictions_ok($ip = FALSE, $restriction = FALSE)
44 {
45 foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;
46 do_action("ws_plugin__s2member_before_ip_restrictions_ok", get_defined_vars());
47 unset($__refs, $__v);
48
49 if(!apply_filters("ws_plugin__s2member_disable_all_ip_restrictions", false, get_defined_vars())
50 /* Also allow specific exclusions here. */ && !apply_filters("ws_plugin__s2member_disable_specific_ip_restriction", false, get_defined_vars())
51 /* And enabled by site owner? */ && $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["max_ip_restriction"] /* And a valid ``$restriction``? */ && $restriction && is_string($restriction)
52 /* Flag indicating that we ARE processing this IP Restriction. Useful in Hooks/Filters. */ && ($processing = true))
53 {
54 $prefix = /* s2Member Transient prefix for all IP Restrictions. */ "s2m_ipr_";
55 $transient_entries = $prefix.md5("s2member_ip_restrictions_".$restriction."_entries");
56 $transient_security_breach = $prefix.md5("s2member_ip_restrictions_".$restriction."_security_breach");
57
58 // If you add Filters, use a string compatible with PHP's strtotime() function.
59 $concurrency = apply_filters("ws_plugin__s2member_ip_restrictions__concurrency_time_per_ip", "30 days");
60
61 $entries = (is_array($entries = get_transient($transient_entries))) ? $entries : array();
62
63 foreach /* Auto-expire entries, based on time. */($entries as $_entry => $_time)
64 if /* Based on time. */($_time < strtotime("-".$concurrency))
65 unset /* Unset this entry value. */($entries[$_entry]);
66
67 $ip = ($ip && is_string($ip)) ? $ip : /* Allow empty IPs. */ "empty";
68 $entries[$ip] = /* Log entry. Add IP with entry time. */ strtotime("now");
69 set_transient($transient_entries, $entries, 2 * (strtotime("+".$concurrency) - strtotime("now")));
70
71 if /* Already breached security? */(get_transient($transient_security_breach))
72 {
73 c_ws_plugin__s2member_no_cache::no_cache_constants(true);
74
75 status_header /* Send a 503 error status header; temporarily unavailable. */(503);
76 wp_clear_auth_cookie /* Clear authorization cookies; we need to log them out now. */();
77 header /* Content-Type text/html with UTF-8. */("Content-Type: text/html; charset=UTF-8");
78 while (@ob_end_clean ()); // Clean any existing output buffers.
79
80 $custom_template = (is_file (TEMPLATEPATH . "/" . "ip-restrictions.php")) ? TEMPLATEPATH . "/" . "ip-restrictions.php" : '';
81 $custom_template = (is_file (get_stylesheet_directory() . "/" . "ip-restrictions.php")) ? get_stylesheet_directory() . "/" . "ip-restrictions.php" : $custom_template;
82 $custom_template = (is_file (WP_CONTENT_DIR . "/" . "ip-restrictions.php")) ? WP_CONTENT_DIR . "/" . "ip-restrictions.php" : $custom_template;
83
84 $msg_503 = trim(file_get_contents((($custom_template) ? $custom_template : dirname(dirname(__FILE__))."/templates/errors/ip-restrictions.php")));
85 $msg_503 = trim(((!$custom_template || !is_multisite() || !c_ws_plugin__s2member_utils_conds::is_multisite_farm() || is_main_site()) ? c_ws_plugin__s2member_utilities::evl($msg_503) : $msg_503));
86
87 foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;
88 do_action("ws_plugin__s2member_during_ip_restrictions_ok_no", get_defined_vars());
89 unset($__refs, $__v);
90
91 exit /* Clean exit with 503 error message. */($msg_503);
92 }
93
94 else if(count($entries) > $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["max_ip_restriction"])
95 {
96 c_ws_plugin__s2member_no_cache::no_cache_constants(true);
97
98 set_transient // A security breach has just occurred. We need to set this Transient now.
99 ($transient_security_breach, 1, $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["max_ip_restriction_time"]);
100
101 status_header /* Send a 503 error status header; temporarily unavailable. */(503);
102 wp_clear_auth_cookie /* Clear authorization cookies; we need to log them out now. */();
103 header /* Content-Type text/html with UTF-8. */("Content-Type: text/html; charset=UTF-8");
104 while (@ob_end_clean ()); // Clean any existing output buffers.
105
106 $custom_template = (is_file (TEMPLATEPATH . "/" . "ip-restrictions.php")) ? TEMPLATEPATH . "/" . "ip-restrictions.php" : '';
107 $custom_template = (is_file (get_stylesheet_directory() . "/" . "ip-restrictions.php")) ? get_stylesheet_directory() . "/" . "ip-restrictions.php" : $custom_template;
108 $custom_template = (is_file (WP_CONTENT_DIR . "/" . "ip-restrictions.php")) ? WP_CONTENT_DIR . "/" . "ip-restrictions.php" : $custom_template;
109
110 $msg_503 = trim(file_get_contents((($custom_template) ? $custom_template : dirname(dirname(__FILE__))."/templates/errors/ip-restrictions.php")));
111 $msg_503 = trim(((!$custom_template || !is_multisite() || !c_ws_plugin__s2member_utils_conds::is_multisite_farm() || is_main_site()) ? c_ws_plugin__s2member_utilities::evl($msg_503) : $msg_503));
112
113 foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;
114 do_action("ws_plugin__s2member_during_ip_restrictions_ok_no", get_defined_vars());
115 unset($__refs, $__v);
116
117 exit /* Clean exit with 503 error message. */($msg_503);
118 }
119 else // OK, this looks legitimate. Apply Filters here and return true.
120 {
121 foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;
122 do_action("ws_plugin__s2member_during_ip_restrictions_ok_yes", get_defined_vars());
123 unset($__refs, $__v);
124
125 return apply_filters("ws_plugin__s2member_ip_restrictions_ok", true, get_defined_vars());
126 }
127 }
128 foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;
129 do_action("ws_plugin__s2member_during_ip_restrictions_ok_yes", get_defined_vars());
130 unset($__refs, $__v);
131
132 return apply_filters("ws_plugin__s2member_ip_restrictions_ok", true, get_defined_vars());
133 }
134 /**
135 * Queries Transients for specific IP Restrictions at or above max allowable.
136 *
137 * @package s2Member\IP_Restrictions
138 * @since 130407
139 *
140 * @param string $restriction Unique IP Restriction name/identifier. Such as Username, or a unique access code.
141 * @return bool TRUE if at or above max allowable IPs; else FALSE.
142 */
143 public static function specific_ip_restriction_at_or_above_max($restriction = FALSE)
144 {
145 do_action("ws_plugin__s2member_before_specific_ip_restriction_at_or_above_max", get_defined_vars());
146
147 if(apply_filters("ws_plugin__s2member_disable_all_ip_restrictions", false, get_defined_vars())
148 || apply_filters("ws_plugin__s2member_disable_specific_ip_restriction", false, get_defined_vars())
149 || !$GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["max_ip_restriction"])
150 return false; // No IP Restrictions in this case.
151
152 $prefix = /* s2Member Transient prefix for all IP Restrictions. Allows s2Member to find these easily. */ "s2m_ipr_";
153 $transient_entries = $prefix.md5("s2member_ip_restrictions_".(string)$restriction."_entries");
154
155 // If you add Filters, use a string compatible with PHP's strtotime() function.
156 $concurrency = apply_filters("ws_plugin__s2member_ip_restrictions__concurrency_time_per_ip", "30 days");
157
158 $entries = (is_array($entries = get_transient($transient_entries))) ? $entries : array();
159
160 foreach /* Auto-expire entries, based on time. */($entries as $_entry => $_time)
161 if /* Based on time. */($_time < strtotime("-".$concurrency))
162 unset /* Unset this entry value. */($entries[$_entry]);
163
164 $at_or_above_max = (count($entries) >= $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["max_ip_restriction"]);
165
166 return apply_filters("ws_plugin__s2member_specific_ip_restriction_at_or_above_max", $at_or_above_max, get_defined_vars());
167 }
168 /**
169 * Queries Transients for specific IP Restrictions associated with a security breach.
170 *
171 * @package s2Member\IP_Restrictions
172 * @since 3.5
173 *
174 * @param string $restriction Unique IP Restriction name/identifier. Such as a Username, or a unique access code.
175 * @return bool True if the specific IP Restriction is associated with a security breach, else false.
176 */
177 public static function specific_ip_restriction_breached_security($restriction = FALSE)
178 {
179 do_action("ws_plugin__s2member_before_specific_ip_restriction_breached_security", get_defined_vars());
180
181 $prefix = /* s2Member Transient prefix for all IP Restrictions. */ "s2m_ipr_";
182 $transient_security_breach = $prefix.md5("s2member_ip_restrictions_".(string)$restriction."_security_breach");
183 $breached_security = $associated_with_security_breach = (get_transient($transient_security_breach)) ? true : false;
184
185 return apply_filters("ws_plugin__s2member_specific_ip_restriction_breached_security", $breached_security, get_defined_vars());
186 }
187 /**
188 * Resets/deletes specific IP Restrictions.
189 *
190 * @package s2Member\IP_Restrictions
191 * @since 3.5
192 *
193 * @param string $restriction Unique IP Restriction name/identifier. Such as a Username, or a unique access code.
194 * @return bool Always returns a `true` value.
195 *
196 * @todo Make return value conditional, based on success.
197 */
198 public static function delete_reset_specific_ip_restrictions($restriction = FALSE)
199 {
200 global /* Need global database object. */ $wpdb;
201
202 do_action("ws_plugin__s2member_before_delete_reset_specific_ip_restrictions", get_defined_vars());
203
204 $prefix /* s2Member Transient prefix for all IP Restrictions. */ = "s2m_ipr_";
205 $transient_entries = $prefix.md5("s2member_ip_restrictions_".(string)$restriction."_entries");
206 $transient_security_breach = $prefix.md5("s2member_ip_restrictions_".(string)$restriction."_security_breach");
207
208 $wpdb->query("DELETE FROM `".$wpdb->options."` WHERE `option_name` LIKE '%".esc_sql(c_ws_plugin__s2member_utils_strings::like_escape($transient_entries))."'");
209 $wpdb->query("DELETE FROM `".$wpdb->options."` WHERE `option_name` LIKE '%".esc_sql(c_ws_plugin__s2member_utils_strings::like_escape($transient_security_breach))."'");
210
211 do_action("ws_plugin__s2member_after_delete_reset_specific_ip_restrictions", get_defined_vars());
212
213 return apply_filters("ws_plugin__s2member_delete_reset_specific_ip_restrictions", true, get_defined_vars());
214 }
215 /**
216 * Resets/deletes specific IP Restrictions via AJAX.
217 *
218 * @package s2Member\IP_Restrictions
219 * @since 3.5
220 *
221 * @attaches-to ``add_action("wp_ajax_ws_plugin__s2member_delete_reset_specific_ip_restrictions_via_ajax");``
222 *
223 * @return null Exits script execution after returning data for AJAX caller.
224 */
225 public static function delete_reset_specific_ip_restrictions_via_ajax()
226 {
227 do_action("ws_plugin__s2member_before_delete_reset_specific_ip_restrictions_via_ajax", get_defined_vars());
228
229 status_header /* Send a 200 OK status header. */(200);
230 header /* Content-Type with UTF-8. */("Content-Type: text/plain; charset=UTF-8");
231 while (@ob_end_clean ()); // Clean any existing output buffers.
232
233 if /* Check privileges. Ability to create Users? */(current_user_can("create_users"))
234
235 if(!empty($_POST["ws_plugin__s2member_delete_reset_specific_ip_restrictions_via_ajax"]))
236 if(($nonce = $_POST["ws_plugin__s2member_delete_reset_specific_ip_restrictions_via_ajax"]))
237 if(wp_verify_nonce($nonce, "ws-plugin--s2member-delete-reset-specific-ip-restrictions-via-ajax"))
238
239 if(!empty($_POST["ws_plugin__s2member_delete_reset_specific_ip_restriction"]))
240 if(is_string /* Must be a string here. */($_POST["ws_plugin__s2member_delete_reset_specific_ip_restriction"]))
241 if(($restriction = trim(stripslashes($_POST["ws_plugin__s2member_delete_reset_specific_ip_restriction"]))))
242
243 if(c_ws_plugin__s2member_ip_restrictions::delete_reset_specific_ip_restrictions($restriction))
244 $success = /* Yes, this IP Restriction was deleted/reset. */ true;
245
246 exit(apply_filters("ws_plugin__s2member_delete_reset_specific_ip_restrictions_via_ajax", ((isset($success) && $success) ? "1" : "0"), get_defined_vars()));
247 }
248 /**
249 * Resets/deletes all IP Restrictions.
250 *
251 * @package s2Member\IP_Restrictions
252 * @since 3.5
253 *
254 * @return bool Always returns a `true` value.
255 *
256 * @todo Make return value conditional, based on success.
257 */
258 public static function delete_reset_all_ip_restrictions()
259 {
260 global /* Need global database object. */ $wpdb;
261
262 do_action("ws_plugin__s2member_before_delete_reset_all_ip_restrictions", get_defined_vars());
263
264 $wpdb->query("DELETE FROM `".$wpdb->options."` WHERE `option_name` LIKE '".esc_sql(c_ws_plugin__s2member_utils_strings::like_escape("_transient_s2m_ipr_"))."%'");
265 $wpdb->query("DELETE FROM `".$wpdb->options."` WHERE `option_name` LIKE '".esc_sql(c_ws_plugin__s2member_utils_strings::like_escape("_transient_timeout_s2m_ipr_"))."%'");
266
267 do_action("ws_plugin__s2member_after_delete_reset_all_ip_restrictions", get_defined_vars());
268
269 return apply_filters("ws_plugin__s2member_delete_reset_all_ip_restrictions", true, get_defined_vars());
270 }
271 /**
272 * Resets/deletes all IP Restrictions via AJAX.
273 *
274 * @package s2Member\IP_Restrictions
275 * @since 3.5
276 *
277 * @attaches-to ``add_action("wp_ajax_ws_plugin__s2member_delete_reset_all_ip_restrictions_via_ajax");``
278 *
279 * @return null Exits script execution after returning data for AJAX caller.
280 */
281 public static function delete_reset_all_ip_restrictions_via_ajax()
282 {
283 do_action("ws_plugin__s2member_before_delete_reset_all_ip_restrictions_via_ajax", get_defined_vars());
284
285 status_header /* Send a 200 OK status header. */(200);
286 header /* Content-Type with UTF-8. */("Content-Type: text/plain; charset=UTF-8");
287 while (@ob_end_clean ()); // Clean any existing output buffers.
288
289 if /* Check privileges. Ability to create Users? */(current_user_can("create_users"))
290
291 if(!empty($_POST["ws_plugin__s2member_delete_reset_all_ip_restrictions_via_ajax"]))
292 if(($nonce = $_POST["ws_plugin__s2member_delete_reset_all_ip_restrictions_via_ajax"]))
293 if(wp_verify_nonce($nonce, "ws-plugin--s2member-delete-reset-all-ip-restrictions-via-ajax"))
294
295 if(c_ws_plugin__s2member_ip_restrictions::delete_reset_all_ip_restrictions())
296 $success = /* Yes, all IP Restrictions were deleted/reset. */ true;
297
298 exit(apply_filters("ws_plugin__s2member_delete_reset_all_ip_restrictions_via_ajax", ((isset($success) && $success) ? "1" : "0"), get_defined_vars()));
299 }
300 }
301 }
302