+
+ public static function pre_check_pingback( $method ) {
+ if ( $method !== 'pingback.ping' )
+ return;
+
+ global $wp_xmlrpc_server;
+
+ if ( !is_object( $wp_xmlrpc_server ) )
+ return false;
+
+ // Lame: tightly coupled with the IXR class.
+ $args = $wp_xmlrpc_server->message->params;
+
+ if ( !empty( $args[1] ) ) {
+ $post_id = url_to_postid( $args[1] );
+
+ // If this gets through the pre-check, make sure we properly identify the outbound request as a pingback verification
+ Akismet::pingback_forwarded_for( null, $args[0] );
+ add_filter( 'http_request_args', array( 'Akismet', 'pingback_forwarded_for' ), 10, 2 );
+
+ $comment = array(
+ 'comment_author_url' => $args[0],
+ 'comment_post_ID' => $post_id,
+ 'comment_author' => '',
+ 'comment_author_email' => '',
+ 'comment_content' => '',
+ 'comment_type' => 'pingback',
+ 'akismet_pre_check' => '1',
+ 'comment_pingback_target' => $args[1],
+ );
+
+ $comment = Akismet::auto_check_comment( $comment );
+
+ if ( isset( $comment['akismet_result'] ) && 'true' == $comment['akismet_result'] ) {
+ // Lame: tightly coupled with the IXR classes. Unfortunately the action provides no context and no way to return anything.
+ $wp_xmlrpc_server->error( new IXR_Error( 0, 'Invalid discovery target' ) );
+ }
+ }
+ }
+
+ public static function pingback_forwarded_for( $r, $url ) {
+ static $urls = array();
+
+ // Call this with $r == null to prime the callback to add headers on a specific URL
+ if ( is_null( $r ) && !in_array( $url, $urls ) ) {
+ $urls[] = $url;
+ }
+
+ // Add X-Pingback-Forwarded-For header, but only for requests to a specific URL (the apparent pingback source)
+ if ( is_array( $r ) && is_array( $r['headers'] ) && !isset( $r['headers']['X-Pingback-Forwarded-For'] ) && in_array( $url, $urls ) ) {
+ $remote_ip = preg_replace( '/[^a-fx0-9:.,]/i', '', $_SERVER['REMOTE_ADDR'] );
+
+ // Note: this assumes REMOTE_ADDR is correct, and it may not be if a reverse proxy or CDN is in use
+ $r['headers']['X-Pingback-Forwarded-For'] = $remote_ip;
+
+ // Also identify the request as a pingback verification in the UA string so it appears in logs
+ $r['user-agent'] .= '; verifying pingback from ' . $remote_ip;
+ }
+
+ return $r;
+ }
+
+ /**
+ * Ensure that we are loading expected scalar values from akismet_as_submitted commentmeta.
+ *
+ * @param mixed $meta_value
+ * @return mixed
+ */
+ private static function sanitize_comment_as_submitted( $meta_value ) {
+ if ( empty( $meta_value ) ) {
+ return $meta_value;
+ }
+
+ $meta_value = (array) $meta_value;
+
+ foreach ( $meta_value as $key => $value ) {
+ if ( ! isset( self::$comment_as_submitted_allowed_keys[$key] ) || ! is_scalar( $value ) ) {
+ unset( $meta_value[$key] );
+ }
+ }
+
+ return $meta_value;
+ }
+}