1<?php
2
3/**
4 * Receive component of the DokuWiki Linkback action 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
11/**
12 * Class action_plugin_blogtng_linkback
13 */
14class action_plugin_blogtng_linkback extends DokuWiki_Action_Plugin {
15    private $run = false;
16
17    /**
18     * Registers a callback function for a given event
19     *
20     * @param Doku_Event_Handler $controller
21     */
22    public function register(Doku_Event_Handler $controller) {
23        $controller->register_hook('DOKUWIKI_STARTED', 'BEFORE', $this, 'checkIfLinkbackAllowed', array ());
24        $controller->register_hook('TPL_ACT_RENDER', 'BEFORE', $this, 'addTrackbackLink', array ());
25        $controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'addPingbackToMetaHeader', array ());
26        $controller->register_hook('ACTION_HEADERS_SEND', 'BEFORE', $this, 'addPinkbackToHTTPHeader', array ());
27    }
28
29    /**
30     * Set $this->run to true if linkback is allowed.
31     *
32     * @param Doku_Event $event  event object by reference
33     * @param array      $params  empty array as passed to register_hook()
34     */
35    public function checkIfLinkbackAllowed(Doku_Event $event, $params) {
36        /** @var helper_plugin_blogtng_linkback $helper */
37        $helper = plugin_load('helper', 'blogtng_linkback');
38        if (!$helper->linkbackAllowed()) {
39            return;
40        }
41        $this->run = true;
42    }
43
44    /**
45     * Handler for the TPL_ACT_RENDER event
46     *
47     * @param Doku_Event $event  event object by reference
48     * @param array      $params  empty array as passed to register_hook()
49     */
50    public function addTrackbackLink(Doku_Event $event, $params) {
51        if (!$this->run) return;
52
53        // Action not 'show'? Quit
54        if ($event->data != 'show')
55            return;
56
57        global $ID;
58        // insert RDF definition of trackback into output
59        echo '<!--<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"' . NL .
60        'xmlns:dc="https://purl.org/dc/elements/1.1/"' . NL .
61        'xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">' . NL .
62        '<rdf:Description' . NL .
63        'rdf:about="' . wl($ID, '', true) . '"' . NL .
64        'dc:identifier="' . wl($ID, '', true) . '"' . NL .
65        'dc:title="' . tpl_pagetitle($ID, true) . '"' . NL .
66        'trackback:ping="' . DOKU_URL . 'lib/plugins/blogtng/exe/trackback.php/' . $ID . '" />' . NL .
67        '</rdf:RDF>-->';
68    }
69
70    /**
71     * Handler for the TPL_METAHEADER_OUTPUT event
72     *
73     * @param Doku_Event $event  event object by reference
74     * @param array      $params  empty array as passed to register_hook()
75     * @return void|bool
76     */
77    public function addPingbackToMetaHeader(Doku_Event $event, $params) {
78        if (!$this->run) return;
79        global $ID;
80
81        // Add pingback metaheader
82        $event->data['link'][] = array (
83            'rel' => 'pingback',
84            'href' => DOKU_URL . 'lib/plugins/blogtng/exe/pingback.php/' . $ID
85        );
86        return true;
87    }
88
89    /**
90     * Handler for the ACTION_HEADERS_SEND event
91     *
92     * @param Doku_Event $event  event object by reference
93     * @param array      $params  empty array as passed to register_hook()
94     * @return void|bool
95     */
96    public function addPinkbackToHTTPHeader(Doku_Event $event, $params) {
97        if (!$this->run) return;
98        global $ID;
99
100        // Add pingback header
101        $event->data[] = 'X-Pingback: ' . DOKU_URL . 'lib/plugins/blogtng/exe/pingback.php/' . $ID;
102        return true;
103    }
104}
105