class.jetpack-data.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. class Jetpack_Data {
  3. /**
  4. * Gets locally stored token
  5. *
  6. * @return object|false
  7. */
  8. public static function get_access_token( $user_id = false ) {
  9. if ( $user_id ) {
  10. if ( !$tokens = Jetpack_Options::get_option( 'user_tokens' ) ) {
  11. return false;
  12. }
  13. if ( $user_id === JETPACK_MASTER_USER ) {
  14. if ( !$user_id = Jetpack_Options::get_option( 'master_user' ) ) {
  15. return false;
  16. }
  17. }
  18. if ( !isset( $tokens[$user_id] ) || !$token = $tokens[$user_id] ) {
  19. return false;
  20. }
  21. $token_chunks = explode( '.', $token );
  22. if ( empty( $token_chunks[1] ) || empty( $token_chunks[2] ) ) {
  23. return false;
  24. }
  25. if ( $user_id != $token_chunks[2] ) {
  26. return false;
  27. }
  28. $token = "{$token_chunks[0]}.{$token_chunks[1]}";
  29. } else {
  30. $token = Jetpack_Options::get_option( 'blog_token' );
  31. if ( empty( $token ) ) {
  32. return false;
  33. }
  34. }
  35. return (object) array(
  36. 'secret' => $token,
  37. 'external_user_id' => (int) $user_id,
  38. );
  39. }
  40. /**
  41. * This function mirrors Jetpack_Data::is_usable_domain() in the WPCOM codebase.
  42. *
  43. * @param $domain
  44. * @param array $extra
  45. *
  46. * @return bool|WP_Error
  47. */
  48. public static function is_usable_domain( $domain, $extra = array() ) {
  49. // If it's empty, just fail out.
  50. if ( ! $domain ) {
  51. return new WP_Error( 'fail_domain_empty', sprintf( __( 'Domain `%1$s` just failed is_usable_domain check as it is empty.', 'jetpack' ), $domain ) );
  52. }
  53. /**
  54. * Skips the usuable domain check when connecting a site.
  55. *
  56. * Allows site administrators with domains that fail gethostname-based checks to pass the request to WP.com
  57. *
  58. * @since 4.1.0
  59. *
  60. * @param bool If the check should be skipped. Default false.
  61. */
  62. if ( apply_filters( 'jetpack_skip_usuable_domain_check', false ) ) {
  63. return true;
  64. }
  65. // None of the explicit localhosts.
  66. $forbidden_domains = array(
  67. 'wordpress.com',
  68. 'localhost',
  69. 'localhost.localdomain',
  70. '127.0.0.1',
  71. 'local.wordpress.dev', // VVV
  72. 'local.wordpress-trunk.dev', // VVV
  73. 'src.wordpress-develop.dev', // VVV
  74. 'build.wordpress-develop.dev', // VVV
  75. );
  76. if ( in_array( $domain, $forbidden_domains ) ) {
  77. return new WP_Error( 'fail_domain_forbidden', sprintf( __( 'Domain `%1$s` just failed is_usable_domain check as it is in the forbidden array.', 'jetpack' ), $domain ) );
  78. }
  79. // No .dev or .local domains
  80. if ( preg_match( '#\.(dev|local)$#i', $domain ) ) {
  81. return new WP_Error( 'fail_domain_tld', sprintf( __( 'Domain `%1$s` just failed is_usable_domain check as it uses an invalid top level domain.', 'jetpack' ), $domain ) );
  82. }
  83. // No WPCOM subdomains
  84. if ( preg_match( '#\.wordpress\.com$#i', $domain ) ) {
  85. return new WP_Error( 'fail_subdomain_wpcom', sprintf( __( 'Domain `%1$s` just failed is_usable_domain check as it is a subdomain of WordPress.com.', 'jetpack' ), $domain ) );
  86. }
  87. // If PHP was compiled without support for the Filter module (very edge case)
  88. if ( ! function_exists( 'filter_var' ) ) {
  89. // Just pass back true for now, and let wpcom sort it out.
  90. return true;
  91. }
  92. return true;
  93. }
  94. /**
  95. * Returns true if the IP address passed in should not be in a reserved range, even if PHP says that it is.
  96. * See: https://bugs.php.net/bug.php?id=66229 and https://github.com/php/php-src/commit/d1314893fd1325ca6aa0831101896e31135a2658
  97. *
  98. * This function mirrors Jetpack_Data::php_bug_66229_check() in the WPCOM codebase.
  99. */
  100. public static function php_bug_66229_check( $ip ) {
  101. if ( ! filter_var( $ip, FILTER_VALIDATE_IP ) ) {
  102. return false;
  103. }
  104. $ip_arr = array_map( 'intval', explode( '.', $ip ) );
  105. if ( 128 == $ip_arr[0] && 0 == $ip_arr[1] ) {
  106. return true;
  107. }
  108. if ( 191 == $ip_arr[0] && 255 == $ip_arr[1] ) {
  109. return true;
  110. }
  111. return false;
  112. }
  113. }