-
-
Notifications
You must be signed in to change notification settings - Fork 32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Receiving Vouches #181
Merged
Merged
Receiving Vouches #181
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ebb80e2
Add basic vouch receiving and add link to add to whitelist
dshanske d4458af
Fix for String Issue
dshanske 58ffd4c
Spec calls for 400
dshanske 02f0829
Put Vouch check behind experimental flag
dshanske f4d2895
Change name of function
dshanske fd2e55e
Merge branch 'master' into vouch
pfefferle 3ca6240
Requested changes
dshanske 54a7582
Remove extra call
dshanske File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
<?php | ||
/** | ||
* Webmention Vouch Class | ||
* | ||
* @author Matthias Pfefferle | ||
*/ | ||
class Webmention_Vouch { | ||
/** | ||
* Initialize, registering WordPress hooks | ||
*/ | ||
public static function init() { | ||
// Webmention helper | ||
add_filter( 'webmention_comment_data', array( 'Webmention_Vouch', 'verify_vouch' ), 10, 1 ); | ||
|
||
self::register_meta(); | ||
} | ||
|
||
/** | ||
* This is more to lay out the data structure than anything else. | ||
*/ | ||
public static function register_meta() { | ||
// Purpose of this is to store whether something has been vouched for | ||
$args = array( | ||
'type' => 'int', | ||
'description' => __( 'Has this Webmention Been Vouched for', 'webmention' ), | ||
'single' => true, | ||
'show_in_rest' => true, | ||
); | ||
register_meta( 'comment', 'webmention_vouched', $args ); | ||
} | ||
|
||
/** | ||
* Verifies vouch is valid and either return an error if not verified or return the array with retrieved | ||
* data. For right now, it just sets a parameter whether or not it would be vouched | ||
* | ||
* @param array $data { | ||
* $comment_type | ||
* $comment_author_url | ||
* $comment_author_IP | ||
* $target | ||
* $vouch | ||
* } | ||
* | ||
* @return array|WP_Error $data Return Error Object or array with added fields { | ||
* $remote_source | ||
* $remote_source_original | ||
* $content_type | ||
* } | ||
* | ||
* @uses apply_filters calls "http_headers_useragent" on the user agent | ||
*/ | ||
public static function verify_vouch( $data ) { | ||
if ( ! $data || is_wp_error( $data ) ) { | ||
return $data; | ||
} | ||
|
||
if ( ! is_array( $data ) || empty( $data ) ) { | ||
return new WP_Error( 'invalid_data', __( 'Invalid data passed', 'webmention' ), array( 'status' => 500 ) ); | ||
} | ||
// The remaining instructions only apply if there is a vouch parameter | ||
if ( ! isset( $data['vouch'] ) ) { | ||
return $data; | ||
} | ||
$data['comment_meta']['webmention_vouch_url'] = esc_url_raw( $data['vouch'] ); | ||
|
||
// Is the person vouching for the relationship using a page on your own site? | ||
$vouch_id = url_to_postid( $data['vouch'] ); | ||
if ( $vouch_id ) { | ||
$data['comment_meta']['webmention_vouched'] = '1'; | ||
return $data; | ||
} | ||
|
||
$wp_version = get_bloginfo( 'version' ); | ||
|
||
$user_agent = apply_filters( 'http_headers_useragent', 'WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' ) ); | ||
$args = array( | ||
'timeout' => 100, | ||
'limit_response_size' => 153600, | ||
'redirection' => 20, | ||
'user-agent' => "$user_agent; verifying Vouch from " . $data['comment_author_IP'], | ||
); | ||
|
||
$response = wp_safe_remote_head( $data['vouch'], $args ); | ||
|
||
// check if vouch is accessible if not reject | ||
if ( is_wp_error( $response ) ) { | ||
return new WP_Error( | ||
'vouch_not_found', __( 'Vouch Not Found', 'webmention' ), array( | ||
'status' => 400, | ||
'data' => $data, | ||
) | ||
); | ||
} | ||
|
||
// A valid response code from the other server would not be considered an error. | ||
$response_code = wp_remote_retrieve_response_code( $response ); | ||
// not an (x)html, sgml, or xml page, so asking the sender to try again | ||
if ( preg_match( '#(image|audio|video|model)/#is', wp_remote_retrieve_header( $response, 'content-type' ) ) ) { | ||
return new WP_Error( | ||
'vouch_not_found', __( 'Vouch Not Found', 'webmention' ), array( | ||
'status' => 449, | ||
'data' => $data, | ||
) | ||
); | ||
} | ||
if ( 200 !== $response_code ) { | ||
return new WP_Error( | ||
'vouch_error', array( | ||
'status' => 400, | ||
'data' => array( $data, $response_code ), | ||
) | ||
); | ||
} | ||
// If this is not | ||
if ( ! Webmention_Receiver::is_source_whitelisted( $data['vouch'] ) ) { | ||
$data['comment_meta']['webmention_vouched'] = '0'; | ||
return $data; | ||
} | ||
|
||
$response = wp_safe_remote_get( $data['vouch'], $args ); | ||
$urls = wp_extract_urls( wp_remote_retrieve_body( $response ) ); | ||
foreach ( $urls as $url ) { | ||
if ( wp_parse_url( $url, PHP_URL_HOST ) === wp_parse_url( $data['source'] ) ) { | ||
$data['comment_meta']['webmention_vouched'] = '1'; | ||
return $data; | ||
} | ||
} | ||
return new WP_Error( | ||
'vouch_error', __( 'Vouch Not Found', 'webmention' ), array( | ||
'status' => 400, | ||
'data' => $data, | ||
) | ||
); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you move this to the vouch class, otherwise it will load, if vouch is enabled or not...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like to store the vouch URL whether or not vouch is enabled or not, in the event it becomes stable in future. Is that fine, or do you feel strongly it shouldn't store it unless enabled?