/modules/after-the-deadline/proxy.php

  1. <?php 
  2. /** 
  3. * This script redirects AtD AJAX requests to the AtD service 
  4. */ 
  5.  
  6. /** 
  7. * Returns array with headers in $response[0] and body in $response[1] 
  8. * Based on a function from Akismet 
  9. */ 
  10. function AtD_http_post( $request, $host, $path, $port = 80 ) { 
  11. $http_args = array( 
  12. 'body' => $request,  
  13. 'headers' => array( 
  14. 'Content-Type' => 'application/x-www-form-urlencoded; charset=' . get_option( 'blog_charset' ),  
  15. 'Host' => $host,  
  16. 'User-Agent' => 'AtD/0.1' 
  17. ),  
  18. 'httpversion' => '1.0',  
  19. /** 
  20. * Change the timeout time for AtD post. 
  21. * 
  22. * @module after-the-deadline 
  23. * 
  24. * @since 1.2.3 
  25. * 
  26. * @param int $var Timeout time in seconds, default 15. 
  27. */ 
  28. 'timeout' => apply_filters( 'atd_http_post_timeout', 15 ),  
  29. ); 
  30.  
  31. // Handle non-standard ports being passed in. 
  32. if ( ( 80 !== $port ) && is_numeric( $port ) && ( intval( $port ) > 0 ) ) { 
  33. $host .= ':' . intval( $port ); 
  34. // Strip any / off the begining so we can add it back and protect against SSRF 
  35. $path = ltrim( $path, '/' ); 
  36. $AtD_url = set_url_scheme( "http://{$host}/{$path}" ); 
  37. $response = wp_remote_post( $AtD_url, $http_args ); 
  38. $code = (int) wp_remote_retrieve_response_code( $response ); 
  39.  
  40. if ( is_wp_error( $response ) ) { 
  41. /** 
  42. * Fires when there is a post error to AtD. 
  43. * 
  44. * @module after-the-deadline 
  45. * 
  46. * @since 1.2.3 
  47. * 
  48. * @param int|string http-error The error that AtD runs into. 
  49. */ 
  50. do_action( 'atd_http_post_error', 'http-error' ); 
  51. return array(); 
  52. } elseif ( 200 != $code ) { 
  53. /** This action is documented in modules/after-the-deadline/proxy.php */ 
  54. do_action( 'atd_http_post_error', $code ); 
  55.  
  56. return array( 
  57. wp_remote_retrieve_headers( $response ),  
  58. wp_remote_retrieve_body( $response ),  
  59. ); 
  60.  
  61. /** 
  62. * This function is called as an action handler to admin-ajax.php 
  63. */ 
  64. function AtD_redirect_call() { 
  65. if ( $_SERVER['REQUEST_METHOD'] === 'POST' ) 
  66. $postText = trim( file_get_contents( 'php://input' ) ); 
  67.  
  68. check_admin_referer( 'proxy_atd' ); 
  69.  
  70. $url = $_GET['url']; 
  71. /** 
  72. * Change the AtD service domain. 
  73. * 
  74. * @module after-the-deadline 
  75. * 
  76. * @since 1.2.3 
  77. * 
  78. * @param string $var The URL for AtD service domain, default is service.afterthedeadline.com. 
  79. */ 
  80. $service = apply_filters( 'atd_service_domain', 'service.afterthedeadline.com' ); 
  81.  
  82. $user = wp_get_current_user(); 
  83.  
  84. $atd_lang = get_locale(); 
  85.  
  86. // If we're on WPCOM, this function should be available. 
  87. if ( function_exists( 'get_user_lang_code' ) ) { 
  88. $atd_lang = get_user_lang_code( $user->ID ); 
  89.  
  90. if ( ! empty( $atd_lang ) ) { 
  91. if ( strpos($atd_lang, 'pt') !== false ) 
  92. $service = 'pt.service.afterthedeadline.com'; 
  93. else if ( strpos($atd_lang, 'de') !== false ) 
  94. $service = 'de.service.afterthedeadline.com'; 
  95. else if ( strpos($atd_lang, 'es') !== false ) 
  96. $service = 'es.service.afterthedeadline.com'; 
  97. else if ( strpos($atd_lang, 'fr') !== false ) 
  98. $service = 'fr.service.afterthedeadline.com'; 
  99.  
  100. $guess = strcmp( AtD_get_setting( $user->ID, 'AtD_guess_lang' ), "true" ) == 0 ? "true" : "false"; 
  101.  
  102. $data = AtD_http_post( $postText . "&guess=$guess", defined('ATD_HOST') ? ATD_HOST : $service, $url, defined('ATD_PORT') ? ATD_PORT : 80 ); 
  103.  
  104. header( 'Content-Type: text/xml' ); 
  105.  
  106. if ( ! empty( $data[1] ) ) 
  107. echo $data[1]; 
  108.  
  109. die(); 
.