1<?php 2/** 3 * Include Plugin: Display a wiki page within another wiki page 4 * 5 * Action plugin component, for cache validity determination 6 * 7 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 8 * @author Christopher Smith <chris@jalakai.co.uk> 9 * @author Michael Klier <chi@chimeric.de> 10 */ 11if(!defined('DOKU_INC')) die(); // no Dokuwiki, no go 12 13if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/'); 14require_once(DOKU_PLUGIN.'action.php'); 15 16/** 17 * All DokuWiki plugins to extend the parser/rendering mechanism 18 * need to inherit from this class 19 */ 20class action_plugin_include extends DokuWiki_Action_Plugin { 21 22 var $supportedModes = array('xhtml', 'metadata'); 23 /* @var helper_plugin_include $helper */ 24 var $helper = null; 25 26 function action_plugin_include() { 27 $this->helper = plugin_load('helper', 'include'); 28 } 29 30 /** 31 * plugin should use this method to register its handlers with the dokuwiki's event controller 32 */ 33 function register(Doku_Event_Handler $controller) { 34 /* @var Doku_event_handler $controller */ 35 $controller->register_hook('INDEXER_PAGE_ADD', 'BEFORE', $this, 'handle_indexer'); 36 $controller->register_hook('INDEXER_VERSION_GET', 'BEFORE', $this, 'handle_indexer_version'); 37 $controller->register_hook('PARSER_CACHE_USE','BEFORE', $this, '_cache_prepare'); 38 $controller->register_hook('HTML_EDITFORM_OUTPUT', 'BEFORE', $this, 'handle_form'); 39 $controller->register_hook('HTML_CONFLICTFORM_OUTPUT', 'BEFORE', $this, 'handle_form'); 40 $controller->register_hook('HTML_DRAFTFORM_OUTPUT', 'BEFORE', $this, 'handle_form'); 41 $controller->register_hook('ACTION_SHOW_REDIRECT', 'BEFORE', $this, 'handle_redirect'); 42 $controller->register_hook('PARSER_HANDLER_DONE', 'BEFORE', $this, 'handle_parser'); 43 $controller->register_hook('PARSER_METADATA_RENDER', 'AFTER', $this, 'handle_metadata'); 44 $controller->register_hook('HTML_SECEDIT_BUTTON', 'BEFORE', $this, 'handle_secedit_button'); 45 $controller->register_hook('PLUGIN_MOVE_HANDLERS_REGISTER', 'BEFORE', $this, 'handle_move_register'); 46 } 47 48 /** 49 * Add a version string to the index so it is rebuilt 50 * whenever the handler is updated or the safeindex setting is changed 51 */ 52 public function handle_indexer_version($event, $param) { 53 $event->data['plugin_include'] = '0.1.safeindex='.$this->getConf('safeindex'); 54 } 55 56 /** 57 * Handles the INDEXER_PAGE_ADD event, prevents indexing of metadata from included pages that aren't public if enabled 58 * 59 * @param Doku_Event $event the event object 60 * @param array $params optional parameters (unused) 61 */ 62 public function handle_indexer(Doku_Event $event, $params) { 63 global $USERINFO; 64 65 // check if the feature is enabled at all 66 if (!$this->getConf('safeindex')) return; 67 68 // is there a user logged in at all? If not everything is fine already 69 if (is_null($USERINFO) && !isset($_SERVER['REMOTE_USER'])) return; 70 71 // get the include metadata in order to see which pages were included 72 $inclmeta = p_get_metadata($event->data['page'], 'plugin_include', METADATA_RENDER_UNLIMITED); 73 $all_public = true; // are all included pages public? 74 // check if the current metadata indicates that non-public pages were included 75 if ($inclmeta !== null && isset($inclmeta['pages'])) { 76 foreach ($inclmeta['pages'] as $page) { 77 if (auth_aclcheck($page['id'], '', array()) < AUTH_READ) { // is $page public? 78 $all_public = false; 79 break; 80 } 81 } 82 } 83 84 if (!$all_public) { // there were non-public pages included - action required! 85 // backup the user information 86 $userinfo_backup = $USERINFO; 87 $remote_user = $_SERVER['REMOTE_USER']; 88 // unset user information - temporary logoff! 89 $USERINFO = null; 90 unset($_SERVER['REMOTE_USER']); 91 92 // metadata is only rendered once for a page in one request - thus we need to render manually. 93 $meta = p_read_metadata($event->data['page']); // load the original metdata 94 $meta = p_render_metadata($event->data['page'], $meta); // render the metadata 95 p_save_metadata($event->data['page'], $meta); // save the metadata so other event handlers get the public metadata, too 96 97 $meta = $meta['current']; // we are only interested in current metadata. 98 99 // check if the tag plugin handler has already been called before the include plugin 100 $tag_called = isset($event->data['metadata']['subject']); 101 102 // Reset the metadata in the renderer. This removes data from all other event handlers, but we need to be on the safe side here. 103 $event->data['metadata'] = array('title' => $meta['title']); 104 105 // restore the relation references metadata 106 if (isset($meta['relation']['references'])) { 107 $event->data['metadata']['relation_references'] = array_keys($meta['relation']['references']); 108 } else { 109 $event->data['metadata']['relation_references'] = array(); 110 } 111 112 // restore the tag metadata if the tag plugin handler has been called before the include plugin handler. 113 if ($tag_called) { 114 $tag_helper = $this->loadHelper('tag', false); 115 if ($tag_helper) { 116 if (isset($meta['subject'])) { 117 $event->data['metadata']['subject'] = $tag_helper->_cleanTagList($meta['subject']); 118 } else { 119 $event->data['metadata']['subject'] = array(); 120 } 121 } 122 } 123 124 // restore user information 125 $USERINFO = $userinfo_backup; 126 $_SERVER['REMOTE_USER'] = $remote_user; 127 } 128 } 129 130 /** 131 * Used for debugging purposes only 132 */ 133 function handle_metadata(&$event, $param) { 134 global $conf; 135 if($conf['allowdebug']) { 136 dbglog('---- PLUGIN INCLUDE META DATA START ----'); 137 dbglog($event->data); 138 dbglog('---- PLUGIN INCLUDE META DATA END ----'); 139 } 140 } 141 142 /** 143 * Supplies the current section level to the include syntax plugin 144 * 145 * @author Michael Klier <chi@chimeric.de> 146 * @author Michael Hamann <michael@content-space.de> 147 */ 148 function handle_parser(Doku_Event &$event, $param) { 149 global $ID; 150 151 $level = 0; 152 $ins =& $event->data->calls; 153 $num = count($ins); 154 for($i=0; $i<$num; $i++) { 155 switch($ins[$i][0]) { 156 case 'plugin': 157 switch($ins[$i][1][0]) { 158 case 'include_include': 159 $ins[$i][1][1][4] = $level; 160 break; 161 /* FIXME: this doesn't work anymore that way with the new structure 162 // some plugins already close open sections 163 // so we need to make sure we don't close them twice 164 case 'box': 165 $this->helper->sec_close = false; 166 break; 167 */ 168 } 169 break; 170 case 'section_open': 171 $level = $ins[$i][1][0]; 172 break; 173 } 174 } 175 } 176 177 /** 178 * Add a hidden input to the form to preserve the redirect_id 179 */ 180 function handle_form(Doku_Event &$event, $param) { 181 if (array_key_exists('redirect_id', $_REQUEST)) { 182 $event->data->addHidden('redirect_id', cleanID($_REQUEST['redirect_id'])); 183 } 184 } 185 186 /** 187 * Modify the data for the redirect when there is a redirect_id set 188 */ 189 function handle_redirect(Doku_Event &$event, $param) { 190 if (array_key_exists('redirect_id', $_REQUEST)) { 191 // Render metadata when this is an older DokuWiki version where 192 // metadata is not automatically re-rendered as the page has probably 193 // been changed but is not directly displayed 194 $versionData = getVersionData(); 195 if ($versionData['date'] < '2010-11-23') { 196 p_set_metadata($event->data['id'], array(), true); 197 } 198 $event->data['id'] = cleanID($_REQUEST['redirect_id']); 199 $event->data['title'] = ''; 200 } 201 } 202 203 /** 204 * prepare the cache object for default _useCache action 205 */ 206 function _cache_prepare(Doku_Event &$event, $param) { 207 global $conf; 208 209 /* @var cache_renderer $cache */ 210 $cache =& $event->data; 211 212 if(!isset($cache->page)) return; 213 if(!isset($cache->mode) || !in_array($cache->mode, $this->supportedModes)) return; 214 215 $depends = p_get_metadata($cache->page, 'plugin_include'); 216 217 if($conf['allowdebug']) { 218 dbglog('---- PLUGIN INCLUDE CACHE DEPENDS START ----'); 219 dbglog($depends); 220 dbglog('---- PLUGIN INCLUDE CACHE DEPENDS END ----'); 221 } 222 223 if (!is_array($depends)) return; // nothing to do for us 224 225 if (!is_array($depends['pages']) || 226 !is_array($depends['instructions']) || 227 $depends['pages'] != $this->helper->_get_included_pages_from_meta_instructions($depends['instructions']) || 228 // the include_content url parameter may change the behavior for included pages 229 $depends['include_content'] != isset($_REQUEST['include_content'])) { 230 231 $cache->depends['purge'] = true; // included pages changed or old metadata - request purge. 232 if($conf['allowdebug']) { 233 dbglog('---- PLUGIN INCLUDE: REQUESTING CACHE PURGE ----'); 234 dbglog('---- PLUGIN INCLUDE CACHE PAGES FROM META START ----'); 235 dbglog($depends['pages']); 236 dbglog('---- PLUGIN INCLUDE CACHE PAGES FROM META END ----'); 237 dbglog('---- PLUGIN INCLUDE CACHE PAGES FROM META_INSTRUCTIONS START ----'); 238 dbglog($this->helper->_get_included_pages_from_meta_instructions($depends['instructions'])); 239 dbglog('---- PLUGIN INCLUDE CACHE PAGES FROM META_INSTRUCTIONS END ----'); 240 241 } 242 } else { 243 // add plugin.info.txt to depends for nicer upgrades 244 $cache->depends['files'][] = dirname(__FILE__) . '/plugin.info.txt'; 245 foreach ($depends['pages'] as $page) { 246 if (!$page['exists']) continue; 247 $file = wikiFN($page['id']); 248 if (!in_array($file, $cache->depends['files'])) { 249 $cache->depends['files'][] = $file; 250 } 251 } 252 } 253 } 254 255 /** 256 * Handle special section edit buttons for the include plugin to get the current page 257 * and replace normal section edit buttons when the current page is different from the 258 * global $ID. 259 */ 260 function handle_secedit_button(Doku_Event &$event, $params) { 261 // stack of included pages in the form ('id' => page, 'rev' => modification time, 'writable' => bool) 262 static $page_stack = array(); 263 264 global $ID, $lang; 265 266 $data = $event->data; 267 268 if ($data['target'] == 'plugin_include_start' || $data['target'] == 'plugin_include_start_noredirect') { 269 // handle the "section edits" added by the include plugin 270 $fn = wikiFN($data['name']); 271 $perm = auth_quickaclcheck($data['name']); 272 array_unshift($page_stack, array( 273 'id' => $data['name'], 274 'rev' => @filemtime($fn), 275 'writable' => (page_exists($data['name']) ? (is_writable($fn) && $perm >= AUTH_EDIT) : $perm >= AUTH_CREATE), 276 'redirect' => ($data['target'] == 'plugin_include_start'), 277 )); 278 } elseif ($data['target'] == 'plugin_include_end') { 279 array_shift($page_stack); 280 } elseif ($data['target'] == 'plugin_include_editbtn') { 281 if ($page_stack[0]['writable']) { 282 $params = array('do' => 'edit', 283 'id' => $page_stack[0]['id']); 284 if ($page_stack[0]['redirect']) 285 $params['redirect_id'] = $ID; 286 $event->result = '<div class="secedit">' . DOKU_LF . 287 html_btn('incledit', $page_stack[0]['id'], '', 288 $params, 'post', 289 $data['name'], 290 $lang['btn_secedit'].' ('.$page_stack[0]['id'].')') . 291 '</div>' . DOKU_LF; 292 } 293 } elseif (!empty($page_stack)) { 294 295 // Special handling for the edittable plugin 296 if ($data['target'] == 'table' && !plugin_isdisabled('edittable')) { 297 /* @var action_plugin_edittable $edittable */ 298 $edittable =& plugin_load('action', 'edittable'); 299 $data['name'] = $edittable->getLang('secedit_name'); 300 } 301 302 if ($page_stack[0]['writable'] && isset($data['name']) && $data['name'] !== '') { 303 $name = $data['name']; 304 unset($data['name']); 305 306 $secid = $data['secid']; 307 unset($data['secid']); 308 309 if ($page_stack[0]['redirect']) 310 $data['redirect_id'] = $ID; 311 312 $event->result = "<div class='secedit editbutton_" . $data['target'] . 313 " editbutton_" . $secid . "'>" . 314 html_btn('secedit', $page_stack[0]['id'], '', 315 array_merge(array('do' => 'edit', 316 'rev' => $page_stack[0]['rev'], 317 'summary' => '['.$name.'] '), $data), 318 'post', $name) . '</div>'; 319 } else { 320 $event->result = ''; 321 } 322 } else { 323 return; // return so the event won't be stopped 324 } 325 326 $event->preventDefault(); 327 $event->stopPropagation(); 328 } 329 330 public function handle_move_register(Doku_Event $event, $params) { 331 $event->data['handlers']['include_include'] = array($this, 'rewrite_include'); 332 } 333 334 public function rewrite_include($match, $pos, $state, $plugin, helper_plugin_move_handler $handler) { 335 $syntax = substr($match, 2, -2); // strip markup 336 $replacers = explode('|', $syntax); 337 $syntax = array_shift($replacers); 338 list($syntax, $flags) = explode('&', $syntax, 2); 339 340 // break the pattern up into its parts 341 list($mode, $page, $sect) = preg_split('/>|#/u', $syntax, 3); 342 $newpage = $handler->adaptRelativeId($page); 343 if ($newpage == $page) { 344 return $match; 345 } else { 346 $result = '{{'.$mode.'>'.$newpage; 347 if ($sect) $result .= '#'.$sect; 348 if ($flags) $result .= '&'.$flags; 349 if ($replacers) $result .= '|'.$replacers; 350 $result .= '}}'; 351 return $result; 352 } 353 } 354} 355// vim:ts=4:sw=4:et: 356