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