OSDN Git Service

2a9559c810b1ba411144347cf289756f9e3f8d18
[pukiwiki/pukiwiki.git] / plugin / edit.inc.php
1 <?php
2 // PukiWiki - Yet another WikiWikiWeb clone.
3 // edit.inc.php
4 // Copyright 2001-2019 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;
15
16         if (PKWK_READONLY) die_message('PKWK_READONLY prohibits editing');
17
18         // Create initial pages
19         plugin_edit_setup_initial_pages();
20
21         $page = isset($vars['page']) ? $vars['page'] : '';
22         check_editable($page, true, true);
23         check_readable($page, true, true);
24
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();
33         }
34
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));
39 }
40
41 /**
42  * Preview with template
43  */
44 function plugin_edit_preview_with_template()
45 {
46         global $vars;
47         $msg = '';
48         $page = isset($vars['page']) ? $vars['page'] : '';
49         // Loading template
50         $template_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));
54                         // Cut fixed anchors
55                         $msg = preg_replace('/^(\*{1,3}.*)\[#[A-Za-z][\w-]+\](.*)$/m', '$1$2', $msg);
56                 }
57         }
58         return plugin_edit_preview($msg);
59 }
60
61 /**
62  * Preview
63  *
64  * @param msg preview target
65  */
66 function plugin_edit_preview($msg)
67 {
68         global $vars;
69         global $_title_preview, $_msg_preview, $_msg_preview_delete;
70
71         $page = isset($vars['page']) ? $vars['page'] : '';
72
73         $msg = preg_replace(PLUGIN_EDIT_FREEZE_REGEX, '', $msg);
74         $postdata = $msg;
75
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));
79                 } else {
80                         $postdata  = @join('', get_source($page)) . "\n\n" . $postdata;
81                 }
82         }
83
84         $body = $_msg_preview . '<br />' . "\n";
85         if ($postdata === '')
86                 $body .= '<strong>' . $_msg_preview_delete . '</strong>';
87         $body .= '<br />' . "\n";
88
89         if ($postdata) {
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";
94         }
95         $body .= edit_form($page, $msg, $vars['digest'], FALSE);
96
97         return array('msg'=>$_title_preview, 'body'=>$body);
98 }
99
100 // Inline: Show edit (or unfreeze text) link
101 function plugin_edit_inline()
102 {
103         static $usage = '&edit(pagename#anchor[[,noicon],nolabel])[{label}];';
104
105         global $vars, $fixed_heading_anchor_edit;
106
107         if (PKWK_READONLY) return ''; // Show nothing 
108
109         // Arguments
110         $args = func_get_args();
111
112         // {label}. Strip anchor tags only
113         $s_label = strip_htmltag(array_pop($args), FALSE);
114
115         $page = array_shift($args);
116         if ($page === NULL) $page = '';
117         $_noicon = $_nolabel = FALSE;
118         foreach($args as $arg){
119                 switch(strtolower($arg)){
120                 case ''       :                   break;
121                 case 'nolabel': $_nolabel = TRUE; break;
122                 case 'noicon' : $_noicon  = TRUE; break;
123                 default       : return $usage;
124                 }
125         }
126
127         // Separate a page-name and a fixed anchor
128         list($s_page, $id, $editable) = anchor_explode($page, TRUE);
129
130         // Default: This one
131         if ($s_page == '') $s_page = isset($vars['page']) ? $vars['page'] : '';
132
133         // $s_page fixed
134         $isfreeze = is_freeze($s_page);
135         $ispage   = is_page($s_page);
136
137         // Paragraph edit enabled or not
138         $short = htmlsc('Edit');
139         if ($fixed_heading_anchor_edit && $editable && $ispage && ! $isfreeze) {
140                 // Paragraph editing
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"';
147         } else {
148                 // Normal editing / unfreeze
149                 $id    = '';
150                 if ($isfreeze) {
151                         $title = 'Unfreeze %s';
152                         $icon  = 'unfreeze.png';
153                 } else {
154                         $title = 'Edit %s';
155                         $icon  = 'edit.png';
156                 }
157                 $title = htmlsc(sprintf($title, $s_page));
158                 $icon = '<img src="' . IMAGE_DIR . $icon .
159                         '" width="20" height="20" alt="' .
160                         $short . '" title="' . $title . '" />';
161                 $class = '';
162         }
163         if ($_noicon) $icon = ''; // No more icon
164         if ($_nolabel) {
165                 if (!$_noicon) {
166                         $s_label = '';     // No label with an icon
167                 } else {
168                         $s_label = $short; // Short label without an icon
169                 }
170         } else {
171                 if ($s_label == '') $s_label = $title; // Rich label with an icon
172         }
173
174         // URL
175         $script = get_base_uri();
176         if ($isfreeze) {
177                 $url   = $script . '?cmd=unfreeze&amp;page=' . rawurlencode($s_page);
178         } else {
179                 $s_id = ($id == '') ? '' : '&amp;id=' . $id;
180                 $url  = $script . '?cmd=edit&amp;page=' . rawurlencode($s_page) . $s_id;
181         }
182         $atag  = '<a' . $class . ' href="' . $url . '" title="' . $title . '">';
183         static $atags = '</a>';
184
185         if ($ispage) {
186                 // Normal edit link
187                 return $atag . $icon . $s_label . $atags;
188         } else {
189                 // Dangling edit link
190                 return '<span class="noexists">' . $atag . $icon . $atags .
191                         $s_label . $atag . '?' . $atags . '</span>';
192         }
193 }
194
195 // Write, add, or insert new comment
196 function plugin_edit_write()
197 {
198         global $vars;
199         global $_title_collided, $_msg_collided_auto, $_msg_collided, $_title_deleted;
200         global $notimeupdate, $_msg_invalidpass, $do_update_diff_table;
201
202         $page   = isset($vars['page'])   ? $vars['page']   : '';
203         $add    = isset($vars['add'])    ? $vars['add']    : '';
204         $digest = isset($vars['digest']) ? $vars['digest'] : '';
205
206         $vars['msg'] = preg_replace(PLUGIN_EDIT_FREEZE_REGEX, '', $vars['msg']);
207         $msg = & $vars['msg']; // Reference
208
209         $retvars = array();
210
211         // Collision Detection
212         $oldpagesrc = join('', get_source($page));
213         $oldpagemd5 = md5($oldpagesrc);
214         if ($digest !== $oldpagemd5) {
215                 $vars['digest'] = $oldpagemd5; // Reset
216
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);
220
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);
225                 return $retvars;
226         }
227
228         // Action?
229         if ($add) {
230                 // Add
231                 if (isset($vars['add_top']) && $vars['add_top']) {
232                         $postdata  = $msg . "\n\n" . @join('', get_source($page));
233                 } else {
234                         $postdata  = @join('', get_source($page)) . "\n\n" . $msg;
235                 }
236         } else {
237                 // Edit or Remove
238                 $postdata = & $msg; // Reference
239         }
240
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);
246                 return $retvars;
247         }
248
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);
255                 return $retvars;
256         }
257
258         page_write($page, $postdata, $notimeupdate != 0 && $notimestamp);
259         pkwk_headers_sent();
260         header('Location: ' . get_page_uri($page, PKWK_URI_ROOT));
261         exit;
262 }
263
264 // Cancel (Back to the page / Escape edit page)
265 function plugin_edit_cancel()
266 {
267         global $vars;
268         pkwk_headers_sent();
269         header('Location: ' . get_page_uri($vars['page'], PKWK_URI_ROOT));
270         exit;
271 }
272
273 /**
274  * Setup initial pages
275  */
276 function plugin_edit_setup_initial_pages()
277 {
278         global $autoalias, $no_autoticketlinkname;
279
280         // Related: Rename plugin
281         if (exist_plugin('rename') && function_exists('plugin_rename_setup_initial_pages')) {
282                 plugin_rename_setup_initial_pages();
283         }
284         // AutoTicketLinkName page
285         if (! $no_autoticketlinkname) {
286                 init_autoticketlink_def_page();
287         }
288         // AutoAliasName page
289         if ($autoalias) {
290                 init_autoalias_def_page();
291         }
292 }