Permalink
Browse files

Added ability to send webmentions.

  • Loading branch information...
sandeepshetty committed Jun 4, 2013
1 parent 8fd5b43 commit 02f0be8820d67f75376bed184df6b5a4d426d53b
Showing with 55 additions and 2 deletions.
  1. +26 −0 helpers.php
  2. +27 −2 index.php
  3. +2 −0 templates/index.html.php
View
@@ -1,5 +1,31 @@
<?php
use phpish\http;
function send_webmention($source, $target)
{
$response_headers = $matches = array();
$target_webmention_endpoint = false;
$response_body = http\request("GET $target", array(), array(), $response_headers);
if (isset($response_headers['link']) and preg_match('#<(httpds?://[^>]+)>; rel="http://webmention.org/"#', $response_headers['link'], $matches))
{
$target_webmention_endpoint = $matches[1];
}
elseif (preg_match('#<link href="([^"]+)" rel="http://webmention.org/" ?/?>#i', $response_body, $matches))
{
$target_webmention_endpoint = $matches[1];
}
if ($target_webmention_endpoint)
{
$response_body = http\request("POST $target_webmention_endpoint", array(), array('source'=>$source, 'target'=>$target), $response_headers);
print_r($response_headers);
print_r($response_body);
}
}
function gravatar_url($email, $s=80, $d='mm', $r='g', $img=false)
{
$url = 'http://www.gravatar.com/avatar/';
View
@@ -68,7 +68,7 @@
});
app\post('/post', function($req) {
app\post(array('/post', '/send-webmention'), function($req) {
if (!isset($_SESSION['user']))
{
@@ -107,18 +107,43 @@
});
app\post('/send-webmention', function($req) {
foreach ($req['form']['targets'] as $target) send_webmention($req['form']['source'], $target);
});
app\get('/send-webmention/{post_id:digits}', function($req) {
$post_id = $req['matches']['post_id'];
list($posts, $pager) = get_post($post_id, true);
$post = $posts[0];
$dom = new DOMDocument;
@$dom->loadHTML($post['content']);
$links = $dom->getElementsByTagName('a');
echo '<form method="post" action="'.SITE_BASE_URL.'send-webmention">';
echo '<input type="hidden" name="source" value="'.SITE_BASE_URL.$post_id.'">';
foreach ($links as $link){
$link_href = $link->getAttribute('href');
echo '<input type="checkbox" name="targets[]" value="'.$link_href.'">'.$link_href.'<br>';
}
echo '<input type="submit" name="action" value="Send WebMentions">';
echo '</form>';
});
// test with curl -d "source=foo.com&target=bar.com" <webmention-endpoint>
app\post('/webmention', function($req) {
// Figure out post_id of target
// Save to db
return app\response(array('result'=>'WebMention was successful'), 202);
});
// test with curl -I <URL>
app\any('/{post_id:digits}', function($req, $authorized=false) {
$response = app\next($req, $authorized);
return app\response($response, 200, array
(
"Link"=>'<http://'.SITE_BASE_URL.'webmention>; rel="http://webmention.org/"'
"Link"=>'<'.SITE_BASE_URL.'webmention>; rel="http://webmention.org/"'
));
});
View
@@ -62,6 +62,8 @@
<a href="http://www.facebook.com/sharer.php?s=100&p[title]=<?php echo urlencode(ltrim($post['title'], '# ')) ?>&p[url]=<?php echo urlencode(SITE_BASE_URL.$post['id']) ?>&p[summary]=<?php echo urlencode($post['raw']) ?>" target="_blank">Share on Facebook</a>
-
<a href="https://plus.google.com/share?url=<?php echo urlencode(SITE_BASE_URL.$post['id']) ?>" target="_blank">Share on Google+</a>
-
<a href="<?php echo SITE_BASE_URL ?>send-webmention/<?php echo $post['id'] ?>" target="_blank">Send WebMentions</a>
<?php endif; ?>
</div>

0 comments on commit 02f0be8

Please sign in to comment.