OSDN Git Service

BugTrack2/62:
[pukiwiki/pukiwiki.git] / plugin / edit.inc.php
1 <?php
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
6 //
7 // Edit plugin (cmd=edit)
8
9 // Remove #freeze written by hand
10 define('PLUGIN_EDIT_FREEZE_REGEX', '/^(?:#freeze(?!\w)\s*)+/im');
11
12 function plugin_edit_action()
13 {
14         global $vars, $_title_edit, $load_template_func;
15
16         if (PKWK_READONLY) die_message('PKWK_READONLY prohibits editing');
17
18         $page = isset($vars['page']) ? $vars['page'] : '';
19
20         check_editable($page, true, true);
21
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();
28         }
29
30         $postdata = @join('', get_source($page));
31         if ($postdata == '') $postdata = auto_template($page);
32
33         return array('msg'=>$_title_edit, 'body'=>edit_form($page, $postdata));
34 }
35
36 // Preview
37 function plugin_edit_preview()
38 {
39         global $vars;
40         global $_title_preview, $_msg_preview, $_msg_preview_delete;
41
42         $page = isset($vars['page']) ? $vars['page'] : '';
43
44         // Loading template
45         if (isset($vars['template_page']) && is_page($vars['template_page'])) {
46
47                 $vars['msg'] = join('', get_source($vars['template_page']));
48
49                 // Cut fixed anchors
50                 $vars['msg'] = preg_replace('/^(\*{1,3}.*)\[#[A-Za-z][\w-]+\](.*)$/m', '$1$2', $vars['msg']);
51         }
52
53         $vars['msg'] = preg_replace(PLUGIN_EDIT_FREEZE_REGEX, '', $vars['msg']);
54         $postdata = $vars['msg'];
55
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));
59                 } else {
60                         $postdata  = @join('', get_source($page)) . "\n\n" . $postdata;
61                 }
62         }
63
64         $body = $_msg_preview . '<br />' . "\n";
65         if ($postdata == '')
66                 $body .= '<strong>' . $_msg_preview_delete . '</strong>';
67         $body .= '<br />' . "\n";
68
69         if ($postdata) {
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";
74         }
75         $body .= edit_form($page, $vars['msg'], $vars['digest'], FALSE);
76
77         return array('msg'=>$_title_preview, 'body'=>$body);
78 }
79
80 // Inline: Show edit (or unfreeze text) link
81 function plugin_edit_inline()
82 {
83         static $usage = '&edit(pagename#anchor[[,noicon],nolabel])[{label}];';
84
85         global $script, $vars, $fixed_heading_anchor_edit;
86
87         if (PKWK_READONLY) return ''; // Show nothing 
88
89         // Arguments
90         $args = func_get_args();
91
92         // {label}. Strip anchor tags only
93         $s_label = strip_htmltag(array_pop($args), FALSE);
94
95         $page    = array_shift($args);
96         if ($page == NULL) $page = '';
97         $_noicon = $_nolabel = FALSE;
98         foreach($args as $arg){
99                 switch(strtolower($arg)){
100                 case ''       :                   break;
101                 case 'nolabel': $_nolabel = TRUE; break;
102                 case 'noicon' : $_noicon  = TRUE; break;
103                 default       : return $usage;
104                 }
105         }
106
107         // Separate a page-name and a fixed anchor
108         list($s_page, $id, $editable) = anchor_explode($page, TRUE);
109
110         // Default: This one
111         if ($s_page == '') $s_page = isset($vars['page']) ? $vars['page'] : '';
112
113         // $s_page fixed
114         $isfreeze = is_freeze($s_page);
115         $ispage   = is_page($s_page);
116
117         // Paragraph edit enabled or not
118         $short = htmlsc('Edit');
119         if ($fixed_heading_anchor_edit && $editable && $ispage && ! $isfreeze) {
120                 // Paragraph editing
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"';
127         } else {
128                 // Normal editing / unfreeze
129                 $id    = '';
130                 if ($isfreeze) {
131                         $title = 'Unfreeze %s';
132                         $icon  = 'unfreeze.png';
133                 } else {
134                         $title = 'Edit %s';
135                         $icon  = 'edit.png';
136                 }
137                 $title = htmlsc(sprintf($title, $s_page));
138                 $icon = '<img src="' . IMAGE_DIR . $icon .
139                         '" width="20" height="20" alt="' .
140                         $short . '" title="' . $title . '" />';
141                 $class = '';
142         }
143         if ($_noicon) $icon = ''; // No more icon
144         if ($_nolabel) {
145                 if (!$_noicon) {
146                         $s_label = '';     // No label with an icon
147                 } else {
148                         $s_label = $short; // Short label without an icon
149                 }
150         } else {
151                 if ($s_label == '') $s_label = $title; // Rich label with an icon
152         }
153
154         // URL
155         if ($isfreeze) {
156                 $url   = $script . '?cmd=unfreeze&amp;page=' . rawurlencode($s_page);
157         } else {
158                 $s_id = ($id == '') ? '' : '&amp;id=' . $id;
159                 $url  = $script . '?cmd=edit&amp;page=' . rawurlencode($s_page) . $s_id;
160         }
161         $atag  = '<a' . $class . ' href="' . $url . '" title="' . $title . '">';
162         static $atags = '</a>';
163
164         if ($ispage) {
165                 // Normal edit link
166                 return $atag . $icon . $s_label . $atags;
167         } else {
168                 // Dangling edit link
169                 return '<span class="noexists">' . $atag . $icon . $atags .
170                         $s_label . $atag . '?' . $atags . '</span>';
171         }
172 }
173
174 // Write, add, or insert new comment
175 function plugin_edit_write()
176 {
177         global $vars;
178         global $_title_collided, $_msg_collided_auto, $_msg_collided, $_title_deleted;
179         global $notimeupdate, $_msg_invalidpass, $do_update_diff_table;
180
181         $page   = isset($vars['page'])   ? $vars['page']   : '';
182         $add    = isset($vars['add'])    ? $vars['add']    : '';
183         $digest = isset($vars['digest']) ? $vars['digest'] : '';
184
185         $vars['msg'] = preg_replace(PLUGIN_EDIT_FREEZE_REGEX, '', $vars['msg']);
186         $msg = & $vars['msg']; // Reference
187
188         $retvars = array();
189
190         // Collision Detection
191         $oldpagesrc = join('', get_source($page));
192         $oldpagemd5 = md5($oldpagesrc);
193         if ($digest != $oldpagemd5) {
194                 $vars['digest'] = $oldpagemd5; // Reset
195
196                 $original = isset($vars['original']) ? $vars['original'] : '';
197                 list($postdata_input, $auto) = do_update_diff($oldpagesrc, $msg, $original);
198
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);
203                 return $retvars;
204         }
205
206         // Action?
207         if ($add) {
208                 // Add
209                 if (isset($vars['add_top']) && $vars['add_top']) {
210                         $postdata  = $msg . "\n\n" . @join('', get_source($page));
211                 } else {
212                         $postdata  = @join('', get_source($page)) . "\n\n" . $msg;
213                 }
214         } else {
215                 // Edit or Remove
216                 $postdata = & $msg; // Reference
217         }
218
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);
224                 return $retvars;
225         }
226
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);
233                 return $retvars;
234         }
235
236         page_write($page, $postdata, $notimeupdate != 0 && $notimestamp);
237         pkwk_headers_sent();
238         header('Location: ' . get_script_uri() . '?' . rawurlencode($page));
239         exit;
240 }
241
242 // Cancel (Back to the page / Escape edit page)
243 function plugin_edit_cancel()
244 {
245         global $vars;
246         pkwk_headers_sent();
247         header('Location: ' . get_script_uri() . '?' . rawurlencode($vars['page']));
248         exit;
249 }
250
251 ?>