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
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']) || ($load_template_func && isset($vars['template']))) {
23 return plugin_edit_preview();
24 } else if (isset($vars['write'])) {
25 return plugin_edit_write();
26 } else if (isset($vars['cancel'])) {
27 return plugin_edit_cancel();
30 $postdata = @join('', get_source($page));
31 if ($postdata == '') $postdata = auto_template($page);
33 return array('msg'=>$_title_edit, 'body'=>edit_form($page, $postdata));
37 function plugin_edit_preview()
40 global $_title_preview, $_msg_preview, $_msg_preview_delete;
42 $page = isset($vars['page']) ? $vars['page'] : '';
45 if (isset($vars['template_page']) && is_page($vars['template_page'])) {
47 $vars['msg'] = join('', get_source($vars['template_page']));
50 $vars['msg'] = preg_replace('/^(\*{1,3}.*)\[#[A-Za-z][\w-]+\](.*)$/m', '$1$2', $vars['msg']);
53 $vars['msg'] = preg_replace(PLUGIN_EDIT_FREEZE_REGEX, '', $vars['msg']);
54 $postdata = $vars['msg'];
56 if (isset($vars['add']) && $vars['add']) {
57 if (isset($vars['add_top']) && $vars['add_top']) {
58 $postdata = $postdata . "\n\n" . @join('', get_source($page));
60 $postdata = @join('', get_source($page)) . "\n\n" . $postdata;
64 $body = $_msg_preview . '<br />' . "\n";
66 $body .= '<strong>' . $_msg_preview_delete . '</strong>';
67 $body .= '<br />' . "\n";
70 $postdata = make_str_rules($postdata);
71 $postdata = explode("\n", $postdata);
72 $postdata = drop_submit(convert_html($postdata));
73 $body .= '<div id="preview">' . $postdata . '</div>' . "\n";
75 $body .= edit_form($page, $vars['msg'], $vars['digest'], FALSE);
77 return array('msg'=>$_title_preview, 'body'=>$body);
80 // Inline: Show edit (or unfreeze text) link
81 function plugin_edit_inline()
83 static $usage = '&edit(pagename#anchor[[,noicon],nolabel])[{label}];';
85 global $script, $vars, $fixed_heading_anchor_edit;
87 if (PKWK_READONLY) return ''; // Show nothing
90 $args = func_get_args();
92 // {label}. Strip anchor tags only
93 $s_label = strip_htmltag(array_pop($args), FALSE);
95 $page = array_shift($args);
96 if ($page == NULL) $page = '';
97 $_noicon = $_nolabel = FALSE;
98 foreach($args as $arg){
99 switch(strtolower($arg)){
101 case 'nolabel': $_nolabel = TRUE; break;
102 case 'noicon' : $_noicon = TRUE; break;
103 default : return $usage;
107 // Separate a page-name and a fixed anchor
108 list($s_page, $id, $editable) = anchor_explode($page, TRUE);
111 if ($s_page == '') $s_page = isset($vars['page']) ? $vars['page'] : '';
114 $isfreeze = is_freeze($s_page);
115 $ispage = is_page($s_page);
117 // Paragraph edit enabled or not
118 $short = htmlsc('Edit');
119 if ($fixed_heading_anchor_edit && $editable && $ispage && ! $isfreeze) {
121 $id = rawurlencode($id);
122 $title = htmlsc(sprintf('Edit %s', $page));
123 $icon = '<img src="' . IMAGE_DIR . 'paraedit.png' .
124 '" width="9" height="9" alt="' .
125 $short . '" title="' . $title . '" /> ';
126 $class = ' class="anchor_super"';
128 // Normal editing / unfreeze
131 $title = 'Unfreeze %s';
132 $icon = 'unfreeze.png';
137 $title = htmlsc(sprintf($title, $s_page));
138 $icon = '<img src="' . IMAGE_DIR . $icon .
139 '" width="20" height="20" alt="' .
140 $short . '" title="' . $title . '" />';
143 if ($_noicon) $icon = ''; // No more icon
146 $s_label = ''; // No label with an icon
148 $s_label = $short; // Short label without an icon
151 if ($s_label == '') $s_label = $title; // Rich label with an icon
156 $url = $script . '?cmd=unfreeze&page=' . rawurlencode($s_page);
158 $s_id = ($id == '') ? '' : '&id=' . $id;
159 $url = $script . '?cmd=edit&page=' . rawurlencode($s_page) . $s_id;
161 $atag = '<a' . $class . ' href="' . $url . '" title="' . $title . '">';
162 static $atags = '</a>';
166 return $atag . $icon . $s_label . $atags;
168 // Dangling edit link
169 return '<span class="noexists">' . $atag . $icon . $atags .
170 $s_label . $atag . '?' . $atags . '</span>';
174 // Write, add, or insert new comment
175 function plugin_edit_write()
178 global $_title_collided, $_msg_collided_auto, $_msg_collided, $_title_deleted;
179 global $notimeupdate, $_msg_invalidpass, $do_update_diff_table;
181 $page = isset($vars['page']) ? $vars['page'] : '';
182 $add = isset($vars['add']) ? $vars['add'] : '';
183 $digest = isset($vars['digest']) ? $vars['digest'] : '';
185 $vars['msg'] = preg_replace(PLUGIN_EDIT_FREEZE_REGEX, '', $vars['msg']);
186 $msg = & $vars['msg']; // Reference
190 // Collision Detection
191 $oldpagesrc = join('', get_source($page));
192 $oldpagemd5 = md5($oldpagesrc);
193 if ($digest != $oldpagemd5) {
194 $vars['digest'] = $oldpagemd5; // Reset
196 $original = isset($vars['original']) ? $vars['original'] : '';
197 list($postdata_input, $auto) = do_update_diff($oldpagesrc, $msg, $original);
199 $retvars['msg' ] = $_title_collided;
200 $retvars['body'] = ($auto ? $_msg_collided_auto : $_msg_collided) . "\n";
201 $retvars['body'] .= $do_update_diff_table;
202 $retvars['body'] .= edit_form($page, $postdata_input, $oldpagemd5, FALSE);
209 if (isset($vars['add_top']) && $vars['add_top']) {
210 $postdata = $msg . "\n\n" . @join('', get_source($page));
212 $postdata = @join('', get_source($page)) . "\n\n" . $msg;
216 $postdata = & $msg; // Reference
219 // NULL POSTING, OR removing existing page
220 if ($postdata == '') {
221 page_write($page, $postdata);
222 $retvars['msg' ] = $_title_deleted;
223 $retvars['body'] = str_replace('$1', htmlsc($page), $_title_deleted);
227 // $notimeupdate: Checkbox 'Do not change timestamp'
228 $notimestamp = isset($vars['notimestamp']) && $vars['notimestamp'] != '';
229 if ($notimeupdate > 1 && $notimestamp && ! pkwk_login($vars['pass'])) {
230 // Enable only administrator & password error
231 $retvars['body'] = '<p><strong>' . $_msg_invalidpass . '</strong></p>' . "\n";
232 $retvars['body'] .= edit_form($page, $msg, $digest, FALSE);
236 page_write($page, $postdata, $notimeupdate != 0 && $notimestamp);
238 header('Location: ' . get_script_uri() . '?' . rawurlencode($page));
242 // Cancel (Back to the page / Escape edit page)
243 function plugin_edit_cancel()
247 header('Location: ' . get_script_uri() . '?' . rawurlencode($vars['page']));