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