OSDN Git Service

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