2 // PukiWiki - Yet another WikiWikiWeb clone.
4 // Copyright 2001-2018 PukiWiki Development Team
5 // License: GPL v2 or (at your option) any later version
7 // Edit plugin (cmd=edit)
9 // Remove #freeze written by hand
10 define('PLUGIN_EDIT_FREEZE_REGEX', '/^(?:#freeze(?!\w)\s*)+/im');
12 function plugin_edit_action()
14 global $vars, $_title_edit;
16 if (PKWK_READONLY) die_message('PKWK_READONLY prohibits editing');
18 $page = isset($vars['page']) ? $vars['page'] : '';
20 check_editable($page, true, true);
21 check_readable($page, true, true);
23 if (isset($vars['preview'])) {
24 return plugin_edit_preview($vars['msg']);
25 } else if (isset($vars['template'])) {
26 return plugin_edit_preview_with_template();
27 } else if (isset($vars['write'])) {
28 return plugin_edit_write();
29 } else if (isset($vars['cancel'])) {
30 return plugin_edit_cancel();
33 $postdata = @join('', get_source($page));
34 if ($postdata === '') $postdata = auto_template($page);
35 $postdata = remove_author_info($postdata);
36 return array('msg'=>$_title_edit, 'body'=>edit_form($page, $postdata));
40 * Preview with template
42 function plugin_edit_preview_with_template()
46 $page = isset($vars['page']) ? $vars['page'] : '';
49 if (isset($vars['template_page']) && is_page($template_page = $vars['template_page'])) {
50 if (is_page_readable($template_page)) {
51 $msg = remove_author_info(get_source($vars['template_page'], TRUE, TRUE));
53 $msg = preg_replace('/^(\*{1,3}.*)\[#[A-Za-z][\w-]+\](.*)$/m', '$1$2', $msg);
56 return plugin_edit_preview($msg);
62 * @param msg preview target
64 function plugin_edit_preview($msg)
67 global $_title_preview, $_msg_preview, $_msg_preview_delete;
69 $page = isset($vars['page']) ? $vars['page'] : '';
71 $msg = preg_replace(PLUGIN_EDIT_FREEZE_REGEX, '', $msg);
74 if (isset($vars['add']) && $vars['add']) {
75 if (isset($vars['add_top']) && $vars['add_top']) {
76 $postdata = $postdata . "\n\n" . @join('', get_source($page));
78 $postdata = @join('', get_source($page)) . "\n\n" . $postdata;
82 $body = $_msg_preview . '<br />' . "\n";
84 $body .= '<strong>' . $_msg_preview_delete . '</strong>';
85 $body .= '<br />' . "\n";
88 $postdata = make_str_rules($postdata);
89 $postdata = explode("\n", $postdata);
90 $postdata = drop_submit(convert_html($postdata));
91 $body .= '<div id="preview">' . $postdata . '</div>' . "\n";
93 $body .= edit_form($page, $msg, $vars['digest'], FALSE);
95 return array('msg'=>$_title_preview, 'body'=>$body);
98 // Inline: Show edit (or unfreeze text) link
99 function plugin_edit_inline()
101 static $usage = '&edit(pagename#anchor[[,noicon],nolabel])[{label}];';
103 global $vars, $fixed_heading_anchor_edit;
105 if (PKWK_READONLY) return ''; // Show nothing
108 $args = func_get_args();
110 // {label}. Strip anchor tags only
111 $s_label = strip_htmltag(array_pop($args), FALSE);
113 $page = array_shift($args);
114 if ($page === NULL) $page = '';
115 $_noicon = $_nolabel = FALSE;
116 foreach($args as $arg){
117 switch(strtolower($arg)){
119 case 'nolabel': $_nolabel = TRUE; break;
120 case 'noicon' : $_noicon = TRUE; break;
121 default : return $usage;
125 // Separate a page-name and a fixed anchor
126 list($s_page, $id, $editable) = anchor_explode($page, TRUE);
129 if ($s_page == '') $s_page = isset($vars['page']) ? $vars['page'] : '';
132 $isfreeze = is_freeze($s_page);
133 $ispage = is_page($s_page);
135 // Paragraph edit enabled or not
136 $short = htmlsc('Edit');
137 if ($fixed_heading_anchor_edit && $editable && $ispage && ! $isfreeze) {
139 $id = rawurlencode($id);
140 $title = htmlsc(sprintf('Edit %s', $page));
141 $icon = '<img src="' . IMAGE_DIR . 'paraedit.png' .
142 '" width="9" height="9" alt="' .
143 $short . '" title="' . $title . '" /> ';
144 $class = ' class="anchor_super"';
146 // Normal editing / unfreeze
149 $title = 'Unfreeze %s';
150 $icon = 'unfreeze.png';
155 $title = htmlsc(sprintf($title, $s_page));
156 $icon = '<img src="' . IMAGE_DIR . $icon .
157 '" width="20" height="20" alt="' .
158 $short . '" title="' . $title . '" />';
161 if ($_noicon) $icon = ''; // No more icon
164 $s_label = ''; // No label with an icon
166 $s_label = $short; // Short label without an icon
169 if ($s_label == '') $s_label = $title; // Rich label with an icon
173 $script = get_base_uri();
175 $url = $script . '?cmd=unfreeze&page=' . rawurlencode($s_page);
177 $s_id = ($id == '') ? '' : '&id=' . $id;
178 $url = $script . '?cmd=edit&page=' . rawurlencode($s_page) . $s_id;
180 $atag = '<a' . $class . ' href="' . $url . '" title="' . $title . '">';
181 static $atags = '</a>';
185 return $atag . $icon . $s_label . $atags;
187 // Dangling edit link
188 return '<span class="noexists">' . $atag . $icon . $atags .
189 $s_label . $atag . '?' . $atags . '</span>';
193 // Write, add, or insert new comment
194 function plugin_edit_write()
197 global $_title_collided, $_msg_collided_auto, $_msg_collided, $_title_deleted;
198 global $notimeupdate, $_msg_invalidpass, $do_update_diff_table;
200 $page = isset($vars['page']) ? $vars['page'] : '';
201 $add = isset($vars['add']) ? $vars['add'] : '';
202 $digest = isset($vars['digest']) ? $vars['digest'] : '';
204 $vars['msg'] = preg_replace(PLUGIN_EDIT_FREEZE_REGEX, '', $vars['msg']);
205 $msg = & $vars['msg']; // Reference
209 // Collision Detection
210 $oldpagesrc = join('', get_source($page));
211 $oldpagemd5 = md5($oldpagesrc);
212 if ($digest !== $oldpagemd5) {
213 $vars['digest'] = $oldpagemd5; // Reset
215 $original = isset($vars['original']) ? $vars['original'] : '';
216 $old_body = remove_author_info($oldpagesrc);
217 list($postdata_input, $auto) = do_update_diff($old_body, $msg, $original);
219 $retvars['msg' ] = $_title_collided;
220 $retvars['body'] = ($auto ? $_msg_collided_auto : $_msg_collided) . "\n";
221 $retvars['body'] .= $do_update_diff_table;
222 $retvars['body'] .= edit_form($page, $postdata_input, $oldpagemd5, FALSE);
229 if (isset($vars['add_top']) && $vars['add_top']) {
230 $postdata = $msg . "\n\n" . @join('', get_source($page));
232 $postdata = @join('', get_source($page)) . "\n\n" . $msg;
236 $postdata = & $msg; // Reference
239 // NULL POSTING, OR removing existing page
240 if ($postdata === '') {
241 page_write($page, $postdata);
242 $retvars['msg' ] = $_title_deleted;
243 $retvars['body'] = str_replace('$1', htmlsc($page), $_title_deleted);
247 // $notimeupdate: Checkbox 'Do not change timestamp'
248 $notimestamp = isset($vars['notimestamp']) && $vars['notimestamp'] != '';
249 if ($notimeupdate > 1 && $notimestamp && ! pkwk_login($vars['pass'])) {
250 // Enable only administrator & password error
251 $retvars['body'] = '<p><strong>' . $_msg_invalidpass . '</strong></p>' . "\n";
252 $retvars['body'] .= edit_form($page, $msg, $digest, FALSE);
256 page_write($page, $postdata, $notimeupdate != 0 && $notimestamp);
258 header('Location: ' . get_page_uri($page, PKWK_URI_ROOT));
262 // Cancel (Back to the page / Escape edit page)
263 function plugin_edit_cancel()
267 header('Location: ' . get_page_uri($vars['page'], PKWK_URI_ROOT));