Nothing Special   »   [go: up one dir, main page]

Skip to content

Commit

Permalink
Added ability to send webmentions.
Browse files Browse the repository at this point in the history
  • Loading branch information
sandeepshetty committed Jun 4, 2013
1 parent 8fd5b43 commit 02f0be8
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
26 changes: 26 additions & 0 deletions helpers.php
Original file line number Diff line number Diff line change
@@ -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/';
Expand Down
29 changes: 27 additions & 2 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
});


app\post('/post', function($req) {
app\post(array('/post', '/send-webmention'), function($req) {

if (!isset($_SESSION['user']))
{
Expand Down Expand Up @@ -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/"'
));
});

Expand Down
2 changes: 2 additions & 0 deletions templates/index.html.php
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down

0 comments on commit 02f0be8

Please sign in to comment.