OSDN Git Service

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