PageRenderTime 61ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/plugin/edit.inc.php

https://github.com/miya5n/pukiwiki
PHP | 254 lines | 177 code | 45 blank | 32 comment | 58 complexity | 9839084efe3716abe42e127d6cfa996d MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. // PukiWiki - Yet another WikiWikiWeb clone.
  3. // $Id: edit.inc.php,v 1.49 2011/01/25 15:01:01 henoheno Exp $
  4. // Copyright (C) 2001-2007 PukiWiki Developers Team
  5. // License: GPL v2 or (at your option) any later version
  6. //
  7. // Edit plugin (cmd=edit)
  8. // Remove #freeze written by hand
  9. define('PLUGIN_EDIT_FREEZE_REGEX', '/^(?:#freeze(?!\w)\s*)+/im');
  10. function plugin_edit_action()
  11. {
  12. global $post, $vars, $_title_edit, $load_template_func;
  13. if (PKWK_READONLY) die_message('PKWK_READONLY prohibits editing');
  14. $page = isset($vars['page']) ? $vars['page'] : '';
  15. check_editable($page, true, true);
  16. if (isset($post['preview']) || ($load_template_func && isset($post['template']))) {
  17. return plugin_edit_preview();
  18. } else if (isset($post['write'])) {
  19. return plugin_edit_write();
  20. } else if (isset($post['cancel'])) {
  21. return plugin_edit_cancel();
  22. }
  23. $postdata = get_source($page, TRUE, TRUE);
  24. if ($postdata == '') $postdata = auto_template($page);
  25. return array('msg'=>$_title_edit, 'body'=>edit_form($page, $postdata));
  26. }
  27. // Preview
  28. function plugin_edit_preview()
  29. {
  30. global $post, $vars, $_title_preview, $_msg_preview, $_msg_preview_delete;
  31. $page = isset($vars['page']) ? $vars['page'] : '';
  32. // Loading template
  33. if (isset($post['template_page']) && is_page($post['template_page'])) {
  34. $post['msg'] = get_source($post['template_page'], TRUE, TRUE);
  35. // Cut fixed anchors
  36. $post['msg'] = preg_replace('/^(\*{1,3}.*)\[#[A-Za-z][\w-]+\](.*)$/m', '$1$2', $post['msg']);
  37. }
  38. $post['msg'] = preg_replace(PLUGIN_EDIT_FREEZE_REGEX, '', $post['msg']);
  39. $postdata = $post['msg'];
  40. // Compat: add plugin and adding contents
  41. if (isset($vars['add']) && $vars['add']) {
  42. if (isset($post['add_top']) && $post['add_top']) {
  43. $postdata = $postdata . "\n\n" . get_source($page, TRUE, TRUE);
  44. } else {
  45. $postdata = get_source($page, TRUE, TRUE) . "\n\n" . $postdata;
  46. }
  47. }
  48. $body = $_msg_preview . '<br />' . "\n";
  49. if ($postdata == '')
  50. $body .= '<strong>' . $_msg_preview_delete . '</strong>';
  51. $body .= '<br />' . "\n";
  52. if ($postdata) {
  53. $postdata = make_str_rules($postdata);
  54. $postdata = explode("\n", $postdata);
  55. $postdata = drop_submit(convert_html($postdata));
  56. $body .= '<div id="preview">' . $postdata . '</div>' . "\n";
  57. }
  58. $body .= edit_form($page, $post['msg'], $post['digest'], FALSE);
  59. return array('msg'=>$_title_preview, 'body'=>$body);
  60. }
  61. // Inline: Show edit (or unfreeze text) link
  62. function plugin_edit_inline()
  63. {
  64. $usage = '&amp;edit(pagename#anchor[[,noicon],nolabel])[{label}];';
  65. global $vars, $fixed_heading_anchor_edit;
  66. if (PKWK_READONLY) return ''; // Show nothing
  67. // Arguments
  68. $args = func_get_args();
  69. // {label}. Strip anchor tags only
  70. $s_label = strip_htmltag(array_pop($args), FALSE);
  71. $page = array_shift($args);
  72. if ($page === NULL) $page = '';
  73. $_noicon = $_nolabel = FALSE;
  74. foreach($args as $arg){
  75. switch(strtolower($arg)){
  76. case '' : break;
  77. case 'nolabel': $_nolabel = TRUE; break;
  78. case 'noicon' : $_noicon = TRUE; break;
  79. default : return $usage;
  80. }
  81. }
  82. // Separate a page-name and a fixed anchor
  83. list($s_page, $id, $editable) = anchor_explode($page, TRUE);
  84. // Default: This one
  85. if ($s_page == '') $s_page = isset($vars['page']) ? $vars['page'] : '';
  86. // $s_page fixed
  87. $isfreeze = is_freeze($s_page);
  88. $ispage = is_page($s_page);
  89. // Paragraph edit enabled or not
  90. $short = htmlsc('Edit');
  91. if ($fixed_heading_anchor_edit && $editable && $ispage && ! $isfreeze) {
  92. // Paragraph editing
  93. $id = rawurlencode($id);
  94. $title = htmlsc(sprintf('Edit %s', $page));
  95. $icon = '<img src="' . IMAGE_DIR . 'paraedit.png' .
  96. '" width="9" height="9" alt="' .
  97. $short . '" title="' . $title . '" /> ';
  98. $class = ' class="anchor_super"';
  99. } else {
  100. // Normal editing / unfreeze
  101. $id = '';
  102. if ($isfreeze) {
  103. $title = 'Unfreeze %s';
  104. $icon = 'unfreeze.png';
  105. } else {
  106. $title = 'Edit %s';
  107. $icon = 'edit.png';
  108. }
  109. $title = htmlsc(sprintf($title, $s_page));
  110. $icon = '<img src="' . IMAGE_DIR . $icon .
  111. '" width="20" height="20" alt="' .
  112. $short . '" title="' . $title . '" />';
  113. $class = '';
  114. }
  115. if ($_noicon) $icon = ''; // No more icon
  116. if ($_nolabel) {
  117. if (!$_noicon) {
  118. $s_label = ''; // No label with an icon
  119. } else {
  120. $s_label = $short; // Short label without an icon
  121. }
  122. } else {
  123. if ($s_label == '') $s_label = $title; // Rich label with an icon
  124. }
  125. // URL
  126. $script = get_script_uri();
  127. if ($isfreeze) {
  128. $url = $script . '?cmd=unfreeze&amp;page=' . rawurlencode($s_page);
  129. } else {
  130. $s_id = ($id == '') ? '' : '&amp;id=' . $id;
  131. $url = $script . '?cmd=edit&amp;page=' . rawurlencode($s_page) . $s_id;
  132. }
  133. $atag = '<a' . $class . ' href="' . $url . '" title="' . $title . '">';
  134. static $atags = '</a>';
  135. if ($ispage) {
  136. // Normal edit link
  137. return $atag . $icon . $s_label . $atags;
  138. } else {
  139. // Dangling edit link
  140. return '<span class="noexists">' . $atag . $icon . $atags .
  141. $s_label . $atag . '?' . $atags . '</span>';
  142. }
  143. }
  144. // Write, add, or insert new comment
  145. function plugin_edit_write()
  146. {
  147. global $post, $vars;
  148. global $_title_collided, $_msg_collided_auto, $_msg_collided, $_title_deleted;
  149. global $notimeupdate, $_msg_invalidpass, $do_update_diff_table;
  150. $page = isset($vars['page']) ? $vars['page'] : '';
  151. $add = isset($vars['add']) ? $vars['add'] : '';
  152. $digest = isset($post['digest']) ? $post['digest'] : '';
  153. $post['msg'] = preg_replace(PLUGIN_EDIT_FREEZE_REGEX, '', $post['msg']);
  154. $msg = & $post['msg'];
  155. $retvars = array();
  156. // Collision Detection
  157. $oldpagesrc = get_source($page, TRUE, TRUE);
  158. $oldpagemd5 = md5($oldpagesrc);
  159. if ($digest !== $oldpagemd5) {
  160. $post['digest'] = $oldpagemd5; // Reset
  161. $original = isset($post['original']) ? $post['original'] : '';
  162. list($postdata_input, $auto) = do_update_diff($oldpagesrc, $msg, $original);
  163. $retvars['msg' ] = $_title_collided;
  164. $retvars['body'] = ($auto ? $_msg_collided_auto : $_msg_collided) . "\n";
  165. $retvars['body'] .= $do_update_diff_table;
  166. $retvars['body'] .= edit_form($page, $postdata_input, $oldpagemd5, FALSE);
  167. return $retvars;
  168. }
  169. // Action?
  170. if ($add) {
  171. // Compat: add plugin and adding contents
  172. if (isset($post['add_top']) && $post['add_top']) {
  173. $postdata = $msg . "\n\n" . get_source($page, TRUE, TRUE);
  174. } else {
  175. $postdata = get_source($page, TRUE, TRUE) . "\n\n" . $msg;
  176. }
  177. } else {
  178. // Edit or Remove
  179. $postdata = & $msg;
  180. }
  181. // NULL POSTING, OR removing existing page
  182. if ($postdata == '') {
  183. page_write($page, $postdata);
  184. $retvars['msg' ] = $_title_deleted;
  185. $retvars['body'] = str_replace('$1', htmlsc($page), $_title_deleted);
  186. return $retvars;
  187. }
  188. // $notimeupdate: Checkbox 'Do not change timestamp'
  189. $notimestamp = isset($post['notimestamp']) && $post['notimestamp'] != '';
  190. if ($notimeupdate > 1 && $notimestamp && ! pkwk_login($post['pass'])) {
  191. // Enable only administrator & password error
  192. $retvars['body'] = '<p><strong>' . $_msg_invalidpass . '</strong></p>' . "\n";
  193. $retvars['body'] .= edit_form($page, $msg, $digest, FALSE);
  194. return $retvars;
  195. }
  196. page_write($page, $postdata, $notimeupdate != 0 && $notimestamp);
  197. pkwk_headers_sent();
  198. header('Location: ' . get_script_uri() . '?' . rawurlencode($page));
  199. exit;
  200. }
  201. // Cancel (Back to the page / Escape edit page)
  202. function plugin_edit_cancel()
  203. {
  204. global $vars;
  205. pkwk_headers_sent();
  206. header('Location: ' . get_script_uri() . '?' . rawurlencode($vars['page']));
  207. exit;
  208. }
  209. ?>