proxy.php 3.3 KB

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