PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/jetpack/modules/protect/shared-functions.php

https://gitlab.com/chernushov881/charity-fund
PHP | 310 lines | 198 code | 15 blank | 97 comment | 59 complexity | 48cf8e102f355d221dbe62168d4a4f87 MD5 | raw file
  1. <?php
  2. /**
  3. * These functions are shared by the Protect module and its related json-endpoints
  4. */
  5. /**
  6. * Returns an array of IP objects that will never be blocked by the Protect module
  7. *
  8. * The array is segmented into a local whitelist which applies only to the current site
  9. * and a global whitelist which, for multisite installs, applies to the entire networko
  10. *
  11. * @return array
  12. */
  13. function jetpack_protect_format_whitelist() {
  14. $local_whitelist = jetpack_protect_get_local_whitelist();
  15. $formatted = array(
  16. 'local' => array(),
  17. );
  18. foreach ( $local_whitelist as $item ) {
  19. if ( $item->range ) {
  20. $formatted['local'][] = $item->range_low . ' - ' . $item->range_high;
  21. } else {
  22. $formatted['local'][] = $item->ip_address;
  23. }
  24. }
  25. if ( is_multisite() && current_user_can( 'manage_network' ) ) {
  26. $formatted['global'] = array();
  27. $global_whitelist = jetpack_protect_get_global_whitelist();
  28. if ( false === $global_whitelist ) {
  29. // If the global whitelist has never been set, check for a legacy option set prior to 3.6.
  30. $global_whitelist = get_site_option( 'jetpack_protect_whitelist', array() );
  31. }
  32. foreach ( $global_whitelist as $item ) {
  33. if ( $item->range ) {
  34. $formatted['global'][] = $item->range_low . ' - ' . $item->range_high;
  35. } else {
  36. $formatted['global'][] = $item->ip_address;
  37. }
  38. }
  39. }
  40. return $formatted;
  41. }
  42. /**
  43. * Gets the local Protect whitelist
  44. *
  45. * The 'local' part of the whitelist only really applies to multisite installs,
  46. * which can have a network wide whitelist, as well as a local list that applies
  47. * only to the current site. On single site installs, there will only be a local
  48. * whitelist.
  49. *
  50. * @return array A list of IP Address objects or an empty array
  51. */
  52. function jetpack_protect_get_local_whitelist() {
  53. $whitelist = Jetpack_Options::get_option( 'protect_whitelist' );
  54. if ( false === $whitelist ) {
  55. // The local whitelist has never been set.
  56. if ( is_multisite() ) {
  57. // On a multisite, we can check for a legacy site_option that existed prior to v 3.6, or default to an empty array.
  58. $whitelist = get_site_option( 'jetpack_protect_whitelist', array() );
  59. } else {
  60. // On a single site, we can just use an empty array.
  61. $whitelist = array();
  62. }
  63. }
  64. return $whitelist;
  65. }
  66. /**
  67. * Get the global, network-wide whitelist
  68. *
  69. * It will revert to the legacy site_option if jetpack_protect_global_whitelist has never been set.
  70. *
  71. * @return array
  72. */
  73. function jetpack_protect_get_global_whitelist() {
  74. $whitelist = get_site_option( 'jetpack_protect_global_whitelist' );
  75. if ( false === $whitelist ) {
  76. // The global whitelist has never been set. Check for legacy site_option, or default to an empty array.
  77. $whitelist = get_site_option( 'jetpack_protect_whitelist', array() );
  78. }
  79. return $whitelist;
  80. }
  81. /**
  82. * Jetpack Protect Save Whitelist.
  83. *
  84. * @access public
  85. * @param mixed $whitelist Whitelist.
  86. * @param bool $global (default: false) Global.
  87. * @return Bool.
  88. */
  89. function jetpack_protect_save_whitelist( $whitelist, $global = false ) {
  90. $whitelist_error = false;
  91. $new_items = array();
  92. if ( ! is_array( $whitelist ) ) {
  93. return new WP_Error( 'invalid_parameters', __( 'Expecting an array', 'jetpack' ) );
  94. }
  95. if ( $global && ! is_multisite() ) {
  96. return new WP_Error( 'invalid_parameters', __( 'Cannot use global flag on non-multisites', 'jetpack' ) );
  97. }
  98. if ( $global && ! current_user_can( 'manage_network' ) ) {
  99. return new WP_Error( 'permission_denied', __( 'Only super admins can edit the global whitelist', 'jetpack' ) );
  100. }
  101. // Validate each item.
  102. foreach ( $whitelist as $item ) {
  103. $item = trim( $item );
  104. if ( empty( $item ) ) {
  105. continue;
  106. }
  107. $range = false;
  108. if ( strpos( $item, '-' ) ) {
  109. $item = explode( '-', $item );
  110. $range = true;
  111. }
  112. $new_item = new stdClass();
  113. $new_item->range = $range;
  114. if ( ! empty( $range ) ) {
  115. $low = trim( $item[0] );
  116. $high = trim( $item[1] );
  117. if ( ! filter_var( $low, FILTER_VALIDATE_IP ) || ! filter_var( $high, FILTER_VALIDATE_IP ) ) {
  118. $whitelist_error = true;
  119. break;
  120. }
  121. if ( ! jetpack_convert_ip_address( $low ) || ! jetpack_convert_ip_address( $high ) ) {
  122. $whitelist_error = true;
  123. break;
  124. }
  125. $new_item->range_low = $low;
  126. $new_item->range_high = $high;
  127. } else {
  128. if ( ! filter_var( $item, FILTER_VALIDATE_IP ) ) {
  129. $whitelist_error = true;
  130. break;
  131. }
  132. if ( ! jetpack_convert_ip_address( $item ) ) {
  133. $whitelist_error = true;
  134. break;
  135. }
  136. $new_item->ip_address = $item;
  137. }
  138. $new_items[] = $new_item;
  139. } // End item loop.
  140. if ( ! empty( $whitelist_error ) ) {
  141. return new WP_Error( 'invalid_ip', __( 'One of your IP addresses was not valid.', 'jetpack' ) );
  142. }
  143. if ( $global ) {
  144. update_site_option( 'jetpack_protect_global_whitelist', $new_items );
  145. // Once a user has saved their global whitelist, we can permanently remove the legacy option.
  146. delete_site_option( 'jetpack_protect_whitelist' );
  147. } else {
  148. Jetpack_Options::update_option( 'protect_whitelist', $new_items );
  149. }
  150. return true;
  151. }
  152. /**
  153. * Jetpack Protect Get IP.
  154. *
  155. * @access public
  156. * @return string|false IP.
  157. */
  158. function jetpack_protect_get_ip() {
  159. $trusted_header_data = get_site_option( 'trusted_ip_header' );
  160. if ( isset( $trusted_header_data->trusted_header ) && isset( $_SERVER[ $trusted_header_data->trusted_header ] ) ) {
  161. $ip = $_SERVER[ $trusted_header_data->trusted_header ];
  162. $segments = $trusted_header_data->segments;
  163. $reverse_order = $trusted_header_data->reverse;
  164. } else {
  165. $ip = $_SERVER['REMOTE_ADDR'];
  166. }
  167. if ( ! $ip ) {
  168. return false;
  169. }
  170. $ips = explode( ',', $ip );
  171. if ( ! isset( $segments ) || ! $segments ) {
  172. $segments = 1;
  173. }
  174. if ( isset( $reverse_order ) && $reverse_order ) {
  175. $ips = array_reverse( $ips );
  176. }
  177. $ip_count = count( $ips );
  178. if ( 1 === $ip_count ) {
  179. return jetpack_clean_ip( $ips[0] );
  180. } elseif ( $ip_count >= $segments ) {
  181. $the_one = $ip_count - $segments;
  182. return jetpack_clean_ip( $ips[ $the_one ] );
  183. } else {
  184. return jetpack_clean_ip( $_SERVER['REMOTE_ADDR'] );
  185. }
  186. }
  187. /**
  188. * Jetpack Clean IP.
  189. *
  190. * @access public
  191. * @param string $ip IP.
  192. * @return string|false IP.
  193. */
  194. function jetpack_clean_ip( $ip ) {
  195. // Some misconfigured servers give back extra info, which comes after "unless".
  196. $ips = explode( ' unless ', $ip );
  197. $ip = $ips[0];
  198. $ip = strtolower( trim( $ip ) );
  199. // Check for IPv4 with port.
  200. if ( preg_match( '/^(\d+\.\d+\.\d+\.\d+):\d+$/', $ip, $matches ) ) {
  201. $ip = $matches[1];
  202. }
  203. // Check for IPv6 (or IPvFuture) with brackets and optional port.
  204. if ( preg_match( '/^\[([a-z0-9\-._~!$&\'()*+,;=:]+)\](?::\d+)?$/', $ip, $matches ) ) {
  205. $ip = $matches[1];
  206. }
  207. // Check for IPv4 IP cast as IPv6.
  208. if ( preg_match( '/^::ffff:(\d+\.\d+\.\d+\.\d+)$/', $ip, $matches ) ) {
  209. $ip = $matches[1];
  210. }
  211. // Validate and return.
  212. return filter_var( $ip, FILTER_VALIDATE_IP ) ? $ip : false;
  213. }
  214. /**
  215. * Checks an IP to see if it is within a private range.
  216. *
  217. * @param int $ip IP.
  218. * @return bool
  219. */
  220. function jetpack_protect_ip_is_private( $ip ) {
  221. // We are dealing with ipv6, so we can simply rely on filter_var.
  222. if ( false === strpos( $ip, '.' ) ) {
  223. return ! filter_var( $ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE );
  224. }
  225. // We are dealing with ipv4.
  226. $private_ip4_addresses = array(
  227. '10.0.0.0|10.255.255.255', // Single class A network.
  228. '172.16.0.0|172.31.255.255', // 16 contiguous class B network.
  229. '192.168.0.0|192.168.255.255', // 256 contiguous class C network.
  230. '169.254.0.0|169.254.255.255', // Link-local address also referred to as Automatic Private IP Addressing.
  231. '127.0.0.0|127.255.255.255', // localhost.
  232. );
  233. $long_ip = ip2long( $ip );
  234. if ( -1 !== $long_ip ) {
  235. foreach ( $private_ip4_addresses as $pri_addr ) {
  236. list ( $start, $end ) = explode( '|', $pri_addr );
  237. if ( $long_ip >= ip2long( $start ) && $long_ip <= ip2long( $end ) ) {
  238. return true;
  239. }
  240. }
  241. }
  242. return false;
  243. }
  244. /**
  245. * Uses inet_pton if available to convert an IP address to a binary string.
  246. * If inet_pton is not available, ip2long will convert the address to an integer.
  247. * Returns false if an invalid IP address is given.
  248. *
  249. * NOTE: ip2long will return false for any ipv6 address. servers that do not support
  250. * inet_pton will not support ipv6
  251. *
  252. * @access public
  253. * @param mixed $ip IP.
  254. * @return int|string|bool
  255. */
  256. function jetpack_convert_ip_address( $ip ) {
  257. if ( function_exists( 'inet_pton' ) ) {
  258. return inet_pton( $ip );
  259. }
  260. return ip2long( $ip );
  261. }
  262. /**
  263. * Checks that a given IP address is within a given low - high range.
  264. * Servers that support inet_pton will use that function to convert the ip to number,
  265. * while other servers will use ip2long.
  266. *
  267. * NOTE: servers that do not support inet_pton cannot support ipv6.
  268. *
  269. * @access public
  270. * @param mixed $ip IP.
  271. * @param mixed $range_low Range Low.
  272. * @param mixed $range_high Range High.
  273. * @return Bool.
  274. */
  275. function jetpack_protect_ip_address_is_in_range( $ip, $range_low, $range_high ) {
  276. // The inet_pton will give us binary string of an ipv4 or ipv6.
  277. // We can then use strcmp to see if the address is in range.
  278. if ( function_exists( 'inet_pton' ) ) {
  279. $ip_num = inet_pton( $ip );
  280. $ip_low = inet_pton( $range_low );
  281. $ip_high = inet_pton( $range_high );
  282. if ( $ip_num && $ip_low && $ip_high && strcmp( $ip_num, $ip_low ) >= 0 && strcmp( $ip_num, $ip_high ) <= 0 ) {
  283. return true;
  284. }
  285. // The ip2long will give us an integer of an ipv4 address only. it will produce FALSE for ipv6.
  286. } else {
  287. $ip_num = ip2long( $ip );
  288. $ip_low = ip2long( $range_low );
  289. $ip_high = ip2long( $range_high );
  290. if ( $ip_num && $ip_low && $ip_high && $ip_num >= $ip_low && $ip_num <= $ip_high ) {
  291. return true;
  292. }
  293. }
  294. return false;
  295. }