OSDN Git Service

BugTrack/407 Show attached file list on editing page
[pukiwiki/pukiwiki.git] / lib / html.php
1 <?php
2 // PukiWiki - Yet another WikiWikiWeb clone.
3 // html.php
4 // Copyright
5 //   2002-2017 PukiWiki Development Team
6 //   2001-2002 Originally written by yu-ji
7 // License: GPL v2 or (at your option) any later version
8 //
9 // HTML-publishing related functions
10
11 // Show page-content
12 function catbody($title, $page, $body)
13 {
14         global $vars, $arg, $defaultpage, $whatsnew, $help_page, $hr;
15         global $attach_link, $related_link, $cantedit, $function_freeze;
16         global $search_word_color, $_msg_word, $foot_explain, $note_hr, $head_tags;
17         global $javascript, $nofollow;
18         global $_LANG, $_LINK, $_IMAGE;
19         global $auth_type, $auth_user;
20
21         global $pkwk_dtd;     // XHTML 1.1, XHTML1.0, HTML 4.01 Transitional...
22         global $page_title;   // Title of this site
23         global $do_backup;    // Do backup or not
24         global $modifier;     // Site administrator's  web page
25         global $modifierlink; // Site administrator's name
26
27         $script = get_base_uri();
28         $enable_login = false;
29         $enable_logout = false;
30         if (AUTH_TYPE_FORM === $auth_type || AUTH_TYPE_EXTERNAL === $auth_type ||
31                 AUTH_TYPE_SAML === $auth_type) {
32                 if ($auth_user) {
33                         $enable_logout = true;
34                 } else {
35                         $enable_login = true;
36                 }
37         } else if (AUTH_TYPE_BASIC === $auth_type) {
38                 if ($auth_user) {
39                         $enable_logout = true;
40                 }
41         }
42         if (! file_exists(SKIN_FILE) || ! is_readable(SKIN_FILE))
43                 die_message('SKIN_FILE is not found');
44
45         $_LINK = $_IMAGE = array();
46
47         $_page  = isset($vars['page']) ? $vars['page'] : '';
48         $r_page = pagename_urlencode($_page);
49
50         // Canonical URL
51         if ($_page === $defaultpage) {
52                 $canonical_url = get_base_uri(PKWK_URI_ABSOLUTE);
53         } else {
54                 $canonical_url = get_page_uri($_page, PKWK_URI_ABSOLUTE);
55         }
56
57         // Set $_LINK for skin
58         $_LINK['add']      = "$script?cmd=add&amp;page=$r_page";
59         $_LINK['backup']   = "$script?cmd=backup&amp;page=$r_page";
60         $_LINK['copy']     = "$script?plugin=template&amp;refer=$r_page";
61         $_LINK['diff']     = "$script?cmd=diff&amp;page=$r_page";
62         $_LINK['edit']     = "$script?cmd=edit&amp;page=$r_page";
63         $_LINK['filelist'] = "$script?cmd=filelist";
64         $_LINK['freeze']   = "$script?cmd=freeze&amp;page=$r_page";
65         $_LINK['help']     = "$script?" . pagename_urlencode($help_page);
66         $_LINK['list']     = "$script?cmd=list";
67         $_LINK['new']      = "$script?plugin=newpage&amp;refer=$r_page";
68         $_LINK['rdf']      = "$script?cmd=rss&amp;ver=1.0";
69         $_LINK['recent']   = "$script?" . pagename_urlencode($whatsnew);
70         $_LINK['reload']   = $canonical_url;
71         $_LINK['rename']   = "$script?plugin=rename&amp;refer=$r_page";
72         $_LINK['rss']      = "$script?cmd=rss";
73         $_LINK['rss10']    = "$script?cmd=rss&amp;ver=1.0"; // Same as 'rdf'
74         $_LINK['rss20']    = "$script?cmd=rss&amp;ver=2.0";
75         $_LINK['search']   = "$script?cmd=search";
76         $_LINK['top']      = "$script?" . pagename_urlencode($defaultpage);
77         $_LINK['unfreeze'] = "$script?cmd=unfreeze&amp;page=$r_page";
78         $_LINK['upload']   = "$script?plugin=attach&amp;pcmd=upload&amp;page=$r_page";
79         $login_link = "#LOGIN_ERROR"; // dummy link that is not used
80         switch ($auth_type) {
81                 case AUTH_TYPE_FORM:
82                         $login_link = "$script?plugin=loginform&pcmd=login&page=$r_page";
83                         break;
84                 case AUTH_TYPE_EXTERNAL:
85                 case AUTH_TYPE_SAML:
86                         $login_link = get_auth_external_login_url($_page, $_LINK['reload']);
87                         break;
88         }
89         $_LINK['login']    = htmlsc($login_link);
90         $_LINK['logout']   = "$script?plugin=loginform&amp;pcmd=logout&amp;page=$r_page";
91
92         // Compat: Skins for 1.4.4 and before
93         $link_add       = & $_LINK['add'];
94         $link_new       = & $_LINK['new'];      // New!
95         $link_edit      = & $_LINK['edit'];
96         $link_diff      = & $_LINK['diff'];
97         $link_top       = & $_LINK['top'];
98         $link_list      = & $_LINK['list'];
99         $link_filelist  = & $_LINK['filelist'];
100         $link_search    = & $_LINK['search'];
101         $link_whatsnew  = & $_LINK['recent'];
102         $link_backup    = & $_LINK['backup'];
103         $link_help      = & $_LINK['help'];
104         $link_trackback = ''; // Removed (compat)
105         $link_rdf       = & $_LINK['rdf'];              // New!
106         $link_rss       = & $_LINK['rss'];
107         $link_rss10     = & $_LINK['rss10'];            // New!
108         $link_rss20     = & $_LINK['rss20'];            // New!
109         $link_freeze    = & $_LINK['freeze'];
110         $link_unfreeze  = & $_LINK['unfreeze'];
111         $link_upload    = & $_LINK['upload'];
112         $link_template  = & $_LINK['copy'];
113         $link_refer     = ''; // Removed (compat)
114         $link_rename    = & $_LINK['rename'];
115
116         // Init flags
117         $is_page = (is_pagename($_page) && ! arg_check('backup') && $_page != $whatsnew);
118         $is_read = (arg_check('read') && is_page($_page));
119         $is_freeze = is_freeze($_page);
120
121         // Last modification date (string) of the page
122         $lastmodified = $is_read ?  format_date(get_filetime($_page)) .
123                 ' ' . get_pg_passage($_page, FALSE) : '';
124
125         // List of attached files to the page
126         $show_attaches = $is_read || arg_check('edit');
127         $attaches = ($attach_link && $show_attaches && exist_plugin_action('attach')) ?
128                 attach_filelist() : '';
129
130         // List of related pages
131         $related  = ($related_link && $is_read) ? make_related($_page) : '';
132
133         // List of footnotes
134         ksort($foot_explain, SORT_NUMERIC);
135         $notes = ! empty($foot_explain) ? $note_hr . join("\n", $foot_explain) : '';
136
137         // Tags will be inserted into <head></head>
138         $head_tag = ! empty($head_tags) ? join("\n", $head_tags) ."\n" : '';
139
140         // 1.3.x compat
141         // Last modification date (UNIX timestamp) of the page
142         $fmt = $is_read ? get_filetime($_page) + LOCALZONE : 0;
143
144         // Output nofollow / noindex regardless os skin file
145         if (!$is_read || $nofollow) {
146                 if (!headers_sent()) {
147                         header("X-Robots-Tag: noindex,nofollow");
148                 }
149         }
150
151         // Send Canonical URL for Search Engine Optimization
152         if ($is_read && !headers_sent()) {
153                 header("Link: <$canonical_url>; rel=\"canonical\"");
154         }
155
156         // Search words
157         if ($search_word_color && isset($vars['word'])) {
158                 $body = '<div class="small">' . $_msg_word . htmlsc($vars['word']) .
159                         '</div>' . $hr . "\n" . $body;
160
161                 // BugTrack2/106: Only variables can be passed by reference from PHP 5.0.5
162                 // with array_splice(), array_flip()
163                 $words = preg_split('/\s+/', $vars['word'], -1, PREG_SPLIT_NO_EMPTY);
164                 $words = array_splice($words, 0, 10); // Max: 10 words
165                 $words = array_flip($words);
166
167                 $keys = array();
168                 foreach ($words as $word=>$id) $keys[$word] = strlen($word);
169                 arsort($keys, SORT_NUMERIC);
170                 $keys = get_search_words(array_keys($keys), TRUE);
171                 $id = 0;
172                 foreach ($keys as $key=>$pattern) {
173                         $s_key    = htmlsc($key);
174                         $pattern  = '/' .
175                                 '<textarea[^>]*>.*?<\/textarea>' .      // Ignore textareas
176                                 '|' . '<[^>]*>' .                       // Ignore tags
177                                 '|' . '&[^;]+;' .                       // Ignore entities
178                                 '|' . '(' . $pattern . ')' .            // $matches[1]: Regex for a search word
179                                 '/sS';
180                         $decorate_Nth_word = create_function(
181                                 '$matches',
182                                 'return (isset($matches[1])) ? ' .
183                                         '\'<strong class="word' .
184                                                 $id .
185                                         '">\' . $matches[1] . \'</strong>\' : ' .
186                                         '$matches[0];'
187                         );
188                         $body  = preg_replace_callback($pattern, $decorate_Nth_word, $body);
189                         $notes = preg_replace_callback($pattern, $decorate_Nth_word, $notes);
190                         ++$id;
191                 }
192         }
193
194         // Embed Scripting data
195         $html_scripting_data = get_html_scripting_data();
196
197         // Compat: 'HTML convert time' without time about MenuBar and skin
198         $taketime = elapsedtime();
199
200         require(SKIN_FILE);
201 }
202
203 /**
204  * Get data used by JavaScript modules
205  */
206 function get_html_scripting_data()
207 {
208         global $ticket_link_sites;
209         if (!isset($ticket_link_sites) || !is_array($ticket_link_sites)) {
210                 return '';
211         }
212         // Require: PHP 5.4+
213         if (!defined('JSON_UNESCAPED_UNICODE')) {
214                 return '';
215         };
216         $text = '';
217         foreach ($ticket_link_sites as $s) {
218                 if (!preg_match('/^([a-zA-Z0-9]+)([\.\-][a-zA-Z0-9]+)*$/', $s['key'])) {
219                         continue;
220                 }
221                 $site_info_json = htmlsc(json_encode($s, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
222                 $text .= <<<EOS
223   <span class="pukiwiki-ticketlink-site" data-site="$site_info_json"></span>
224 EOS;
225                 $text .= "\n";
226         }
227         $data = <<<EOS
228 <div class="pukiwiki-ticketlink-def" style="display:none;">
229 $text
230 </div>
231 EOS;
232         return $data;
233 }
234
235 // Show 'edit' form
236 function edit_form($page, $postdata, $digest = FALSE, $b_template = TRUE)
237 {
238         global $vars, $rows, $cols, $hr, $function_freeze;
239         global $_btn_preview, $_btn_repreview, $_btn_update, $_btn_cancel, $_msg_help;
240         global $whatsnew, $_btn_template, $_btn_load, $load_template_func;
241         global $notimeupdate;
242         global $_title_list, $_label_template_pages;
243         global $rule_page;
244
245         $script = get_base_uri();
246         // Newly generate $digest or not
247         if ($digest === FALSE) $digest = md5(join('', get_source($page)));
248
249         $refer = $template = '';
250  
251         // Add plugin
252         $addtag = $add_top = '';
253         if(isset($vars['add'])) {
254                 global $_btn_addtop;
255                 $addtag  = '<input type="hidden" name="add"    value="true" />';
256                 $add_top = isset($vars['add_top']) ? ' checked="checked"' : '';
257                 $add_top = '<input type="checkbox" name="add_top" ' .
258                         'id="_edit_form_add_top" value="true"' . $add_top . ' />' . "\n" .
259                         '  <label for="_edit_form_add_top">' .
260                                 '<span class="small">' . $_btn_addtop . '</span>' .
261                         '</label>';
262         }
263
264         if($load_template_func && $b_template) {
265                 $tpage_names = array(); // Pages marked as template
266                 $template_page = ':config/Templates';
267                 $page_max = 100;
268                 foreach(get_source($template_page) as $_templates) {
269                         $m = array();
270                         if (! preg_match('#\-\s*\[\[([^\[\]]+)\]\]#', $_templates, $m)) continue;
271                         $tpage = preg_replace('#^./#', "$template_page/", $m[1]);
272                         if (! is_page($tpage)) continue;
273                         $tpage_names[] = $tpage;
274                 }
275                 $page_names = array();
276                 foreach(get_existpages() as $_page) {
277                         if ($_page == $whatsnew || check_non_list($_page) ||
278                                 !is_page_readable($_page))
279                                 continue;
280                         if (preg_match('/template/i', $_page)) {
281                                 $tpage_names[] = $_page;
282                         } else {
283                                 if (count($page_names) >= $page_max) continue;
284                                 $page_names[] = $_page;
285                         }
286                 }
287                 $tpage_names2 = array_values(array_unique($tpage_names));
288                 natcasesort($tpage_names2);
289                 natcasesort($page_names);
290                 $tpages = array(); // Template pages
291                 $npages = array(); // Normal pages
292                 foreach($tpage_names2 as $p) {
293                         $ps = htmlsc($p);
294                         $tpages[] = '   <option value="' . $ps . '">' . $ps . '</option>';
295                 }
296                 foreach($page_names as $p) {
297                         $ps = htmlsc($p);
298                         $npages[] = '   <option value="' . $ps . '">' . $ps . '</option>';
299                 }
300                 if (count($page_names) === $page_max) {
301                         $npages[] = '   <option value="">...</option>';
302                 }
303                 $s_tpages  = join("\n", $tpages);
304                 $s_npages  = join("\n", $npages);
305                 $template = <<<EOD
306   <select name="template_page">
307    <option value="">-- $_btn_template --</option>
308    <optgroup label="$_label_template_pages">
309 $s_tpages
310    </optgroup>
311    <optgroup label="$_title_list">
312 $s_npages
313    </optgroup>
314   </select>
315   <input type="submit" name="template" value="$_btn_load" accesskey="r" />
316   <br />
317 EOD;
318
319                 if (isset($vars['refer']) && $vars['refer'] != '')
320                         $refer = '[[' . strip_bracket($vars['refer']) . ']]' . "\n\n";
321         }
322
323         $r_page      = rawurlencode($page);
324         $s_page      = htmlsc($page);
325         $s_digest    = htmlsc($digest);
326         $s_postdata  = htmlsc($refer . $postdata);
327         $s_original  = isset($vars['original']) ? htmlsc($vars['original']) : $s_postdata;
328         $b_preview   = isset($vars['preview']); // TRUE when preview
329         $btn_preview = $b_preview ? $_btn_repreview : $_btn_preview;
330
331         // Checkbox 'do not change timestamp'
332         $add_notimestamp = '';
333         if ($notimeupdate != 0) {
334                 global $_btn_notchangetimestamp;
335                 $checked_time = isset($vars['notimestamp']) ? ' checked="checked"' : '';
336                 // Only for administrator
337                 if ($notimeupdate == 2) {
338                         $add_notimestamp = '   ' .
339                                 '<input type="password" name="pass" size="12" />' . "\n";
340                 }
341                 $add_notimestamp = '<input type="checkbox" name="notimestamp" ' .
342                         'id="_edit_form_notimestamp" value="true"' . $checked_time . ' />' . "\n" .
343                         '   ' . '<label for="_edit_form_notimestamp"><span class="small">' .
344                         $_btn_notchangetimestamp . '</span></label>' . "\n" .
345                         $add_notimestamp .
346                         '&nbsp;';
347         }
348
349         // 'margin-bottom', 'float:left', and 'margin-top'
350         // are for layout of 'cancel button'
351         $body = <<<EOD
352 <div class="edit_form">
353  <form action="$script" method="post" style="margin-bottom:0px;">
354 $template
355   $addtag
356   <input type="hidden" name="cmd"    value="edit" />
357   <input type="hidden" name="page"   value="$s_page" />
358   <input type="hidden" name="digest" value="$s_digest" />
359   <textarea name="msg" rows="$rows" cols="$cols">$s_postdata</textarea>
360   <br />
361   <div style="float:left;">
362    <input type="submit" name="preview" value="$btn_preview" accesskey="p" />
363    <input type="submit" name="write"   value="$_btn_update" accesskey="s" />
364    $add_top
365    $add_notimestamp
366   </div>
367   <textarea name="original" rows="1" cols="1" style="display:none">$s_original</textarea>
368  </form>
369  <form action="$script" method="post" style="margin-top:0px;">
370   <input type="hidden" name="cmd"    value="edit" />
371   <input type="hidden" name="page"   value="$s_page" />
372   <input type="submit" name="cancel" value="$_btn_cancel" accesskey="c" />
373  </form>
374 </div>
375 EOD;
376
377         $body .= '<ul><li><a href="' .
378                 $script . '?' . pagename_urlencode($rule_page) .
379                 '" target="_blank">' . $_msg_help . '</a></li></ul>';
380         return $body;
381 }
382
383 // Related pages
384 function make_related($page, $tag = '')
385 {
386         global $vars, $rule_related_str, $related_str;
387
388         $script = get_base_uri();
389         prepare_links_related($page);
390         $links = links_get_related($page);
391
392         if ($tag) {
393                 ksort($links, SORT_STRING);             // Page name, alphabetical order
394         } else {
395                 arsort($links, SORT_NUMERIC);   // Last modified date, newer
396         }
397
398         $_links = array();
399         foreach ($links as $page=>$lastmod) {
400                 if (check_non_list($page)) continue;
401
402                 $r_page   = pagename_urlencode($page);
403                 $s_page   = htmlsc($page);
404                 $passage  = get_passage($lastmod);
405                 $_links[] = $tag ?
406                         '<a href="' . $script . '?' . $r_page . '" title="' .
407                         $s_page . ' ' . $passage . '">' . $s_page . '</a>' :
408                         '<a href="' . $script . '?' . $r_page . '">' .
409                         $s_page . '</a>' . $passage;
410         }
411         if (empty($_links)) return ''; // Nothing
412
413         if ($tag == 'p') { // From the line-head
414                 $style  = sprintf(pkwk_list_attrs_template(), 1, 1);
415                 $retval =  "\n" . '<ul' . $style . '>' . "\n" .
416                         '<li>' . join($rule_related_str, $_links) . '</li>' . "\n" .
417                         '</ul>' . "\n";
418         } else if ($tag) {
419                 $retval = join($rule_related_str, $_links);
420         } else {
421                 $retval = join($related_str, $_links);
422         }
423
424         return $retval;
425 }
426
427 // User-defined rules (convert without replacing source)
428 function make_line_rules($str)
429 {
430         global $line_rules;
431         static $pattern, $replace;
432
433         if (! isset($pattern)) {
434                 $pattern = array_map(create_function('$a',
435                         'return \'/\' . $a . \'/\';'), array_keys($line_rules));
436                 $replace = array_values($line_rules);
437                 unset($line_rules);
438         }
439
440         return preg_replace($pattern, $replace, $str);
441 }
442
443 // Remove all HTML tags(or just anchor tags), and WikiName-speific decorations
444 function strip_htmltag($str, $all = TRUE)
445 {
446         global $_symbol_noexists;
447         static $noexists_pattern;
448
449         if (! isset($noexists_pattern))
450                 $noexists_pattern = '#<span class="noexists">([^<]*)<a[^>]+>' .
451                         preg_quote($_symbol_noexists, '#') . '</a></span>#';
452
453         // Strip Dagnling-Link decoration (Tags and "$_symbol_noexists")
454         $str = preg_replace($noexists_pattern, '$1', $str);
455
456         if ($all) {
457                 // All other HTML tags
458                 return preg_replace('#<[^>]+>#',        '', $str);
459         } else {
460                 // All other anchor-tags only
461                 return preg_replace('#<a[^>]+>|</a>#i', '', $str);
462         }
463 }
464
465 // Remove AutoLink marker with AutLink itself
466 function strip_autolink($str)
467 {
468         return preg_replace('#<!--autolink--><a [^>]+>|</a><!--/autolink-->#', '', $str);
469 }
470
471 // Make a backlink. searching-link of the page name, by the page name, for the page name
472 function make_search($page)
473 {
474         $s_page = htmlsc($page);
475         $r_page = rawurlencode($page);
476         return '<a href="' . get_base_uri() . '?plugin=related&amp;page=' . $r_page .
477                 '">' . $s_page . '</a> ';
478 }
479
480 // Make heading string (remove heading-related decorations from Wiki text)
481 function make_heading(& $str, $strip = TRUE)
482 {
483         global $NotePattern;
484
485         // Cut fixed-heading anchors
486         $id = '';
487         $matches = array();
488         if (preg_match('/^(\*{0,3})(.*?)\[#([A-Za-z][\w-]+)\](.*?)$/m', $str, $matches)) {
489                 $str = $matches[2] . $matches[4];
490                 $id  = & $matches[3];
491         } else {
492                 $str = preg_replace('/^\*{0,3}/', '', $str);
493         }
494
495         // Cut footnotes and tags
496         if ($strip === TRUE)
497                 $str = strip_htmltag(make_link(preg_replace($NotePattern, '', $str)));
498
499         return $id;
500 }
501
502 // Separate a page-name(or URL or null string) and an anchor
503 // (last one standing) without sharp
504 function anchor_explode($page, $strict_editable = FALSE)
505 {
506         $pos = strrpos($page, '#');
507         if ($pos === FALSE) return array($page, '', FALSE);
508
509         // Ignore the last sharp letter
510         if ($pos + 1 == strlen($page)) {
511                 $pos = strpos(substr($page, $pos + 1), '#');
512                 if ($pos === FALSE) return array($page, '', FALSE);
513         }
514
515         $s_page = substr($page, 0, $pos);
516         $anchor = substr($page, $pos + 1);
517
518         if($strict_editable === TRUE &&  preg_match('/^[a-z][a-f0-9]{7}$/', $anchor)) {
519                 return array ($s_page, $anchor, TRUE); // Seems fixed-anchor
520         } else {
521                 return array ($s_page, $anchor, FALSE);
522         }
523 }
524
525 // Check HTTP header()s were sent already, or
526 // there're blank lines or something out of php blocks
527 function pkwk_headers_sent()
528 {
529         if (PKWK_OPTIMISE) return;
530
531         $file = $line = '';
532         if (version_compare(PHP_VERSION, '4.3.0', '>=')) {
533                 if (headers_sent($file, $line))
534                     die('Headers already sent at ' .
535                         htmlsc($file) .
536                         ' line ' . $line . '.');
537         } else {
538                 if (headers_sent())
539                         die('Headers already sent.');
540         }
541 }
542
543 // Output common HTTP headers
544 function pkwk_common_headers()
545 {
546         if (! PKWK_OPTIMISE) pkwk_headers_sent();
547
548         if(defined('PKWK_ZLIB_LOADABLE_MODULE')) {
549                 $matches = array();
550                 if(ini_get('zlib.output_compression') &&
551                     preg_match('/\b(gzip|deflate)\b/i', $_SERVER['HTTP_ACCEPT_ENCODING'], $matches)) {
552                         // Bug #29350 output_compression compresses everything _without header_ as loadable module
553                         // http://bugs.php.net/bug.php?id=29350
554                         header('Content-Encoding: ' . $matches[1]);
555                         header('Vary: Accept-Encoding');
556                 }
557         }
558 }
559
560 // DTD definitions
561 define('PKWK_DTD_XHTML_1_1',              17); // Strict only
562 define('PKWK_DTD_XHTML_1_0',              16); // Strict
563 define('PKWK_DTD_XHTML_1_0_STRICT',       16);
564 define('PKWK_DTD_XHTML_1_0_TRANSITIONAL', 15);
565 define('PKWK_DTD_XHTML_1_0_FRAMESET',     14);
566 define('PKWK_DTD_HTML_4_01',               3); // Strict
567 define('PKWK_DTD_HTML_4_01_STRICT',        3);
568 define('PKWK_DTD_HTML_4_01_TRANSITIONAL',  2);
569 define('PKWK_DTD_HTML_4_01_FRAMESET',      1);
570
571 define('PKWK_DTD_TYPE_XHTML',  1);
572 define('PKWK_DTD_TYPE_HTML',   0);
573
574 // Output HTML DTD, <html> start tag. Return content-type.
575 function pkwk_output_dtd($pkwk_dtd = PKWK_DTD_XHTML_1_1, $charset = CONTENT_CHARSET)
576 {
577         static $called;
578         if (isset($called)) die('pkwk_output_dtd() already called. Why?');
579         $called = TRUE;
580
581         $type = PKWK_DTD_TYPE_XHTML;
582         $option = '';
583         switch($pkwk_dtd){
584         case PKWK_DTD_XHTML_1_1             :
585                 $version = '1.1' ;
586                 $dtd     = 'http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd';
587                 break;
588         case PKWK_DTD_XHTML_1_0_STRICT      :
589                 $version = '1.0' ;
590                 $option  = 'Strict';
591                 $dtd     = 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd';
592                 break;
593         case PKWK_DTD_XHTML_1_0_TRANSITIONAL:
594                 $version = '1.0' ;
595                 $option  = 'Transitional';
596                 $dtd     = 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd';
597                 break;
598
599         case PKWK_DTD_HTML_4_01_STRICT      :
600                 $type    = PKWK_DTD_TYPE_HTML;
601                 $version = '4.01';
602                 $dtd     = 'http://www.w3.org/TR/html4/strict.dtd';
603                 break;
604         case PKWK_DTD_HTML_4_01_TRANSITIONAL:
605                 $type    = PKWK_DTD_TYPE_HTML;
606                 $version = '4.01';
607                 $option  = 'Transitional';
608                 $dtd     = 'http://www.w3.org/TR/html4/loose.dtd';
609                 break;
610
611         default: die('DTD not specified or invalid DTD');
612                 break;
613         }
614
615         $charset = htmlsc($charset);
616
617         // Output XML or not
618         if ($type == PKWK_DTD_TYPE_XHTML) echo '<?xml version="1.0" encoding="' . $charset . '" ?>' . "\n";
619
620         // Output doctype
621         echo '<!DOCTYPE html PUBLIC "-//W3C//DTD ' .
622                 ($type == PKWK_DTD_TYPE_XHTML ? 'XHTML' : 'HTML') . ' ' .
623                 $version .
624                 ($option != '' ? ' ' . $option : '') .
625                 '//EN" "' .
626                 $dtd .
627                 '">' . "\n";
628
629         // Output <html> start tag
630         echo '<html';
631         if ($type == PKWK_DTD_TYPE_XHTML) {
632                 echo ' xmlns="http://www.w3.org/1999/xhtml"'; // dir="ltr" /* LeftToRight */
633                 echo ' xml:lang="' . LANG . '"';
634                 if ($version == '1.0') echo ' lang="' . LANG . '"'; // Only XHTML 1.0
635         } else {
636                 echo ' lang="' . LANG . '"'; // HTML
637         }
638         echo '>' . "\n"; // <html>
639
640         // Return content-type (with MIME type)
641         if ($type == PKWK_DTD_TYPE_XHTML) {
642                 // NOTE: XHTML 1.1 browser will ignore http-equiv
643                 return '<meta http-equiv="content-type" content="application/xhtml+xml; charset=' . $charset . '" />' . "\n";
644         } else {
645                 return '<meta http-equiv="content-type" content="text/html; charset=' . $charset . '" />' . "\n";
646         }
647 }
648
649 /**
650  * Get template of List (ul, ol, dl) attributes
651  */
652 function pkwk_list_attrs_template() {
653         return ' class="list%d list-indent%d"';
654 }