Changeset 7196 for trunk/src/includes/extend/akismet.php
- Timestamp:
- 04/26/2021 06:39:32 PM (2 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/includes/extend/akismet.php
r7193 r7196 161 161 ) ); 162 162 163 // Set the result headers (from maybe_spam() above) 164 $post_data['bbp_akismet_result_headers'] = ! empty( $_post['bbp_akismet_result_headers'] ) 165 ? $_post['bbp_akismet_result_headers'] // raw 166 : esc_html__( 'No response', 'bbpress' ); 167 168 // Set the result (from maybe_spam() above) 169 $post_data['bbp_akismet_result'] = ! empty( $_post['bbp_akismet_result'] ) 170 ? $_post['bbp_akismet_result'] // raw 171 : esc_html__( 'No response', 'bbpress' ); 172 173 // Avoid recurrsion by unsetting results 163 // Set the results (from maybe_spam() above) 164 $post_data['bbp_akismet_result_headers'] = $_post['bbp_akismet_result_headers']; 165 $post_data['bbp_akismet_result'] = $_post['bbp_akismet_result']; 166 $post_data['bbp_post_as_submitted'] = $_post; 167 168 // Avoid recursion by unsetting results from post-as-submitted 174 169 unset( 175 $ _post['bbp_akismet_result_headers'],176 $ _post['bbp_akismet_result']170 $post_data['bbp_post_as_submitted']['bbp_akismet_result_headers'], 171 $post_data['bbp_post_as_submitted']['bbp_akismet_result'] 177 172 ); 178 $post_data['bbp_post_as_submitted'] = $_post;179 180 // Cleanup to avoid touching this variable again below181 unset( $_post );182 173 183 174 // Allow post_data to be manipulated … … 421 412 422 413 // Define variables 423 $query_string = $path = $response = ''; 414 $query_string = $path = ''; 415 $response = array( '', '' ); 424 416 425 417 // Make sure post data is an array … … 439 431 foreach ( $_POST as $key => $value ) { 440 432 if ( is_string( $value ) ) { 441 $post_data[ 'POST_' . $key] = $value;433 $post_data[ 'POST_' . $key ] = $value; 442 434 } 443 435 } … … 470 462 } 471 463 464 // Only accepts spam|ham 465 if ( ! in_array( $spam, array( 'spam', 'ham' ), true ) ) { 466 $spam = 'spam'; 467 } 468 472 469 // Setup the API route 473 470 if ( 'check' === $check ) { … … 478 475 479 476 // Send data to Akismet 480 $response = ! apply_filters( 'bbp_bypass_check_for_spam', false, $post_data )481 ? $this->http_post( $query_string, $akismet_api_host, $path, $akismet_api_port )482 : false;477 if ( ! apply_filters( 'bbp_bypass_check_for_spam', false, $post_data ) ) { 478 $response = $this->http_post( $query_string, $akismet_api_host, $path, $akismet_api_port ); 479 } 483 480 484 481 // Set the result headers 485 482 $post_data['bbp_akismet_result_headers'] = ! empty( $response[0] ) 486 ? $response[0] 483 ? $response[0] // raw 487 484 : esc_html__( 'No response', 'bbpress' ); 488 485 489 486 // Set the result 490 487 $post_data['bbp_akismet_result'] = ! empty( $response[1] ) 491 ? $response[1] 488 ? $response[1] // raw 492 489 : esc_html__( 'No response', 'bbpress' ); 493 490 … … 737 734 // Preload required variables 738 735 $bbp_version = bbp_get_version(); 736 $ak_version = constant( 'AKISMET_VERSION' ); 739 737 $http_host = $host; 740 738 $blog_charset = get_option( 'blog_charset' ); 741 $response = ''; 742 743 // Untque User Agent 744 $akismet_ua = "bbPress/{$bbp_version} | "; 745 $akismet_ua .= 'Akismet/' . constant( 'AKISMET_VERSION' ); 739 740 // User Agent & Content Type 741 $akismet_ua = "bbPress/{$bbp_version} | Akismet/{$ak_version}"; 742 $content_type = 'application/x-www-form-urlencoded; charset=' . $blog_charset; 746 743 747 744 // Use specific IP (if provided) … … 752 749 // Setup the arguments 753 750 $http_args = array( 754 'body' => $request, 755 'headers' => array( 756 'Content-Type' => 'application/x-www-form-urlencoded; charset=' . $blog_charset, 751 'httpversion' => '1.0', 752 'timeout' => 15, 753 'body' => $request, 754 'headers' => array( 755 'Content-Type' => $content_type, 757 756 'Host' => $host, 758 757 'User-Agent' => $akismet_ua 759 ), 760 'httpversion' => '1.0', 761 'timeout' => 15 758 ) 762 759 ); 763 760 764 // Where we are sending our request 765 $akismet_url = 'http://' . $http_host . $path; 766 767 // Send the request 768 $response = wp_remote_post( $akismet_url, $http_args ); 769 770 // Bail if the response is an error 761 // Return the response 762 return $this->get_response( $http_host . $path, $http_args ); 763 } 764 765 /** 766 * Handles the repeated calls to wp_remote_post(), including SSL support. 767 * 768 * @since 2.6.7 (bbPress r7194) 769 * 770 * @param string $host_and_path Scheme-less URL 771 * @param array $http_args Array of arguments for wp_remote_post() 772 * @return array 773 */ 774 private function get_response( $host_and_path = '', $http_args = array() ) { 775 776 // Default variables 777 $akismet_url = $http_akismet_url = 'http://' . $host_and_path; 778 $is_ssl = $ssl_failed = false; 779 $now = time(); 780 781 // Check if SSL requests were disabled fewer than 24 hours ago 782 $ssl_disabled_time = get_option( 'akismet_ssl_disabled' ); 783 784 // Clean-up if 24 hours have passed 785 if ( ! empty( $ssl_disabled_time ) && ( $ssl_disabled_time < ( $now - DAY_IN_SECONDS ) ) ) { 786 delete_option( 'akismet_ssl_disabled' ); 787 $ssl_disabled_time = false; 788 } 789 790 // Maybe HTTPS if not disabled 791 if ( empty( $ssl_disabled_time ) && ( $is_ssl = wp_http_supports( array( 'ssl' ) ) ) ) { 792 $akismet_url = set_url_scheme( $akismet_url, 'https' ); 793 } 794 795 // Initial remote request 796 $response = wp_remote_post( $akismet_url, $http_args ); 797 798 // Initial request produced an error, so retry... 799 if ( ! empty( $is_ssl ) && is_wp_error( $response ) ) { 800 801 // Intermittent connection problems may cause the first HTTPS 802 // request to fail and subsequent HTTP requests to succeed randomly. 803 // Retry the HTTPS request once before disabling SSL for a time. 804 $response = wp_remote_post( $akismet_url, $http_args ); 805 806 // SSL request failed twice, so try again without it 807 if ( is_wp_error( $response ) ) { 808 $response = wp_remote_post( $http_akismet_url, $http_args ); 809 $ssl_failed = true; 810 } 811 } 812 813 // Bail if errored 771 814 if ( is_wp_error( $response ) ) { 772 815 return array( '', '' ); 773 816 } 774 817 818 // Maybe disable SSL for future requests 819 if ( ! empty( $ssl_failed ) ) { 820 update_option( 'akismet_ssl_disabled', $now ); 821 } 822 775 823 // No errors so return response 776 return array( $response['headers'], $response['body'] ); 824 return array( 825 $response['headers'], 826 $response['body'] 827 ); 777 828 } 778 829
Note: See TracChangeset
for help on using the changeset viewer.