You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

222 lines
6.8 KiB

  1. <?php
  2. /**
  3. * Move Plugin Page Rename Functionality
  4. *
  5. * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
  6. * @author Andreas Gohr <gohr@cosmocode.de>
  7. */
  8. // must be run within Dokuwiki
  9. if(!defined('DOKU_INC')) die();
  10. /**
  11. * Class action_plugin_move_rename
  12. */
  13. class action_plugin_move_rename extends DokuWiki_Action_Plugin {
  14. /**
  15. * Register event handlers.
  16. *
  17. * @param Doku_Event_Handler $controller The plugin controller
  18. */
  19. public function register(Doku_Event_Handler $controller) {
  20. $controller->register_hook('DOKUWIKI_STARTED', 'AFTER', $this, 'handle_init');
  21. // TODO: DEPRECATED JAN 2018
  22. $controller->register_hook('TEMPLATE_PAGETOOLS_DISPLAY', 'BEFORE', $this, 'handle_pagetools');
  23. $controller->register_hook('MENU_ITEMS_ASSEMBLY', 'AFTER', $this, 'addsvgbutton', array());
  24. $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handle_ajax');
  25. $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handleAjaxMediaManager');
  26. }
  27. /**
  28. * set JavaScript info if renaming of current page is possible
  29. */
  30. public function handle_init() {
  31. global $JSINFO;
  32. global $INFO;
  33. global $INPUT;
  34. global $USERINFO;
  35. if (isset($INFO['id'])) {
  36. $JSINFO['move_renameokay'] = $this->renameOkay($INFO['id']);
  37. } else {
  38. $JSINFO['move_renameokay'] = false;
  39. }
  40. $JSINFO['move_allowrename'] = auth_isMember(
  41. $this->getConf('allowrename'),
  42. $INPUT->server->str('REMOTE_USER'),
  43. $USERINFO['grps'] ?? []
  44. );
  45. }
  46. /**
  47. * Adds a button to the default template
  48. *
  49. * TODO: DEPRECATED JAN 2018
  50. *
  51. * @param Doku_Event $event
  52. */
  53. public function handle_pagetools(Doku_Event $event) {
  54. if($event->data['view'] != 'main') return;
  55. if (!$this->getConf('pagetools_integration')) {
  56. return;
  57. }
  58. $newitem = '<li class="plugin_move_page"><a href=""><span>' . $this->getLang('renamepage') . '</span></a></li>';
  59. $offset = count($event->data['items']) - 1;
  60. $event->data['items'] =
  61. array_slice($event->data['items'], 0, $offset, true) +
  62. array('plugin_move' => $newitem) +
  63. array_slice($event->data['items'], $offset, null, true);
  64. }
  65. /**
  66. * Add 'rename' button to page tools, new SVG based mechanism
  67. *
  68. * @param Doku_Event $event
  69. */
  70. public function addsvgbutton(Doku_Event $event) {
  71. global $INFO, $JSINFO;
  72. if(
  73. $event->data['view'] !== 'page' ||
  74. !$this->getConf('pagetools_integration') ||
  75. empty($JSINFO['move_renameokay'])
  76. ) {
  77. return;
  78. }
  79. if(!$INFO['exists']) {
  80. return;
  81. }
  82. array_splice($event->data['items'], -1, 0, array(new \dokuwiki\plugin\move\MenuItem()));
  83. }
  84. /**
  85. * Rename a single page
  86. */
  87. public function handle_ajax(Doku_Event $event) {
  88. if($event->data != 'plugin_move_rename') return;
  89. $event->preventDefault();
  90. $event->stopPropagation();
  91. global $MSG;
  92. global $INPUT;
  93. $src = cleanID($INPUT->str('id'));
  94. $dst = cleanID($INPUT->str('newid'));
  95. /** @var helper_plugin_move_op $MoveOperator */
  96. $MoveOperator = plugin_load('helper', 'move_op');
  97. $JSON = new JSON();
  98. header('Content-Type: application/json');
  99. if($this->renameOkay($src) && $MoveOperator->movePage($src, $dst)) {
  100. // all went well, redirect
  101. echo $JSON->encode(array('redirect_url' => wl($dst, '', true, '&')));
  102. } else {
  103. if(isset($MSG[0])) {
  104. $error = $MSG[0]; // first error
  105. } else {
  106. $error = $this->getLang('cantrename');
  107. }
  108. echo $JSON->encode(array('error' => $error));
  109. }
  110. }
  111. /**
  112. * Handle media renames in media manager
  113. *
  114. * @param Doku_Event $event
  115. * @return void
  116. */
  117. public function handleAjaxMediaManager(Doku_Event $event)
  118. {
  119. if ($event->data !== 'plugin_move_rename_mediamanager') return;
  120. if (!checkSecurityToken()) {
  121. throw new \Exception('Security token did not match');
  122. }
  123. $event->preventDefault();
  124. $event->stopPropagation();
  125. global $INPUT;
  126. global $MSG;
  127. global $USERINFO;
  128. $src = cleanID($INPUT->str('src'));
  129. $dst = cleanID($INPUT->str('dst'));
  130. /** @var helper_plugin_move_op $moveOperator */
  131. $moveOperator = plugin_load('helper', 'move_op');
  132. if ($src && $dst) {
  133. header('Content-Type: application/json');
  134. $response = [];
  135. // check user/group restrictions
  136. if (
  137. !auth_isMember($this->getConf('allowrename'), $INPUT->server->str('REMOTE_USER'), (array) $USERINFO['grps'])
  138. ) {
  139. $response['error'] = $this->getLang('notallowed');
  140. echo json_encode($response);
  141. return;
  142. }
  143. $response['success'] = $moveOperator->moveMedia($src, $dst);
  144. if ($response['success']) {
  145. $ns = getNS($dst);
  146. $response['redirect_url'] = wl($dst, ['do' => 'media', 'ns' => $ns], true, '&');
  147. } else {
  148. $response['error'] = sprintf($this->getLang('mediamoveerror'), $src);
  149. if (isset($MSG)) {
  150. foreach ($MSG as $msg) {
  151. $response['error'] .= ' ' . $msg['msg'];
  152. }
  153. }
  154. }
  155. echo json_encode($response);
  156. }
  157. }
  158. /**
  159. * Determines if it would be okay to show a rename page button for the given page and current user
  160. *
  161. * @param $id
  162. * @return bool
  163. */
  164. public function renameOkay($id) {
  165. global $conf;
  166. global $ACT;
  167. global $USERINFO;
  168. if(!($ACT == 'show' || empty($ACT))) return false;
  169. if(!page_exists($id)) return false;
  170. if(auth_quickaclcheck($id) < AUTH_EDIT) return false;
  171. if(checklock($id) !== false || @file_exists(wikiLockFN($id))) return false;
  172. if(!$conf['useacl']) return true;
  173. if(!isset($_SERVER['REMOTE_USER'])) return false;
  174. if(!auth_isMember($this->getConf('allowrename'), $_SERVER['REMOTE_USER'], (array) $USERINFO['grps'])) return false;
  175. return true;
  176. }
  177. /**
  178. * Use this in your template to add a simple "move this page" link
  179. *
  180. * Alternatively give anything the class "plugin_move_page" - it will automatically be hidden and shown and
  181. * trigger the page move dialog.
  182. */
  183. public function tpl() {
  184. echo '<a href="" class="plugin_move_page">';
  185. echo $this->getLang('renamepage');
  186. echo '</a>';
  187. }
  188. }