| 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 |
header("HTTP/1.0 503 Service Temporarily Unavailable"); /* Sends a status header. */ |
| 75 |
/**/ |
| 76 |
echo '<strong>503: Service Temporarily Unavailable</strong><br />' . "\n"; |
| 77 |
echo 'Too many IP addresses accessing one secure area<em>!</em><br />' . "\n"; |
| 78 |
echo 'Please contact Support if you require assistance.'; |
| 79 |
/**/ |
| 80 |
exit (); /* Clean exit. */ |
| 81 |
} |
| 82 |
else if (count ($entries) > $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["max_ip_restriction"]) |
| 83 |
{ |
| 84 |
c_ws_plugin__s2member_no_cache::no_cache_constants (true) . wp_clear_auth_cookie (); |
| 85 |
/**/ |
| 86 |
set_transient ($transient_security_breach, 1, $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["max_ip_restriction_time"]); |
| 87 |
/**/ |
| 88 |
do_action ("ws_plugin__s2member_during_ip_restrictions_ok_no", get_defined_vars ()); |
| 89 |
/**/ |
| 90 |
header("HTTP/1.0 503 Service Temporarily Unavailable"); /* Sends a status header. */ |
| 91 |
/**/ |
| 92 |
echo '<strong>503: Service Temporarily Unavailable</strong><br />' . "\n"; |
| 93 |
echo 'Too many IP addresses accessing one secure area<em>!</em><br />' . "\n"; |
| 94 |
echo 'Please contact Support if you require assistance.'; |
| 95 |
/**/ |
| 96 |
exit (); /* Clean exit. */ |
| 97 |
} |
| 98 |
else /* OK, this looks legitimate. Apply Filters here and return true. */ |
| 99 |
{ |
| 100 |
eval('foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;'); |
| 101 |
do_action ("ws_plugin__s2member_during_ip_restrictions_ok_yes", get_defined_vars ()); |
| 102 |
unset ($__refs, $__v); /* Unset defined __refs, __v. */ |
| 103 |
/**/ |
| 104 |
return apply_filters ("ws_plugin__s2member_ip_restrictions_ok", true, get_defined_vars ()); |
| 105 |
} |
| 106 |
} |
| 107 |
/**/ |
| 108 |
return apply_filters ("ws_plugin__s2member_ip_restrictions_ok", true, get_defined_vars ()); |
| 109 |
} |
| 110 |
/** |
| 111 |
* Queries Transients for specific IP Restrictions associated with a security breach. |
| 112 |
* |
| 113 |
* @package s2Member\IP_Restrictions |
| 114 |
* @since 3.5 |
| 115 |
* |
| 116 |
* @param str $restriction Unique IP Restriction name/identifier. Such as a Username, or a unique access code. |
| 117 |
* @return bool True if the specific IP Restriction is associated with a security breach, else false. |
| 118 |
*/ |
| 119 |
public static function specific_ip_restriction_breached_security ($restriction = FALSE) |
| 120 |
{ |
| 121 |
do_action ("ws_plugin__s2member_before_specific_ip_restriction_breached_security", get_defined_vars ()); |
| 122 |
/**/ |
| 123 |
$prefix = "s2m_ipr_"; /* s2Member Transient prefix for all IP Restrictions. */ |
| 124 |
$transient_security_breach = $prefix . md5 ("s2member_ip_restrictions_" . $restriction . "_security_breach"); |
| 125 |
$associated_with_security_breach = (get_transient ($transient_security_breach)) ? true : false; |
| 126 |
/**/ |
| 127 |
return apply_filters ("ws_plugin__s2member_before_specific_ip_restriction_breached_security", $associated_with_security_breach, get_defined_vars ()); |
| 128 |
} |
| 129 |
/** |
| 130 |
* Resets/deletes specific IP Restrictions. |
| 131 |
* |
| 132 |
* @package s2Member\IP_Restrictions |
| 133 |
* @since 3.5 |
| 134 |
* |
| 135 |
* @param str $restriction Unique IP Restriction name/identifier. Such as a Username, or a unique access code. |
| 136 |
* @return null |
| 137 |
*/ |
| 138 |
public static function delete_reset_specific_ip_restrictions ($restriction = FALSE) |
| 139 |
{ |
| 140 |
global $wpdb; /* Need global database object. */ |
| 141 |
/**/ |
| 142 |
do_action ("ws_plugin__s2member_before_delete_reset_specific_ip_restrictions", get_defined_vars ()); |
| 143 |
/**/ |
| 144 |
$prefix = "s2m_ipr_"; /* s2Member Transient prefix for all IP Restrictions. */ |
| 145 |
$transient_entries = $prefix . md5 ("s2member_ip_restrictions_" . $restriction . "_entries"); |
| 146 |
$transient_security_breach = $prefix . md5 ("s2member_ip_restrictions_" . $restriction . "_security_breach"); |
| 147 |
/**/ |
| 148 |
$wpdb->query ("DELETE FROM `" . $wpdb->options . "` WHERE `option_name` LIKE '%" . esc_sql (like_escape ($transient_entries)) . "'"); |
| 149 |
$wpdb->query ("DELETE FROM `" . $wpdb->options . "` WHERE `option_name` LIKE '%" . esc_sql (like_escape ($transient_security_breach)) . "'"); |
| 150 |
/**/ |
| 151 |
do_action ("ws_plugin__s2member_after_delete_reset_specific_ip_restrictions", get_defined_vars ()); |
| 152 |
/**/ |
| 153 |
return; /* Return for uniformity. */ |
| 154 |
} |
| 155 |
/** |
| 156 |
* Resets/deletes specific IP Restrictions via AJAX. |
| 157 |
* |
| 158 |
* @package s2Member\IP_Restrictions |
| 159 |
* @since 3.5 |
| 160 |
* |
| 161 |
* @attaches-to: ``add_action("wp_ajax_ws_plugin__s2member_delete_reset_specific_ip_restrictions_via_ajax");`` |
| 162 |
* |
| 163 |
* @return null Exits script execution after returning data for AJAX caller. |
| 164 |
*/ |
| 165 |
public static function delete_reset_specific_ip_restrictions_via_ajax () |
| 166 |
{ |
| 167 |
do_action ("ws_plugin__s2member_before_delete_reset_specific_ip_restrictions_via_ajax", get_defined_vars ()); |
| 168 |
/**/ |
| 169 |
if (current_user_can ("create_users")) /* Check priveledges as well. */ |
| 170 |
/**/ |
| 171 |
if (!empty ($_POST["ws_plugin__s2member_delete_reset_specific_ip_restrictions_via_ajax"])) |
| 172 |
if (($nonce = $_POST["ws_plugin__s2member_delete_reset_specific_ip_restrictions_via_ajax"])) |
| 173 |
if (wp_verify_nonce ($nonce, "ws-plugin--s2member-delete-reset-specific-ip-restrictions-via-ajax")) |
| 174 |
/**/ |
| 175 |
if (!empty ($_POST["ws_plugin__s2member_delete_reset_specific_ip_restriction"])) |
| 176 |
if (($restriction = trim (stripslashes ($_POST["ws_plugin__s2member_delete_reset_specific_ip_restriction"])))) |
| 177 |
/**/ |
| 178 |
if (c_ws_plugin__s2member_ip_restrictions::delete_reset_specific_ip_restrictions ($restriction) !== "nill") |
| 179 |
echo apply_filters ("ws_plugin__s2member_delete_reset_specific_ip_restrictions_via_ajax", 1, get_defined_vars ()); |
| 180 |
/**/ |
| 181 |
exit (); /* Clean exit. */ |
| 182 |
} |
| 183 |
/** |
| 184 |
* Resets/deletes all IP Restrictions. |
| 185 |
* |
| 186 |
* @package s2Member\IP_Restrictions |
| 187 |
* @since 3.5 |
| 188 |
* |
| 189 |
* @return null |
| 190 |
*/ |
| 191 |
public static function delete_reset_all_ip_restrictions () |
| 192 |
{ |
| 193 |
global $wpdb; /* Need global database object. */ |
| 194 |
/**/ |
| 195 |
do_action ("ws_plugin__s2member_before_delete_reset_all_ip_restrictions", get_defined_vars ()); |
| 196 |
/**/ |
| 197 |
$wpdb->query ("DELETE FROM `" . $wpdb->options . "` WHERE `option_name` LIKE '" . esc_sql (like_escape ("_transient_s2m_ipr_")) . "%'"); |
| 198 |
$wpdb->query ("DELETE FROM `" . $wpdb->options . "` WHERE `option_name` LIKE '" . esc_sql (like_escape ("_transient_timeout_s2m_ipr_")) . "%'"); |
| 199 |
/**/ |
| 200 |
do_action ("ws_plugin__s2member_after_delete_reset_all_ip_restrictions", get_defined_vars ()); |
| 201 |
/**/ |
| 202 |
return; /* Return for uniformity. */ |
| 203 |
} |
| 204 |
/** |
| 205 |
* Resets/deletes all IP Restrictions via AJAX. |
| 206 |
* |
| 207 |
* @package s2Member\IP_Restrictions |
| 208 |
* @since 3.5 |
| 209 |
* |
| 210 |
* @attaches-to: ``add_action("wp_ajax_ws_plugin__s2member_delete_reset_all_ip_restrictions_via_ajax");`` |
| 211 |
* |
| 212 |
* @return null Exits script execution after returning data for AJAX caller. |
| 213 |
*/ |
| 214 |
public static function delete_reset_all_ip_restrictions_via_ajax () |
| 215 |
{ |
| 216 |
do_action ("ws_plugin__s2member_before_delete_reset_all_ip_restrictions_via_ajax", get_defined_vars ()); |
| 217 |
/**/ |
| 218 |
if (current_user_can ("create_users")) /* Check priveledges as well. */ |
| 219 |
/**/ |
| 220 |
if (!empty ($_POST["ws_plugin__s2member_delete_reset_all_ip_restrictions_via_ajax"])) |
| 221 |
if (($nonce = $_POST["ws_plugin__s2member_delete_reset_all_ip_restrictions_via_ajax"])) |
| 222 |
if (wp_verify_nonce ($nonce, "ws-plugin--s2member-delete-reset-all-ip-restrictions-via-ajax")) |
| 223 |
/**/ |
| 224 |
if (c_ws_plugin__s2member_ip_restrictions::delete_reset_all_ip_restrictions () !== "nill") |
| 225 |
echo apply_filters ("ws_plugin__s2member_delete_reset_all_ip_restrictions_via_ajax", 1, get_defined_vars ()); |
| 226 |
/**/ |
| 227 |
exit (); /* Clean exit. */ |
| 228 |
} |
| 229 |
} |
| 230 |
} |
| 231 |
?> |