shared-functions.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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 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 mixed $ip IP.
  192. * @return $ip 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 = trim( $ip );
  199. // Check for IPv4 IP cast as IPv6.
  200. if ( preg_match( '/^::ffff:(\d+\.\d+\.\d+\.\d+)$/', $ip, $matches ) ) {
  201. $ip = $matches[1];
  202. }
  203. if ( function_exists( 'parse_url' ) ) {
  204. $parsed_url = parse_url( $ip );
  205. if ( isset( $parsed_url['host'] ) ) {
  206. $ip = $parsed_url['host'];
  207. } elseif ( isset( $parsed_url['path'] ) ) {
  208. $ip = $parsed_url['path'];
  209. }
  210. } else {
  211. $colon_count = substr_count( $ip, ':' );
  212. if ( 1 == $colon_count ) {
  213. $ips = explode( ':', $ip );
  214. $ip = $ips[0];
  215. }
  216. }
  217. return $ip;
  218. }
  219. /**
  220. * Checks an IP to see if it is within a private range.
  221. *
  222. * @param int $ip IP.
  223. * @return bool
  224. */
  225. function jetpack_protect_ip_is_private( $ip ) {
  226. // We are dealing with ipv6, so we can simply rely on filter_var.
  227. if ( false === strpos( $ip, '.' ) ) {
  228. return ! filter_var( $ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE );
  229. }
  230. // We are dealing with ipv4.
  231. $private_ip4_addresses = array(
  232. '10.0.0.0|10.255.255.255', // Single class A network.
  233. '172.16.0.0|172.31.255.255', // 16 contiguous class B network.
  234. '192.168.0.0|192.168.255.255', // 256 contiguous class C network.
  235. '169.254.0.0|169.254.255.255', // Link-local address also referred to as Automatic Private IP Addressing.
  236. '127.0.0.0|127.255.255.255', // localhost.
  237. );
  238. $long_ip = ip2long( $ip );
  239. if ( -1 !== $long_ip ) {
  240. foreach ( $private_ip4_addresses as $pri_addr ) {
  241. list ( $start, $end ) = explode( '|', $pri_addr );
  242. if ( $long_ip >= ip2long( $start ) && $long_ip <= ip2long( $end ) ) {
  243. return true;
  244. }
  245. }
  246. }
  247. return false;
  248. }
  249. /**
  250. * Uses inet_pton if available to convert an IP address to a binary string.
  251. * If inet_pton is not available, ip2long will convert the address to an integer.
  252. * Returns false if an invalid IP address is given.
  253. *
  254. * NOTE: ip2long will return false for any ipv6 address. servers that do not support
  255. * inet_pton will not support ipv6
  256. *
  257. * @access public
  258. * @param mixed $ip IP.
  259. * @return int|string|bool
  260. */
  261. function jetpack_convert_ip_address( $ip ) {
  262. if ( function_exists( 'inet_pton' ) ) {
  263. return inet_pton( $ip );
  264. }
  265. return ip2long( $ip );
  266. }
  267. /**
  268. * Checks that a given IP address is within a given low - high range.
  269. * Servers that support inet_pton will use that function to convert the ip to number,
  270. * while other servers will use ip2long.
  271. *
  272. * NOTE: servers that do not support inet_pton cannot support ipv6.
  273. *
  274. * @access public
  275. * @param mixed $ip IP.
  276. * @param mixed $range_low Range Low.
  277. * @param mixed $range_high Range High.
  278. * @return Bool.
  279. */
  280. function jetpack_protect_ip_address_is_in_range( $ip, $range_low, $range_high ) {
  281. // The inet_pton will give us binary string of an ipv4 or ipv6.
  282. // We can then use strcmp to see if the address is in range.
  283. if ( function_exists( 'inet_pton' ) ) {
  284. $ip_num = inet_pton( $ip );
  285. $ip_low = inet_pton( $range_low );
  286. $ip_high = inet_pton( $range_high );
  287. if ( $ip_num && $ip_low && $ip_high && strcmp( $ip_num, $ip_low ) >= 0 && strcmp( $ip_num, $ip_high ) <= 0 ) {
  288. return true;
  289. }
  290. // The ip2long will give us an integer of an ipv4 address only. it will produce FALSE for ipv6.
  291. } else {
  292. $ip_num = ip2long( $ip );
  293. $ip_low = ip2long( $range_low );
  294. $ip_high = ip2long( $range_high );
  295. if ( $ip_num && $ip_low && $ip_high && $ip_num >= $ip_low && $ip_num <= $ip_high ) {
  296. return true;
  297. }
  298. }
  299. return false;
  300. }