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