Commit 1be376cf authored by mmn's avatar mmn

Merge branch 'inbound-linkback' into 'master'

Accept mentions/replies/rsvps/likes via pingback and webmention

This makes the Linkback plugin bidirectional, allowing reply/mention federation between GNU Social and any pingback-enabled service (including any Wordpress site).

See merge request !41
parents 1eff1085 4f0fae9e
......@@ -32,6 +32,7 @@ if (!defined('STATUSNET')) {
}
require_once('Auth/Yadis/Yadis.php');
require_once(__DIR__ . '/lib/util.php');
define('LINKBACKPLUGIN_VERSION', '0.1');
......@@ -306,6 +307,19 @@ class LinkbackPlugin extends Plugin
}
}
public function onRouterInitialized(URLMapper $m)
{
$m->connect('main/linkback/webmention', array('action' => 'webmention'));
$m->connect('main/linkback/pingback', array('action' => 'pingback'));
}
public function onStartShowHTML($action)
{
header('Link: <' . common_local_url('webmention') . '>; rel="webmention"', false);
header('X-Pingback: ' . common_local_url('pingback'));
}
public function version()
{
return LINKBACKPLUGIN_VERSION;
......@@ -344,4 +358,34 @@ class LinkbackPlugin extends Plugin
$action_name === 'linkbacksettings');
return true;
}
function onStartNoticeSourceLink($notice, &$name, &$url, &$title)
{
// If we don't handle this, keep the event handler going
if (!in_array($notice->source, array('linkback'))) {
return true;
}
try {
$url = $notice->getUrl();
// If getUrl() throws exception, $url is never set
$bits = parse_url($url);
$domain = $bits['host'];
if (substr($domain, 0, 4) == 'www.') {
$name = substr($domain, 4);
} else {
$name = $domain;
}
// TRANS: Title. %s is a domain name.
$title = sprintf(_m('Sent from %s via Linkback'), $domain);
// Abort event handler, we have a name and URL!
return false;
} catch (InvalidUrlException $e) {
// This just means we don't have the notice source data
return true;
}
}
}
<?php
/*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
if (!defined('STATUSNET')) {
exit(1);
}
class PingbackAction extends Action
{
protected function handle()
{
GNUsocial::setApi(true); // Minimize error messages to aid in debugging
parent::handle();
if ($this->isPost()) {
return $this->handlePost();
}
return false;
}
function handlePost()
{
$server = xmlrpc_server_create();
xmlrpc_server_register_method($server, 'pingback.ping', array($this, 'ping'));
echo xmlrpc_server_call_method($server, file_get_contents('php://input'), null, array('encoding' => 'utf-8'));
xmlrpc_server_destroy($server);
return true;
}
function ping($method, $parameters) {
list($source, $target) = $parameters;
if(!$source) {
return array(
'faultCode' => 0x0010,
'faultString' => '"source" is missing'
);
}
if(!$target) {
return array(
'faultCode' => 0x0020,
'faultString' => '"target" is missing'
);
}
$response = linkback_get_source($source, $target);
if(!$response) {
return array(
'faultCode' => 0x0011,
'faultString' => 'Source does not link to target'
);
}
$notice = linkback_get_target($target);
if(!$notice) {
return array(
'faultCode' => 0x0021,
'faultString' => 'Target not found'
);
}
$url = linkback_save($source, $target, $response, $notice);
if(!$url) {
return array(
'faultCode' => 0,
'faultString' => 'An error occured while saving.'
);
}
return array('Success');
}
}
<?php
/*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
if (!defined('STATUSNET')) {
exit(1);
}
class WebmentionAction extends Action
{
protected function handle()
{
GNUsocial::setApi(true); // Minimize error messages to aid in debugging
parent::handle();
if ($this->isPost()) {
return $this->handlePost();
}
return false;
}
function handlePost()
{
$source = $this->arg('source');
$target = $this->arg('target');
header('Content-Type: text/plain; charset=utf-8');
if(!$source) {
echo _m('"source" is missing')."\n";
throw new ServerException(_m('"source" is missing'), 400);
}
if(!$target) {
echo _m('"target" is missing')."\n";
throw new ServerException(_m('"target" is missing'), 400);
}
$response = linkback_get_source($source, $target);
if(!$response) {
echo _m('Source does not link to target.')."\n";
throw new ServerException(_m('Source does not link to target.'), 400);
}
$notice = linkback_get_target($target);
if(!$notice) {
echo _m('Target not found')."\n";
throw new ServerException(_m('Target not found'), 404);
}
$url = linkback_save($source, $target, $response, $notice);
if(!$url) {
echo _m('An error occured while saving.')."\n";
throw new ServerException(_m('An error occured while saving.'), 500);
}
echo $url."\n";
return true;
}
}
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment