OSDN Git Service

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