OSDN Git Service

BugTrack2/35: Added the function that 'do not change timestamp' at edit enable/disabl...
[pukiwiki/pukiwiki.git] / lib / html.php
1 <?php
2 // PukiWiki - Yet another WikiWikiWeb clone.
3 // $Id: html.php,v 1.30 2005/03/13 17:29:02 teanan Exp $
4 //
5 // HTML-publishing related functions
6
7 // Show page-content
8 function catbody($title, $page, $body)
9 {
10         global $script, $vars, $arg, $defaultpage, $whatsnew, $help_page, $hr;
11         global $related_link, $cantedit, $function_freeze, $search_word_color, $_msg_word;
12         global $foot_explain, $note_hr, $head_tags;
13         global $trackback, $trackback_javascript, $referer, $javascript;
14         global $_LANG, $_LINK, $_IMAGE;
15
16         global $pkwk_dtd;     // XHTML 1.1, XHTML1.0, HTML 4.01 Transitional...
17         global $page_title;   // Title of this site
18         global $do_backup;    // Do backup or not
19         global $modifier;     // Site administrator's  web page
20         global $modifierlink; // Site administrator's name
21
22         if (! file_exists(SKIN_FILE) || ! is_readable(SKIN_FILE))
23                 die_message('SKIN_FILE is not found');
24
25         $_LINK = $_IMAGE = array();
26
27         // Add JavaScript header when ...
28         if ($trackback && $trackback_javascript) $javascript = 1; // Set something If you want
29         if (! PKWK_ALLOW_JAVASCRIPT) unset($javascript);
30
31         $_page  = isset($vars['page']) ? $vars['page'] : '';
32         $r_page = rawurlencode($_page);
33
34         // Set $_LINK for skin
35         $_LINK['add']      = "$script?cmd=add&amp;page=$r_page";
36         $_LINK['backup']   = "$script?cmd=backup&amp;page=$r_page";
37         $_LINK['copy']     = "$script?plugin=template&amp;refer=$r_page";
38         $_LINK['diff']     = "$script?cmd=diff&amp;page=$r_page";
39         $_LINK['edit']     = "$script?cmd=edit&amp;page=$r_page";
40         $_LINK['filelist'] = "$script?cmd=filelist";
41         $_LINK['freeze']   = "$script?cmd=freeze&amp;page=$r_page";
42         $_LINK['help']     = "$script?" . rawurlencode($help_page);
43         $_LINK['list']     = "$script?cmd=list";
44         $_LINK['new']      = "$script?plugin=newpage&amp;refer=$r_page";
45         $_LINK['rdf']      = "$script?cmd=rss&amp;ver=1.0";
46         $_LINK['recent']   = "$script?" . rawurlencode($whatsnew);
47         $_LINK['refer']    = "$script?plugin=referer&amp;page=$r_page";
48         $_LINK['reload']   = "$script?$r_page";
49         $_LINK['rename']   = "$script?plugin=rename&amp;refer=$r_page";
50         $_LINK['rss']      = "$script?cmd=rss";
51         $_LINK['rss10']    = "$script?cmd=rss&amp;ver=1.0"; // Same as 'rdf'
52         $_LINK['rss20']    = "$script?cmd=rss&amp;ver=2.0";
53         $_LINK['search']   = "$script?cmd=search";
54         $_LINK['top']      = "$script?" . rawurlencode($defaultpage);
55         if ($trackback) {
56                 $tb_id = tb_get_id($_page);
57                 $_LINK['trackback'] = "$script?plugin=tb&amp;__mode=view&amp;tb_id=$tb_id";
58         }
59         $_LINK['unfreeze'] = "$script?cmd=unfreeze&amp;page=$r_page";
60         $_LINK['upload']   = "$script?plugin=attach&amp;pcmd=upload&amp;page=$r_page";
61
62         // Compat: Skins for 1.4.4 and before
63         $link_add       = & $_LINK['add'];
64         $link_new       = & $_LINK['new'];      // New!
65         $link_edit      = & $_LINK['edit'];
66         $link_diff      = & $_LINK['diff'];
67         $link_top       = & $_LINK['top'];
68         $link_list      = & $_LINK['list'];
69         $link_filelist  = & $_LINK['filelist'];
70         $link_search    = & $_LINK['search'];
71         $link_whatsnew  = & $_LINK['recent'];
72         $link_backup    = & $_LINK['backup'];
73         $link_help      = & $_LINK['help'];
74         $link_trackback = & $_LINK['trackback'];        // New!
75         $link_rdf       = & $_LINK['rdf'];              // New!
76         $link_rss       = & $_LINK['rss'];
77         $link_rss10     = & $_LINK['rss10'];            // New!
78         $link_rss20     = & $_LINK['rss20'];            // New!
79         $link_freeze    = & $_LINK['freeze'];
80         $link_unfreeze  = & $_LINK['unfreeze'];
81         $link_upload    = & $_LINK['upload'];
82         $link_template  = & $_LINK['copy'];
83         $link_refer     = & $_LINK['refer'];    // New!
84         $link_rename    = & $_LINK['rename'];
85
86         // Init flags
87         $is_page = (is_pagename($_page) && ! arg_check('backup') && $_page != $whatsnew);
88         $is_read = (arg_check('read') && is_page($_page));
89         $is_freeze = is_freeze($_page);
90
91         // Last modification date (string) of the page
92         $lastmodified = $is_read ?  get_date('D, d M Y H:i:s T', get_filetime($_page)) .
93                 ' ' . get_pg_passage($_page, FALSE) : '';
94
95         // List of related pages
96         $related = ($is_read && $related_link) ? make_related($_page) : '';
97
98         // List of attached files of the page
99         $attaches = ($is_read && exist_plugin_action('attach')) ? attach_filelist() : '';
100
101         // List of footnotes
102         ksort($foot_explain, SORT_NUMERIC);
103         $notes = ! empty($foot_explain) ? $note_hr . join("\n", $foot_explain) : '';
104
105         // Tags will be inserted into <head></head>
106         $head_tag = ! empty($head_tags) ? join("\n", $head_tags) ."\n" : '';
107
108         // 1.3.x compat
109         // Last modification date (UNIX timestamp) of the page
110         $fmt = $is_read ? get_filetime($_page) + LOCALZONE : 0;
111
112         // Search words
113         if ($search_word_color && isset($vars['word'])) {
114                 $body = '<div class="small">' . $_msg_word . htmlspecialchars($vars['word']) .
115                         '</div>' . $hr . "\n" . $body;
116                 $words = array_flip(array_splice(
117                         preg_split('/\s+/', $vars['word'], -1, PREG_SPLIT_NO_EMPTY),
118                         0, 10));
119                 $keys = array();
120                 foreach ($words as $word=>$id) $keys[$word] = strlen($word);
121                 arsort($keys, SORT_NUMERIC);
122                 $keys = get_search_words(array_keys($keys), TRUE);
123                 $id = 0;
124                 foreach ($keys as $key=>$pattern) {
125                         $s_key    = htmlspecialchars($key);
126                         $pattern  = '/<[^>]*>|(' . $pattern . ')|&[^;]+;/';
127                         $callback = create_function(
128                                 '$arr',
129                                 'return (count($arr) > 1) ? \'<strong class="word' .
130                                         $id++ . '">\' . $arr[1] . \'</strong>\' : $arr[0];'
131                         );
132                         $body  = preg_replace_callback($pattern, $callback, $body);
133                         $notes = preg_replace_callback($pattern, $callback, $notes);
134                 }
135         }
136
137         $longtaketime = getmicrotime() - MUTIME;
138         $taketime     = sprintf('%01.03f', $longtaketime);
139
140         require(SKIN_FILE);
141 }
142
143 // Show 'edit' form
144 function edit_form($page, $postdata, $digest = 0, $b_template = TRUE)
145 {
146         global $script, $vars, $rows, $cols, $hr, $function_freeze;
147         global $_btn_addtop, $_btn_preview, $_btn_repreview, $_btn_update, $_btn_cancel,
148                 $_msg_help, $_btn_notchangetimestamp;
149         global $whatsnew, $_btn_template, $_btn_load, $non_list, $load_template_func;
150         global $notimeupdate;
151
152         $refer = $template = $addtag = $add_top = '';
153
154         if ($digest == 0) $digest = md5(join('', get_source($page)));
155
156         $checked_top  = isset($vars['add_top'])     ? ' checked="checked"' : '';
157         $checked_time = isset($vars['notimestamp']) ? ' checked="checked"' : '';
158
159         if(isset($vars['add'])) {
160                 $addtag  = '<input type="hidden" name="add" value="true" />';
161                 $add_top = '<input type="checkbox" name="add_top" value="true"' .
162                         $checked_top . ' /><span class="small">' .
163                         $_btn_addtop . '</span>';
164         }
165
166         if($load_template_func && $b_template) {
167                 $pages  = array();
168                 $non_list_pattern = '/' . $non_list . '/';
169                 foreach(get_existpages() as $_page) {
170                         if ($_page == $whatsnew || preg_match($non_list_pattern, $_page))
171                                 continue;
172                         $s_page = htmlspecialchars($_page);
173                         $pages[$_page] = '   <option value="' . $s_page . '">' .
174                                 $s_page . '</option>';
175                 }
176                 ksort($pages);
177                 $s_pages  = join("\n", $pages);
178                 $template = <<<EOD
179   <select name="template_page">
180    <option value="">-- $_btn_template --</option>
181 $s_pages
182   </select>
183   <input type="submit" name="template" value="$_btn_load" accesskey="r" />
184   <br />
185 EOD;
186
187                 if (isset($vars['refer']) && $vars['refer'] != '')
188                         $refer = '[[' . strip_bracket($vars['refer']) . ']]' . "\n\n";
189         }
190
191         $r_page      = rawurlencode($page);
192         $s_page      = htmlspecialchars($page);
193         $s_digest    = htmlspecialchars($digest);
194         $s_postdata  = htmlspecialchars($refer . $postdata);
195         $s_original  = isset($vars['original']) ? htmlspecialchars($vars['original']) : $s_postdata;
196         $b_preview   = isset($vars['preview']); // TRUE when preview
197         $btn_preview = $b_preview ? $_btn_repreview : $_btn_preview;
198
199         $add_notimestamp = '';
200         if ( $notimeupdate != 0 ) {
201                 // enable 'do not change timestamp'
202                 $add_notimestamp = <<<EOD
203   <input type="checkbox" name="notimestamp" id="_edit_form_notimestamp" value="true"$checked_time />
204   <label for="_edit_form_notimestamp"><span class="small">$_btn_notchangetimestamp</span></label>
205 EOD;
206                 if ( $notimeupdate == 2 ) {
207                         // enable only administrator
208                         $add_notimestamp .= <<<EOD
209   <input type="password" name="pass" size="12" />
210 EOD;
211                 }
212                 $add_notimestamp .= '&nbsp;';
213         }
214
215         $body = <<<EOD
216 <form action="$script" method="post">
217  <div class="edit_form">
218 $template
219   $addtag
220   <input type="hidden" name="cmd"    value="edit" />
221   <input type="hidden" name="page"   value="$s_page" />
222   <input type="hidden" name="digest" value="$s_digest" />
223   <textarea name="msg" rows="$rows" cols="$cols">$s_postdata</textarea>
224   <br />
225   <input type="submit" name="preview" value="$btn_preview" accesskey="p" />
226   <input type="submit" name="write"   value="$_btn_update" accesskey="s" />
227   $add_top
228   $add_notimestamp
229   <input type="submit" name="cancel"  value="$_btn_cancel" accesskey="c" />
230   <textarea name="original" rows="1" cols="1" style="display:none">$s_original</textarea>
231  </div>
232 </form>
233 EOD;
234
235         if (isset($vars['help'])) {
236                 $body .= $hr . catrule();
237         } else {
238                 $body .= '<ul><li><a href="' .
239                         $script . '?cmd=edit&amp;help=true&amp;page=' . $r_page .
240                         '">' . $_msg_help . '</a></li></ul>';
241         }
242
243         return $body;
244 }
245
246 // Related pages
247 function make_related($page, $tag = '')
248 {
249         global $script, $vars, $rule_related_str, $related_str, $non_list;
250         global $_ul_left_margin, $_ul_margin, $_list_pad_str;
251
252         $links = links_get_related($page);
253
254         if ($tag) {
255                 ksort($links);
256         } else {
257                 arsort($links);
258         }
259
260         $_links = array();
261         $non_list_pattern = '/' . $non_list . '/';
262         foreach ($links as $page=>$lastmod) {
263                 if (preg_match($non_list_pattern, $page)) continue;
264
265                 $r_page   = rawurlencode($page);
266                 $s_page   = htmlspecialchars($page);
267                 $passage  = get_passage($lastmod);
268                 $_links[] = $tag ?
269                         '<a href="' . $script . '?' . $r_page . '" title="' .
270                         $s_page . ' ' . $passage . '">' . $s_page . '</a>' :
271                         '<a href="' . $script . '?' . $r_page . '">' .
272                         $s_page . '</a>' . $passage;
273         }
274         if (empty($_links)) return ''; // Nothing
275
276         if ($tag == 'p') { // From the line-head
277                 $margin = $_ul_left_margin + $_ul_margin;
278                 $style  = sprintf($_list_pad_str, 1, $margin, $margin);
279                 $retval =  "\n" . '<ul' . $style . '>' . "\n" .
280                         '<li>' . join($rule_related_str, $_links) . '</li>' . "\n" .
281                         '</ul>' . "\n";
282         } else if ($tag) {
283                 $retval = join($rule_related_str, $_links);
284         } else {
285                 $retval = join($related_str, $_links);
286         }
287
288         return $retval;
289 }
290
291 // User-defined rules (convert without replacing source)
292 function make_line_rules($str)
293 {
294         global $line_rules;
295         static $pattern, $replace;
296
297         if (! isset($pattern)) {
298                 $pattern = array_map(create_function('$a',
299                         'return \'/\' . $a . \'/\';'), array_keys($line_rules));
300                 $replace = array_values($line_rules);
301                 unset($line_rules);
302         }
303
304         return preg_replace($pattern, $replace, $str);
305 }
306
307 function strip_htmltag($str)
308 {
309         global $_symbol_noexists;
310
311         $noexists_pattern = '#<span class="noexists">([^<]*)<a[^>]+>' .
312                 preg_quote($_symbol_noexists, '#') . '</a></span>#';
313
314         $str = preg_replace($noexists_pattern, '$1', $str);
315         //$str = preg_replace('/<a[^>]+>\?<\/a>/', '', $str);
316         return preg_replace('/<[^>]+>/', '', $str);
317 }
318
319 // Make a backlink. searching-link of the page name, by the page name, for the page name
320 function make_search($page)
321 {
322         global $script;
323
324         $s_page = htmlspecialchars($page);
325         $r_page = rawurlencode($page);
326
327         return '<a href="' . $script . '?plugin=related&amp;page=' . $r_page .
328                 '">' . $s_page . '</a> ';
329 }
330
331 // Make heading (remove footnotes and HTML tags)
332 function make_heading(& $str, $strip = TRUE)
333 {
334         global $NotePattern;
335
336         // Cut fixed-anchors
337         $id = '';
338         $matches = array();
339         if (preg_match('/^(\*{0,3})(.*?)\[#([A-Za-z][\w-]+)\](.*?)$/m', $str, $matches)) {
340                 $str = $matches[2] . $matches[4];
341                 $id  = $matches[3];
342         } else {
343                 $str = preg_replace('/^\*{0,3}/', '', $str);
344         }
345
346         // Cut footnotes and tags
347         if ($strip === TRUE)
348                 $str = strip_htmltag(make_link(preg_replace($NotePattern, '', $str)));
349
350         return $id;
351 }
352
353 // Separate a page-name(or URL or null string) and an anchor
354 // (last one standing) without sharp
355 function anchor_explode($page, $strict_editable = FALSE)
356 {
357         $pos = strrpos($page, '#');
358         if ($pos === FALSE) return array($page, '', FALSE);
359
360         // Ignore the last sharp letter
361         if ($pos + 1 == strlen($page)) {
362                 $pos = strpos(substr($page, $pos + 1), '#');
363                 if ($pos === FALSE) return array($page, '', FALSE);
364         }
365
366         $s_page = substr($page, 0, $pos);
367         $anchor = substr($page, $pos + 1);
368
369         if($strict_editable === TRUE &&  preg_match('/^[a-z][a-f0-9]{7}$/', $anchor)) {
370                 return array ($s_page, $anchor, TRUE); // Seems fixed-anchor
371         } else {
372                 return array ($s_page, $anchor, FALSE);
373         }
374 }
375
376 // Check HTTP header()s were sent already, or
377 // there're blank lines or something out of php blocks
378 function pkwk_headers_sent()
379 {
380         if (PKWK_OPTIMISE) return;
381
382         $file = $line = '';
383         if (version_compare(PHP_VERSION, '4.3.0', '>=')) {
384                 if (headers_sent($file, $line))
385                     die('Headers already sent at ' .
386                         htmlspecialchars($file) .
387                         ' line ' . $line . '.');
388         } else {
389                 if (headers_sent())
390                         die('Headers already sent.');
391         }
392 }
393
394 // Output common HTTP headers
395 function pkwk_common_headers()
396 {
397         if (! PKWK_OPTIMISE) pkwk_headers_sent();
398
399         if(defined('PKWK_ZLIB_LOADABLE_MODULE')) {
400                 $matches = array();
401                 if(ini_get('zlib.output_compression') &&
402                     preg_match('/\b(gzip|deflate)\b/i', $_SERVER['HTTP_ACCEPT_ENCODING'], $matches)) {
403                         // Bug #29350 output_compression compresses everything _without header_ as loadable module
404                         // http://bugs.php.net/bug.php?id=29350
405                         header('Content-Encoding: ' . $matches[1]);
406                         header('Vary: Accept-Encoding');
407                 }
408         }
409 }
410
411 // DTD definitions
412 define('PKWK_DTD_XHTML_1_1',              17); // Strict only
413 define('PKWK_DTD_XHTML_1_0',              16); // Strict
414 define('PKWK_DTD_XHTML_1_0_STRICT',       16);
415 define('PKWK_DTD_XHTML_1_0_TRANSITIONAL', 15);
416 define('PKWK_DTD_XHTML_1_0_FRAMESET',     14);
417 define('PKWK_DTD_HTML_4_01',               3); // Strict
418 define('PKWK_DTD_HTML_4_01_STRICT',        3);
419 define('PKWK_DTD_HTML_4_01_TRANSITIONAL',  2);
420 define('PKWK_DTD_HTML_4_01_FRAMESET',      1);
421
422 // Output HTML DTD, <html> start tag. Return content-type.
423 function pkwk_output_dtd($pkwk_dtd = PKWK_DTD_XHTML_1_1)
424 {
425         static $called;
426         if (isset($called)) die('pkwk_output_dtd() already called. Why?');
427         $called = TRUE;
428
429         $type = 'XHTML';
430         $option = '';
431         switch($pkwk_dtd){
432         case PKWK_DTD_XHTML_1_1             : $version = '1.1' ; $dtd = 'http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd'; break;
433         case PKWK_DTD_XHTML_1_0_STRICT      : $version = '1.0' ; $option = 'Strict';       $dtd = 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd';      break;
434         case PKWK_DTD_XHTML_1_0_TRANSITIONAL: $version = '1.0' ; $option = 'Transitional'; $dtd = 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'; break;
435         case PKWK_DTD_HTML_4_01_STRICT      : $type = 'HTML'; $version = '4.01'; $dtd = 'http://www.w3.org/TR/html4/strict.dtd';   break;
436         case PKWK_DTD_HTML_4_01_TRANSITIONAL: $type = 'HTML'; $version = '4.01'; $option = 'Transitional'; $dtd = 'http://www.w3.org/TR/html4/loose.dtd';    break;
437         default: die('DTD not specified or invalid DTD'); break;
438         }
439
440         // Output XML or not
441         if ($type == 'XHTML') echo '<?xml version="1.0" encoding="' . CONTENT_CHARSET . '" ?>' . "\n";
442
443         // Output doctype
444         echo '<!DOCTYPE html PUBLIC "-//W3C//DTD ' . $type . ' ' . $version . ($option != '' ? ' ' . $option : '') . '//EN" "' . $dtd . '">' . "\n";
445
446         // Output <html> start tag
447         echo '<html';
448         if ($type == 'XHTML') {
449                 echo ' xmlns="http://www.w3.org/1999/xhtml"'; // dir="ltr" /* LeftToRight */
450                 echo ' xml:lang="' . LANG . '"';
451                 if ($version == '1.0') echo ' lang="' . LANG . '"'; // Only XHTML 1.0
452         } else {
453                 echo ' lang="' . LANG . '"'; // HTML
454         }
455         echo '>' . "\n"; // <html>
456
457         // Return content-type (with MIME type)
458         if ($type == 'XHTML') {
459                 // NOTE: XHTML 1.1 browser will ignore http-equiv
460                 return '<meta http-equiv="content-type" content="application/xhtml+xml; charset=' . CONTENT_CHARSET . '" />' . "\n";
461         } else {
462                 return '<meta http-equiv="content-type" content="text/html; charset=' . CONTENT_CHARSET . '" />' . "\n";
463         }
464 }
465 ?>