2 // PukiWiki - Yet another WikiWikiWeb clone.
5 // 2002-2016 PukiWiki Development Team
6 // 2001-2002 Originally written by yu-ji
7 // License: GPL v2 or (at your option) any later version
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 // Add JavaScript header when ...
46 if (! PKWK_ALLOW_JAVASCRIPT) unset($javascript);
48 $_page = isset($vars['page']) ? $vars['page'] : '';
49 $r_page = pagename_urlencode($_page);
51 // Set $_LINK for skin
52 $_LINK['add'] = "$script?cmd=add&page=$r_page";
53 $_LINK['backup'] = "$script?cmd=backup&page=$r_page";
54 $_LINK['copy'] = "$script?plugin=template&refer=$r_page";
55 $_LINK['diff'] = "$script?cmd=diff&page=$r_page";
56 $_LINK['edit'] = "$script?cmd=edit&page=$r_page";
57 $_LINK['filelist'] = "$script?cmd=filelist";
58 $_LINK['freeze'] = "$script?cmd=freeze&page=$r_page";
59 $_LINK['help'] = "$script?" . pagename_urlencode($help_page);
60 $_LINK['list'] = "$script?cmd=list";
61 $_LINK['new'] = "$script?plugin=newpage&refer=$r_page";
62 $_LINK['rdf'] = "$script?cmd=rss&ver=1.0";
63 $_LINK['recent'] = "$script?" . pagename_urlencode($whatsnew);
64 $_LINK['reload'] = "$script?$r_page";
65 $_LINK['rename'] = "$script?plugin=rename&refer=$r_page";
66 $_LINK['rss'] = "$script?cmd=rss";
67 $_LINK['rss10'] = "$script?cmd=rss&ver=1.0"; // Same as 'rdf'
68 $_LINK['rss20'] = "$script?cmd=rss&ver=2.0";
69 $_LINK['search'] = "$script?cmd=search";
70 $_LINK['top'] = "$script?" . pagename_urlencode($defaultpage);
71 $_LINK['unfreeze'] = "$script?cmd=unfreeze&page=$r_page";
72 $_LINK['upload'] = "$script?plugin=attach&pcmd=upload&page=$r_page";
75 $login_link = "$script?plugin=loginform&pcmd=login&page=$r_page";
77 case AUTH_TYPE_EXTERNAL:
78 $login_link = get_auth_external_login_url($_page, $_LINK['reload']);
81 $_LINK['login'] = htmlsc($login_link);
82 $_LINK['logout'] = "$script?plugin=loginform&pcmd=logout&page=$r_page";
84 // Compat: Skins for 1.4.4 and before
85 $link_add = & $_LINK['add'];
86 $link_new = & $_LINK['new']; // New!
87 $link_edit = & $_LINK['edit'];
88 $link_diff = & $_LINK['diff'];
89 $link_top = & $_LINK['top'];
90 $link_list = & $_LINK['list'];
91 $link_filelist = & $_LINK['filelist'];
92 $link_search = & $_LINK['search'];
93 $link_whatsnew = & $_LINK['recent'];
94 $link_backup = & $_LINK['backup'];
95 $link_help = & $_LINK['help'];
96 $link_trackback = ''; // Removed (compat)
97 $link_rdf = & $_LINK['rdf']; // New!
98 $link_rss = & $_LINK['rss'];
99 $link_rss10 = & $_LINK['rss10']; // New!
100 $link_rss20 = & $_LINK['rss20']; // New!
101 $link_freeze = & $_LINK['freeze'];
102 $link_unfreeze = & $_LINK['unfreeze'];
103 $link_upload = & $_LINK['upload'];
104 $link_template = & $_LINK['copy'];
105 $link_refer = ''; // Removed (compat)
106 $link_rename = & $_LINK['rename'];
109 $is_page = (is_pagename($_page) && ! arg_check('backup') && $_page != $whatsnew);
110 $is_read = (arg_check('read') && is_page($_page));
111 $is_freeze = is_freeze($_page);
113 // Last modification date (string) of the page
114 $lastmodified = $is_read ? format_date(get_filetime($_page)) .
115 ' ' . get_pg_passage($_page, FALSE) : '';
117 // List of attached files to the page
118 $attaches = ($attach_link && $is_read && exist_plugin_action('attach')) ?
119 attach_filelist() : '';
121 // List of related pages
122 $related = ($related_link && $is_read) ? make_related($_page) : '';
125 ksort($foot_explain, SORT_NUMERIC);
126 $notes = ! empty($foot_explain) ? $note_hr . join("\n", $foot_explain) : '';
128 // Tags will be inserted into <head></head>
129 $head_tag = ! empty($head_tags) ? join("\n", $head_tags) ."\n" : '';
132 // Last modification date (UNIX timestamp) of the page
133 $fmt = $is_read ? get_filetime($_page) + LOCALZONE : 0;
135 // Output nofollow / noindex regardless os skin file
136 if (!$is_read || $nofollow) {
137 if (!headers_sent()) {
138 header("X-Robots-Tag: noindex,nofollow");
143 if ($search_word_color && isset($vars['word'])) {
144 $body = '<div class="small">' . $_msg_word . htmlsc($vars['word']) .
145 '</div>' . $hr . "\n" . $body;
147 // BugTrack2/106: Only variables can be passed by reference from PHP 5.0.5
148 // with array_splice(), array_flip()
149 $words = preg_split('/\s+/', $vars['word'], -1, PREG_SPLIT_NO_EMPTY);
150 $words = array_splice($words, 0, 10); // Max: 10 words
151 $words = array_flip($words);
154 foreach ($words as $word=>$id) $keys[$word] = strlen($word);
155 arsort($keys, SORT_NUMERIC);
156 $keys = get_search_words(array_keys($keys), TRUE);
158 foreach ($keys as $key=>$pattern) {
159 $s_key = htmlsc($key);
161 '<textarea[^>]*>.*?<\/textarea>' . // Ignore textareas
162 '|' . '<[^>]*>' . // Ignore tags
163 '|' . '&[^;]+;' . // Ignore entities
164 '|' . '(' . $pattern . ')' . // $matches[1]: Regex for a search word
166 $decorate_Nth_word = create_function(
168 'return (isset($matches[1])) ? ' .
169 '\'<strong class="word' .
171 '">\' . $matches[1] . \'</strong>\' : ' .
174 $body = preg_replace_callback($pattern, $decorate_Nth_word, $body);
175 $notes = preg_replace_callback($pattern, $decorate_Nth_word, $notes);
180 // Compat: 'HTML convert time' without time about MenuBar and skin
181 $taketime = elapsedtime();
187 function edit_form($page, $postdata, $digest = FALSE, $b_template = TRUE)
189 global $script, $vars, $rows, $cols, $hr, $function_freeze;
190 global $_btn_preview, $_btn_repreview, $_btn_update, $_btn_cancel, $_msg_help;
191 global $whatsnew, $_btn_template, $_btn_load, $load_template_func;
192 global $notimeupdate;
195 // Newly generate $digest or not
196 if ($digest === FALSE) $digest = md5(join('', get_source($page)));
198 $refer = $template = '';
201 $addtag = $add_top = '';
202 if(isset($vars['add'])) {
204 $addtag = '<input type="hidden" name="add" value="true" />';
205 $add_top = isset($vars['add_top']) ? ' checked="checked"' : '';
206 $add_top = '<input type="checkbox" name="add_top" ' .
207 'id="_edit_form_add_top" value="true"' . $add_top . ' />' . "\n" .
208 ' <label for="_edit_form_add_top">' .
209 '<span class="small">' . $_btn_addtop . '</span>' .
213 if($load_template_func && $b_template) {
214 $tpage_names = array(); // Pages marked as template
215 $template_page = ':config/Templates';
217 foreach(get_source($template_page) as $_templates) {
219 if (! preg_match('#\-\s*\[\[([^\[\]]+)\]\]#', $_templates, $m)) continue;
220 $tpage = preg_replace('#^./#', "$template_page/", $m[1]);
221 if (! is_page($tpage)) continue;
222 $tpage_names[] = $tpage;
224 $page_names = array();
225 foreach(get_existpages() as $_page) {
226 if ($_page == $whatsnew || check_non_list($_page))
228 if (preg_match('/template/i', $_page)) {
229 $tpage_names[] = $_page;
231 if (count($page_names) >= $page_max) continue;
232 $page_names[] = $_page;
235 $tpage_names2 = array_values(array_unique($tpage_names));
236 natcasesort($tpage_names2);
237 natcasesort($page_names);
238 $tpages = array(); // Template pages
239 $npages = array(); // Normal pages
240 foreach($tpage_names2 as $p) {
242 $tpages[] = ' <option value="' . $ps . '">' . $ps . '</option>';
244 foreach($page_names as $p) {
246 $npages[] = ' <option value="' . $ps . '">' . $ps . '</option>';
248 if (count($page_names) === $page_max) {
249 $npages[] = ' <option value="">...</option>';
251 $s_tpages = join("\n", $tpages);
252 $s_npages = join("\n", $npages);
254 <select name="template_page">
255 <option value="">-- $_btn_template --</option>
256 <optgroup label="$_btn_template">
259 <optgroup label="$_title_list">
263 <input type="submit" name="template" value="$_btn_load" accesskey="r" />
267 if (isset($vars['refer']) && $vars['refer'] != '')
268 $refer = '[[' . strip_bracket($vars['refer']) . ']]' . "\n\n";
271 $r_page = rawurlencode($page);
272 $s_page = htmlsc($page);
273 $s_digest = htmlsc($digest);
274 $s_postdata = htmlsc($refer . $postdata);
275 $s_original = isset($vars['original']) ? htmlsc($vars['original']) : $s_postdata;
276 $b_preview = isset($vars['preview']); // TRUE when preview
277 $btn_preview = $b_preview ? $_btn_repreview : $_btn_preview;
279 // Checkbox 'do not change timestamp'
280 $add_notimestamp = '';
281 if ($notimeupdate != 0) {
282 global $_btn_notchangetimestamp;
283 $checked_time = isset($vars['notimestamp']) ? ' checked="checked"' : '';
284 // Only for administrator
285 if ($notimeupdate == 2) {
286 $add_notimestamp = ' ' .
287 '<input type="password" name="pass" size="12" />' . "\n";
289 $add_notimestamp = '<input type="checkbox" name="notimestamp" ' .
290 'id="_edit_form_notimestamp" value="true"' . $checked_time . ' />' . "\n" .
291 ' ' . '<label for="_edit_form_notimestamp"><span class="small">' .
292 $_btn_notchangetimestamp . '</span></label>' . "\n" .
297 // 'margin-bottom', 'float:left', and 'margin-top'
298 // are for layout of 'cancel button'
300 <div class="edit_form">
301 <form action="$script" method="post" style="margin-bottom:0px;">
304 <input type="hidden" name="cmd" value="edit" />
305 <input type="hidden" name="page" value="$s_page" />
306 <input type="hidden" name="digest" value="$s_digest" />
307 <textarea name="msg" rows="$rows" cols="$cols">$s_postdata</textarea>
309 <div style="float:left;">
310 <input type="submit" name="preview" value="$btn_preview" accesskey="p" />
311 <input type="submit" name="write" value="$_btn_update" accesskey="s" />
315 <textarea name="original" rows="1" cols="1" style="display:none">$s_original</textarea>
317 <form action="$script" method="post" style="margin-top:0px;">
318 <input type="hidden" name="cmd" value="edit" />
319 <input type="hidden" name="page" value="$s_page" />
320 <input type="submit" name="cancel" value="$_btn_cancel" accesskey="c" />
325 if (isset($vars['help'])) {
326 $body .= $hr . catrule();
328 $body .= '<ul><li><a href="' .
329 $script . '?cmd=edit&help=true&page=' . $r_page .
330 '">' . $_msg_help . '</a></li></ul>';
337 function make_related($page, $tag = '')
339 global $script, $vars, $rule_related_str, $related_str;
340 global $_ul_left_margin, $_ul_margin, $_list_pad_str;
342 $links = links_get_related($page);
345 ksort($links, SORT_STRING); // Page name, alphabetical order
347 arsort($links, SORT_NUMERIC); // Last modified date, newer
351 foreach ($links as $page=>$lastmod) {
352 if (check_non_list($page)) continue;
354 $r_page = pagename_urlencode($page);
355 $s_page = htmlsc($page);
356 $passage = get_passage($lastmod);
358 '<a href="' . $script . '?' . $r_page . '" title="' .
359 $s_page . ' ' . $passage . '">' . $s_page . '</a>' :
360 '<a href="' . $script . '?' . $r_page . '">' .
361 $s_page . '</a>' . $passage;
363 if (empty($_links)) return ''; // Nothing
365 if ($tag == 'p') { // From the line-head
366 $margin = $_ul_left_margin + $_ul_margin;
367 $style = sprintf($_list_pad_str, 1, $margin, $margin);
368 $retval = "\n" . '<ul' . $style . '>' . "\n" .
369 '<li>' . join($rule_related_str, $_links) . '</li>' . "\n" .
372 $retval = join($rule_related_str, $_links);
374 $retval = join($related_str, $_links);
380 // User-defined rules (convert without replacing source)
381 function make_line_rules($str)
384 static $pattern, $replace;
386 if (! isset($pattern)) {
387 $pattern = array_map(create_function('$a',
388 'return \'/\' . $a . \'/\';'), array_keys($line_rules));
389 $replace = array_values($line_rules);
393 return preg_replace($pattern, $replace, $str);
396 // Remove all HTML tags(or just anchor tags), and WikiName-speific decorations
397 function strip_htmltag($str, $all = TRUE)
399 global $_symbol_noexists;
400 static $noexists_pattern;
402 if (! isset($noexists_pattern))
403 $noexists_pattern = '#<span class="noexists">([^<]*)<a[^>]+>' .
404 preg_quote($_symbol_noexists, '#') . '</a></span>#';
406 // Strip Dagnling-Link decoration (Tags and "$_symbol_noexists")
407 $str = preg_replace($noexists_pattern, '$1', $str);
410 // All other HTML tags
411 return preg_replace('#<[^>]+>#', '', $str);
413 // All other anchor-tags only
414 return preg_replace('#<a[^>]+>|</a>#i', '', $str);
418 // Remove AutoLink marker with AutLink itself
419 function strip_autolink($str)
421 return preg_replace('#<!--autolink--><a [^>]+>|</a><!--/autolink-->#', '', $str);
424 // Make a backlink. searching-link of the page name, by the page name, for the page name
425 function make_search($page)
429 $s_page = htmlsc($page);
430 $r_page = rawurlencode($page);
432 return '<a href="' . $script . '?plugin=related&page=' . $r_page .
433 '">' . $s_page . '</a> ';
436 // Make heading string (remove heading-related decorations from Wiki text)
437 function make_heading(& $str, $strip = TRUE)
441 // Cut fixed-heading anchors
444 if (preg_match('/^(\*{0,3})(.*?)\[#([A-Za-z][\w-]+)\](.*?)$/m', $str, $matches)) {
445 $str = $matches[2] . $matches[4];
448 $str = preg_replace('/^\*{0,3}/', '', $str);
451 // Cut footnotes and tags
453 $str = strip_htmltag(make_link(preg_replace($NotePattern, '', $str)));
458 // Separate a page-name(or URL or null string) and an anchor
459 // (last one standing) without sharp
460 function anchor_explode($page, $strict_editable = FALSE)
462 $pos = strrpos($page, '#');
463 if ($pos === FALSE) return array($page, '', FALSE);
465 // Ignore the last sharp letter
466 if ($pos + 1 == strlen($page)) {
467 $pos = strpos(substr($page, $pos + 1), '#');
468 if ($pos === FALSE) return array($page, '', FALSE);
471 $s_page = substr($page, 0, $pos);
472 $anchor = substr($page, $pos + 1);
474 if($strict_editable === TRUE && preg_match('/^[a-z][a-f0-9]{7}$/', $anchor)) {
475 return array ($s_page, $anchor, TRUE); // Seems fixed-anchor
477 return array ($s_page, $anchor, FALSE);
481 // Check HTTP header()s were sent already, or
482 // there're blank lines or something out of php blocks
483 function pkwk_headers_sent()
485 if (PKWK_OPTIMISE) return;
488 if (version_compare(PHP_VERSION, '4.3.0', '>=')) {
489 if (headers_sent($file, $line))
490 die('Headers already sent at ' .
492 ' line ' . $line . '.');
495 die('Headers already sent.');
499 // Output common HTTP headers
500 function pkwk_common_headers()
502 if (! PKWK_OPTIMISE) pkwk_headers_sent();
504 if(defined('PKWK_ZLIB_LOADABLE_MODULE')) {
506 if(ini_get('zlib.output_compression') &&
507 preg_match('/\b(gzip|deflate)\b/i', $_SERVER['HTTP_ACCEPT_ENCODING'], $matches)) {
508 // Bug #29350 output_compression compresses everything _without header_ as loadable module
509 // http://bugs.php.net/bug.php?id=29350
510 header('Content-Encoding: ' . $matches[1]);
511 header('Vary: Accept-Encoding');
517 define('PKWK_DTD_XHTML_1_1', 17); // Strict only
518 define('PKWK_DTD_XHTML_1_0', 16); // Strict
519 define('PKWK_DTD_XHTML_1_0_STRICT', 16);
520 define('PKWK_DTD_XHTML_1_0_TRANSITIONAL', 15);
521 define('PKWK_DTD_XHTML_1_0_FRAMESET', 14);
522 define('PKWK_DTD_HTML_4_01', 3); // Strict
523 define('PKWK_DTD_HTML_4_01_STRICT', 3);
524 define('PKWK_DTD_HTML_4_01_TRANSITIONAL', 2);
525 define('PKWK_DTD_HTML_4_01_FRAMESET', 1);
527 define('PKWK_DTD_TYPE_XHTML', 1);
528 define('PKWK_DTD_TYPE_HTML', 0);
530 // Output HTML DTD, <html> start tag. Return content-type.
531 function pkwk_output_dtd($pkwk_dtd = PKWK_DTD_XHTML_1_1, $charset = CONTENT_CHARSET)
534 if (isset($called)) die('pkwk_output_dtd() already called. Why?');
537 $type = PKWK_DTD_TYPE_XHTML;
540 case PKWK_DTD_XHTML_1_1 :
542 $dtd = 'http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd';
544 case PKWK_DTD_XHTML_1_0_STRICT :
547 $dtd = 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd';
549 case PKWK_DTD_XHTML_1_0_TRANSITIONAL:
551 $option = 'Transitional';
552 $dtd = 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd';
555 case PKWK_DTD_HTML_4_01_STRICT :
556 $type = PKWK_DTD_TYPE_HTML;
558 $dtd = 'http://www.w3.org/TR/html4/strict.dtd';
560 case PKWK_DTD_HTML_4_01_TRANSITIONAL:
561 $type = PKWK_DTD_TYPE_HTML;
563 $option = 'Transitional';
564 $dtd = 'http://www.w3.org/TR/html4/loose.dtd';
567 default: die('DTD not specified or invalid DTD');
571 $charset = htmlsc($charset);
574 if ($type == PKWK_DTD_TYPE_XHTML) echo '<?xml version="1.0" encoding="' . $charset . '" ?>' . "\n";
577 echo '<!DOCTYPE html PUBLIC "-//W3C//DTD ' .
578 ($type == PKWK_DTD_TYPE_XHTML ? 'XHTML' : 'HTML') . ' ' .
580 ($option != '' ? ' ' . $option : '') .
585 // Output <html> start tag
587 if ($type == PKWK_DTD_TYPE_XHTML) {
588 echo ' xmlns="http://www.w3.org/1999/xhtml"'; // dir="ltr" /* LeftToRight */
589 echo ' xml:lang="' . LANG . '"';
590 if ($version == '1.0') echo ' lang="' . LANG . '"'; // Only XHTML 1.0
592 echo ' lang="' . LANG . '"'; // HTML
594 echo '>' . "\n"; // <html>
596 // Return content-type (with MIME type)
597 if ($type == PKWK_DTD_TYPE_XHTML) {
598 // NOTE: XHTML 1.1 browser will ignore http-equiv
599 return '<meta http-equiv="content-type" content="application/xhtml+xml; charset=' . $charset . '" />' . "\n";
601 return '<meta http-equiv="content-type" content="text/html; charset=' . $charset . '" />' . "\n";