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 | } |
| 35 | // Strip any / off the begining so we can add it back and protect against SSRF |
| 36 | $path = ltrim( $path, '/' ); |
| 37 | $AtD_url = set_url_scheme( "http://{$host}/{$path}" ); |
| 38 | $response = wp_remote_post( $AtD_url, $http_args ); |
| 39 | $code = (int) wp_remote_retrieve_response_code( $response ); |
| 40 | |
| 41 | if ( is_wp_error( $response ) ) { |
| 42 | /** |
| 43 | * Fires when there is a post error to AtD. |
| 44 | * |
| 45 | * @module after-the-deadline |
| 46 | * |
| 47 | * @since 1.2.3 |
| 48 | * |
| 49 | * @param int|string http-error The error that AtD runs into. |
| 50 | */ |
| 51 | do_action( 'atd_http_post_error', 'http-error' ); |
| 52 | return array(); |
| 53 | } elseif ( 200 != $code ) { |
| 54 | /** This action is documented in modules/after-the-deadline/proxy.php */ |
| 55 | do_action( 'atd_http_post_error', $code ); |
| 56 | } |
| 57 | |
| 58 | return array( |
| 59 | wp_remote_retrieve_headers( $response ), |
| 60 | wp_remote_retrieve_body( $response ), |
| 61 | ); |
| 62 | } |
| 63 | |
| 64 | /* |
| 65 | * This function is called as an action handler to admin-ajax.php |
| 66 | */ |
| 67 | function AtD_redirect_call() { |
| 68 | if ( $_SERVER['REQUEST_METHOD'] === 'POST' ) { |
| 69 | $postText = trim( file_get_contents( 'php://input' ) ); |
| 70 | } |
| 71 | |
| 72 | check_admin_referer( 'proxy_atd' ); |
| 73 | |
| 74 | $url = $_GET['url']; |
| 75 | /** |
| 76 | * Change the AtD service domain. |
| 77 | * |
| 78 | * @module after-the-deadline |
| 79 | * |
| 80 | * @since 1.2.3 |
| 81 | * |
| 82 | * @param string $var The URL for AtD service domain, default is service.afterthedeadline.com. |
| 83 | */ |
| 84 | $service = apply_filters( 'atd_service_domain', 'service.afterthedeadline.com' ); |
| 85 | |
| 86 | $user = wp_get_current_user(); |
| 87 | |
| 88 | $atd_lang = get_user_locale( $user->ID ); |
| 89 | |
| 90 | if ( ! empty( $atd_lang ) ) { |
| 91 | if ( strpos( $atd_lang, 'pt' ) !== false ) { |
| 92 | $service = 'pt.service.afterthedeadline.com'; |
| 93 | } elseif ( strpos( $atd_lang, 'de' ) !== false ) { |
| 94 | $service = 'de.service.afterthedeadline.com'; |
| 95 | } elseif ( strpos( $atd_lang, 'es' ) !== false ) { |
| 96 | $service = 'es.service.afterthedeadline.com'; |
| 97 | } elseif ( strpos( $atd_lang, 'fr' ) !== false ) { |
| 98 | $service = 'fr.service.afterthedeadline.com'; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | $guess = strcmp( AtD_get_setting( $user->ID, 'AtD_guess_lang' ), 'true' ) == 0 ? 'true' : 'false'; |
| 103 | |
| 104 | $data = AtD_http_post( $postText . "&guess=$guess", defined( 'ATD_HOST' ) ? ATD_HOST : $service, $url, defined( 'ATD_PORT' ) ? ATD_PORT : 80 ); |
| 105 | |
| 106 | header( 'Content-Type: text/xml' ); |
| 107 | |
| 108 | if ( ! empty( $data[1] ) ) { |
| 109 | echo $data[1]; |
| 110 | } |
| 111 | |
| 112 | die(); |
| 113 | } |
| 114 |