OSDN Git Service

BugTrack2/374 Remove author info when the page is used as template
[pukiwiki/pukiwiki.git] / plugin / edit.inc.php
1 <?php
2 // PukiWiki - Yet another WikiWikiWeb clone.
3 // edit.inc.php
4 // Copyright (C) 2001-2016 PukiWiki Development 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         $postdata = remove_author_info($postdata);
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                 $vars['msg'] = remove_author_info(get_source($vars['template_page'], TRUE, TRUE));
47                 // Cut fixed anchors
48                 $vars['msg'] = preg_replace('/^(\*{1,3}.*)\[#[A-Za-z][\w-]+\](.*)$/m', '$1$2', $vars['msg']);
49         }
50
51         $vars['msg'] = preg_replace(PLUGIN_EDIT_FREEZE_REGEX, '', $vars['msg']);
52         $postdata = $vars['msg'];
53
54         if (isset($vars['add']) && $vars['add']) {
55                 if (isset($vars['add_top']) && $vars['add_top']) {
56                         $postdata  = $postdata . "\n\n" . @join('', get_source($page));
57                 } else {
58                         $postdata  = @join('', get_source($page)) . "\n\n" . $postdata;
59                 }
60         }
61
62         $body = $_msg_preview . '<br />' . "\n";
63         if ($postdata == '')
64                 $body .= '<strong>' . $_msg_preview_delete . '</strong>';
65         $body .= '<br />' . "\n";
66
67         if ($postdata) {
68                 $postdata = make_str_rules($postdata);
69                 $postdata = explode("\n", $postdata);
70                 $postdata = drop_submit(convert_html($postdata));
71                 $body .= '<div id="preview">' . $postdata . '</div>' . "\n";
72         }
73         $body .= edit_form($page, $vars['msg'], $vars['digest'], FALSE);
74
75         return array('msg'=>$_title_preview, 'body'=>$body);
76 }
77
78 // Inline: Show edit (or unfreeze text) link
79 function plugin_edit_inline()
80 {
81         static $usage = '&edit(pagename#anchor[[,noicon],nolabel])[{label}];';
82
83         global $vars, $fixed_heading_anchor_edit;
84
85         if (PKWK_READONLY) return ''; // Show nothing 
86
87         // Arguments
88         $args = func_get_args();
89
90         // {label}. Strip anchor tags only
91         $s_label = strip_htmltag(array_pop($args), FALSE);
92
93         $page    = array_shift($args);
94         if ($page === NULL) $page = '';
95         $_noicon = $_nolabel = FALSE;
96         foreach($args as $arg){
97                 switch(strtolower($arg)){
98                 case ''       :                   break;
99                 case 'nolabel': $_nolabel = TRUE; break;
100                 case 'noicon' : $_noicon  = TRUE; break;
101                 default       : return $usage;
102                 }
103         }
104
105         // Separate a page-name and a fixed anchor
106         list($s_page, $id, $editable) = anchor_explode($page, TRUE);
107
108         // Default: This one
109         if ($s_page == '') $s_page = isset($vars['page']) ? $vars['page'] : '';
110
111         // $s_page fixed
112         $isfreeze = is_freeze($s_page);
113         $ispage   = is_page($s_page);
114
115         // Paragraph edit enabled or not
116         $short = htmlsc('Edit');
117         if ($fixed_heading_anchor_edit && $editable && $ispage && ! $isfreeze) {
118                 // Paragraph editing
119                 $id    = rawurlencode($id);
120                 $title = htmlsc(sprintf('Edit %s', $page));
121                 $icon = '<img src="' . IMAGE_DIR . 'paraedit.png' .
122                         '" width="9" height="9" alt="' .
123                         $short . '" title="' . $title . '" /> ';
124                 $class = ' class="anchor_super"';
125         } else {
126                 // Normal editing / unfreeze
127                 $id    = '';
128                 if ($isfreeze) {
129                         $title = 'Unfreeze %s';
130                         $icon  = 'unfreeze.png';
131                 } else {
132                         $title = 'Edit %s';
133                         $icon  = 'edit.png';
134                 }
135                 $title = htmlsc(sprintf($title, $s_page));
136                 $icon = '<img src="' . IMAGE_DIR . $icon .
137                         '" width="20" height="20" alt="' .
138                         $short . '" title="' . $title . '" />';
139                 $class = '';
140         }
141         if ($_noicon) $icon = ''; // No more icon
142         if ($_nolabel) {
143                 if (!$_noicon) {
144                         $s_label = '';     // No label with an icon
145                 } else {
146                         $s_label = $short; // Short label without an icon
147                 }
148         } else {
149                 if ($s_label == '') $s_label = $title; // Rich label with an icon
150         }
151
152         // URL
153         $script = get_script_uri();
154         if ($isfreeze) {
155                 $url   = $script . '?cmd=unfreeze&amp;page=' . rawurlencode($s_page);
156         } else {
157                 $s_id = ($id == '') ? '' : '&amp;id=' . $id;
158                 $url  = $script . '?cmd=edit&amp;page=' . rawurlencode($s_page) . $s_id;
159         }
160         $atag  = '<a' . $class . ' href="' . $url . '" title="' . $title . '">';
161         static $atags = '</a>';
162
163         if ($ispage) {
164                 // Normal edit link
165                 return $atag . $icon . $s_label . $atags;
166         } else {
167                 // Dangling edit link
168                 return '<span class="noexists">' . $atag . $icon . $atags .
169                         $s_label . $atag . '?' . $atags . '</span>';
170         }
171 }
172
173 // Write, add, or insert new comment
174 function plugin_edit_write()
175 {
176         global $vars;
177         global $_title_collided, $_msg_collided_auto, $_msg_collided, $_title_deleted;
178         global $notimeupdate, $_msg_invalidpass, $do_update_diff_table;
179
180         $page   = isset($vars['page'])   ? $vars['page']   : '';
181         $add    = isset($vars['add'])    ? $vars['add']    : '';
182         $digest = isset($vars['digest']) ? $vars['digest'] : '';
183
184         $vars['msg'] = preg_replace(PLUGIN_EDIT_FREEZE_REGEX, '', $vars['msg']);
185         $msg = & $vars['msg']; // Reference
186
187         $retvars = array();
188
189         // Collision Detection
190         $oldpagesrc = join('', get_source($page));
191         $oldpagemd5 = md5($oldpagesrc);
192         if ($digest !== $oldpagemd5) {
193                 $vars['digest'] = $oldpagemd5; // Reset
194
195                 $original = isset($vars['original']) ? $vars['original'] : '';
196                 list($postdata_input, $auto) = do_update_diff($oldpagesrc, $msg, $original);
197
198                 $retvars['msg' ] = $_title_collided;
199                 $retvars['body'] = ($auto ? $_msg_collided_auto : $_msg_collided) . "\n";
200                 $retvars['body'] .= $do_update_diff_table;
201                 $retvars['body'] .= edit_form($page, $postdata_input, $oldpagemd5, FALSE);
202                 return $retvars;
203         }
204
205         // Action?
206         if ($add) {
207                 // Add
208                 if (isset($vars['add_top']) && $vars['add_top']) {
209                         $postdata  = $msg . "\n\n" . @join('', get_source($page));
210                 } else {
211                         $postdata  = @join('', get_source($page)) . "\n\n" . $msg;
212                 }
213         } else {
214                 // Edit or Remove
215                 $postdata = & $msg; // Reference
216         }
217
218         // NULL POSTING, OR removing existing page
219         if ($postdata == '') {
220                 page_write($page, $postdata);
221                 $retvars['msg' ] = $_title_deleted;
222                 $retvars['body'] = str_replace('$1', htmlsc($page), $_title_deleted);
223                 return $retvars;
224         }
225
226         // $notimeupdate: Checkbox 'Do not change timestamp'
227         $notimestamp = isset($vars['notimestamp']) && $vars['notimestamp'] != '';
228         if ($notimeupdate > 1 && $notimestamp && ! pkwk_login($vars['pass'])) {
229                 // Enable only administrator & password error
230                 $retvars['body']  = '<p><strong>' . $_msg_invalidpass . '</strong></p>' . "\n";
231                 $retvars['body'] .= edit_form($page, $msg, $digest, FALSE);
232                 return $retvars;
233         }
234
235         page_write($page, $postdata, $notimeupdate != 0 && $notimestamp);
236         pkwk_headers_sent();
237         header('Location: ' . get_script_uri() . '?' . pagename_urlencode($page));
238         exit;
239 }
240
241 // Cancel (Back to the page / Escape edit page)
242 function plugin_edit_cancel()
243 {
244         global $vars;
245         pkwk_headers_sent();
246         header('Location: ' . get_script_uri() . '?' . pagename_urlencode($vars['page']));
247         exit;
248 }