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 ||
30 AUTH_TYPE_SAML === $auth_type) {
32 $enable_logout = true;
36 } else if (AUTH_TYPE_BASIC === $auth_type) {
38 $enable_logout = true;
41 if (! file_exists(SKIN_FILE) || ! is_readable(SKIN_FILE))
42 die_message('SKIN_FILE is not found');
44 $_LINK = $_IMAGE = array();
46 $_page = isset($vars['page']) ? $vars['page'] : '';
47 $r_page = pagename_urlencode($_page);
50 $canonical_url = $script;
51 if ($_page !== $defaultpage) {
52 $canonical_url = $script . '?' . $r_page;
55 // Set $_LINK for skin
56 $_LINK['add'] = "$script?cmd=add&page=$r_page";
57 $_LINK['backup'] = "$script?cmd=backup&page=$r_page";
58 $_LINK['copy'] = "$script?plugin=template&refer=$r_page";
59 $_LINK['diff'] = "$script?cmd=diff&page=$r_page";
60 $_LINK['edit'] = "$script?cmd=edit&page=$r_page";
61 $_LINK['filelist'] = "$script?cmd=filelist";
62 $_LINK['freeze'] = "$script?cmd=freeze&page=$r_page";
63 $_LINK['help'] = "$script?" . pagename_urlencode($help_page);
64 $_LINK['list'] = "$script?cmd=list";
65 $_LINK['new'] = "$script?plugin=newpage&refer=$r_page";
66 $_LINK['rdf'] = "$script?cmd=rss&ver=1.0";
67 $_LINK['recent'] = "$script?" . pagename_urlencode($whatsnew);
68 $_LINK['reload'] = $canonical_url;
69 $_LINK['rename'] = "$script?plugin=rename&refer=$r_page";
70 $_LINK['rss'] = "$script?cmd=rss";
71 $_LINK['rss10'] = "$script?cmd=rss&ver=1.0"; // Same as 'rdf'
72 $_LINK['rss20'] = "$script?cmd=rss&ver=2.0";
73 $_LINK['search'] = "$script?cmd=search";
74 $_LINK['top'] = "$script?" . pagename_urlencode($defaultpage);
75 $_LINK['unfreeze'] = "$script?cmd=unfreeze&page=$r_page";
76 $_LINK['upload'] = "$script?plugin=attach&pcmd=upload&page=$r_page";
77 $login_link = "#LOGIN_ERROR"; // dummy link that is not used
80 $login_link = "$script?plugin=loginform&pcmd=login&page=$r_page";
82 case AUTH_TYPE_EXTERNAL:
84 $login_link = get_auth_external_login_url($_page, $_LINK['reload']);
87 $_LINK['login'] = htmlsc($login_link);
88 $_LINK['logout'] = "$script?plugin=loginform&pcmd=logout&page=$r_page";
90 // Compat: Skins for 1.4.4 and before
91 $link_add = & $_LINK['add'];
92 $link_new = & $_LINK['new']; // New!
93 $link_edit = & $_LINK['edit'];
94 $link_diff = & $_LINK['diff'];
95 $link_top = & $_LINK['top'];
96 $link_list = & $_LINK['list'];
97 $link_filelist = & $_LINK['filelist'];
98 $link_search = & $_LINK['search'];
99 $link_whatsnew = & $_LINK['recent'];
100 $link_backup = & $_LINK['backup'];
101 $link_help = & $_LINK['help'];
102 $link_trackback = ''; // Removed (compat)
103 $link_rdf = & $_LINK['rdf']; // New!
104 $link_rss = & $_LINK['rss'];
105 $link_rss10 = & $_LINK['rss10']; // New!
106 $link_rss20 = & $_LINK['rss20']; // New!
107 $link_freeze = & $_LINK['freeze'];
108 $link_unfreeze = & $_LINK['unfreeze'];
109 $link_upload = & $_LINK['upload'];
110 $link_template = & $_LINK['copy'];
111 $link_refer = ''; // Removed (compat)
112 $link_rename = & $_LINK['rename'];
115 $is_page = (is_pagename($_page) && ! arg_check('backup') && $_page != $whatsnew);
116 $is_read = (arg_check('read') && is_page($_page));
117 $is_freeze = is_freeze($_page);
119 // Last modification date (string) of the page
120 $lastmodified = $is_read ? format_date(get_filetime($_page)) .
121 ' ' . get_pg_passage($_page, FALSE) : '';
123 // List of attached files to the page
124 $attaches = ($attach_link && $is_read && exist_plugin_action('attach')) ?
125 attach_filelist() : '';
127 // List of related pages
128 $related = ($related_link && $is_read) ? make_related($_page) : '';
131 ksort($foot_explain, SORT_NUMERIC);
132 $notes = ! empty($foot_explain) ? $note_hr . join("\n", $foot_explain) : '';
134 // Tags will be inserted into <head></head>
135 $head_tag = ! empty($head_tags) ? join("\n", $head_tags) ."\n" : '';
138 // Last modification date (UNIX timestamp) of the page
139 $fmt = $is_read ? get_filetime($_page) + LOCALZONE : 0;
141 // Output nofollow / noindex regardless os skin file
142 if (!$is_read || $nofollow) {
143 if (!headers_sent()) {
144 header("X-Robots-Tag: noindex,nofollow");
148 // Send Canonical URL for Search Engine Optimization
149 if ($is_read && !headers_sent()) {
150 header("Link: <$canonical_url>; rel=\"canonical\"");
154 if ($search_word_color && isset($vars['word'])) {
155 $body = '<div class="small">' . $_msg_word . htmlsc($vars['word']) .
156 '</div>' . $hr . "\n" . $body;
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);
165 foreach ($words as $word=>$id) $keys[$word] = strlen($word);
166 arsort($keys, SORT_NUMERIC);
167 $keys = get_search_words(array_keys($keys), TRUE);
169 foreach ($keys as $key=>$pattern) {
170 $s_key = htmlsc($key);
172 '<textarea[^>]*>.*?<\/textarea>' . // Ignore textareas
173 '|' . '<[^>]*>' . // Ignore tags
174 '|' . '&[^;]+;' . // Ignore entities
175 '|' . '(' . $pattern . ')' . // $matches[1]: Regex for a search word
177 $decorate_Nth_word = create_function(
179 'return (isset($matches[1])) ? ' .
180 '\'<strong class="word' .
182 '">\' . $matches[1] . \'</strong>\' : ' .
185 $body = preg_replace_callback($pattern, $decorate_Nth_word, $body);
186 $notes = preg_replace_callback($pattern, $decorate_Nth_word, $notes);
191 // Embed Scripting data
192 $html_scripting_data = get_html_scripting_data();
194 // Compat: 'HTML convert time' without time about MenuBar and skin
195 $taketime = elapsedtime();
201 * Get data used by JavaScript modules
203 function get_html_scripting_data()
205 global $ticket_link_sites;
206 if (!isset($ticket_link_sites) || !is_array($ticket_link_sites)) {
210 if (!defined('JSON_UNESCAPED_UNICODE')) {
214 foreach ($ticket_link_sites as $s) {
215 if (!preg_match('/^([a-zA-Z0-9]+)([\.\-][a-zA-Z0-9]+)*$/', $s['key'])) {
218 $site_info_json = htmlsc(json_encode($s, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
220 <span class="pukiwiki-ticketlink-site" data-site="$site_info_json"></span>
225 <div class="pukiwiki-ticketlink-def" style="display:none;">
233 function edit_form($page, $postdata, $digest = FALSE, $b_template = TRUE)
235 global $script, $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;
242 // Newly generate $digest or not
243 if ($digest === FALSE) $digest = md5(join('', get_source($page)));
245 $refer = $template = '';
248 $addtag = $add_top = '';
249 if(isset($vars['add'])) {
251 $addtag = '<input type="hidden" name="add" value="true" />';
252 $add_top = isset($vars['add_top']) ? ' checked="checked"' : '';
253 $add_top = '<input type="checkbox" name="add_top" ' .
254 'id="_edit_form_add_top" value="true"' . $add_top . ' />' . "\n" .
255 ' <label for="_edit_form_add_top">' .
256 '<span class="small">' . $_btn_addtop . '</span>' .
260 if($load_template_func && $b_template) {
261 $tpage_names = array(); // Pages marked as template
262 $template_page = ':config/Templates';
264 foreach(get_source($template_page) as $_templates) {
266 if (! preg_match('#\-\s*\[\[([^\[\]]+)\]\]#', $_templates, $m)) continue;
267 $tpage = preg_replace('#^./#', "$template_page/", $m[1]);
268 if (! is_page($tpage)) continue;
269 $tpage_names[] = $tpage;
271 $page_names = array();
272 foreach(get_existpages() as $_page) {
273 if ($_page == $whatsnew || check_non_list($_page) ||
274 !is_page_readable($_page))
276 if (preg_match('/template/i', $_page)) {
277 $tpage_names[] = $_page;
279 if (count($page_names) >= $page_max) continue;
280 $page_names[] = $_page;
283 $tpage_names2 = array_values(array_unique($tpage_names));
284 natcasesort($tpage_names2);
285 natcasesort($page_names);
286 $tpages = array(); // Template pages
287 $npages = array(); // Normal pages
288 foreach($tpage_names2 as $p) {
290 $tpages[] = ' <option value="' . $ps . '">' . $ps . '</option>';
292 foreach($page_names as $p) {
294 $npages[] = ' <option value="' . $ps . '">' . $ps . '</option>';
296 if (count($page_names) === $page_max) {
297 $npages[] = ' <option value="">...</option>';
299 $s_tpages = join("\n", $tpages);
300 $s_npages = join("\n", $npages);
302 <select name="template_page">
303 <option value="">-- $_btn_template --</option>
304 <optgroup label="$_label_template_pages">
307 <optgroup label="$_title_list">
311 <input type="submit" name="template" value="$_btn_load" accesskey="r" />
315 if (isset($vars['refer']) && $vars['refer'] != '')
316 $refer = '[[' . strip_bracket($vars['refer']) . ']]' . "\n\n";
319 $r_page = rawurlencode($page);
320 $s_page = htmlsc($page);
321 $s_digest = htmlsc($digest);
322 $s_postdata = htmlsc($refer . $postdata);
323 $s_original = isset($vars['original']) ? htmlsc($vars['original']) : $s_postdata;
324 $b_preview = isset($vars['preview']); // TRUE when preview
325 $btn_preview = $b_preview ? $_btn_repreview : $_btn_preview;
327 // Checkbox 'do not change timestamp'
328 $add_notimestamp = '';
329 if ($notimeupdate != 0) {
330 global $_btn_notchangetimestamp;
331 $checked_time = isset($vars['notimestamp']) ? ' checked="checked"' : '';
332 // Only for administrator
333 if ($notimeupdate == 2) {
334 $add_notimestamp = ' ' .
335 '<input type="password" name="pass" size="12" />' . "\n";
337 $add_notimestamp = '<input type="checkbox" name="notimestamp" ' .
338 'id="_edit_form_notimestamp" value="true"' . $checked_time . ' />' . "\n" .
339 ' ' . '<label for="_edit_form_notimestamp"><span class="small">' .
340 $_btn_notchangetimestamp . '</span></label>' . "\n" .
345 // 'margin-bottom', 'float:left', and 'margin-top'
346 // are for layout of 'cancel button'
348 <div class="edit_form">
349 <form action="$script" method="post" style="margin-bottom:0px;">
352 <input type="hidden" name="cmd" value="edit" />
353 <input type="hidden" name="page" value="$s_page" />
354 <input type="hidden" name="digest" value="$s_digest" />
355 <textarea name="msg" rows="$rows" cols="$cols">$s_postdata</textarea>
357 <div style="float:left;">
358 <input type="submit" name="preview" value="$btn_preview" accesskey="p" />
359 <input type="submit" name="write" value="$_btn_update" accesskey="s" />
363 <textarea name="original" rows="1" cols="1" style="display:none">$s_original</textarea>
365 <form action="$script" method="post" style="margin-top:0px;">
366 <input type="hidden" name="cmd" value="edit" />
367 <input type="hidden" name="page" value="$s_page" />
368 <input type="submit" name="cancel" value="$_btn_cancel" accesskey="c" />
373 $body .= '<ul><li><a href="' .
374 $script . '?' . pagename_urlencode($rule_page) .
375 '" target="_blank">' . $_msg_help . '</a></li></ul>';
380 function make_related($page, $tag = '')
382 global $script, $vars, $rule_related_str, $related_str;
383 global $_ul_left_margin, $_ul_margin, $_list_pad_str;
385 prepare_links_related($page);
386 $links = links_get_related($page);
389 ksort($links, SORT_STRING); // Page name, alphabetical order
391 arsort($links, SORT_NUMERIC); // Last modified date, newer
395 foreach ($links as $page=>$lastmod) {
396 if (check_non_list($page)) continue;
398 $r_page = pagename_urlencode($page);
399 $s_page = htmlsc($page);
400 $passage = get_passage($lastmod);
402 '<a href="' . $script . '?' . $r_page . '" title="' .
403 $s_page . ' ' . $passage . '">' . $s_page . '</a>' :
404 '<a href="' . $script . '?' . $r_page . '">' .
405 $s_page . '</a>' . $passage;
407 if (empty($_links)) return ''; // Nothing
409 if ($tag == 'p') { // From the line-head
410 $margin = $_ul_left_margin + $_ul_margin;
411 $style = sprintf($_list_pad_str, 1, $margin, $margin);
412 $retval = "\n" . '<ul' . $style . '>' . "\n" .
413 '<li>' . join($rule_related_str, $_links) . '</li>' . "\n" .
416 $retval = join($rule_related_str, $_links);
418 $retval = join($related_str, $_links);
424 // User-defined rules (convert without replacing source)
425 function make_line_rules($str)
428 static $pattern, $replace;
430 if (! isset($pattern)) {
431 $pattern = array_map(create_function('$a',
432 'return \'/\' . $a . \'/\';'), array_keys($line_rules));
433 $replace = array_values($line_rules);
437 return preg_replace($pattern, $replace, $str);
440 // Remove all HTML tags(or just anchor tags), and WikiName-speific decorations
441 function strip_htmltag($str, $all = TRUE)
443 global $_symbol_noexists;
444 static $noexists_pattern;
446 if (! isset($noexists_pattern))
447 $noexists_pattern = '#<span class="noexists">([^<]*)<a[^>]+>' .
448 preg_quote($_symbol_noexists, '#') . '</a></span>#';
450 // Strip Dagnling-Link decoration (Tags and "$_symbol_noexists")
451 $str = preg_replace($noexists_pattern, '$1', $str);
454 // All other HTML tags
455 return preg_replace('#<[^>]+>#', '', $str);
457 // All other anchor-tags only
458 return preg_replace('#<a[^>]+>|</a>#i', '', $str);
462 // Remove AutoLink marker with AutLink itself
463 function strip_autolink($str)
465 return preg_replace('#<!--autolink--><a [^>]+>|</a><!--/autolink-->#', '', $str);
468 // Make a backlink. searching-link of the page name, by the page name, for the page name
469 function make_search($page)
473 $s_page = htmlsc($page);
474 $r_page = rawurlencode($page);
476 return '<a href="' . $script . '?plugin=related&page=' . $r_page .
477 '">' . $s_page . '</a> ';
480 // Make heading string (remove heading-related decorations from Wiki text)
481 function make_heading(& $str, $strip = TRUE)
485 // Cut fixed-heading anchors
488 if (preg_match('/^(\*{0,3})(.*?)\[#([A-Za-z][\w-]+)\](.*?)$/m', $str, $matches)) {
489 $str = $matches[2] . $matches[4];
492 $str = preg_replace('/^\*{0,3}/', '', $str);
495 // Cut footnotes and tags
497 $str = strip_htmltag(make_link(preg_replace($NotePattern, '', $str)));
502 // Separate a page-name(or URL or null string) and an anchor
503 // (last one standing) without sharp
504 function anchor_explode($page, $strict_editable = FALSE)
506 $pos = strrpos($page, '#');
507 if ($pos === FALSE) return array($page, '', FALSE);
509 // Ignore the last sharp letter
510 if ($pos + 1 == strlen($page)) {
511 $pos = strpos(substr($page, $pos + 1), '#');
512 if ($pos === FALSE) return array($page, '', FALSE);
515 $s_page = substr($page, 0, $pos);
516 $anchor = substr($page, $pos + 1);
518 if($strict_editable === TRUE && preg_match('/^[a-z][a-f0-9]{7}$/', $anchor)) {
519 return array ($s_page, $anchor, TRUE); // Seems fixed-anchor
521 return array ($s_page, $anchor, FALSE);
525 // Check HTTP header()s were sent already, or
526 // there're blank lines or something out of php blocks
527 function pkwk_headers_sent()
529 if (PKWK_OPTIMISE) return;
532 if (version_compare(PHP_VERSION, '4.3.0', '>=')) {
533 if (headers_sent($file, $line))
534 die('Headers already sent at ' .
536 ' line ' . $line . '.');
539 die('Headers already sent.');
543 // Output common HTTP headers
544 function pkwk_common_headers()
546 if (! PKWK_OPTIMISE) pkwk_headers_sent();
548 if(defined('PKWK_ZLIB_LOADABLE_MODULE')) {
550 if(ini_get('zlib.output_compression') &&
551 preg_match('/\b(gzip|deflate)\b/i', $_SERVER['HTTP_ACCEPT_ENCODING'], $matches)) {
552 // Bug #29350 output_compression compresses everything _without header_ as loadable module
553 // http://bugs.php.net/bug.php?id=29350
554 header('Content-Encoding: ' . $matches[1]);
555 header('Vary: Accept-Encoding');
561 define('PKWK_DTD_XHTML_1_1', 17); // Strict only
562 define('PKWK_DTD_XHTML_1_0', 16); // Strict
563 define('PKWK_DTD_XHTML_1_0_STRICT', 16);
564 define('PKWK_DTD_XHTML_1_0_TRANSITIONAL', 15);
565 define('PKWK_DTD_XHTML_1_0_FRAMESET', 14);
566 define('PKWK_DTD_HTML_4_01', 3); // Strict
567 define('PKWK_DTD_HTML_4_01_STRICT', 3);
568 define('PKWK_DTD_HTML_4_01_TRANSITIONAL', 2);
569 define('PKWK_DTD_HTML_4_01_FRAMESET', 1);
571 define('PKWK_DTD_TYPE_XHTML', 1);
572 define('PKWK_DTD_TYPE_HTML', 0);
574 // Output HTML DTD, <html> start tag. Return content-type.
575 function pkwk_output_dtd($pkwk_dtd = PKWK_DTD_XHTML_1_1, $charset = CONTENT_CHARSET)
578 if (isset($called)) die('pkwk_output_dtd() already called. Why?');
581 $type = PKWK_DTD_TYPE_XHTML;
584 case PKWK_DTD_XHTML_1_1 :
586 $dtd = 'http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd';
588 case PKWK_DTD_XHTML_1_0_STRICT :
591 $dtd = 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd';
593 case PKWK_DTD_XHTML_1_0_TRANSITIONAL:
595 $option = 'Transitional';
596 $dtd = 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd';
599 case PKWK_DTD_HTML_4_01_STRICT :
600 $type = PKWK_DTD_TYPE_HTML;
602 $dtd = 'http://www.w3.org/TR/html4/strict.dtd';
604 case PKWK_DTD_HTML_4_01_TRANSITIONAL:
605 $type = PKWK_DTD_TYPE_HTML;
607 $option = 'Transitional';
608 $dtd = 'http://www.w3.org/TR/html4/loose.dtd';
611 default: die('DTD not specified or invalid DTD');
615 $charset = htmlsc($charset);
618 if ($type == PKWK_DTD_TYPE_XHTML) echo '<?xml version="1.0" encoding="' . $charset . '" ?>' . "\n";
621 echo '<!DOCTYPE html PUBLIC "-//W3C//DTD ' .
622 ($type == PKWK_DTD_TYPE_XHTML ? 'XHTML' : 'HTML') . ' ' .
624 ($option != '' ? ' ' . $option : '') .
629 // Output <html> start tag
631 if ($type == PKWK_DTD_TYPE_XHTML) {
632 echo ' xmlns="http://www.w3.org/1999/xhtml"'; // dir="ltr" /* LeftToRight */
633 echo ' xml:lang="' . LANG . '"';
634 if ($version == '1.0') echo ' lang="' . LANG . '"'; // Only XHTML 1.0
636 echo ' lang="' . LANG . '"'; // HTML
638 echo '>' . "\n"; // <html>
640 // Return content-type (with MIME type)
641 if ($type == PKWK_DTD_TYPE_XHTML) {
642 // NOTE: XHTML 1.1 browser will ignore http-equiv
643 return '<meta http-equiv="content-type" content="application/xhtml+xml; charset=' . $charset . '" />' . "\n";
645 return '<meta http-equiv="content-type" content="text/html; charset=' . $charset . '" />' . "\n";