2 // PukiWiki - Yet another WikiWikiWeb clone.
4 // Copyright 2001-2019 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 // Create initial pages
19 plugin_edit_setup_initial_pages();
21 $page = isset($vars['page']) ? $vars['page'] : '';
22 check_editable($page, true, true);
23 check_readable($page, true, true);
25 if (isset($vars['preview'])) {
26 return plugin_edit_preview($vars['msg']);
27 } else if (isset($vars['template'])) {
28 return plugin_edit_preview_with_template();
29 } else if (isset($vars['write'])) {
30 return plugin_edit_write();
31 } else if (isset($vars['cancel'])) {
32 return plugin_edit_cancel();
35 $postdata = @join('', get_source($page));
36 if ($postdata === '') $postdata = auto_template($page);
37 $postdata = remove_author_info($postdata);
38 return array('msg'=>$_title_edit, 'body'=>edit_form($page, $postdata));
42 * Preview with template
44 function plugin_edit_preview_with_template()
48 $page = isset($vars['page']) ? $vars['page'] : '';
51 if (isset($vars['template_page']) && is_page($template_page = $vars['template_page'])) {
52 if (is_page_readable($template_page)) {
53 $msg = remove_author_info(get_source($vars['template_page'], TRUE, TRUE));
55 $msg = preg_replace('/^(\*{1,3}.*)\[#[A-Za-z][\w-]+\](.*)$/m', '$1$2', $msg);
58 return plugin_edit_preview($msg);
64 * @param msg preview target
66 function plugin_edit_preview($msg)
69 global $_title_preview, $_msg_preview, $_msg_preview_delete;
71 $page = isset($vars['page']) ? $vars['page'] : '';
73 $msg = preg_replace(PLUGIN_EDIT_FREEZE_REGEX, '', $msg);
76 if (isset($vars['add']) && $vars['add']) {
77 if (isset($vars['add_top']) && $vars['add_top']) {
78 $postdata = $postdata . "\n\n" . @join('', get_source($page));
80 $postdata = @join('', get_source($page)) . "\n\n" . $postdata;
84 $body = $_msg_preview . '<br />' . "\n";
86 $body .= '<strong>' . $_msg_preview_delete . '</strong>';
87 $body .= '<br />' . "\n";
90 $postdata = make_str_rules($postdata);
91 $postdata = explode("\n", $postdata);
92 $postdata = drop_submit(convert_html($postdata));
93 $body .= '<div id="preview">' . $postdata . '</div>' . "\n";
95 $body .= edit_form($page, $msg, $vars['digest'], FALSE);
97 return array('msg'=>$_title_preview, 'body'=>$body);
100 // Inline: Show edit (or unfreeze text) link
101 function plugin_edit_inline()
103 static $usage = '&edit(pagename#anchor[[,noicon],nolabel])[{label}];';
105 global $vars, $fixed_heading_anchor_edit;
107 if (PKWK_READONLY) return ''; // Show nothing
110 $args = func_get_args();
112 // {label}. Strip anchor tags only
113 $s_label = strip_htmltag(array_pop($args), FALSE);
115 $page = array_shift($args);
116 if ($page === NULL) $page = '';
117 $_noicon = $_nolabel = FALSE;
118 foreach($args as $arg){
119 switch(strtolower($arg)){
121 case 'nolabel': $_nolabel = TRUE; break;
122 case 'noicon' : $_noicon = TRUE; break;
123 default : return $usage;
127 // Separate a page-name and a fixed anchor
128 list($s_page, $id, $editable) = anchor_explode($page, TRUE);
131 if ($s_page == '') $s_page = isset($vars['page']) ? $vars['page'] : '';
134 $isfreeze = is_freeze($s_page);
135 $ispage = is_page($s_page);
137 // Paragraph edit enabled or not
138 $short = htmlsc('Edit');
139 if ($fixed_heading_anchor_edit && $editable && $ispage && ! $isfreeze) {
141 $id = rawurlencode($id);
142 $title = htmlsc(sprintf('Edit %s', $page));
143 $icon = '<img src="' . IMAGE_DIR . 'paraedit.png' .
144 '" width="9" height="9" alt="' .
145 $short . '" title="' . $title . '" /> ';
146 $class = ' class="anchor_super"';
148 // Normal editing / unfreeze
151 $title = 'Unfreeze %s';
152 $icon = 'unfreeze.png';
157 $title = htmlsc(sprintf($title, $s_page));
158 $icon = '<img src="' . IMAGE_DIR . $icon .
159 '" width="20" height="20" alt="' .
160 $short . '" title="' . $title . '" />';
163 if ($_noicon) $icon = ''; // No more icon
166 $s_label = ''; // No label with an icon
168 $s_label = $short; // Short label without an icon
171 if ($s_label == '') $s_label = $title; // Rich label with an icon
175 $script = get_base_uri();
177 $url = $script . '?cmd=unfreeze&page=' . rawurlencode($s_page);
179 $s_id = ($id == '') ? '' : '&id=' . $id;
180 $url = $script . '?cmd=edit&page=' . rawurlencode($s_page) . $s_id;
182 $atag = '<a' . $class . ' href="' . $url . '" title="' . $title . '">';
183 static $atags = '</a>';
187 return $atag . $icon . $s_label . $atags;
189 // Dangling edit link
190 return '<span class="noexists">' . $atag . $icon . $atags .
191 $s_label . $atag . '?' . $atags . '</span>';
195 // Write, add, or insert new comment
196 function plugin_edit_write()
199 global $_title_collided, $_msg_collided_auto, $_msg_collided, $_title_deleted;
200 global $notimeupdate, $_msg_invalidpass, $do_update_diff_table;
202 $page = isset($vars['page']) ? $vars['page'] : '';
203 $add = isset($vars['add']) ? $vars['add'] : '';
204 $digest = isset($vars['digest']) ? $vars['digest'] : '';
206 $vars['msg'] = preg_replace(PLUGIN_EDIT_FREEZE_REGEX, '', $vars['msg']);
207 $msg = & $vars['msg']; // Reference
211 // Collision Detection
212 $oldpagesrc = join('', get_source($page));
213 $oldpagemd5 = md5($oldpagesrc);
214 if ($digest !== $oldpagemd5) {
215 $vars['digest'] = $oldpagemd5; // Reset
217 $original = isset($vars['original']) ? $vars['original'] : '';
218 $old_body = remove_author_info($oldpagesrc);
219 list($postdata_input, $auto) = do_update_diff($old_body, $msg, $original);
221 $retvars['msg' ] = $_title_collided;
222 $retvars['body'] = ($auto ? $_msg_collided_auto : $_msg_collided) . "\n";
223 $retvars['body'] .= $do_update_diff_table;
224 $retvars['body'] .= edit_form($page, $postdata_input, $oldpagemd5, FALSE);
231 if (isset($vars['add_top']) && $vars['add_top']) {
232 $postdata = $msg . "\n\n" . @join('', get_source($page));
234 $postdata = @join('', get_source($page)) . "\n\n" . $msg;
238 $postdata = & $msg; // Reference
241 // NULL POSTING, OR removing existing page
242 if ($postdata === '') {
243 page_write($page, $postdata);
244 $retvars['msg' ] = $_title_deleted;
245 $retvars['body'] = str_replace('$1', htmlsc($page), $_title_deleted);
249 // $notimeupdate: Checkbox 'Do not change timestamp'
250 $notimestamp = isset($vars['notimestamp']) && $vars['notimestamp'] != '';
251 if ($notimeupdate > 1 && $notimestamp && ! pkwk_login($vars['pass'])) {
252 // Enable only administrator & password error
253 $retvars['body'] = '<p><strong>' . $_msg_invalidpass . '</strong></p>' . "\n";
254 $retvars['body'] .= edit_form($page, $msg, $digest, FALSE);
258 page_write($page, $postdata, $notimeupdate != 0 && $notimestamp);
260 header('Location: ' . get_page_uri($page, PKWK_URI_ROOT));
264 // Cancel (Back to the page / Escape edit page)
265 function plugin_edit_cancel()
269 header('Location: ' . get_page_uri($vars['page'], PKWK_URI_ROOT));
274 * Setup initial pages
276 function plugin_edit_setup_initial_pages()
278 global $autoalias, $no_autoticketlinkname;
280 // Related: Rename plugin
281 if (exist_plugin('rename') && function_exists('plugin_rename_setup_initial_pages')) {
282 plugin_rename_setup_initial_pages();
284 // AutoTicketLinkName page
285 if (! $no_autoticketlinkname) {
286 init_autoticketlink_def_page();
288 // AutoAliasName page
290 init_autoalias_def_page();