OSDN Git Service

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