OSDN Git Service

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