2 // PukiWiki - Yet another WikiWikiWeb clone.
4 // Copyright 2001-2017 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, $load_template_func;
16 if (PKWK_READONLY) die_message('PKWK_READONLY prohibits editing');
18 $page = isset($vars['page']) ? $vars['page'] : '';
20 check_editable($page, true, true);
22 if (isset($vars['preview'])) {
23 return plugin_edit_preview($vars['msg']);
24 } else if ($load_template_func && isset($vars['template'])) {
25 return plugin_edit_preview_with_template();
26 } else if (isset($vars['write'])) {
27 return plugin_edit_write();
28 } else if (isset($vars['cancel'])) {
29 return plugin_edit_cancel();
32 $postdata = @join('', get_source($page));
33 if ($postdata === '') $postdata = auto_template($page);
34 $postdata = remove_author_info($postdata);
35 return array('msg'=>$_title_edit, 'body'=>edit_form($page, $postdata));
39 * Preview with template
41 function plugin_edit_preview_with_template()
45 $page = isset($vars['page']) ? $vars['page'] : '';
48 if (isset($vars['template_page']) && is_page($template_page = $vars['template_page'])) {
49 if (is_page_readable($template_page)) {
50 $msg = remove_author_info(get_source($vars['template_page'], TRUE, TRUE));
52 $msg = preg_replace('/^(\*{1,3}.*)\[#[A-Za-z][\w-]+\](.*)$/m', '$1$2', $msg);
55 return plugin_edit_preview($msg);
61 * @param msg preview target
63 function plugin_edit_preview($msg)
66 global $_title_preview, $_msg_preview, $_msg_preview_delete;
68 $page = isset($vars['page']) ? $vars['page'] : '';
70 $msg = preg_replace(PLUGIN_EDIT_FREEZE_REGEX, '', $msg);
73 if (isset($vars['add']) && $vars['add']) {
74 if (isset($vars['add_top']) && $vars['add_top']) {
75 $postdata = $postdata . "\n\n" . @join('', get_source($page));
77 $postdata = @join('', get_source($page)) . "\n\n" . $postdata;
81 $body = $_msg_preview . '<br />' . "\n";
83 $body .= '<strong>' . $_msg_preview_delete . '</strong>';
84 $body .= '<br />' . "\n";
87 $postdata = make_str_rules($postdata);
88 $postdata = explode("\n", $postdata);
89 $postdata = drop_submit(convert_html($postdata));
90 $body .= '<div id="preview">' . $postdata . '</div>' . "\n";
92 $body .= edit_form($page, $msg, $vars['digest'], FALSE);
94 return array('msg'=>$_title_preview, 'body'=>$body);
97 // Inline: Show edit (or unfreeze text) link
98 function plugin_edit_inline()
100 static $usage = '&edit(pagename#anchor[[,noicon],nolabel])[{label}];';
102 global $vars, $fixed_heading_anchor_edit;
104 if (PKWK_READONLY) return ''; // Show nothing
107 $args = func_get_args();
109 // {label}. Strip anchor tags only
110 $s_label = strip_htmltag(array_pop($args), FALSE);
112 $page = array_shift($args);
113 if ($page === NULL) $page = '';
114 $_noicon = $_nolabel = FALSE;
115 foreach($args as $arg){
116 switch(strtolower($arg)){
118 case 'nolabel': $_nolabel = TRUE; break;
119 case 'noicon' : $_noicon = TRUE; break;
120 default : return $usage;
124 // Separate a page-name and a fixed anchor
125 list($s_page, $id, $editable) = anchor_explode($page, TRUE);
128 if ($s_page == '') $s_page = isset($vars['page']) ? $vars['page'] : '';
131 $isfreeze = is_freeze($s_page);
132 $ispage = is_page($s_page);
134 // Paragraph edit enabled or not
135 $short = htmlsc('Edit');
136 if ($fixed_heading_anchor_edit && $editable && $ispage && ! $isfreeze) {
138 $id = rawurlencode($id);
139 $title = htmlsc(sprintf('Edit %s', $page));
140 $icon = '<img src="' . IMAGE_DIR . 'paraedit.png' .
141 '" width="9" height="9" alt="' .
142 $short . '" title="' . $title . '" /> ';
143 $class = ' class="anchor_super"';
145 // Normal editing / unfreeze
148 $title = 'Unfreeze %s';
149 $icon = 'unfreeze.png';
154 $title = htmlsc(sprintf($title, $s_page));
155 $icon = '<img src="' . IMAGE_DIR . $icon .
156 '" width="20" height="20" alt="' .
157 $short . '" title="' . $title . '" />';
160 if ($_noicon) $icon = ''; // No more icon
163 $s_label = ''; // No label with an icon
165 $s_label = $short; // Short label without an icon
168 if ($s_label == '') $s_label = $title; // Rich label with an icon
172 $script = get_base_uri();
174 $url = $script . '?cmd=unfreeze&page=' . rawurlencode($s_page);
176 $s_id = ($id == '') ? '' : '&id=' . $id;
177 $url = $script . '?cmd=edit&page=' . rawurlencode($s_page) . $s_id;
179 $atag = '<a' . $class . ' href="' . $url . '" title="' . $title . '">';
180 static $atags = '</a>';
184 return $atag . $icon . $s_label . $atags;
186 // Dangling edit link
187 return '<span class="noexists">' . $atag . $icon . $atags .
188 $s_label . $atag . '?' . $atags . '</span>';
192 // Write, add, or insert new comment
193 function plugin_edit_write()
196 global $_title_collided, $_msg_collided_auto, $_msg_collided, $_title_deleted;
197 global $notimeupdate, $_msg_invalidpass, $do_update_diff_table;
199 $page = isset($vars['page']) ? $vars['page'] : '';
200 $add = isset($vars['add']) ? $vars['add'] : '';
201 $digest = isset($vars['digest']) ? $vars['digest'] : '';
203 $vars['msg'] = preg_replace(PLUGIN_EDIT_FREEZE_REGEX, '', $vars['msg']);
204 $msg = & $vars['msg']; // Reference
208 // Collision Detection
209 $oldpagesrc = join('', get_source($page));
210 $oldpagemd5 = md5($oldpagesrc);
211 if ($digest !== $oldpagemd5) {
212 $vars['digest'] = $oldpagemd5; // Reset
214 $original = isset($vars['original']) ? $vars['original'] : '';
215 $old_body = remove_author_info($oldpagesrc);
216 list($postdata_input, $auto) = do_update_diff($old_body, $msg, $original);
218 $retvars['msg' ] = $_title_collided;
219 $retvars['body'] = ($auto ? $_msg_collided_auto : $_msg_collided) . "\n";
220 $retvars['body'] .= $do_update_diff_table;
221 $retvars['body'] .= edit_form($page, $postdata_input, $oldpagemd5, FALSE);
228 if (isset($vars['add_top']) && $vars['add_top']) {
229 $postdata = $msg . "\n\n" . @join('', get_source($page));
231 $postdata = @join('', get_source($page)) . "\n\n" . $msg;
235 $postdata = & $msg; // Reference
238 // NULL POSTING, OR removing existing page
239 if ($postdata === '') {
240 page_write($page, $postdata);
241 $retvars['msg' ] = $_title_deleted;
242 $retvars['body'] = str_replace('$1', htmlsc($page), $_title_deleted);
246 // $notimeupdate: Checkbox 'Do not change timestamp'
247 $notimestamp = isset($vars['notimestamp']) && $vars['notimestamp'] != '';
248 if ($notimeupdate > 1 && $notimestamp && ! pkwk_login($vars['pass'])) {
249 // Enable only administrator & password error
250 $retvars['body'] = '<p><strong>' . $_msg_invalidpass . '</strong></p>' . "\n";
251 $retvars['body'] .= edit_form($page, $msg, $digest, FALSE);
255 page_write($page, $postdata, $notimeupdate != 0 && $notimestamp);
257 header('Location: ' . get_page_uri($page, PKWK_URI_ROOT));
261 // Cancel (Back to the page / Escape edit page)
262 function plugin_edit_cancel()
266 header('Location: ' . get_page_uri($vars['page'], PKWK_URI_ROOT));