1<?php
2
3/**
4 * Trackback server for use with the DokuWiki Linkback Plugin.
5 *
6 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
7 * @author     Gina Haeussge <osd@foosel.net>
8 * @link       http://wiki.foosel.net/snippets/dokuwiki/linkback
9 */
10
11use dokuwiki\HTTP\DokuHTTPClient;
12
13if (!defined('DOKU_INC'))
14    define('DOKU_INC', realpath(dirname(__FILE__) . '/../../../../') . '/');
15
16require_once (DOKU_INC . 'inc/init.php');
17
18/**
19 * Class TrackbackServer
20 */
21class TrackbackServer {
22
23    /** @var  helper_plugin_blogtng_linkback */
24    var $tools;
25
26    /**
27     * Construct helper and process request.
28     */
29    function __construct() {
30        $this->tools = plugin_load('helper', 'blogtng_linkback');
31        $this->_process();
32    }
33
34    /**
35     * Process trackback request.
36     */
37    function _process() {
38        // get ID
39        global $ID, $INPUT;
40        $ID = substr($_SERVER['PATH_INFO'], 1);
41        $sourceUri = $INPUT->str('url');
42
43        if (is_null($this->tools) || !$this->tools->linkbackAllowed()) {
44            $this->_printTrackbackError('Trackbacks disabled.');
45            return;
46        }
47
48        // No POST request? Quit
49        if ($INPUT->server->str('REQUEST_METHOD') != 'POST') {
50            $this->_printTrackbackError('Trackback was not received via HTTP POST.');
51            return;
52        }
53
54        // Given URL is not an url? Quit
55        if (!preg_match("#^([a-z0-9\-\.+]+?)://.*#i", $sourceUri)) {
56            $this->_printTrackbackError('Given trackback URL is not an URL.');
57            return;
58        }
59
60        // Source does not exist? Quit
61        $http = new DokuHTTPClient;
62        $page = $http->get($sourceUri);
63        if ($page === false) {
64            $this->_printTrackbackError('Linked page cannot be reached');
65            return;
66        }
67
68        if (!$this->tools->saveLinkback('trackback', strip_tags($INPUT->post->str('title')),
69                                        $sourceUri, strip_tags($INPUT->post->str('excerpt')), $ID)) {
70            $this->_printTrackbackError('Trackback already received.');
71            return;
72        }
73        $this->_printTrackbackSuccess();
74    }
75
76    /**
77     * Print trackback success xml.
78     */
79    function _printTrackbackSuccess() {
80        echo '<?xml version="1.0" encoding="iso-8859-1"?>' . NL .
81        '<response>' . NL .
82        '<error>0</error>' . NL .
83        '</response>';
84    }
85
86    /**
87     * Print trackback error xml.
88     *
89     * @param string $reason
90     */
91    function _printTrackbackError($reason = '') {
92        echo '<?xml version="1.0" encoding="iso-8859-1"?>' . NL .
93        '<response>' . NL .
94        '<error>1</error>' . NL .
95        '<message>' . $reason . '</message>' . NL .
96        '</response>';
97    }
98}
99
100$server = new TrackbackServer();
101