Skip to content

Commit

Permalink
Init logic to receive webmentions
Browse files Browse the repository at this point in the history
Adds a codepath that would store them as trackback or pingback, depending on the metadata available on the sending page.
  • Loading branch information
onli committed Jun 2, 2024
1 parent 1e7edd4 commit 1c75f66
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
16 changes: 16 additions & 0 deletions comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,22 @@
log_pingback('PINGBACK FAILURE');;
report_pingback_failure();
}
} else if ($type == 'webmention') {
if (isset($_REQUEST['entry_id'])) {
$id = (int)$_REQUEST['entry_id'];;
} else if (preg_match('@/(\d+)_[^/]*$@', $uri, $matches)) {
$id = (int)$matches[1];
}

// TODO: Check that given $target url matches the $id of this endpoint

if (add_webmention($id, $_REQUEST['source'], $_REQUEST['target'] )) {
log_trackback('WEBMENTION SUCCESS');
report_trackback_success();
} else {
log_trackback('WEBMENTION FAILURE');
report_trackback_failure();
}
} else {
$id = (int)(!empty($serendipity['POST']['entry_id']) ? $serendipity['POST']['entry_id'] : $serendipity['GET']['entry_id']);
$serendipity['head_subtitle'] = COMMENTS;
Expand Down
59 changes: 59 additions & 0 deletions include/functions_trackbacks.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,65 @@ function add_pingback($id, $postdata) {
return 0;
}

/**
* Receive a webmention. Converts the webmention to a trackback for internal storage
*
* @access public
* @param int The ID of our entry
* @param string The URL of the foreign blog
* @return true
*/
function add_webmention($id, $url, $target) {
global $serendipity;

log_trackback('add_trackback:' . print_r(func_get_args(), true));

// We can't accept a webmention if we don't get any URL
// This is a protocol rule.
if (empty($url)) {
log_trackback('Empty URL.');

return 0;
}

// We need to get a fallback name. A easy solution would be to fetch the HEAD of the $url and
// fetch the title. Alternative and suggestion from indieweb.org: rel-author
$sourceHTML = serendipity_request_url($url)
$doc = new DOMDocument();
$doc->loadHTML($sourceHTML);
$xpath = new DOMXPath($doc);
$name = $xpath->query('//head/title');

// We could now already store the webmention like a pingback. The source (in $url) still needs
// to be verified, but he spamblock plugin does that for us later when storing this as
// trackback or pingback.
// But we can also try to detect more metadata first by interpreting the microformat on the
// source page. See https://indieweb.org/comments#How_to_display
$microdata = Mf2\parse($sourceHTML, '$url');
if ($microdata) {
$title = ''
$excerpt = ''
// TODO: Check the microdata entry for a $title and an $excerpt, and optionally for a better
// name

if ($title != '' && $excerpt != '') {
// Now we have everything to store the webmention as a trackback
return add_trackback($id, $title, $url, $name, $excerpt)
}
}

if ($id>0) {
$comment['title'] = 'Webmention';
$comment['url'] = $url;
$comment['comment'] = '';
$comment['name'] = $name;
serendipity_saveComment($id, $comment, 'PINGBACK');
return 1;
}
return 0;
}


function evaluateIdByLocalUrl($localUrl) {
global $serendipity;

Expand Down
1 change: 1 addition & 0 deletions templates/2k11/index.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
{if $entry_id}
<link rel="trackback" type="application/x-www-form-urlencoded" href="{$serendipityBaseURL}comment.php?type=trackback&amp;entry_id={$entry_id}">
<link rel="pingback" href="{$serendipityBaseURL}comment.php?type=pingback&amp;entry_id={$entry_id}">
<link rel="webmention" href="{$serendipityBaseURL}comment.php?type=webmention&amp;entry_id={$entry_id}" />
{/if}
{serendipity_hookPlugin hook="frontend_header"}
<script src="{$head_link_script}"></script>
Expand Down

0 comments on commit 1c75f66

Please sign in to comment.