OSDN Git Service

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