2 // PukiWiki - Yet another WikiWikiWeb clone.
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
9 // HTML-publishing related functions
12 function catbody($title, $page, $body)
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;
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
27 $enable_login = false;
28 $enable_logout = false;
29 if (AUTH_TYPE_FORM === $auth_type || AUTH_TYPE_EXTERNAL === $auth_type) {
31 $enable_logout = true;
35 } else if (AUTH_TYPE_BASIC === $auth_type) {
37 $enable_logout = true;
40 if (! file_exists(SKIN_FILE) || ! is_readable(SKIN_FILE))
41 die_message('SKIN_FILE is not found');
43 $_LINK = $_IMAGE = array();
45 $_page = isset($vars['page']) ? $vars['page'] : '';
46 $r_page = pagename_urlencode($_page);
49 $canonical_url = $script;
50 if ($_page !== $defaultpage) {
51 $canonical_url = $script . '?' . $r_page;
54 // Set $_LINK for skin
55 $_LINK['add'] = "$script?cmd=add&page=$r_page";
56 $_LINK['backup'] = "$script?cmd=backup&page=$r_page";
57 $_LINK['copy'] = "$script?plugin=template&refer=$r_page";
58 $_LINK['diff'] = "$script?cmd=diff&page=$r_page";
59 $_LINK['edit'] = "$script?cmd=edit&page=$r_page";
60 $_LINK['filelist'] = "$script?cmd=filelist";
61 $_LINK['freeze'] = "$script?cmd=freeze&page=$r_page";
62 $_LINK['help'] = "$script?" . pagename_urlencode($help_page);
63 $_LINK['list'] = "$script?cmd=list";
64 $_LINK['new'] = "$script?plugin=newpage&refer=$r_page";
65 $_LINK['rdf'] = "$script?cmd=rss&ver=1.0";
66 $_LINK['recent'] = "$script?" . pagename_urlencode($whatsnew);
67 $_LINK['reload'] = $canonical_url;
68 $_LINK['rename'] = "$script?plugin=rename&refer=$r_page";
69 $_LINK['rss'] = "$script?cmd=rss";
70 $_LINK['rss10'] = "$script?cmd=rss&ver=1.0"; // Same as 'rdf'
71 $_LINK['rss20'] = "$script?cmd=rss&ver=2.0";
72 $_LINK['search'] = "$script?cmd=search";
73 $_LINK['top'] = "$script?" . pagename_urlencode($defaultpage);
74 $_LINK['unfreeze'] = "$script?cmd=unfreeze&page=$r_page";
75 $_LINK['upload'] = "$script?plugin=attach&pcmd=upload&page=$r_page";
76 $login_link = "#LOGIN_ERROR"; // dummy link that is not used
79 $login_link = "$script?plugin=loginform&pcmd=login&page=$r_page";
81 case AUTH_TYPE_EXTERNAL:
82 $login_link = get_auth_external_login_url($_page, $_LINK['reload']);
85 $_LINK['login'] = htmlsc($login_link);
86 $_LINK['logout'] = "$script?plugin=loginform&pcmd=logout&page=$r_page";
88 // Compat: Skins for 1.4.4 and before
89 $link_add = & $_LINK['add'];
90 $link_new = & $_LINK['new']; // New!
91 $link_edit = & $_LINK['edit'];
92 $link_diff = & $_LINK['diff'];
93 $link_top = & $_LINK['top'];
94 $link_list = & $_LINK['list'];
95 $link_filelist = & $_LINK['filelist'];
96 $link_search = & $_LINK['search'];
97 $link_whatsnew = & $_LINK['recent'];
98 $link_backup = & $_LINK['backup'];
99 $link_help = & $_LINK['help'];
100 $link_trackback = ''; // Removed (compat)
101 $link_rdf = & $_LINK['rdf']; // New!
102 $link_rss = & $_LINK['rss'];
103 $link_rss10 = & $_LINK['rss10']; // New!
104 $link_rss20 = & $_LINK['rss20']; // New!
105 $link_freeze = & $_LINK['freeze'];
106 $link_unfreeze = & $_LINK['unfreeze'];
107 $link_upload = & $_LINK['upload'];
108 $link_template = & $_LINK['copy'];
109 $link_refer = ''; // Removed (compat)
110 $link_rename = & $_LINK['rename'];
113 $is_page = (is_pagename($_page) && ! arg_check('backup') && $_page != $whatsnew);
114 $is_read = (arg_check('read') && is_page($_page));
115 $is_freeze = is_freeze($_page);
117 // Last modification date (string) of the page
118 $lastmodified = $is_read ? format_date(get_filetime($_page)) .
119 ' ' . get_pg_passage($_page, FALSE) : '';
121 // List of attached files to the page
122 $attaches = ($attach_link && $is_read && exist_plugin_action('attach')) ?
123 attach_filelist() : '';
125 // List of related pages
126 $related = ($related_link && $is_read) ? make_related($_page) : '';
129 ksort($foot_explain, SORT_NUMERIC);
130 $notes = ! empty($foot_explain) ? $note_hr . join("\n", $foot_explain) : '';
132 // Tags will be inserted into <head></head>
133 $head_tag = ! empty($head_tags) ? join("\n", $head_tags) ."\n" : '';
136 // Last modification date (UNIX timestamp) of the page
137 $fmt = $is_read ? get_filetime($_page) + LOCALZONE : 0;
139 // Output nofollow / noindex regardless os skin file
140 if (!$is_read || $nofollow) {
141 if (!headers_sent()) {
142 header("X-Robots-Tag: noindex,nofollow");
146 // Send Canonical URL for Search Engine Optimization
147 if ($is_read && !headers_sent()) {
148 header("Link: <$canonical_url>; rel=\"canonical\"");
152 if ($search_word_color && isset($vars['word'])) {
153 $body = '<div class="small">' . $_msg_word . htmlsc($vars['word']) .
154 '</div>' . $hr . "\n" . $body;
156 // BugTrack2/106: Only variables can be passed by reference from PHP 5.0.5
157 // with array_splice(), array_flip()
158 $words = preg_split('/\s+/', $vars['word'], -1, PREG_SPLIT_NO_EMPTY);
159 $words = array_splice($words, 0, 10); // Max: 10 words
160 $words = array_flip($words);
163 foreach ($words as $word=>$id) $keys[$word] = strlen($word);
164 arsort($keys, SORT_NUMERIC);
165 $keys = get_search_words(array_keys($keys), TRUE);
167 foreach ($keys as $key=>$pattern) {
168 $s_key = htmlsc($key);
170 '<textarea[^>]*>.*?<\/textarea>' . // Ignore textareas
171 '|' . '<[^>]*>' . // Ignore tags
172 '|' . '&[^;]+;' . // Ignore entities
173 '|' . '(' . $pattern . ')' . // $matches[1]: Regex for a search word
175 $decorate_Nth_word = create_function(
177 'return (isset($matches[1])) ? ' .
178 '\'<strong class="word' .
180 '">\' . $matches[1] . \'</strong>\' : ' .
183 $body = preg_replace_callback($pattern, $decorate_Nth_word, $body);
184 $notes = preg_replace_callback($pattern, $decorate_Nth_word, $notes);
189 // Embed Scripting data
190 $html_scripting_data = get_html_scripting_data();
192 // Compat: 'HTML convert time' without time about MenuBar and skin
193 $taketime = elapsedtime();
199 * Get data used by JavaScript modules
201 function get_html_scripting_data()
203 global $ticket_link_sites;
204 if (!isset($ticket_link_sites) || !is_array($ticket_link_sites)) {
208 if (!defined('JSON_UNESCAPED_UNICODE')) {
212 foreach ($ticket_link_sites as $s) {
213 if (!preg_match('/^([a-zA-Z0-9]+)([\.\-][a-zA-Z0-9]+)*$/', $s['key'])) {
216 $site_info_json = htmlsc(json_encode($s, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
218 <span class="pukiwiki-ticketlink-site" data-site="$site_info_json"></span>
223 <div class="pukiwiki-ticketlink-def" style="display:none;">
231 function edit_form($page, $postdata, $digest = FALSE, $b_template = TRUE)
233 global $script, $vars, $rows, $cols, $hr, $function_freeze;
234 global $_btn_preview, $_btn_repreview, $_btn_update, $_btn_cancel, $_msg_help;
235 global $whatsnew, $_btn_template, $_btn_load, $load_template_func;
236 global $notimeupdate;
237 global $_title_list, $_label_template_pages;
240 // Newly generate $digest or not
241 if ($digest === FALSE) $digest = md5(join('', get_source($page)));
243 $refer = $template = '';
246 $addtag = $add_top = '';
247 if(isset($vars['add'])) {
249 $addtag = '<input type="hidden" name="add" value="true" />';
250 $add_top = isset($vars['add_top']) ? ' checked="checked"' : '';
251 $add_top = '<input type="checkbox" name="add_top" ' .
252 'id="_edit_form_add_top" value="true"' . $add_top . ' />' . "\n" .
253 ' <label for="_edit_form_add_top">' .
254 '<span class="small">' . $_btn_addtop . '</span>' .
258 if($load_template_func && $b_template) {
259 $tpage_names = array(); // Pages marked as template
260 $template_page = ':config/Templates';
262 foreach(get_source($template_page) as $_templates) {
264 if (! preg_match('#\-\s*\[\[([^\[\]]+)\]\]#', $_templates, $m)) continue;
265 $tpage = preg_replace('#^./#', "$template_page/", $m[1]);
266 if (! is_page($tpage)) continue;
267 $tpage_names[] = $tpage;
269 $page_names = array();
270 foreach(get_existpages() as $_page) {
271 if ($_page == $whatsnew || check_non_list($_page) ||
272 !is_page_readable($_page))
274 if (preg_match('/template/i', $_page)) {
275 $tpage_names[] = $_page;
277 if (count($page_names) >= $page_max) continue;
278 $page_names[] = $_page;
281 $tpage_names2 = array_values(array_unique($tpage_names));
282 natcasesort($tpage_names2);
283 natcasesort($page_names);
284 $tpages = array(); // Template pages
285 $npages = array(); // Normal pages
286 foreach($tpage_names2 as $p) {
288 $tpages[] = ' <option value="' . $ps . '">' . $ps . '</option>';
290 foreach($page_names as $p) {
292 $npages[] = ' <option value="' . $ps . '">' . $ps . '</option>';
294 if (count($page_names) === $page_max) {
295 $npages[] = ' <option value="">...</option>';
297 $s_tpages = join("\n", $tpages);
298 $s_npages = join("\n", $npages);
300 <select name="template_page">
301 <option value="">-- $_btn_template --</option>
302 <optgroup label="$_label_template_pages">
305 <optgroup label="$_title_list">
309 <input type="submit" name="template" value="$_btn_load" accesskey="r" />
313 if (isset($vars['refer']) && $vars['refer'] != '')
314 $refer = '[[' . strip_bracket($vars['refer']) . ']]' . "\n\n";
317 $r_page = rawurlencode($page);
318 $s_page = htmlsc($page);
319 $s_digest = htmlsc($digest);
320 $s_postdata = htmlsc($refer . $postdata);
321 $s_original = isset($vars['original']) ? htmlsc($vars['original']) : $s_postdata;
322 $b_preview = isset($vars['preview']); // TRUE when preview
323 $btn_preview = $b_preview ? $_btn_repreview : $_btn_preview;
325 // Checkbox 'do not change timestamp'
326 $add_notimestamp = '';
327 if ($notimeupdate != 0) {
328 global $_btn_notchangetimestamp;
329 $checked_time = isset($vars['notimestamp']) ? ' checked="checked"' : '';
330 // Only for administrator
331 if ($notimeupdate == 2) {
332 $add_notimestamp = ' ' .
333 '<input type="password" name="pass" size="12" />' . "\n";
335 $add_notimestamp = '<input type="checkbox" name="notimestamp" ' .
336 'id="_edit_form_notimestamp" value="true"' . $checked_time . ' />' . "\n" .
337 ' ' . '<label for="_edit_form_notimestamp"><span class="small">' .
338 $_btn_notchangetimestamp . '</span></label>' . "\n" .
343 // 'margin-bottom', 'float:left', and 'margin-top'
344 // are for layout of 'cancel button'
346 <div class="edit_form">
347 <form action="$script" method="post" style="margin-bottom:0px;">
350 <input type="hidden" name="cmd" value="edit" />
351 <input type="hidden" name="page" value="$s_page" />
352 <input type="hidden" name="digest" value="$s_digest" />
353 <textarea name="msg" rows="$rows" cols="$cols">$s_postdata</textarea>
355 <div style="float:left;">
356 <input type="submit" name="preview" value="$btn_preview" accesskey="p" />
357 <input type="submit" name="write" value="$_btn_update" accesskey="s" />
361 <textarea name="original" rows="1" cols="1" style="display:none">$s_original</textarea>
363 <form action="$script" method="post" style="margin-top:0px;">
364 <input type="hidden" name="cmd" value="edit" />
365 <input type="hidden" name="page" value="$s_page" />
366 <input type="submit" name="cancel" value="$_btn_cancel" accesskey="c" />
371 $body .= '<ul><li><a href="' .
372 $script . '?' . pagename_urlencode($rule_page) .
373 '" target="_blank">' . $_msg_help . '</a></li></ul>';
378 function make_related($page, $tag = '')
380 global $script, $vars, $rule_related_str, $related_str;
381 global $_ul_left_margin, $_ul_margin, $_list_pad_str;
383 prepare_links_related($page);
384 $links = links_get_related($page);
387 ksort($links, SORT_STRING); // Page name, alphabetical order
389 arsort($links, SORT_NUMERIC); // Last modified date, newer
393 foreach ($links as $page=>$lastmod) {
394 if (check_non_list($page)) continue;
396 $r_page = pagename_urlencode($page);
397 $s_page = htmlsc($page);
398 $passage = get_passage($lastmod);
400 '<a href="' . $script . '?' . $r_page . '" title="' .
401 $s_page . ' ' . $passage . '">' . $s_page . '</a>' :
402 '<a href="' . $script . '?' . $r_page . '">' .
403 $s_page . '</a>' . $passage;
405 if (empty($_links)) return ''; // Nothing
407 if ($tag == 'p') { // From the line-head
408 $margin = $_ul_left_margin + $_ul_margin;
409 $style = sprintf($_list_pad_str, 1, $margin, $margin);
410 $retval = "\n" . '<ul' . $style . '>' . "\n" .
411 '<li>' . join($rule_related_str, $_links) . '</li>' . "\n" .
414 $retval = join($rule_related_str, $_links);
416 $retval = join($related_str, $_links);
422 // User-defined rules (convert without replacing source)
423 function make_line_rules($str)
426 static $pattern, $replace;
428 if (! isset($pattern)) {
429 $pattern = array_map(create_function('$a',
430 'return \'/\' . $a . \'/\';'), array_keys($line_rules));
431 $replace = array_values($line_rules);
435 return preg_replace($pattern, $replace, $str);
438 // Remove all HTML tags(or just anchor tags), and WikiName-speific decorations
439 function strip_htmltag($str, $all = TRUE)
441 global $_symbol_noexists;
442 static $noexists_pattern;
444 if (! isset($noexists_pattern))
445 $noexists_pattern = '#<span class="noexists">([^<]*)<a[^>]+>' .
446 preg_quote($_symbol_noexists, '#') . '</a></span>#';
448 // Strip Dagnling-Link decoration (Tags and "$_symbol_noexists")
449 $str = preg_replace($noexists_pattern, '$1', $str);
452 // All other HTML tags
453 return preg_replace('#<[^>]+>#', '', $str);
455 // All other anchor-tags only
456 return preg_replace('#<a[^>]+>|</a>#i', '', $str);
460 // Remove AutoLink marker with AutLink itself
461 function strip_autolink($str)
463 return preg_replace('#<!--autolink--><a [^>]+>|</a><!--/autolink-->#', '', $str);
466 // Make a backlink. searching-link of the page name, by the page name, for the page name
467 function make_search($page)
471 $s_page = htmlsc($page);
472 $r_page = rawurlencode($page);
474 return '<a href="' . $script . '?plugin=related&page=' . $r_page .
475 '">' . $s_page . '</a> ';
478 // Make heading string (remove heading-related decorations from Wiki text)
479 function make_heading(& $str, $strip = TRUE)
483 // Cut fixed-heading anchors
486 if (preg_match('/^(\*{0,3})(.*?)\[#([A-Za-z][\w-]+)\](.*?)$/m', $str, $matches)) {
487 $str = $matches[2] . $matches[4];
490 $str = preg_replace('/^\*{0,3}/', '', $str);
493 // Cut footnotes and tags
495 $str = strip_htmltag(make_link(preg_replace($NotePattern, '', $str)));
500 // Separate a page-name(or URL or null string) and an anchor
501 // (last one standing) without sharp
502 function anchor_explode($page, $strict_editable = FALSE)
504 $pos = strrpos($page, '#');
505 if ($pos === FALSE) return array($page, '', FALSE);
507 // Ignore the last sharp letter
508 if ($pos + 1 == strlen($page)) {
509 $pos = strpos(substr($page, $pos + 1), '#');
510 if ($pos === FALSE) return array($page, '', FALSE);
513 $s_page = substr($page, 0, $pos);
514 $anchor = substr($page, $pos + 1);
516 if($strict_editable === TRUE && preg_match('/^[a-z][a-f0-9]{7}$/', $anchor)) {
517 return array ($s_page, $anchor, TRUE); // Seems fixed-anchor
519 return array ($s_page, $anchor, FALSE);
523 // Check HTTP header()s were sent already, or
524 // there're blank lines or something out of php blocks
525 function pkwk_headers_sent()
527 if (PKWK_OPTIMISE) return;
530 if (version_compare(PHP_VERSION, '4.3.0', '>=')) {
531 if (headers_sent($file, $line))
532 die('Headers already sent at ' .
534 ' line ' . $line . '.');
537 die('Headers already sent.');
541 // Output common HTTP headers
542 function pkwk_common_headers()
544 if (! PKWK_OPTIMISE) pkwk_headers_sent();
546 if(defined('PKWK_ZLIB_LOADABLE_MODULE')) {
548 if(ini_get('zlib.output_compression') &&
549 preg_match('/\b(gzip|deflate)\b/i', $_SERVER['HTTP_ACCEPT_ENCODING'], $matches)) {
550 // Bug #29350 output_compression compresses everything _without header_ as loadable module
551 // http://bugs.php.net/bug.php?id=29350
552 header('Content-Encoding: ' . $matches[1]);
553 header('Vary: Accept-Encoding');
559 define('PKWK_DTD_XHTML_1_1', 17); // Strict only
560 define('PKWK_DTD_XHTML_1_0', 16); // Strict
561 define('PKWK_DTD_XHTML_1_0_STRICT', 16);
562 define('PKWK_DTD_XHTML_1_0_TRANSITIONAL', 15);
563 define('PKWK_DTD_XHTML_1_0_FRAMESET', 14);
564 define('PKWK_DTD_HTML_4_01', 3); // Strict
565 define('PKWK_DTD_HTML_4_01_STRICT', 3);
566 define('PKWK_DTD_HTML_4_01_TRANSITIONAL', 2);
567 define('PKWK_DTD_HTML_4_01_FRAMESET', 1);
569 define('PKWK_DTD_TYPE_XHTML', 1);
570 define('PKWK_DTD_TYPE_HTML', 0);
572 // Output HTML DTD, <html> start tag. Return content-type.
573 function pkwk_output_dtd($pkwk_dtd = PKWK_DTD_XHTML_1_1, $charset = CONTENT_CHARSET)
576 if (isset($called)) die('pkwk_output_dtd() already called. Why?');
579 $type = PKWK_DTD_TYPE_XHTML;
582 case PKWK_DTD_XHTML_1_1 :
584 $dtd = 'http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd';
586 case PKWK_DTD_XHTML_1_0_STRICT :
589 $dtd = 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd';
591 case PKWK_DTD_XHTML_1_0_TRANSITIONAL:
593 $option = 'Transitional';
594 $dtd = 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd';
597 case PKWK_DTD_HTML_4_01_STRICT :
598 $type = PKWK_DTD_TYPE_HTML;
600 $dtd = 'http://www.w3.org/TR/html4/strict.dtd';
602 case PKWK_DTD_HTML_4_01_TRANSITIONAL:
603 $type = PKWK_DTD_TYPE_HTML;
605 $option = 'Transitional';
606 $dtd = 'http://www.w3.org/TR/html4/loose.dtd';
609 default: die('DTD not specified or invalid DTD');
613 $charset = htmlsc($charset);
616 if ($type == PKWK_DTD_TYPE_XHTML) echo '<?xml version="1.0" encoding="' . $charset . '" ?>' . "\n";
619 echo '<!DOCTYPE html PUBLIC "-//W3C//DTD ' .
620 ($type == PKWK_DTD_TYPE_XHTML ? 'XHTML' : 'HTML') . ' ' .
622 ($option != '' ? ' ' . $option : '') .
627 // Output <html> start tag
629 if ($type == PKWK_DTD_TYPE_XHTML) {
630 echo ' xmlns="http://www.w3.org/1999/xhtml"'; // dir="ltr" /* LeftToRight */
631 echo ' xml:lang="' . LANG . '"';
632 if ($version == '1.0') echo ' lang="' . LANG . '"'; // Only XHTML 1.0
634 echo ' lang="' . LANG . '"'; // HTML
636 echo '>' . "\n"; // <html>
638 // Return content-type (with MIME type)
639 if ($type == PKWK_DTD_TYPE_XHTML) {
640 // NOTE: XHTML 1.1 browser will ignore http-equiv
641 return '<meta http-equiv="content-type" content="application/xhtml+xml; charset=' . $charset . '" />' . "\n";
643 return '<meta http-equiv="content-type" content="text/html; charset=' . $charset . '" />' . "\n";