action.php 16 KB

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