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
11class action_plugin_linkback_receive extends DokuWiki_Action_Plugin {
12
13    /**
14     * Register the eventhandlers.
15     */
16    function register(Doku_Event_Handler $controller) {
17        $controller->register_hook('TPL_ACT_RENDER', 'BEFORE', $this, 'handle_act_render', array ());
18        $controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'handle_metaheader_output', array ());
19        $controller->register_hook('ACTION_HEADERS_SEND', 'BEFORE', $this, 'handle_headers_send', array ());
20    }
21
22    /**
23     * Handler for the TPL_ACT_RENDER event
24     */
25    function handle_act_render(Doku_Event $event, $params) {
26        global $ID;
27
28        // Action not 'show'? Quit
29        if ($event->data != 'show')
30            return;
31
32        // Trackbacks disabled? Quit
33        if (!$this->getConf('enable_trackback'))
34            return;
35
36        // Get linkback metadata
37        $file = metaFN($ID, '.linkbacks');
38        $data = array (
39            'send' => false,
40            'receive' => false,
41            'display' => false,
42            'sentpings' => array (),
43            'receivedpings' => array (),
44            'number' => 0,
45
46        );
47        if (@ file_exists($file))
48            $data = unserialize(io_readFile($file, false));
49
50        // Does not accept linkbacks? Quit
51        if (!$data['receive'])
52            return;
53
54        // if trackbacks are enabled, insert RDF definition of trackback into output
55        if ($this->getConf('enable_trackback')) {
56            echo '<!--<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"' . NL .
57            'xmlns:dc="https://purl.org/dc/elements/1.1/"' . NL .
58            'xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">' . NL .
59            '<rdf:Description' . NL .
60            'rdf:about="' . wl($ID, '', true) . '"' . NL .
61            'dc:identifier="' . wl($ID, '', true) . '"' . NL .
62            'dc:title="' . tpl_pagetitle($ID, true) . '"' . NL .
63            'trackback:ping="' . DOKU_URL . 'lib/plugins/linkback/exe/trackback.php/' . $ID . '" />' . NL .
64            '</rdf:RDF>-->';
65        }
66    }
67
68    /**
69     * Handler for the TPL_METAHEADER_OUTPUT event
70     * @param Doku_Event $event
71     * @param $params
72     * @return bool|void
73     */
74    function handle_metaheader_output(Doku_Event $event, $params) {
75        global $ID;
76
77        // Pingbacks disabled? Quit
78        if (!$this->getConf('enable_pingback'))
79            return;
80
81        // Get linkback metadata
82        $file = metaFN($ID, '.linkbacks');
83        $data = array (
84            'send' => false,
85            'receive' => false,
86            'display' => false,
87            'sentpings' => array (),
88            'receivedpings' => array (),
89            'number' => 0,
90
91        );
92        if (@ file_exists($file))
93            $data = unserialize(io_readFile($file, false));
94
95        // Does not accept linkbacks? Quit
96        if (!$data['receive'])
97            return;
98
99        // Add pingback metaheader
100        $event->data['link'][] = array (
101            'rel' => 'pingback',
102            'href' => DOKU_URL . 'lib/plugins/linkback/exe/pingback.php/' . $ID
103        );
104        return true;
105    }
106
107    /**
108     * Handler for the ACTION_HEADERS_SEND event
109     *
110     * @param Doku_Event $event
111     * @param $params
112     * @return bool|void
113     */
114    function handle_headers_send(Doku_Event $event, $params) {
115        global $ID;
116
117        // Pingbacks disabled? Quit
118        if (!$this->getConf('enable_pingback'))
119            return;
120
121        // Get linkback metadata
122        $file = metaFN($ID, '.linkbacks');
123        $data = array (
124            'send' => false,
125            'receive' => false,
126            'display' => false,
127            'sentpings' => array (),
128            'receivedpings' => array (),
129            'number' => 0,
130
131        );
132        if (@ file_exists($file))
133            $data = unserialize(io_readFile($file, false));
134
135        // Does not accept linkbacks? Quit
136        if (!$data['receive'])
137            return;
138
139        // Add pingback header
140        $event->data[] = 'X-Pingback: ' . DOKU_URL . 'lib/plugins/linkback/exe/pingback.php/' . $ID;
141        return true;
142    }
143}
144