Skip to content
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

Send webmention when adding link #201

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,29 @@ function html_extract_title($html)
return preg_match('!<title>(.*?)</title>!is', $html, $matches) ? trim(str_replace("\n",' ', $matches[1])) : '' ;
}

// Send a [webmention](http://webmention.org/) to the giver $url referencing the $source.
function sendWebmention($url, $source)
{
list($status,$headers,$data)=getHTTP($url,4);
if (strpos($status,'200 OK')!==false)
{
$endpoint=null;
// Get webmention endpoint. Can be rel="webmention", rel="http://webmention.org/", rel="webmention http://webmention.org/", etc.
preg_match('!<link .*rel *= *\"(?:(?:webmention|http://webmention.org/?) *)+\" .*href *= *\"(.+?)\"!',$data,$matches);
if (!empty($matches[1])) $endpoint=$matches[1];
preg_match('!<link .*href *= *\"(.+?)\" .*rel *= *\"(?:(?:webmention|http://webmention.org/?) *)+\"!',$data,$matches); // The order between rel and href can be different
if (!empty($matches[1])) $endpoint=$matches[1];
if ($endpoint)
{
$postdata=http_build_query(array('source'=>$source,'target'=>$url));
$options=array('http'=>array('method'=>'POST','timeout'=>4,'header'=>'Content-type: application/x-www-form-urlencoded','content'=>$postdata));
$context=stream_context_create($options);
$res = file_get_contents($endpoint, false, $context); // Don't care the result
}
}
}


// ------------------------------------------------------------------------------------------
// Token management for XSRF protection
// Token should be used in any form which acts on data (create,update,delete,import...).
Expand All @@ -625,6 +648,7 @@ function tokenOk($token)
return false; // Wrong token, or already used.
}


// ------------------------------------------------------------------------------------------
/* This class is in charge of building the final page.
(This is basically a wrapper around RainTPL which pre-fills some fields.)
Expand Down Expand Up @@ -1489,6 +1513,7 @@ function renderPage()
if ($link['title']=='') $link['title']=$link['url']; // If title is empty, use the URL as title.
$LINKSDB[$linkdate] = $link;
$LINKSDB->savedb(); // save to disk
sendWebmention($url, $_SERVER['SCRIPT_URI'].'?'.smallHash($linkdate));
pubsubhub();

// If we are called from the bookmarklet, we must close the popup:
Expand Down