OSDN Git Service

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