OSDN Git Service

BugTrack2/361 Send Canonical URL link http header for Search Engines
[pukiwiki/pukiwiki.git] / lib / html.php
1 <?php
2 // PukiWiki - Yet another WikiWikiWeb clone.
3 // html.php
4 // Copyright
5 //   2002-2016 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         // Add JavaScript header when ...
46         if (! PKWK_ALLOW_JAVASCRIPT) unset($javascript);
47
48         $_page  = isset($vars['page']) ? $vars['page'] : '';
49         $r_page = pagename_urlencode($_page);
50
51         // Canonical URL
52         $canonical_url = $script;
53         if ($_page !== $defaultpage) {
54                 $canonical_url = $script . '?' . $r_page;
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                         $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         // Compat: 'HTML convert time' without time about MenuBar and skin
193         $taketime = elapsedtime();
194
195         require(SKIN_FILE);
196 }
197
198 // Show 'edit' form
199 function edit_form($page, $postdata, $digest = FALSE, $b_template = TRUE)
200 {
201         global $script, $vars, $rows, $cols, $hr, $function_freeze;
202         global $_btn_preview, $_btn_repreview, $_btn_update, $_btn_cancel, $_msg_help;
203         global $whatsnew, $_btn_template, $_btn_load, $load_template_func;
204         global $notimeupdate;
205         global $_title_list, $_label_template_pages;
206
207         // Newly generate $digest or not
208         if ($digest === FALSE) $digest = md5(join('', get_source($page)));
209
210         $refer = $template = '';
211  
212         // Add plugin
213         $addtag = $add_top = '';
214         if(isset($vars['add'])) {
215                 global $_btn_addtop;
216                 $addtag  = '<input type="hidden" name="add"    value="true" />';
217                 $add_top = isset($vars['add_top']) ? ' checked="checked"' : '';
218                 $add_top = '<input type="checkbox" name="add_top" ' .
219                         'id="_edit_form_add_top" value="true"' . $add_top . ' />' . "\n" .
220                         '  <label for="_edit_form_add_top">' .
221                                 '<span class="small">' . $_btn_addtop . '</span>' .
222                         '</label>';
223         }
224
225         if($load_template_func && $b_template) {
226                 $tpage_names = array(); // Pages marked as template
227                 $template_page = ':config/Templates';
228                 $page_max = 100;
229                 foreach(get_source($template_page) as $_templates) {
230                         $m = array();
231                         if (! preg_match('#\-\s*\[\[([^\[\]]+)\]\]#', $_templates, $m)) continue;
232                         $tpage = preg_replace('#^./#', "$template_page/", $m[1]);
233                         if (! is_page($tpage)) continue;
234                         $tpage_names[] = $tpage;
235                 }
236                 $page_names = array();
237                 foreach(get_existpages() as $_page) {
238                         if ($_page == $whatsnew || check_non_list($_page))
239                                 continue;
240                         if (preg_match('/template/i', $_page)) {
241                                 $tpage_names[] = $_page;
242                         } else {
243                                 if (count($page_names) >= $page_max) continue;
244                                 $page_names[] = $_page;
245                         }
246                 }
247                 $tpage_names2 = array_values(array_unique($tpage_names));
248                 natcasesort($tpage_names2);
249                 natcasesort($page_names);
250                 $tpages = array(); // Template pages
251                 $npages = array(); // Normal pages
252                 foreach($tpage_names2 as $p) {
253                         $ps = htmlsc($p);
254                         $tpages[] = '   <option value="' . $ps . '">' . $ps . '</option>';
255                 }
256                 foreach($page_names as $p) {
257                         $ps = htmlsc($p);
258                         $npages[] = '   <option value="' . $ps . '">' . $ps . '</option>';
259                 }
260                 if (count($page_names) === $page_max) {
261                         $npages[] = '   <option value="">...</option>';
262                 }
263                 $s_tpages  = join("\n", $tpages);
264                 $s_npages  = join("\n", $npages);
265                 $template = <<<EOD
266   <select name="template_page">
267    <option value="">-- $_btn_template --</option>
268    <optgroup label="$_label_template_pages">
269 $s_tpages
270    </optgroup>
271    <optgroup label="$_title_list">
272 $s_npages
273    </optgroup>
274   </select>
275   <input type="submit" name="template" value="$_btn_load" accesskey="r" />
276   <br />
277 EOD;
278
279                 if (isset($vars['refer']) && $vars['refer'] != '')
280                         $refer = '[[' . strip_bracket($vars['refer']) . ']]' . "\n\n";
281         }
282
283         $r_page      = rawurlencode($page);
284         $s_page      = htmlsc($page);
285         $s_digest    = htmlsc($digest);
286         $s_postdata  = htmlsc($refer . $postdata);
287         $s_original  = isset($vars['original']) ? htmlsc($vars['original']) : $s_postdata;
288         $b_preview   = isset($vars['preview']); // TRUE when preview
289         $btn_preview = $b_preview ? $_btn_repreview : $_btn_preview;
290
291         // Checkbox 'do not change timestamp'
292         $add_notimestamp = '';
293         if ($notimeupdate != 0) {
294                 global $_btn_notchangetimestamp;
295                 $checked_time = isset($vars['notimestamp']) ? ' checked="checked"' : '';
296                 // Only for administrator
297                 if ($notimeupdate == 2) {
298                         $add_notimestamp = '   ' .
299                                 '<input type="password" name="pass" size="12" />' . "\n";
300                 }
301                 $add_notimestamp = '<input type="checkbox" name="notimestamp" ' .
302                         'id="_edit_form_notimestamp" value="true"' . $checked_time . ' />' . "\n" .
303                         '   ' . '<label for="_edit_form_notimestamp"><span class="small">' .
304                         $_btn_notchangetimestamp . '</span></label>' . "\n" .
305                         $add_notimestamp .
306                         '&nbsp;';
307         }
308
309         // 'margin-bottom', 'float:left', and 'margin-top'
310         // are for layout of 'cancel button'
311         $body = <<<EOD
312 <div class="edit_form">
313  <form action="$script" method="post" style="margin-bottom:0px;">
314 $template
315   $addtag
316   <input type="hidden" name="cmd"    value="edit" />
317   <input type="hidden" name="page"   value="$s_page" />
318   <input type="hidden" name="digest" value="$s_digest" />
319   <textarea name="msg" rows="$rows" cols="$cols">$s_postdata</textarea>
320   <br />
321   <div style="float:left;">
322    <input type="submit" name="preview" value="$btn_preview" accesskey="p" />
323    <input type="submit" name="write"   value="$_btn_update" accesskey="s" />
324    $add_top
325    $add_notimestamp
326   </div>
327   <textarea name="original" rows="1" cols="1" style="display:none">$s_original</textarea>
328  </form>
329  <form action="$script" method="post" style="margin-top:0px;">
330   <input type="hidden" name="cmd"    value="edit" />
331   <input type="hidden" name="page"   value="$s_page" />
332   <input type="submit" name="cancel" value="$_btn_cancel" accesskey="c" />
333  </form>
334 </div>
335 EOD;
336
337         if (isset($vars['help'])) {
338                 $body .= $hr . catrule();
339         } else {
340                 $body .= '<ul><li><a href="' .
341                         $script . '?cmd=edit&amp;help=true&amp;page=' . $r_page .
342                         '">' . $_msg_help . '</a></li></ul>';
343         }
344
345         return $body;
346 }
347
348 // Related pages
349 function make_related($page, $tag = '')
350 {
351         global $script, $vars, $rule_related_str, $related_str;
352         global $_ul_left_margin, $_ul_margin, $_list_pad_str;
353
354         $links = links_get_related($page);
355
356         if ($tag) {
357                 ksort($links, SORT_STRING);             // Page name, alphabetical order
358         } else {
359                 arsort($links, SORT_NUMERIC);   // Last modified date, newer
360         }
361
362         $_links = array();
363         foreach ($links as $page=>$lastmod) {
364                 if (check_non_list($page)) continue;
365
366                 $r_page   = pagename_urlencode($page);
367                 $s_page   = htmlsc($page);
368                 $passage  = get_passage($lastmod);
369                 $_links[] = $tag ?
370                         '<a href="' . $script . '?' . $r_page . '" title="' .
371                         $s_page . ' ' . $passage . '">' . $s_page . '</a>' :
372                         '<a href="' . $script . '?' . $r_page . '">' .
373                         $s_page . '</a>' . $passage;
374         }
375         if (empty($_links)) return ''; // Nothing
376
377         if ($tag == 'p') { // From the line-head
378                 $margin = $_ul_left_margin + $_ul_margin;
379                 $style  = sprintf($_list_pad_str, 1, $margin, $margin);
380                 $retval =  "\n" . '<ul' . $style . '>' . "\n" .
381                         '<li>' . join($rule_related_str, $_links) . '</li>' . "\n" .
382                         '</ul>' . "\n";
383         } else if ($tag) {
384                 $retval = join($rule_related_str, $_links);
385         } else {
386                 $retval = join($related_str, $_links);
387         }
388
389         return $retval;
390 }
391
392 // User-defined rules (convert without replacing source)
393 function make_line_rules($str)
394 {
395         global $line_rules;
396         static $pattern, $replace;
397
398         if (! isset($pattern)) {
399                 $pattern = array_map(create_function('$a',
400                         'return \'/\' . $a . \'/\';'), array_keys($line_rules));
401                 $replace = array_values($line_rules);
402                 unset($line_rules);
403         }
404
405         return preg_replace($pattern, $replace, $str);
406 }
407
408 // Remove all HTML tags(or just anchor tags), and WikiName-speific decorations
409 function strip_htmltag($str, $all = TRUE)
410 {
411         global $_symbol_noexists;
412         static $noexists_pattern;
413
414         if (! isset($noexists_pattern))
415                 $noexists_pattern = '#<span class="noexists">([^<]*)<a[^>]+>' .
416                         preg_quote($_symbol_noexists, '#') . '</a></span>#';
417
418         // Strip Dagnling-Link decoration (Tags and "$_symbol_noexists")
419         $str = preg_replace($noexists_pattern, '$1', $str);
420
421         if ($all) {
422                 // All other HTML tags
423                 return preg_replace('#<[^>]+>#',        '', $str);
424         } else {
425                 // All other anchor-tags only
426                 return preg_replace('#<a[^>]+>|</a>#i', '', $str);
427         }
428 }
429
430 // Remove AutoLink marker with AutLink itself
431 function strip_autolink($str)
432 {
433         return preg_replace('#<!--autolink--><a [^>]+>|</a><!--/autolink-->#', '', $str);
434 }
435
436 // Make a backlink. searching-link of the page name, by the page name, for the page name
437 function make_search($page)
438 {
439         global $script;
440
441         $s_page = htmlsc($page);
442         $r_page = rawurlencode($page);
443
444         return '<a href="' . $script . '?plugin=related&amp;page=' . $r_page .
445                 '">' . $s_page . '</a> ';
446 }
447
448 // Make heading string (remove heading-related decorations from Wiki text)
449 function make_heading(& $str, $strip = TRUE)
450 {
451         global $NotePattern;
452
453         // Cut fixed-heading anchors
454         $id = '';
455         $matches = array();
456         if (preg_match('/^(\*{0,3})(.*?)\[#([A-Za-z][\w-]+)\](.*?)$/m', $str, $matches)) {
457                 $str = $matches[2] . $matches[4];
458                 $id  = & $matches[3];
459         } else {
460                 $str = preg_replace('/^\*{0,3}/', '', $str);
461         }
462
463         // Cut footnotes and tags
464         if ($strip === TRUE)
465                 $str = strip_htmltag(make_link(preg_replace($NotePattern, '', $str)));
466
467         return $id;
468 }
469
470 // Separate a page-name(or URL or null string) and an anchor
471 // (last one standing) without sharp
472 function anchor_explode($page, $strict_editable = FALSE)
473 {
474         $pos = strrpos($page, '#');
475         if ($pos === FALSE) return array($page, '', FALSE);
476
477         // Ignore the last sharp letter
478         if ($pos + 1 == strlen($page)) {
479                 $pos = strpos(substr($page, $pos + 1), '#');
480                 if ($pos === FALSE) return array($page, '', FALSE);
481         }
482
483         $s_page = substr($page, 0, $pos);
484         $anchor = substr($page, $pos + 1);
485
486         if($strict_editable === TRUE &&  preg_match('/^[a-z][a-f0-9]{7}$/', $anchor)) {
487                 return array ($s_page, $anchor, TRUE); // Seems fixed-anchor
488         } else {
489                 return array ($s_page, $anchor, FALSE);
490         }
491 }
492
493 // Check HTTP header()s were sent already, or
494 // there're blank lines or something out of php blocks
495 function pkwk_headers_sent()
496 {
497         if (PKWK_OPTIMISE) return;
498
499         $file = $line = '';
500         if (version_compare(PHP_VERSION, '4.3.0', '>=')) {
501                 if (headers_sent($file, $line))
502                     die('Headers already sent at ' .
503                         htmlsc($file) .
504                         ' line ' . $line . '.');
505         } else {
506                 if (headers_sent())
507                         die('Headers already sent.');
508         }
509 }
510
511 // Output common HTTP headers
512 function pkwk_common_headers()
513 {
514         if (! PKWK_OPTIMISE) pkwk_headers_sent();
515
516         if(defined('PKWK_ZLIB_LOADABLE_MODULE')) {
517                 $matches = array();
518                 if(ini_get('zlib.output_compression') &&
519                     preg_match('/\b(gzip|deflate)\b/i', $_SERVER['HTTP_ACCEPT_ENCODING'], $matches)) {
520                         // Bug #29350 output_compression compresses everything _without header_ as loadable module
521                         // http://bugs.php.net/bug.php?id=29350
522                         header('Content-Encoding: ' . $matches[1]);
523                         header('Vary: Accept-Encoding');
524                 }
525         }
526 }
527
528 // DTD definitions
529 define('PKWK_DTD_XHTML_1_1',              17); // Strict only
530 define('PKWK_DTD_XHTML_1_0',              16); // Strict
531 define('PKWK_DTD_XHTML_1_0_STRICT',       16);
532 define('PKWK_DTD_XHTML_1_0_TRANSITIONAL', 15);
533 define('PKWK_DTD_XHTML_1_0_FRAMESET',     14);
534 define('PKWK_DTD_HTML_4_01',               3); // Strict
535 define('PKWK_DTD_HTML_4_01_STRICT',        3);
536 define('PKWK_DTD_HTML_4_01_TRANSITIONAL',  2);
537 define('PKWK_DTD_HTML_4_01_FRAMESET',      1);
538
539 define('PKWK_DTD_TYPE_XHTML',  1);
540 define('PKWK_DTD_TYPE_HTML',   0);
541
542 // Output HTML DTD, <html> start tag. Return content-type.
543 function pkwk_output_dtd($pkwk_dtd = PKWK_DTD_XHTML_1_1, $charset = CONTENT_CHARSET)
544 {
545         static $called;
546         if (isset($called)) die('pkwk_output_dtd() already called. Why?');
547         $called = TRUE;
548
549         $type = PKWK_DTD_TYPE_XHTML;
550         $option = '';
551         switch($pkwk_dtd){
552         case PKWK_DTD_XHTML_1_1             :
553                 $version = '1.1' ;
554                 $dtd     = 'http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd';
555                 break;
556         case PKWK_DTD_XHTML_1_0_STRICT      :
557                 $version = '1.0' ;
558                 $option  = 'Strict';
559                 $dtd     = 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd';
560                 break;
561         case PKWK_DTD_XHTML_1_0_TRANSITIONAL:
562                 $version = '1.0' ;
563                 $option  = 'Transitional';
564                 $dtd     = 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd';
565                 break;
566
567         case PKWK_DTD_HTML_4_01_STRICT      :
568                 $type    = PKWK_DTD_TYPE_HTML;
569                 $version = '4.01';
570                 $dtd     = 'http://www.w3.org/TR/html4/strict.dtd';
571                 break;
572         case PKWK_DTD_HTML_4_01_TRANSITIONAL:
573                 $type    = PKWK_DTD_TYPE_HTML;
574                 $version = '4.01';
575                 $option  = 'Transitional';
576                 $dtd     = 'http://www.w3.org/TR/html4/loose.dtd';
577                 break;
578
579         default: die('DTD not specified or invalid DTD');
580                 break;
581         }
582
583         $charset = htmlsc($charset);
584
585         // Output XML or not
586         if ($type == PKWK_DTD_TYPE_XHTML) echo '<?xml version="1.0" encoding="' . $charset . '" ?>' . "\n";
587
588         // Output doctype
589         echo '<!DOCTYPE html PUBLIC "-//W3C//DTD ' .
590                 ($type == PKWK_DTD_TYPE_XHTML ? 'XHTML' : 'HTML') . ' ' .
591                 $version .
592                 ($option != '' ? ' ' . $option : '') .
593                 '//EN" "' .
594                 $dtd .
595                 '">' . "\n";
596
597         // Output <html> start tag
598         echo '<html';
599         if ($type == PKWK_DTD_TYPE_XHTML) {
600                 echo ' xmlns="http://www.w3.org/1999/xhtml"'; // dir="ltr" /* LeftToRight */
601                 echo ' xml:lang="' . LANG . '"';
602                 if ($version == '1.0') echo ' lang="' . LANG . '"'; // Only XHTML 1.0
603         } else {
604                 echo ' lang="' . LANG . '"'; // HTML
605         }
606         echo '>' . "\n"; // <html>
607
608         // Return content-type (with MIME type)
609         if ($type == PKWK_DTD_TYPE_XHTML) {
610                 // NOTE: XHTML 1.1 browser will ignore http-equiv
611                 return '<meta http-equiv="content-type" content="application/xhtml+xml; charset=' . $charset . '" />' . "\n";
612         } else {
613                 return '<meta http-equiv="content-type" content="text/html; charset=' . $charset . '" />' . "\n";
614         }
615 }