Jetpack_Data

The Jetpack by WordPress.com Jetpack Data class.

Defined (1)

The class is defined in the following location(s).

/class.jetpack-data.php  
  1. class Jetpack_Data { 
  2. /** 
  3. * Gets locally stored token 
  4. * @return object|false 
  5. */ 
  6. public static function get_access_token( $user_id = false ) { 
  7. if ( $user_id ) { 
  8. if ( !$tokens = Jetpack_Options::get_option( 'user_tokens' ) ) { 
  9. return false; 
  10. if ( $user_id === JETPACK_MASTER_USER ) { 
  11. if ( !$user_id = Jetpack_Options::get_option( 'master_user' ) ) { 
  12. return false; 
  13. if ( !isset( $tokens[$user_id] ) || !$token = $tokens[$user_id] ) { 
  14. return false; 
  15. $token_chunks = explode( '.', $token ); 
  16. if ( empty( $token_chunks[1] ) || empty( $token_chunks[2] ) ) { 
  17. return false; 
  18. if ( $user_id != $token_chunks[2] ) { 
  19. return false; 
  20. $token = "{$token_chunks[0]}.{$token_chunks[1]}"; 
  21. } else { 
  22. $token = Jetpack_Options::get_option( 'blog_token' ); 
  23. if ( empty( $token ) ) { 
  24. return false; 
  25.  
  26. return (object) array( 
  27. 'secret' => $token,  
  28. 'external_user_id' => (int) $user_id,  
  29. ); 
  30.  
  31. /** 
  32. * This function mirrors Jetpack_Data::is_usable_domain() in the WPCOM codebase. 
  33. * @param $domain 
  34. * @param array $extra 
  35. * @return bool|WP_Error 
  36. */ 
  37. public static function is_usable_domain( $domain, $extra = array() ) { 
  38.  
  39. // If it's empty, just fail out. 
  40. if ( ! $domain ) { 
  41. return new WP_Error( 'fail_domain_empty', sprintf( __( 'Domain `%1$s` just failed is_usable_domain check as it is empty.', 'jetpack' ), $domain ) ); 
  42.  
  43. // None of the explicit localhosts. 
  44. $forbidden_domains = array( 
  45. 'wordpress.com',  
  46. 'localhost',  
  47. 'localhost.localdomain',  
  48. '127.0.0.1',  
  49. 'local.wordpress.dev', // VVV 
  50. 'local.wordpress-trunk.dev', // VVV 
  51. 'src.wordpress-develop.dev', // VVV 
  52. 'build.wordpress-develop.dev', // VVV 
  53. ); 
  54. if ( in_array( $domain, $forbidden_domains ) ) { 
  55. 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 ) ); 
  56.  
  57. // No .dev or .local domains 
  58. if ( preg_match( '#\.(dev|local)$#i', $domain ) ) { 
  59. 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 ) ); 
  60.  
  61. // No WPCOM subdomains 
  62. if ( preg_match( '#\.wordpress\.com$#i', $domain ) ) { 
  63. 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 ) ); 
  64.  
  65. // If PHP was compiled without support for the Filter module (very edge case) 
  66. if ( ! function_exists( 'filter_var' ) ) { 
  67. // Just pass back true for now, and let wpcom sort it out. 
  68. return true; 
  69.  
  70. // Check the IP to make sure it's pingable. 
  71. $ip = gethostbyname( $domain ); 
  72.  
  73. // Doing this again as I was getting some false positives when gethostbyname() flaked out and returned the domain. 
  74. $ip = filter_var( $ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 ) ? $ip : gethostbyname( $ip ); 
  75.  
  76. if ( ! filter_var( $ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE | FILTER_FLAG_IPV4 ) && ! self::php_bug_66229_check( $ip ) ) { 
  77. return new WP_Error( 'fail_domain_bad_ip_range', sprintf( __( 'Domain `%1$s` just failed is_usable_domain check as its IP `%2$s` is either invalid, or in a reserved or private range.', 'jetpack' ), $domain, $ip ) ); 
  78.  
  79. return true; 
  80.  
  81. /** 
  82. * Returns true if the IP address passed in should not be in a reserved range, even if PHP says that it is. 
  83. * See: https://bugs.php.net/bug.php?id=66229 and https://github.com/php/php-src/commit/d1314893fd1325ca6aa0831101896e31135a2658 
  84. * This function mirrors Jetpack_Data::php_bug_66229_check() in the WPCOM codebase. 
  85. */ 
  86. public static function php_bug_66229_check( $ip ) { 
  87. if ( ! filter_var( $ip, FILTER_VALIDATE_IP ) ) { 
  88. return false; 
  89.  
  90. $ip_arr = array_map( 'intval', explode( '.', $ip ) ); 
  91.  
  92. if ( 128 == $ip_arr[0] && 0 == $ip_arr[1] ) { 
  93. return true; 
  94.  
  95. if ( 191 == $ip_arr[0] && 255 == $ip_arr[1] ) { 
  96. return true; 
  97.  
  98. return false;