Files
oddmuse/modules/pingback-server.pl

141 lines
4.6 KiB
Perl
Raw Permalink Normal View History

2004-05-26 22:46:46 +00:00
# Copyright (C) 2004 Brock Wilcox <awwaiid@thelackthereof.org>
2019-01-26 22:17:19 +01:00
# Copyright (C) 2019 Alex Schroeder <alex@gnu.org>
2004-05-26 22:46:46 +00:00
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
2004-05-26 22:46:46 +00:00
# (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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2004-05-26 22:46:46 +00:00
use strict;
use v5.10;
2004-05-26 22:46:46 +00:00
2019-01-26 22:17:19 +01:00
use LWP::UserAgent;
2004-05-26 22:46:46 +00:00
use RPC::XML::Parser;
2019-01-26 22:17:19 +01:00
use RPC::XML;
2004-05-26 22:46:46 +00:00
2015-04-04 23:12:55 +03:00
AddModuleDescription('pingback-server.pl', 'Pingback Server Extension');
2019-01-26 22:17:19 +01:00
# Specification: http://www.hixie.ch/specs/pingback/pingback
# XML-RPC errors: http://xmlrpc-epi.sourceforge.net/specs/rfc.fault_codes.php
2004-05-26 22:46:46 +00:00
2019-01-26 22:17:19 +01:00
our ($CommentsPrefix, $q, $HtmlHeaders, %Action, $QuestionaskerSecretKey,
@MyInitVariables, %IndexHash);
2004-05-26 22:46:46 +00:00
2019-01-26 22:17:19 +01:00
push(@MyInitVariables, \&PingbackServerAddLink);
sub PingbackServerAddLink {
SetParam('action', 'pingback') if $q->path_info =~ m|/pingback\b|;
my $id = GetId();
return unless $id;
return if $id =~ /^$CommentsPrefix/;
my $link = '<link rel="alternate" type="application/wiki" href="'
. ScriptUrl('pingback/' . UrlEncode($id)) . '" />';
$HtmlHeaders .= $link unless index($HtmlHeaders, /$link/) != -1;
2004-05-26 22:46:46 +00:00
}
2019-01-26 22:17:19 +01:00
$Action{pingback} = \&DoPingbackServer;
2004-05-26 22:46:46 +00:00
sub DoPingbackServer {
my $id = FreeToNormal(shift);
2019-01-26 22:17:19 +01:00
# some sanity checks for the request
if ($q->request_method() ne 'POST') {
ReportError(T('Only XML-RPC POST requests recognised'), '405 METHOD NOT ALLOWED');
}
if ($q->content_type() ne 'text/xml') {
ReportError(T('Only XML-RPC POST requests recognised'), '415 UNSUPPORTED MEDIA TYPE');
}
2004-05-26 22:46:46 +00:00
2019-01-26 22:17:19 +01:00
# some sanity checks for the target page name
if (not $id) {
PingbackServerFault('400 NO ID', 33, "No page specified");
2019-01-26 22:17:19 +01:00
}
my $error = ValidId($id);
if ($error) {
PingbackServerFault('400 INVALID ID', 33, "Invalid page name: $id");
2004-05-26 22:46:46 +00:00
}
2019-01-26 22:17:19 +01:00
# check the IP number for bans
my $rule = UserIsBanned();
if ($rule) {
PingbackServerFault('403 FORBIDDEN', 49, "Your IP number is blocked");
2004-05-26 22:46:46 +00:00
}
2019-01-26 22:17:19 +01:00
# check that the target page exists
AllPagesList();
if (not $IndexHash{$id}) {
PingbackServerFault('404 NOT FOUND', 32, "Page does not exist: $id");
}
2004-05-26 22:46:46 +00:00
2019-01-26 22:17:19 +01:00
# parse the remote procedure call
my $data = $q->param('POSTDATA');
2004-05-26 22:46:46 +00:00
my $parser = RPC::XML::Parser->new();
2019-01-26 22:17:19 +01:00
my $request = $parser->parse($data);
2004-05-26 22:46:46 +00:00
if (not ref($request)) {
PingbackServerFault('400 NO DATA', -32700, "Could not parse XML-RPC");
2004-05-26 22:46:46 +00:00
}
2019-01-26 22:17:19 +01:00
# sanity check the function and argument number
2004-05-26 22:46:46 +00:00
my $name = $request->name;
my $arguments = $request->args;
if ($name ne 'pingback.ping') {
2019-01-26 22:17:19 +01:00
PingbackServerFault('501 NOT IMPLEMENTED', -32601, "Method $name not supported");
2004-05-26 22:46:46 +00:00
}
if (@$arguments != 2) {
PingbackServerFault('400 WRONG NUMBER OF ARGS', -32602, "Wrong number of arguments");
2004-05-26 22:46:46 +00:00
}
2019-01-26 22:17:19 +01:00
# extract the two arguments
2004-05-26 22:46:46 +00:00
my $source = $arguments->[0]->value;
my $target = $arguments->[1]->value;
2019-01-26 22:17:19 +01:00
# verify that the source isn't banned
$rule = BannedContent($source);
if ($rule) {
PingbackServerFault('403 FORBIDDEN', 49, "The URL is blocked");
}
2004-05-26 22:46:46 +00:00
2019-01-26 22:17:19 +01:00
# verify that the pingback is legit
2004-05-26 22:46:46 +00:00
my $ua = LWP::UserAgent->new;
2019-01-26 22:17:19 +01:00
my $response = $ua->get($source);
if (not $response->is_success) {
PingbackServerFault('400 NO SOURCE', 16, "Cannot retrieve $source");
2004-05-26 22:46:46 +00:00
}
2019-01-26 22:17:19 +01:00
my $self = ScriptUrl(UrlEncode($id));
if ($response->decoded_content !~ /$self/) {
PingbackServerFault('403 FORBIDDEN', "$source does not link to $self");
}
2019-01-26 22:17:19 +01:00
$id = $CommentsPrefix . $id;
if (GetPageContent($id) =~ /$source/) {
PingbackServerFault('400 ALREADY REGISTERED', 48, "$source has already been registered");
2004-05-26 22:46:46 +00:00
}
2019-01-26 22:17:19 +01:00
# post a comment without redirect at the end
SetParam('aftertext', 'Pingback: ' . $source);
SetParam('summary', 'Pingback');
SetParam('username', T('Anonymous'));
SetParam($QuestionaskerSecretKey, 1) if $QuestionaskerSecretKey;
local *ReBrowsePage = sub {};
2019-01-26 22:17:19 +01:00
DoPost($id);
# response
my $message = "Oddmuse PingbackServer! $id OK";
my $response = RPC::XML::response->new(RPC::XML::string->new($message));
print GetHttpHeader('text/xml', 'nocache', '200 OK'), $response->as_string, "\n\n";
}
2004-05-26 22:46:46 +00:00
2019-01-26 22:17:19 +01:00
sub PingbackServerFault {
my($status, $error, $data) = @_;
my $fault = RPC::XML::response->new(RPC::XML::fault->new($error, $data));
print GetHttpHeader('text/xml', 'nocache', $status), $fault->as_string, "\n\n";
exit 2;
}