Source Code Documentation


s2member\includes\classes\ip-restrictions.inc.php


  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. 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 /* Unset defined __refs, __v. */ ($__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 = /* s2Member Transient prefix for all IP Restrictions. Allows s2Member to find these easily. */ "s2m_ipr_";
  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 /* Auto-expire entries, based on time. */($entries as $_entry => $_time)
  66. if /* Based on time. */($_time < strtotime("-".$concurrency))
  67. unset /* Unset this entry value. */($entries[$_entry]);
  68.  
  69. $ip = ($ip && is_string($ip)) ? $ip : /* Allow empty IPs. */ "empty";
  70. $entries[$ip] = /* Log entry. Add IP with entry time. */ strtotime("now");
  71. set_transient($transient_entries, $entries, 2 * (strtotime("+".$concurrency) - strtotime("now")));
  72.  
  73. if /* Already breached security? */(get_transient($transient_security_breach))
  74. {
  75. c_ws_plugin__s2member_no_cache::no_cache_constants(true);
  76.  
  77. status_header /* Send a 503 error status header; temporarily unavailable. */(503);
  78. wp_clear_auth_cookie /* Clear authorization cookies; we need to log them out now. */();
  79. header /* Content-Type text/html with UTF-8. */("Content-Type: text/html; charset=UTF-8");
  80. while (@ob_end_clean ()); // Clean any existing output buffers.
  81.  
  82. 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 /* Unset defined __refs, __v. */ ($__refs, $__v);
  85.  
  86. exit /* Clean exit with 503 error message. */($msg_503);
  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 /* Send a 503 error status header; temporarily unavailable. */(503);
  97. wp_clear_auth_cookie /* Clear authorization cookies; we need to log them out now. */();
  98. header /* Content-Type text/html with UTF-8. */("Content-Type: text/html; charset=UTF-8");
  99. while (@ob_end_clean ()); // Clean any existing output buffers.
  100.  
  101. 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 /* Unset defined __refs, __v. */ ($__refs, $__v);
  104.  
  105. exit /* Clean exit with 503 error message. */($msg_503);
  106. }
  107.  
  108. else // OK, this looks legitimate. Apply Filters here and return true.
  109. {
  110. 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 /* Unset defined __refs, __v. */ ($__refs, $__v);
  113.  
  114. return apply_filters("ws_plugin__s2member_ip_restrictions_ok", true, get_defined_vars());
  115. }
  116. }
  117.  
  118. 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 /* Unset defined __refs, __v. */ ($__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 at or above max allowable.
  126. *
  127. * @package s2Member\IP_Restrictions
  128. * @since 130407
  129. *
  130. * @param str $restriction Unique IP Restriction name/identifier. Such as Username, or a unique access code.
  131. * @return bool TRUE if at or above max allowable IPs; else FALSE.
  132. */
  133. public static function specific_ip_restriction_at_or_above_max($restriction = FALSE)
  134. {
  135. do_action("ws_plugin__s2member_before_specific_ip_restriction_at_or_above_max", get_defined_vars());
  136.  
  137. if(apply_filters("ws_plugin__s2member_disable_all_ip_restrictions", false, get_defined_vars())
  138. || apply_filters("ws_plugin__s2member_disable_specific_ip_restriction", false, get_defined_vars())
  139. || !$GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["max_ip_restriction"])
  140. return false; // No IP Restrictions in this case.
  141.  
  142. $prefix = /* s2Member Transient prefix for all IP Restrictions. Allows s2Member to find these easily. */ "s2m_ipr_";
  143. $transient_entries = $prefix.md5("s2member_ip_restrictions_".(string)$restriction."_entries");
  144.  
  145. // If you add Filters, use a string compatible with PHP's strtotime() function.
  146. $concurrency = apply_filters("ws_plugin__s2member_ip_restrictions__concurrency_time_per_ip", "30 days");
  147.  
  148. $entries = (is_array($entries = get_transient($transient_entries))) ? $entries : array();
  149.  
  150. foreach /* Auto-expire entries, based on time. */($entries as $_entry => $_time)
  151. if /* Based on time. */($_time < strtotime("-".$concurrency))
  152. unset /* Unset this entry value. */($entries[$_entry]);
  153.  
  154. $at_or_above_max = (count($entries) >= $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["max_ip_restriction"]);
  155.  
  156. return apply_filters("ws_plugin__s2member_specific_ip_restriction_at_or_above_max", $at_or_above_max, get_defined_vars());
  157. }
  158. /**
  159. * Queries Transients for specific IP Restrictions associated with a security breach.
  160. *
  161. * @package s2Member\IP_Restrictions
  162. * @since 3.5
  163. *
  164. * @param str $restriction Unique IP Restriction name/identifier. Such as a Username, or a unique access code.
  165. * @return bool True if the specific IP Restriction is associated with a security breach, else false.
  166. */
  167. public static function specific_ip_restriction_breached_security($restriction = FALSE)
  168. {
  169. do_action("ws_plugin__s2member_before_specific_ip_restriction_breached_security", get_defined_vars());
  170.  
  171. $prefix = /* s2Member Transient prefix for all IP Restrictions. */ "s2m_ipr_";
  172. $transient_security_breach = $prefix.md5("s2member_ip_restrictions_".(string)$restriction."_security_breach");
  173. $breached_security = $associated_with_security_breach = (get_transient($transient_security_breach)) ? true : false;
  174.  
  175. return apply_filters("ws_plugin__s2member_specific_ip_restriction_breached_security", $breached_security, get_defined_vars());
  176. }
  177. /**
  178. * Resets/deletes specific IP Restrictions.
  179. *
  180. * @package s2Member\IP_Restrictions
  181. * @since 3.5
  182. *
  183. * @param str $restriction Unique IP Restriction name/identifier. Such as a Username, or a unique access code.
  184. * @return bool Always returns a `true` value.
  185. *
  186. * @todo Make return value conditional, based on success.
  187. */
  188. public static function delete_reset_specific_ip_restrictions($restriction = FALSE)
  189. {
  190. global /* Need global database object. */ $wpdb;
  191.  
  192. do_action("ws_plugin__s2member_before_delete_reset_specific_ip_restrictions", get_defined_vars());
  193.  
  194. $prefix /* s2Member Transient prefix for all IP Restrictions. */ = "s2m_ipr_";
  195. $transient_entries = $prefix.md5("s2member_ip_restrictions_".(string)$restriction."_entries");
  196. $transient_security_breach = $prefix.md5("s2member_ip_restrictions_".(string)$restriction."_security_breach");
  197.  
  198. $wpdb->query("DELETE FROM `".$wpdb->options."` WHERE `option_name` LIKE '%".esc_sql(like_escape($transient_entries))."'");
  199. $wpdb->query("DELETE FROM `".$wpdb->options."` WHERE `option_name` LIKE '%".esc_sql(like_escape($transient_security_breach))."'");
  200.  
  201. do_action("ws_plugin__s2member_after_delete_reset_specific_ip_restrictions", get_defined_vars());
  202.  
  203. return apply_filters("ws_plugin__s2member_delete_reset_specific_ip_restrictions", true, get_defined_vars());
  204. }
  205. /**
  206. * Resets/deletes specific IP Restrictions via AJAX.
  207. *
  208. * @package s2Member\IP_Restrictions
  209. * @since 3.5
  210. *
  211. * @attaches-to ``add_action("wp_ajax_ws_plugin__s2member_delete_reset_specific_ip_restrictions_via_ajax");``
  212. *
  213. * @return null Exits script execution after returning data for AJAX caller.
  214. */
  215. public static function delete_reset_specific_ip_restrictions_via_ajax()
  216. {
  217. do_action("ws_plugin__s2member_before_delete_reset_specific_ip_restrictions_via_ajax", get_defined_vars());
  218.  
  219. status_header /* Send a 200 OK status header. */(200);
  220. header /* Content-Type with UTF-8. */("Content-Type: text/plain; charset=UTF-8");
  221. while (@ob_end_clean ()); // Clean any existing output buffers.
  222.  
  223. if /* Check privileges. Ability to create Users? */(current_user_can("create_users"))
  224.  
  225. if(!empty($_POST["ws_plugin__s2member_delete_reset_specific_ip_restrictions_via_ajax"]))
  226. if(($nonce = $_POST["ws_plugin__s2member_delete_reset_specific_ip_restrictions_via_ajax"]))
  227. if(wp_verify_nonce($nonce, "ws-plugin--s2member-delete-reset-specific-ip-restrictions-via-ajax"))
  228.  
  229. if(!empty($_POST["ws_plugin__s2member_delete_reset_specific_ip_restriction"]))
  230. if(is_string /* Must be a string here. */($_POST["ws_plugin__s2member_delete_reset_specific_ip_restriction"]))
  231. if(($restriction = trim(stripslashes($_POST["ws_plugin__s2member_delete_reset_specific_ip_restriction"]))))
  232.  
  233. if(c_ws_plugin__s2member_ip_restrictions::delete_reset_specific_ip_restrictions($restriction))
  234. $success = /* Yes, this IP Restriction was deleted/reset. */ true;
  235.  
  236. exit(apply_filters("ws_plugin__s2member_delete_reset_specific_ip_restrictions_via_ajax", ((isset($success) && $success) ? "1" : "0"), get_defined_vars()));
  237. }
  238. /**
  239. * Resets/deletes all IP Restrictions.
  240. *
  241. * @package s2Member\IP_Restrictions
  242. * @since 3.5
  243. *
  244. * @return bool Always returns a `true` value.
  245. *
  246. * @todo Make return value conditional, based on success.
  247. */
  248. public static function delete_reset_all_ip_restrictions()
  249. {
  250. global /* Need global database object. */ $wpdb;
  251.  
  252. do_action("ws_plugin__s2member_before_delete_reset_all_ip_restrictions", get_defined_vars());
  253.  
  254. $wpdb->query("DELETE FROM `".$wpdb->options."` WHERE `option_name` LIKE '".esc_sql(like_escape("_transient_s2m_ipr_"))."%'");
  255. $wpdb->query("DELETE FROM `".$wpdb->options."` WHERE `option_name` LIKE '".esc_sql(like_escape("_transient_timeout_s2m_ipr_"))."%'");
  256.  
  257. do_action("ws_plugin__s2member_after_delete_reset_all_ip_restrictions", get_defined_vars());
  258.  
  259. return apply_filters("ws_plugin__s2member_delete_reset_all_ip_restrictions", true, get_defined_vars());
  260. }
  261. /**
  262. * Resets/deletes all IP Restrictions via AJAX.
  263. *
  264. * @package s2Member\IP_Restrictions
  265. * @since 3.5
  266. *
  267. * @attaches-to ``add_action("wp_ajax_ws_plugin__s2member_delete_reset_all_ip_restrictions_via_ajax");``
  268. *
  269. * @return null Exits script execution after returning data for AJAX caller.
  270. */
  271. public static function delete_reset_all_ip_restrictions_via_ajax()
  272. {
  273. do_action("ws_plugin__s2member_before_delete_reset_all_ip_restrictions_via_ajax", get_defined_vars());
  274.  
  275. status_header /* Send a 200 OK status header. */(200);
  276. header /* Content-Type with UTF-8. */("Content-Type: text/plain; charset=UTF-8");
  277. while (@ob_end_clean ()); // Clean any existing output buffers.
  278.  
  279. if /* Check privileges. Ability to create Users? */(current_user_can("create_users"))
  280.  
  281. if(!empty($_POST["ws_plugin__s2member_delete_reset_all_ip_restrictions_via_ajax"]))
  282. if(($nonce = $_POST["ws_plugin__s2member_delete_reset_all_ip_restrictions_via_ajax"]))
  283. if(wp_verify_nonce($nonce, "ws-plugin--s2member-delete-reset-all-ip-restrictions-via-ajax"))
  284.  
  285. if(c_ws_plugin__s2member_ip_restrictions::delete_reset_all_ip_restrictions())
  286. $success = /* Yes, all IP Restrictions were deleted/reset. */ true;
  287.  
  288. exit(apply_filters("ws_plugin__s2member_delete_reset_all_ip_restrictions_via_ajax", ((isset($success) && $success) ? "1" : "0"), get_defined_vars()));
  289. }
  290. }
  291. }
  292. ?>

s2member\includes\classes\ip-restrictions.inc.php

Source Code Documentation