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