OSDN Git Service

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