| 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 as Transients. |
| 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 |
/* Also allow specific exclusions here. */ && !apply_filters ("ws_plugin__s2member_disable_specific_ip_restriction", false, get_defined_vars ()) |
| 50 |
/* And enabled by site owner? */ && $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["max_ip_restriction"] /* And a valid ``$restriction``? */ && $restriction && is_string ($restriction) |
| 51 |
/* Flag indicating that we ARE processing this IP Restriction. Useful in Hooks/Filters. */ && ($processing = true)) |
| 52 |
{ |
| 53 |
$msg_503 = _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"); |
| 54 |
/**/ |
| 55 |
$prefix = "s2m_ipr_"; /* s2Member Transient prefix for all IP Restrictions. Allows s2Member to find these easily. */ |
| 56 |
/**/ |
| 57 |
$transient_entries = $prefix . md5 ("s2member_ip_restrictions_" . $restriction . "_entries"); |
| 58 |
$transient_security_breach = $prefix . md5 ("s2member_ip_restrictions_" . $restriction . "_security_breach"); |
| 59 |
/**/ |
| 60 |
/* If you add Filters, use a string compatible with PHP's strtotime() function. */ |
| 61 |
$concurrency = apply_filters ("ws_plugin__s2member_ip_restrictions__concurrency_time_per_ip", "30 days"); |
| 62 |
/**/ |
| 63 |
$entries = (is_array ($entries = get_transient ($transient_entries))) ? $entries : array (); |
| 64 |
/**/ |
| 65 |
foreach ($entries as $_entry => $_time) /* Auto-expire entries, based on time. */ |
| 66 |
if ($_time < strtotime ("-" . $concurrency)) /* Based on time. */ |
| 67 |
unset($entries[$_entry]); /* Unset this entry value. */ |
| 68 |
/**/ |
| 69 |
$ip = ($ip && is_string ($ip)) ? $ip : "empty"; /* Allow empty IPs. */ |
| 70 |
$entries[$ip] = strtotime ("now"); /* Log entry. Add IP with entry time. */ |
| 71 |
set_transient ($transient_entries, $entries, 2 * (strtotime ("+" . $concurrency) - strtotime ("now"))); |
| 72 |
/**/ |
| 73 |
if (get_transient ($transient_security_breach)) /* Already breached security? */ |
| 74 |
{ |
| 75 |
c_ws_plugin__s2member_no_cache::no_cache_constants (true); |
| 76 |
/**/ |
| 77 |
status_header(503); /* Send a 503 error status header; temporarily unavailable. */ |
| 78 |
wp_clear_auth_cookie (); /* Clear authorization cookies; we need to log them out now. */ |
| 79 |
header("Content-Type: text/html; charset=utf-8"); /* Content-Type text/html with UTF-8. */ |
| 80 |
eval('while (@ob_end_clean ());'); /* End/clean any output buffers that may exist. */ |
| 81 |
/**/ |
| 82 |
eval('foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;'); |
| 83 |
do_action ("ws_plugin__s2member_during_ip_restrictions_ok_no", get_defined_vars ()); |
| 84 |
unset ($__refs, $__v); /* Unset defined __refs, __v. */ |
| 85 |
/**/ |
| 86 |
exit($msg_503); /* Clean exit with 503 error message. */ |
| 87 |
} |
| 88 |
/**/ |
| 89 |
else if (count ($entries) > $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["max_ip_restriction"]) |
| 90 |
{ |
| 91 |
c_ws_plugin__s2member_no_cache::no_cache_constants (true); |
| 92 |
/**/ |
| 93 |
set_transient /* A security breach has just occurred. We need to set this Transient now. */ |
| 94 |
($transient_security_breach, 1, $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["max_ip_restriction_time"]); |
| 95 |
/**/ |
| 96 |
status_header(503); /* Send a 503 error status header; temporarily unavailable. */ |
| 97 |
wp_clear_auth_cookie (); /* Clear authorization cookies; we need to log them out now. */ |
| 98 |
header("Content-Type: text/html; charset=utf-8"); /* Content-Type text/html with UTF-8. */ |
| 99 |
eval('while (@ob_end_clean ());'); /* End/clean any output buffers that may exist. */ |
| 100 |
/**/ |
| 101 |
eval('foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;'); |
| 102 |
do_action ("ws_plugin__s2member_during_ip_restrictions_ok_no", get_defined_vars ()); |
| 103 |
unset ($__refs, $__v); /* Unset defined __refs, __v. */ |
| 104 |
/**/ |
| 105 |
exit($msg_503); /* Clean exit with 503 error message. */ |
| 106 |
} |
| 107 |
/**/ |
| 108 |
else /* OK, this looks legitimate. Apply Filters here and return true. */ |
| 109 |
{ |
| 110 |
eval('foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;'); |
| 111 |
do_action ("ws_plugin__s2member_during_ip_restrictions_ok_yes", get_defined_vars ()); |
| 112 |
unset ($__refs, $__v); /* Unset defined __refs, __v. */ |
| 113 |
/**/ |
| 114 |
return apply_filters ("ws_plugin__s2member_ip_restrictions_ok", true, get_defined_vars ()); |
| 115 |
} |
| 116 |
} |
| 117 |
/**/ |
| 118 |
eval('foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;'); |
| 119 |
do_action ("ws_plugin__s2member_during_ip_restrictions_ok_yes", get_defined_vars ()); |
| 120 |
unset ($__refs, $__v); /* Unset defined __refs, __v. */ |
| 121 |
/**/ |
| 122 |
return apply_filters ("ws_plugin__s2member_ip_restrictions_ok", true, get_defined_vars ()); |
| 123 |
} |
| 124 |
/** |
| 125 |
* Queries Transients for specific IP Restrictions associated with a security breach. |
| 126 |
* |
| 127 |
* @package s2Member\IP_Restrictions |
| 128 |
* @since 3.5 |
| 129 |
* |
| 130 |
* @param str $restriction Unique IP Restriction name/identifier. Such as a Username, or a unique access code. |
| 131 |
* @return bool True if the specific IP Restriction is associated with a security breach, else false. |
| 132 |
*/ |
| 133 |
public static function specific_ip_restriction_breached_security ($restriction = FALSE) |
| 134 |
{ |
| 135 |
do_action ("ws_plugin__s2member_before_specific_ip_restriction_breached_security", get_defined_vars ()); |
| 136 |
/**/ |
| 137 |
$prefix = "s2m_ipr_"; /* s2Member Transient prefix for all IP Restrictions. */ |
| 138 |
$transient_security_breach = $prefix . md5 ("s2member_ip_restrictions_" . (string)$restriction . "_security_breach"); |
| 139 |
$breached_security = $associated_with_security_breach = (get_transient ($transient_security_breach)) ? true : false; |
| 140 |
/**/ |
| 141 |
return apply_filters ("ws_plugin__s2member_before_specific_ip_restriction_breached_security", $breached_security, get_defined_vars ()); |
| 142 |
} |
| 143 |
/** |
| 144 |
* Resets/deletes specific IP Restrictions. |
| 145 |
* |
| 146 |
* @package s2Member\IP_Restrictions |
| 147 |
* @since 3.5 |
| 148 |
* |
| 149 |
* @param str $restriction Unique IP Restriction name/identifier. Such as a Username, or a unique access code. |
| 150 |
* @return bool Always returns a `true` value. |
| 151 |
* |
| 152 |
* @todo Make return value conditional, based on success. |
| 153 |
*/ |
| 154 |
public static function delete_reset_specific_ip_restrictions ($restriction = FALSE) |
| 155 |
{ |
| 156 |
global $wpdb; /* Need global database object. */ |
| 157 |
/**/ |
| 158 |
do_action ("ws_plugin__s2member_before_delete_reset_specific_ip_restrictions", get_defined_vars ()); |
| 159 |
/**/ |
| 160 |
$prefix = "s2m_ipr_"; /* s2Member Transient prefix for all IP Restrictions. */ |
| 161 |
$transient_entries = $prefix . md5 ("s2member_ip_restrictions_" . (string)$restriction . "_entries"); |
| 162 |
$transient_security_breach = $prefix . md5 ("s2member_ip_restrictions_" . (string)$restriction . "_security_breach"); |
| 163 |
/**/ |
| 164 |
$wpdb->query ("DELETE FROM `" . $wpdb->options . "` WHERE `option_name` LIKE '%" . esc_sql (like_escape ($transient_entries)) . "'"); |
| 165 |
$wpdb->query ("DELETE FROM `" . $wpdb->options . "` WHERE `option_name` LIKE '%" . esc_sql (like_escape ($transient_security_breach)) . "'"); |
| 166 |
/**/ |
| 167 |
do_action ("ws_plugin__s2member_after_delete_reset_specific_ip_restrictions", get_defined_vars ()); |
| 168 |
/**/ |
| 169 |
return apply_filters ("ws_plugin__s2member_delete_reset_specific_ip_restrictions", true, get_defined_vars ()); |
| 170 |
} |
| 171 |
/** |
| 172 |
* Resets/deletes specific IP Restrictions via AJAX. |
| 173 |
* |
| 174 |
* @package s2Member\IP_Restrictions |
| 175 |
* @since 3.5 |
| 176 |
* |
| 177 |
* @attaches-to ``add_action("wp_ajax_ws_plugin__s2member_delete_reset_specific_ip_restrictions_via_ajax");`` |
| 178 |
* |
| 179 |
* @return null Exits script execution after returning data for AJAX caller. |
| 180 |
*/ |
| 181 |
public static function delete_reset_specific_ip_restrictions_via_ajax () |
| 182 |
{ |
| 183 |
do_action ("ws_plugin__s2member_before_delete_reset_specific_ip_restrictions_via_ajax", get_defined_vars ()); |
| 184 |
/**/ |
| 185 |
status_header(200); /* Send a 200 OK status header. */ |
| 186 |
header("Content-Type: text/plain; charset=utf-8"); /* Content-Type with UTF-8. */ |
| 187 |
eval('while (@ob_end_clean ());'); /* End/clean all output buffers that may exist. */ |
| 188 |
/**/ |
| 189 |
if (current_user_can ("create_users")) /* Check priveledges. Ability to create Users? */ |
| 190 |
/**/ |
| 191 |
if (!empty ($_POST["ws_plugin__s2member_delete_reset_specific_ip_restrictions_via_ajax"])) |
| 192 |
if (($nonce = $_POST["ws_plugin__s2member_delete_reset_specific_ip_restrictions_via_ajax"])) |
| 193 |
if (wp_verify_nonce ($nonce, "ws-plugin--s2member-delete-reset-specific-ip-restrictions-via-ajax")) |
| 194 |
/**/ |
| 195 |
if (!empty ($_POST["ws_plugin__s2member_delete_reset_specific_ip_restriction"])) |
| 196 |
if (is_string /* Must be a string here. */ ($_POST["ws_plugin__s2member_delete_reset_specific_ip_restriction"])) |
| 197 |
if (($restriction = trim (stripslashes ($_POST["ws_plugin__s2member_delete_reset_specific_ip_restriction"])))) |
| 198 |
/**/ |
| 199 |
if (c_ws_plugin__s2member_ip_restrictions::delete_reset_specific_ip_restrictions ($restriction)) |
| 200 |
$success = true; /* Yes, this IP Restriction was deleted/reset. */ |
| 201 |
/**/ |
| 202 |
exit(apply_filters ("ws_plugin__s2member_delete_reset_specific_ip_restrictions_via_ajax", ((isset ($success) && $success) ? "1" : "0"), get_defined_vars ())); |
| 203 |
} |
| 204 |
/** |
| 205 |
* Resets/deletes all IP Restrictions. |
| 206 |
* |
| 207 |
* @package s2Member\IP_Restrictions |
| 208 |
* @since 3.5 |
| 209 |
* |
| 210 |
* @return bool Always returns a `true` value. |
| 211 |
* |
| 212 |
* @todo Make return value conditional, based on success. |
| 213 |
*/ |
| 214 |
public static function delete_reset_all_ip_restrictions () |
| 215 |
{ |
| 216 |
global $wpdb; /* Need global database object. */ |
| 217 |
/**/ |
| 218 |
do_action ("ws_plugin__s2member_before_delete_reset_all_ip_restrictions", get_defined_vars ()); |
| 219 |
/**/ |
| 220 |
$wpdb->query ("DELETE FROM `" . $wpdb->options . "` WHERE `option_name` LIKE '" . esc_sql (like_escape ("_transient_s2m_ipr_")) . "%'"); |
| 221 |
$wpdb->query ("DELETE FROM `" . $wpdb->options . "` WHERE `option_name` LIKE '" . esc_sql (like_escape ("_transient_timeout_s2m_ipr_")) . "%'"); |
| 222 |
/**/ |
| 223 |
do_action ("ws_plugin__s2member_after_delete_reset_all_ip_restrictions", get_defined_vars ()); |
| 224 |
/**/ |
| 225 |
return apply_filters ("ws_plugin__s2member_delete_reset_all_ip_restrictions", true, get_defined_vars ()); |
| 226 |
} |
| 227 |
/** |
| 228 |
* Resets/deletes all IP Restrictions via AJAX. |
| 229 |
* |
| 230 |
* @package s2Member\IP_Restrictions |
| 231 |
* @since 3.5 |
| 232 |
* |
| 233 |
* @attaches-to ``add_action("wp_ajax_ws_plugin__s2member_delete_reset_all_ip_restrictions_via_ajax");`` |
| 234 |
* |
| 235 |
* @return null Exits script execution after returning data for AJAX caller. |
| 236 |
*/ |
| 237 |
public static function delete_reset_all_ip_restrictions_via_ajax () |
| 238 |
{ |
| 239 |
do_action ("ws_plugin__s2member_before_delete_reset_all_ip_restrictions_via_ajax", get_defined_vars ()); |
| 240 |
/**/ |
| 241 |
status_header(200); /* Send a 200 OK status header. */ |
| 242 |
header("Content-Type: text/plain; charset=utf-8"); /* Content-Type with UTF-8. */ |
| 243 |
eval('while (@ob_end_clean ());'); /* End/clean all output buffers that may exist. */ |
| 244 |
/**/ |
| 245 |
if (current_user_can ("create_users")) /* Check priveledges. Ability to create Users? */ |
| 246 |
/**/ |
| 247 |
if (!empty ($_POST["ws_plugin__s2member_delete_reset_all_ip_restrictions_via_ajax"])) |
| 248 |
if (($nonce = $_POST["ws_plugin__s2member_delete_reset_all_ip_restrictions_via_ajax"])) |
| 249 |
if (wp_verify_nonce ($nonce, "ws-plugin--s2member-delete-reset-all-ip-restrictions-via-ajax")) |
| 250 |
/**/ |
| 251 |
if (c_ws_plugin__s2member_ip_restrictions::delete_reset_all_ip_restrictions ()) |
| 252 |
$success = true; /* Yes, all IP Restrictions were deleted/reset. */ |
| 253 |
/**/ |
| 254 |
exit(apply_filters ("ws_plugin__s2member_delete_reset_all_ip_restrictions_via_ajax", ((isset ($success) && $success) ? "1" : "0"), get_defined_vars ())); |
| 255 |
} |
| 256 |
} |
| 257 |
} |
| 258 |
?> |