xref: /plugin/notfound/action.php (revision 4f173a43dacaeab1f9af8f3b9731cd11f9d44c3f)
1c348a0f4SAndreas Gohr<?php
2*4f173a43SAndreas Gohr
3*4f173a43SAndreas Gohruse dokuwiki\Extension\ActionPlugin;
4*4f173a43SAndreas Gohruse dokuwiki\Extension\EventHandler;
5*4f173a43SAndreas Gohruse dokuwiki\Extension\Event;
6c348a0f4SAndreas Gohr
7c348a0f4SAndreas Gohr
8*4f173a43SAndreas Gohrclass action_plugin_notfound extends ActionPlugin
9*4f173a43SAndreas Gohr{
10*4f173a43SAndreas Gohr    /** @inheritdoc */
11*4f173a43SAndreas Gohr    public function register(EventHandler $controller)
12*4f173a43SAndreas Gohr    {
13*4f173a43SAndreas Gohr        $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'check404');
14*4f173a43SAndreas Gohr        $controller->register_hook('TPL_CONTENT_DISPLAY', 'BEFORE', $this, 'show404');
15c348a0f4SAndreas Gohr    }
16c348a0f4SAndreas Gohr
17*4f173a43SAndreas Gohr    /**
18*4f173a43SAndreas Gohr     * Event handler for ACTION_ACT_PREPROCESS
19*4f173a43SAndreas Gohr     *
20*4f173a43SAndreas Gohr     * Check if the requested page exists, if not change the action to 'notfound'
21*4f173a43SAndreas Gohr     *
22*4f173a43SAndreas Gohr     * @see https://www.dokuwiki.org/devel:events:ACTION_ACT_PREPROCESS
23*4f173a43SAndreas Gohr     * @param Event $event Event object
24*4f173a43SAndreas Gohr     * @param mixed $param optional parameter passed when event was registered
25*4f173a43SAndreas Gohr     * @return bool true if the event was handled, false to let other handlers process it
26*4f173a43SAndreas Gohr     */
27*4f173a43SAndreas Gohr    public function check404(Event $event, $param)
28*4f173a43SAndreas Gohr    {
29c348a0f4SAndreas Gohr        if ($event->data != 'show') return false;
30c348a0f4SAndreas Gohr        global $INFO;
31c348a0f4SAndreas Gohr        if ($INFO['exists']) return false;
32c348a0f4SAndreas Gohr
33c348a0f4SAndreas Gohr        $event->data = 'notfound';
34c348a0f4SAndreas Gohr        $event->stopPropagation();
35c348a0f4SAndreas Gohr        $event->preventDefault();
36c348a0f4SAndreas Gohr        return true;
37c348a0f4SAndreas Gohr    }
38c348a0f4SAndreas Gohr
39*4f173a43SAndreas Gohr    /**
40*4f173a43SAndreas Gohr     * Event handler for TPL_CONTENT_DISPLAY
41*4f173a43SAndreas Gohr     *
42*4f173a43SAndreas Gohr     * If the action is 'notfound', display the configured 404 page instead
43*4f173a43SAndreas Gohr     *
44*4f173a43SAndreas Gohr     * @see https://www.dokuwiki.org/devel:events:TPL_CONTENT_DISPLAY
45*4f173a43SAndreas Gohr     * @param Event $event Event object
46*4f173a43SAndreas Gohr     * @param mixed $param optional parameter passed when event was registered
47*4f173a43SAndreas Gohr     * @return bool true if the event was handled, false to let other handlers process it
48*4f173a43SAndreas Gohr     */
49*4f173a43SAndreas Gohr    public function show404(Event $event, $param)
50*4f173a43SAndreas Gohr    {
51c348a0f4SAndreas Gohr        global $ACT;
52c348a0f4SAndreas Gohr        if ($ACT != 'notfound') return false;
53c348a0f4SAndreas Gohr        $event->stopPropagation();
54c348a0f4SAndreas Gohr        $event->preventDefault();
55c348a0f4SAndreas Gohr
56c348a0f4SAndreas Gohr        global $ID;
57c348a0f4SAndreas Gohr        $oldid = $ID;
58c348a0f4SAndreas Gohr        $ID = $this->getConf('404page');
59c348a0f4SAndreas Gohr        echo p_wiki_xhtml($ID, '', false);
60c348a0f4SAndreas Gohr        $ID = $oldid;
61c348a0f4SAndreas Gohr        $ACT = 'show';
62c348a0f4SAndreas Gohr
63c348a0f4SAndreas Gohr        return true;
64c348a0f4SAndreas Gohr    }
65c348a0f4SAndreas Gohr}
66