PluginProbe
s2Member – Excellent for All Kinds of Memberships, Content Restriction Paywalls & Member Access Subscriptions / 110915
s2Member – Excellent for All Kinds of Memberships, Content Restriction Paywalls & Member Access Subscriptions v110915
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 / includes / classes / ip-restrictions.inc.php

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

227 lines 10.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * IP Restrictions.
4 *
5 * Copyright: © 2009-2011
6 * {@link http://www.websharks-inc.com/ WebSharks, Inc.}
7 * ( coded in the USA )
8 *
9 * Released under the terms of the GNU General Public License.
10 * You should have received a copy of the GNU General Public License,
11 * along with this software. In the main directory, see: /licensing/
12 * If not, see: {@link http://www.gnu.org/licenses/}.
13 *
14 * @package s2Member\IP_Restrictions
15 * @since 3.5
16 */
17 if (realpath (__FILE__) === realpath ($_SERVER["SCRIPT_FILENAME"]))
18 exit ("Do not access this file directly.");
19 /**/
20 if (!class_exists ("c_ws_plugin__s2member_ip_restrictions"))
21 {
22 /**
23 * IP Restrictions.
24 *
25 * @package s2Member\IP_Restrictions
26 * @since 3.5
27 */
28 class c_ws_plugin__s2member_ip_restrictions
29 {
30 /**
31 * Handles IP Restrictions.
32 *
33 * IP address details are stored in Transient fields.
34 *
35 * @package s2Member\IP_Restrictions
36 * @since 3.5
37 *
38 * @param str $ip IP Address.
39 * @param str $restriction Unique IP Restriction name/identifier. Such as a Username, or a unique access code.
40 * @return bool True if IP Restrictions are OK, otherwise this function will exit script execution after issuing a warning.
41 */
42 public static function ip_restrictions_ok ($ip = FALSE, $restriction = FALSE)
43 {
44 eval ('foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;');
45 do_action ("ws_plugin__s2member_before_ip_restrictions_ok", get_defined_vars ());
46 unset ($__refs, $__v); /* Unset defined __refs, __v. */
47 /**/
48 if (!apply_filters ("ws_plugin__s2member_disable_all_ip_restrictions", false, get_defined_vars ()))
49 if ($GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["max_ip_restriction"] && $restriction)
50 {
51 $prefix = "s2m_ipr_"; /* s2Member Transient prefix for all IP Restrictions. */
52 $transient_entries = $prefix . md5 ("s2member_ip_restrictions_" . $restriction . "_entries");
53 $transient_security_breach = $prefix . md5 ("s2member_ip_restrictions_" . $restriction . "_security_breach");
54 /**/
55 /* If you add Filters, use a string compatible with PHP's strtotime() function. */
56 $concurrency = apply_filters ("ws_plugin__s2member_ip_restrictions__concurrency_time_per_ip", "30 days");
57 /**/
58 $entries = (is_array ($entries = get_transient ($transient_entries))) ? $entries : array ();
59 /**/
60 foreach ($entries as $_entry => $_time) /* Auto-expire entries. */
61 if ($_time < strtotime ("-" . $concurrency))
62 unset ($entries[$_entry]);
63 /**/
64 $ip = ($ip) ? $ip : "empty"; /* Allow empty IPs. */
65 $entries[$ip] = strtotime ("now"); /* Log this entry. */
66 set_transient ($transient_entries, $entries, 2 * (strtotime ("+" . $concurrency) - strtotime ("now")));
67 /**/
68 if (get_transient ($transient_security_breach)) /* Has this restriction already been breached? */
69 {
70 c_ws_plugin__s2member_no_cache::no_cache_constants (true) . wp_clear_auth_cookie ();
71 /**/
72 do_action ("ws_plugin__s2member_during_ip_restrictions_ok_no", get_defined_vars ());
73 /**/
74 status_header (503); /* Sends a status header; temporarily unavailable. */
75 /**/
76 echo _x ('<strong>503: Service Temporarily Unavailable</strong><br />Too many IP addresses accessing one secure area<em>!</em><br />Please contact Support if you need assistance.', "s2member-front", "s2member");
77 /**/
78 exit (); /* Clean exit. */
79 }
80 else if (count ($entries) > $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["max_ip_restriction"])
81 {
82 c_ws_plugin__s2member_no_cache::no_cache_constants (true) . wp_clear_auth_cookie ();
83 /**/
84 set_transient ($transient_security_breach, 1, $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["max_ip_restriction_time"]);
85 /**/
86 do_action ("ws_plugin__s2member_during_ip_restrictions_ok_no", get_defined_vars ());
87 /**/
88 status_header (503); /* Sends a status header; temporarily unavailable. */
89 /**/
90 echo _x ('<strong>503: Service Temporarily Unavailable</strong><br />Too many IP addresses accessing one secure area<em>!</em><br />Please contact Support if you need assistance.', "s2member-front", "s2member");
91 /**/
92 exit (); /* Clean exit. */
93 }
94 else /* OK, this looks legitimate. Apply Filters here and return true. */
95 {
96 eval ('foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;');
97 do_action ("ws_plugin__s2member_during_ip_restrictions_ok_yes", get_defined_vars ());
98 unset ($__refs, $__v); /* Unset defined __refs, __v. */
99 /**/
100 return apply_filters ("ws_plugin__s2member_ip_restrictions_ok", true, get_defined_vars ());
101 }
102 }
103 /**/
104 return apply_filters ("ws_plugin__s2member_ip_restrictions_ok", true, get_defined_vars ());
105 }
106 /**
107 * Queries Transients for specific IP Restrictions associated with a security breach.
108 *
109 * @package s2Member\IP_Restrictions
110 * @since 3.5
111 *
112 * @param str $restriction Unique IP Restriction name/identifier. Such as a Username, or a unique access code.
113 * @return bool True if the specific IP Restriction is associated with a security breach, else false.
114 */
115 public static function specific_ip_restriction_breached_security ($restriction = FALSE)
116 {
117 do_action ("ws_plugin__s2member_before_specific_ip_restriction_breached_security", get_defined_vars ());
118 /**/
119 $prefix = "s2m_ipr_"; /* s2Member Transient prefix for all IP Restrictions. */
120 $transient_security_breach = $prefix . md5 ("s2member_ip_restrictions_" . $restriction . "_security_breach");
121 $associated_with_security_breach = (get_transient ($transient_security_breach)) ? true : false;
122 /**/
123 return apply_filters ("ws_plugin__s2member_before_specific_ip_restriction_breached_security", $associated_with_security_breach, get_defined_vars ());
124 }
125 /**
126 * Resets/deletes specific IP Restrictions.
127 *
128 * @package s2Member\IP_Restrictions
129 * @since 3.5
130 *
131 * @param str $restriction Unique IP Restriction name/identifier. Such as a Username, or a unique access code.
132 * @return null
133 */
134 public static function delete_reset_specific_ip_restrictions ($restriction = FALSE)
135 {
136 global $wpdb; /* Need global database object. */
137 /**/
138 do_action ("ws_plugin__s2member_before_delete_reset_specific_ip_restrictions", get_defined_vars ());
139 /**/
140 $prefix = "s2m_ipr_"; /* s2Member Transient prefix for all IP Restrictions. */
141 $transient_entries = $prefix . md5 ("s2member_ip_restrictions_" . $restriction . "_entries");
142 $transient_security_breach = $prefix . md5 ("s2member_ip_restrictions_" . $restriction . "_security_breach");
143 /**/
144 $wpdb->query ("DELETE FROM `" . $wpdb->options . "` WHERE `option_name` LIKE '%" . esc_sql (like_escape ($transient_entries)) . "'");
145 $wpdb->query ("DELETE FROM `" . $wpdb->options . "` WHERE `option_name` LIKE '%" . esc_sql (like_escape ($transient_security_breach)) . "'");
146 /**/
147 do_action ("ws_plugin__s2member_after_delete_reset_specific_ip_restrictions", get_defined_vars ());
148 /**/
149 return; /* Return for uniformity. */
150 }
151 /**
152 * Resets/deletes specific IP Restrictions via AJAX.
153 *
154 * @package s2Member\IP_Restrictions
155 * @since 3.5
156 *
157 * @attaches-to: ``add_action("wp_ajax_ws_plugin__s2member_delete_reset_specific_ip_restrictions_via_ajax");``
158 *
159 * @return null Exits script execution after returning data for AJAX caller.
160 */
161 public static function delete_reset_specific_ip_restrictions_via_ajax ()
162 {
163 do_action ("ws_plugin__s2member_before_delete_reset_specific_ip_restrictions_via_ajax", get_defined_vars ());
164 /**/
165 if (current_user_can ("create_users")) /* Check priveledges as well. */
166 /**/
167 if (!empty ($_POST["ws_plugin__s2member_delete_reset_specific_ip_restrictions_via_ajax"]))
168 if (($nonce = $_POST["ws_plugin__s2member_delete_reset_specific_ip_restrictions_via_ajax"]))
169 if (wp_verify_nonce ($nonce, "ws-plugin--s2member-delete-reset-specific-ip-restrictions-via-ajax"))
170 /**/
171 if (!empty ($_POST["ws_plugin__s2member_delete_reset_specific_ip_restriction"]))
172 if (($restriction = trim (stripslashes ($_POST["ws_plugin__s2member_delete_reset_specific_ip_restriction"]))))
173 /**/
174 if (c_ws_plugin__s2member_ip_restrictions::delete_reset_specific_ip_restrictions ($restriction) !== "nill")
175 echo apply_filters ("ws_plugin__s2member_delete_reset_specific_ip_restrictions_via_ajax", 1, get_defined_vars ());
176 /**/
177 exit (); /* Clean exit. */
178 }
179 /**
180 * Resets/deletes all IP Restrictions.
181 *
182 * @package s2Member\IP_Restrictions
183 * @since 3.5
184 *
185 * @return null
186 */
187 public static function delete_reset_all_ip_restrictions ()
188 {
189 global $wpdb; /* Need global database object. */
190 /**/
191 do_action ("ws_plugin__s2member_before_delete_reset_all_ip_restrictions", get_defined_vars ());
192 /**/
193 $wpdb->query ("DELETE FROM `" . $wpdb->options . "` WHERE `option_name` LIKE '" . esc_sql (like_escape ("_transient_s2m_ipr_")) . "%'");
194 $wpdb->query ("DELETE FROM `" . $wpdb->options . "` WHERE `option_name` LIKE '" . esc_sql (like_escape ("_transient_timeout_s2m_ipr_")) . "%'");
195 /**/
196 do_action ("ws_plugin__s2member_after_delete_reset_all_ip_restrictions", get_defined_vars ());
197 /**/
198 return; /* Return for uniformity. */
199 }
200 /**
201 * Resets/deletes all IP Restrictions via AJAX.
202 *
203 * @package s2Member\IP_Restrictions
204 * @since 3.5
205 *
206 * @attaches-to: ``add_action("wp_ajax_ws_plugin__s2member_delete_reset_all_ip_restrictions_via_ajax");``
207 *
208 * @return null Exits script execution after returning data for AJAX caller.
209 */
210 public static function delete_reset_all_ip_restrictions_via_ajax ()
211 {
212 do_action ("ws_plugin__s2member_before_delete_reset_all_ip_restrictions_via_ajax", get_defined_vars ());
213 /**/
214 if (current_user_can ("create_users")) /* Check priveledges as well. */
215 /**/
216 if (!empty ($_POST["ws_plugin__s2member_delete_reset_all_ip_restrictions_via_ajax"]))
217 if (($nonce = $_POST["ws_plugin__s2member_delete_reset_all_ip_restrictions_via_ajax"]))
218 if (wp_verify_nonce ($nonce, "ws-plugin--s2member-delete-reset-all-ip-restrictions-via-ajax"))
219 /**/
220 if (c_ws_plugin__s2member_ip_restrictions::delete_reset_all_ip_restrictions () !== "nill")
221 echo apply_filters ("ws_plugin__s2member_delete_reset_all_ip_restrictions_via_ajax", 1, get_defined_vars ());
222 /**/
223 exit (); /* Clean exit. */
224 }
225 }
226 }
227 ?>