2 // PukiWiki - Yet another WikiWikiWeb clone.
3 // $Id: edit.inc.php,v 1.35 2005/06/16 15:04:08 henoheno Exp $
8 // Remove #freeze written by hand
9 define('PLUGIN_EDIT_FREEZE_REGEX', '/^(?:#freeze(?!\w)\s*)+/im');
11 function plugin_edit_action()
13 global $vars, $_title_edit, $load_template_func;
15 if (PKWK_READONLY) die_message('PKWK_READONLY prohibits editing');
17 $page = isset($vars['page']) ? $vars['page'] : '';
19 check_editable($page, true, true);
21 if (isset($vars['preview']) || ($load_template_func && isset($vars['template']))) {
22 return plugin_edit_preview();
23 } else if (isset($vars['write'])) {
24 return plugin_edit_write();
25 } else if (isset($vars['cancel'])) {
26 return plugin_edit_cancel();
29 $postdata = @join('', get_source($page));
30 if ($postdata == '') $postdata = auto_template($page);
32 return array('msg'=>$_title_edit, 'body'=>edit_form($page, $postdata));
36 function plugin_edit_preview()
39 global $_title_preview, $_msg_preview, $_msg_preview_delete;
41 $page = isset($vars['page']) ? $vars['page'] : '';
44 if (isset($vars['template_page']) && is_page($vars['template_page'])) {
46 $vars['msg'] = join('', get_source($vars['template_page']));
49 $vars['msg'] = preg_replace('/^(\*{1,3}.*)\[#[A-Za-z][\w-]+\](.*)$/m', '$1$2', $vars['msg']);
52 $vars['msg'] = preg_replace(PLUGIN_EDIT_FREEZE_REGEX, '', $vars['msg']);
53 $postdata = $vars['msg'];
55 if (isset($vars['add']) && $vars['add']) {
56 if (isset($vars['add_top']) && $vars['add_top']) {
57 $postdata = $postdata . "\n\n" . @join('', get_source($page));
59 $postdata = @join('', get_source($page)) . "\n\n" . $postdata;
63 $body = "$_msg_preview<br />\n";
65 $body .= "<strong>$_msg_preview_delete</strong>";
69 $postdata = make_str_rules($postdata);
70 $postdata = explode("\n", $postdata);
71 $postdata = drop_submit(convert_html($postdata));
72 $body .= '<div id="preview">' . $postdata . '</div>' . "\n";
74 $body .= edit_form($page, $vars['msg'], $vars['digest'], FALSE);
76 return array('msg'=>$_title_preview, 'body'=>$body);
79 // Inline: Show edit (or unfreeze text) link
80 function plugin_edit_inline()
82 static $usage = '&edit(pagename#anchor[[,noicon],nolabel])[{label}];';
84 global $script, $vars, $fixed_heading_anchor_edit;
86 if (PKWK_READONLY) return ''; // Show nothing
89 $args = func_get_args();
90 $s_label = strip_autolink(array_pop($args)); // {label}
91 $page = array_shift($args);
92 if($page == NULL) $page = '';
93 $_noicon = $_nolabel = FALSE;
94 foreach($args as $arg){
97 case 'nolabel': $_nolabel = TRUE; break;
98 case 'noicon': $_noicon = TRUE; break;
99 default: return $usage;
103 // Separate a page-name and a fixed anchor
104 list($s_page, $id, $editable) = anchor_explode($page, TRUE);
106 if ($s_page == '') $s_page = isset($vars['page']) ? $vars['page'] : '';
108 $isfreeze = is_freeze($s_page);
109 $ispage = is_page($s_page);
111 // Paragraph edit enabled or not
112 $short = htmlspecialchars('Edit');
113 if ($fixed_heading_anchor_edit && $editable && $ispage && ! $isfreeze) {
115 $id = rawurlencode($id);
116 $title = htmlspecialchars(sprintf('Edit %s', $page));
117 $icon = '<img src="' . IMAGE_DIR . 'paraedit.png' .
118 '" width="9" height="9" alt="' .
119 $short . '" title="' . $title . '" /> ';
120 $class = ' class="anchor_super"';
122 // Normal editing / unfreeze
125 $title = 'Unfreeze %s';
126 $icon = 'unfreeze.png';
131 $title = htmlspecialchars(sprintf($title, $s_page));
132 $icon = '<img src="' . IMAGE_DIR . $icon .
133 '" width="20" height="20" alt="' .
134 $short . '" title="' . $title . '" />';
137 if ($_noicon) $icon = ''; // No more icon
140 $s_label = ''; // No label with an icon
142 $s_label = $short; // Short label without an icon
145 if ($s_label == '') $s_label = $title; // Rich label with an icon
150 $url = $script . '?cmd=unfreeze&page=' . rawurlencode($s_page);
153 $s_id = '&id=' . $id;
157 $url = $script . '?cmd=edit&page=' . rawurlencode($s_page) . $s_id;
159 $atag = '<a' . $class . ' href="' . $url . '" title="' . $title . '">';
160 static $atags = '</a>';
164 return $atag . $icon . $s_label . $atags;
166 // Dangling edit link
167 return '<span class="noexists">' . $atag . $icon . $atags .
168 $s_label . $atag . '?' . $atags . '</span>';
172 // Write, add, or insert new comment
173 function plugin_edit_write()
176 global $_title_collided, $_msg_collided_auto, $_msg_collided, $_title_deleted;
177 global $notimeupdate, $_msg_invalidpass;
179 $page = isset($vars['page']) ? $vars['page'] : '';
182 $vars['msg'] = preg_replace(PLUGIN_EDIT_FREEZE_REGEX,'',$vars['msg']);
183 $postdata = $postdata_input = $vars['msg'];
185 if (isset($vars['add']) && $vars['add']) {
186 if (isset($vars['add_top']) && $vars['add_top']) {
187 $postdata = $postdata . "\n\n" . @join('', get_source($page));
189 $postdata = @join('', get_source($page)) . "\n\n" . $postdata;
193 $oldpagesrc = join('', get_source($page));
194 $oldpagemd5 = md5($oldpagesrc);
196 if (! isset($vars['digest']) || $vars['digest'] != $oldpagemd5) {
197 $vars['digest'] = $oldpagemd5;
199 $retvars['msg'] = $_title_collided;
200 list($postdata_input, $auto) = do_update_diff($oldpagesrc, $postdata_input, $vars['original']);
202 $retvars['body'] = ($auto ? $_msg_collided_auto : $_msg_collided) . "\n";
205 global $do_update_diff_table;
206 $retvars['body'] .= $do_update_diff_table;
209 $retvars['body'] .= edit_form($page, $postdata_input, $oldpagemd5, FALSE);
213 $notimestamp = ($notimeupdate != 0) && (isset($vars['notimestamp']) && $vars['notimestamp'] != '');
214 if($notimestamp && ($notimeupdate == 2) && !pkwk_login($vars['pass'])) {
215 // enable only administrator & password error
216 $retvars['body'] = "<p><strong>$_msg_invalidpass</strong></p>\n";
217 $retvars['body'] .= edit_form($page, $vars['msg'], $vars['digest'], FALSE);
219 page_write($page, $postdata, $notimestamp);
221 header('Location: ' . get_script_uri() . '?' . rawurlencode($page));
225 page_write($page, $postdata);
226 $retvars['msg'] = $_title_deleted;
227 $retvars['body'] = str_replace('$1', htmlspecialchars($page), $_title_deleted);
235 // Cancel (Back to the page / Escape edit page)
236 function plugin_edit_cancel()
240 header('Location: ' . get_script_uri() . '?' . rawurlencode($vars['page']));