1<?php 2 3/** 4 * DokuWiki Plugin preview (Action Component) 5 * 6 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 7 * @author Andreas Gohr <gohr@cosmocode.de> 8 */ 9class action_plugin_preview extends \dokuwiki\Extension\ActionPlugin 10{ 11 12 /** @inheritDoc */ 13 public function register(Doku_Event_Handler $controller) 14 { 15 $controller->register_hook('DOKUWIKI_STARTED', 'BEFORE', $this, 'handleConfig'); 16 $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handlePreview'); 17 } 18 19 /** 20 * Event handler for DOKUWIKI_STARTED 21 * 22 * @see https://www.dokuwiki.org/devel:events:DOKUWIKI_STARTED 23 * @param Doku_Event $event Event object 24 * @param mixed $param optional parameter passed when event was registered 25 * @return void 26 */ 27 public function handleConfig(Doku_Event $event, $param) { 28 global $JSINFO; 29 $JSINFO['plugin']['preview'] = [ 30 'selector' => $this->getConf('selector'), 31 ]; 32 } 33 34 35 /** 36 * Event handler for AJAX_CALL_UNKNOWN 37 * 38 * @see https://www.dokuwiki.org/devel:events:AJAX_CALL_UNKNOWN 39 * @param Doku_Event $event Event object 40 * @param mixed $param optional parameter passed when event was registered 41 * @return void 42 */ 43 public function handlePreview(Doku_Event $event, $param) 44 { 45 if ($event->data != 'plugin_preview') return; 46 $event->preventDefault(); 47 $event->stopPropagation(); 48 49 global $INPUT; 50 51 $id = $INPUT->str('id'); 52 if (!$id) http_status(404, 'No ID given'); 53 if (!page_exists($id)) http_status(404, 'Page does not exist'); 54 if (!auth_quickaclcheck($id) >= AUTH_READ) http_status(403, 'Access denied'); 55 56 $title = trim(p_get_first_heading($id)); 57 if ($title == '') http_status(404, 'Page has no title, probably not important'); 58 $abstract = p_get_metadata($id, 'description abstract'); 59 $image = p_get_metadata($id, 'relation firstimage'); 60 61 $abstract = substr($abstract, strlen($title)); // remove title from abstract 62 $abstract = trim($abstract, '.…') . '…'; // always have ellipsis 63 64 header('Content-Type: text/html; charset=utf-8'); 65 echo '<h2>' . hsc($title) . '</h2>'; 66 echo '<p>' . hsc($abstract) . '</p>'; 67 if ($image) echo '<img src="' . ml($image, ['w' => 400]) . '" alt="" />'; 68 } 69 70} 71 72