1<?php 2 3use dokuwiki\Extension\ActionPlugin; 4use dokuwiki\Extension\Event; 5use dokuwiki\Extension\EventHandler; 6use dokuwiki\plugin\extension\Extension; 7use dokuwiki\plugin\extension\GuiExtension; 8 9/** DokuWiki Plugin extension (Action Component) 10 * 11 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 12 * @author Andreas Gohr <andi@splitbrain.org> 13 */ 14class action_plugin_extension extends ActionPlugin 15{ 16 /** 17 * Registers a callback function for a given event 18 * 19 * @param EventHandler $controller DokuWiki's event controller object 20 * @return void 21 */ 22 public function register(EventHandler $controller) 23 { 24 $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handleAjaxToggle'); 25 } 26 27 /** 28 * Toggle an extension via AJAX 29 * 30 * Returns the new HTML for the extension 31 * 32 * @param Event $event 33 * @param $param 34 */ 35 public function handleAjaxToggle(Event $event, $param) 36 { 37 global $INPUT; 38 39 if ($event->data != 'plugin_extension') return; 40 $event->preventDefault(); 41 $event->stopPropagation(); 42 43 // error responses below are plain text; the success path overrides this before rendering HTML 44 header('Content-Type: text/plain; charset=utf-8'); 45 46 /** @var admin_plugin_extension $admin */ 47 $admin = plugin_load('admin', 'extension'); 48 if (!$admin->isAccessibleByCurrentUser()) { 49 http_status(403); 50 echo 'Forbidden'; 51 exit; 52 } 53 54 $ext = $INPUT->str('ext'); 55 if (!$ext) { 56 http_status(400); 57 echo 'no extension given'; 58 return; 59 } 60 61 if (getSecurityToken() != $INPUT->str('sectok')) { 62 http_status(403); 63 echo 'Security Token did not match. Possible CSRF attack.'; 64 return; 65 } 66 67 try { 68 $extension = Extension::createFromId($ext); 69 $extension->toggle(); 70 } catch (Exception $e) { 71 http_status(500); 72 echo $e->getMessage(); 73 return; 74 } 75 76 header('Content-Type: text/html; charset=utf-8'); 77 echo (new GuiExtension($extension))->render(); 78 } 79} 80