OSDN Git Service

BugTrack/598 Stop reading page that is not readable as template
[pukiwiki/pukiwiki.git] / plugin / edit.inc.php
1 <?php
2 // PukiWiki - Yet another WikiWikiWeb clone.
3 // edit.inc.php
4 // Copyright 2001-2017 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         $template_page;
46         if (isset($vars['template_page']) && is_page($template_page = $vars['template_page'])) {
47                 if (is_page_readable($template_page)) {
48                         $vars['msg'] = remove_author_info(get_source($vars['template_page'], TRUE, TRUE));
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 $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         $script = get_script_uri();
156         if ($isfreeze) {
157                 $url   = $script . '?cmd=unfreeze&amp;page=' . rawurlencode($s_page);
158         } else {
159                 $s_id = ($id == '') ? '' : '&amp;id=' . $id;
160                 $url  = $script . '?cmd=edit&amp;page=' . rawurlencode($s_page) . $s_id;
161         }
162         $atag  = '<a' . $class . ' href="' . $url . '" title="' . $title . '">';
163         static $atags = '</a>';
164
165         if ($ispage) {
166                 // Normal edit link
167                 return $atag . $icon . $s_label . $atags;
168         } else {
169                 // Dangling edit link
170                 return '<span class="noexists">' . $atag . $icon . $atags .
171                         $s_label . $atag . '?' . $atags . '</span>';
172         }
173 }
174
175 // Write, add, or insert new comment
176 function plugin_edit_write()
177 {
178         global $vars;
179         global $_title_collided, $_msg_collided_auto, $_msg_collided, $_title_deleted;
180         global $notimeupdate, $_msg_invalidpass, $do_update_diff_table;
181
182         $page   = isset($vars['page'])   ? $vars['page']   : '';
183         $add    = isset($vars['add'])    ? $vars['add']    : '';
184         $digest = isset($vars['digest']) ? $vars['digest'] : '';
185
186         $vars['msg'] = preg_replace(PLUGIN_EDIT_FREEZE_REGEX, '', $vars['msg']);
187         $msg = & $vars['msg']; // Reference
188
189         $retvars = array();
190
191         // Collision Detection
192         $oldpagesrc = join('', get_source($page));
193         $oldpagemd5 = md5($oldpagesrc);
194         if ($digest !== $oldpagemd5) {
195                 $vars['digest'] = $oldpagemd5; // Reset
196
197                 $original = isset($vars['original']) ? $vars['original'] : '';
198                 $old_body = remove_author_info($oldpagesrc);
199                 list($postdata_input, $auto) = do_update_diff($old_body, $msg, $original);
200
201                 $retvars['msg' ] = $_title_collided;
202                 $retvars['body'] = ($auto ? $_msg_collided_auto : $_msg_collided) . "\n";
203                 $retvars['body'] .= $do_update_diff_table;
204                 $retvars['body'] .= edit_form($page, $postdata_input, $oldpagemd5, FALSE);
205                 return $retvars;
206         }
207
208         // Action?
209         if ($add) {
210                 // Add
211                 if (isset($vars['add_top']) && $vars['add_top']) {
212                         $postdata  = $msg . "\n\n" . @join('', get_source($page));
213                 } else {
214                         $postdata  = @join('', get_source($page)) . "\n\n" . $msg;
215                 }
216         } else {
217                 // Edit or Remove
218                 $postdata = & $msg; // Reference
219         }
220
221         // NULL POSTING, OR removing existing page
222         if ($postdata == '') {
223                 page_write($page, $postdata);
224                 $retvars['msg' ] = $_title_deleted;
225                 $retvars['body'] = str_replace('$1', htmlsc($page), $_title_deleted);
226                 return $retvars;
227         }
228
229         // $notimeupdate: Checkbox 'Do not change timestamp'
230         $notimestamp = isset($vars['notimestamp']) && $vars['notimestamp'] != '';
231         if ($notimeupdate > 1 && $notimestamp && ! pkwk_login($vars['pass'])) {
232                 // Enable only administrator & password error
233                 $retvars['body']  = '<p><strong>' . $_msg_invalidpass . '</strong></p>' . "\n";
234                 $retvars['body'] .= edit_form($page, $msg, $digest, FALSE);
235                 return $retvars;
236         }
237
238         page_write($page, $postdata, $notimeupdate != 0 && $notimestamp);
239         pkwk_headers_sent();
240         header('Location: ' . get_script_uri() . '?' . pagename_urlencode($page));
241         exit;
242 }
243
244 // Cancel (Back to the page / Escape edit page)
245 function plugin_edit_cancel()
246 {
247         global $vars;
248         pkwk_headers_sent();
249         header('Location: ' . get_script_uri() . '?' . pagename_urlencode($vars['page']));
250         exit;
251 }