2 // PukiWiki - Yet another WikiWikiWeb clone.
3 // $Id: html.php,v 1.50 2006/04/09 04:32:05 henoheno Exp $
5 // 2002-2006 PukiWiki Developers 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 $trackback, $trackback_javascript, $referer, $javascript;
19 global $_LANG, $_LINK, $_IMAGE;
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 if (! file_exists(SKIN_FILE) || ! is_readable(SKIN_FILE))
28 die_message('SKIN_FILE is not found');
30 $_LINK = $_IMAGE = array();
32 // Add JavaScript header when ...
33 if ($trackback && $trackback_javascript) $javascript = 1; // Set something If you want
34 if (! PKWK_ALLOW_JAVASCRIPT) unset($javascript);
36 $_page = isset($vars['page']) ? $vars['page'] : '';
37 $r_page = rawurlencode($_page);
39 // Set $_LINK for skin
40 $_LINK['add'] = "$script?cmd=add&page=$r_page";
41 $_LINK['backup'] = "$script?cmd=backup&page=$r_page";
42 $_LINK['copy'] = "$script?plugin=template&refer=$r_page";
43 $_LINK['diff'] = "$script?cmd=diff&page=$r_page";
44 $_LINK['edit'] = "$script?cmd=edit&page=$r_page";
45 $_LINK['filelist'] = "$script?cmd=filelist";
46 $_LINK['freeze'] = "$script?cmd=freeze&page=$r_page";
47 $_LINK['help'] = "$script?" . rawurlencode($help_page);
48 $_LINK['list'] = "$script?cmd=list";
49 $_LINK['new'] = "$script?plugin=newpage&refer=$r_page";
50 $_LINK['rdf'] = "$script?cmd=rss&ver=1.0";
51 $_LINK['recent'] = "$script?" . rawurlencode($whatsnew);
52 $_LINK['refer'] = "$script?plugin=referer&page=$r_page";
53 $_LINK['reload'] = "$script?$r_page";
54 $_LINK['rename'] = "$script?plugin=rename&refer=$r_page";
55 $_LINK['rss'] = "$script?cmd=rss";
56 $_LINK['rss10'] = "$script?cmd=rss&ver=1.0"; // Same as 'rdf'
57 $_LINK['rss20'] = "$script?cmd=rss&ver=2.0";
58 $_LINK['search'] = "$script?cmd=search";
59 $_LINK['top'] = "$script?" . rawurlencode($defaultpage);
61 $tb_id = tb_get_id($_page);
62 $_LINK['trackback'] = "$script?plugin=tb&__mode=view&tb_id=$tb_id";
64 $_LINK['unfreeze'] = "$script?cmd=unfreeze&page=$r_page";
65 $_LINK['upload'] = "$script?plugin=attach&pcmd=upload&page=$r_page";
67 // Compat: Skins for 1.4.4 and before
68 $link_add = & $_LINK['add'];
69 $link_new = & $_LINK['new']; // New!
70 $link_edit = & $_LINK['edit'];
71 $link_diff = & $_LINK['diff'];
72 $link_top = & $_LINK['top'];
73 $link_list = & $_LINK['list'];
74 $link_filelist = & $_LINK['filelist'];
75 $link_search = & $_LINK['search'];
76 $link_whatsnew = & $_LINK['recent'];
77 $link_backup = & $_LINK['backup'];
78 $link_help = & $_LINK['help'];
79 $link_trackback = & $_LINK['trackback']; // New!
80 $link_rdf = & $_LINK['rdf']; // New!
81 $link_rss = & $_LINK['rss'];
82 $link_rss10 = & $_LINK['rss10']; // New!
83 $link_rss20 = & $_LINK['rss20']; // New!
84 $link_freeze = & $_LINK['freeze'];
85 $link_unfreeze = & $_LINK['unfreeze'];
86 $link_upload = & $_LINK['upload'];
87 $link_template = & $_LINK['copy'];
88 $link_refer = & $_LINK['refer']; // New!
89 $link_rename = & $_LINK['rename'];
92 $is_page = (is_pagename($_page) && ! arg_check('backup') && $_page != $whatsnew);
93 $is_read = (arg_check('read') && is_page($_page));
94 $is_freeze = is_freeze($_page);
96 // Last modification date (string) of the page
97 $lastmodified = $is_read ? format_date(get_filetime($_page)) .
98 ' ' . get_pg_passage($_page, FALSE) : '';
100 // List of attached files to the page
101 $attaches = ($attach_link && $is_read && exist_plugin_action('attach')) ?
102 attach_filelist() : '';
104 // List of related pages
105 $related = ($related_link && $is_read) ? make_related($_page) : '';
108 ksort($foot_explain, SORT_NUMERIC);
109 $notes = ! empty($foot_explain) ? $note_hr . join("\n", $foot_explain) : '';
111 // Tags will be inserted into <head></head>
112 $head_tag = ! empty($head_tags) ? join("\n", $head_tags) ."\n" : '';
115 // Last modification date (UNIX timestamp) of the page
116 $fmt = $is_read ? get_filetime($_page) + LOCALZONE : 0;
119 if ($search_word_color && isset($vars['word'])) {
120 $body = '<div class="small">' . $_msg_word . htmlspecialchars($vars['word']) .
121 '</div>' . $hr . "\n" . $body;
123 // BugTrack2/106: Only variables can be passed by reference from PHP 5.0.5
124 // with array_splice(), array_flip()
125 $words = preg_split('/\s+/', $vars['word'], -1, PREG_SPLIT_NO_EMPTY);
126 $words = array_splice($words, 0, 10); // Max: 10 words
127 $words = array_flip($words);
130 foreach ($words as $word=>$id) $keys[$word] = strlen($word);
131 arsort($keys, SORT_NUMERIC);
132 $keys = get_search_words(array_keys($keys), TRUE);
134 foreach ($keys as $key=>$pattern) {
135 $s_key = htmlspecialchars($key);
137 '<textarea[^>]*>.*?<\/textarea>' . // Ignore textareas
138 '|' . '<[^>]*>' . // Ignore tags
139 '|' . '&[^;]+;' . // Ignore entities
140 '|' . '(' . $pattern . ')' . // $matches[1]: Regex for a search word
142 $decorate_Nth_word = create_function(
144 'return (isset($matches[1])) ? ' .
145 '\'<strong class="word' .
147 '">\' . $matches[1] . \'</strong>\' : ' .
150 $body = preg_replace_callback($pattern, $decorate_Nth_word, $body);
151 $notes = preg_replace_callback($pattern, $decorate_Nth_word, $notes);
156 $longtaketime = getmicrotime() - MUTIME;
157 $taketime = sprintf('%01.03f', $longtaketime);
163 function edit_form($page, $postdata, $digest = FALSE, $b_template = TRUE)
165 global $script, $vars, $rows, $cols, $hr, $function_freeze;
166 global $_btn_preview, $_btn_repreview, $_btn_update, $_btn_cancel,
167 $_msg_help, $_btn_notchangetimestamp;
168 global $whatsnew, $_btn_template, $_btn_load, $load_template_func;
169 global $notimeupdate;
171 // Newly generate $digest or not
172 if ($digest === FALSE) $digest = md5(join('', get_source($page)));
174 $refer = $template = '';
175 $checked_time = isset($vars['notimestamp']) ? ' checked="checked"' : '';
178 $addtag = $add_top = '';
179 if(isset($vars['add'])) {
181 $addtag = '<input type="hidden" name="add" value="true" />';
182 $add_top = isset($vars['add_top']) ? ' checked="checked"' : '';
183 $add_top = '<input type="checkbox" name="add_top" ' .
184 'id="_edit_form_add_top" value="true"' . $add_top . ' />' . "\n" .
185 ' <label for="_edit_form_add_top">' .
186 '<span class="small">' . $_btn_addtop . '</span>' .
190 if($load_template_func && $b_template) {
192 foreach(get_existpages() as $_page) {
193 if ($_page == $whatsnew || check_non_list($_page))
195 $s_page = htmlspecialchars($_page);
196 $pages[$_page] = ' <option value="' . $s_page . '">' .
197 $s_page . '</option>';
200 $s_pages = join("\n", $pages);
202 <select name="template_page">
203 <option value="">-- $_btn_template --</option>
206 <input type="submit" name="template" value="$_btn_load" accesskey="r" />
210 if (isset($vars['refer']) && $vars['refer'] != '')
211 $refer = '[[' . strip_bracket($vars['refer']) . ']]' . "\n\n";
214 $r_page = rawurlencode($page);
215 $s_page = htmlspecialchars($page);
216 $s_digest = htmlspecialchars($digest);
217 $s_postdata = htmlspecialchars($refer . $postdata);
218 $s_original = isset($vars['original']) ? htmlspecialchars($vars['original']) : $s_postdata;
219 $b_preview = isset($vars['preview']); // TRUE when preview
220 $btn_preview = $b_preview ? $_btn_repreview : $_btn_preview;
222 $add_notimestamp = '';
223 if ( $notimeupdate != 0 ) {
224 // enable 'do not change timestamp'
225 $add_notimestamp = <<<EOD
226 <input type="checkbox" name="notimestamp" id="_edit_form_notimestamp" value="true"$checked_time />
227 <label for="_edit_form_notimestamp"><span class="small">$_btn_notchangetimestamp</span></label>
229 if ( $notimeupdate == 2 ) {
230 // enable only administrator
231 $add_notimestamp .= <<<EOD
232 <input type="password" name="pass" size="12" />
235 $add_notimestamp .= ' ';
239 <form action="$script" method="post">
240 <div class="edit_form">
243 <input type="hidden" name="cmd" value="edit" />
244 <input type="hidden" name="page" value="$s_page" />
245 <input type="hidden" name="digest" value="$s_digest" />
246 <textarea name="msg" rows="$rows" cols="$cols">$s_postdata</textarea>
248 <input type="submit" name="preview" value="$btn_preview" accesskey="p" />
249 <input type="submit" name="write" value="$_btn_update" accesskey="s" />
252 <input type="submit" name="cancel" value="$_btn_cancel" accesskey="c" />
253 <textarea name="original" rows="1" cols="1" style="display:none">$s_original</textarea>
258 if (isset($vars['help'])) {
259 $body .= $hr . catrule();
261 $body .= '<ul><li><a href="' .
262 $script . '?cmd=edit&help=true&page=' . $r_page .
263 '">' . $_msg_help . '</a></li></ul>';
270 function make_related($page, $tag = '')
272 global $script, $vars, $rule_related_str, $related_str;
273 global $_ul_left_margin, $_ul_margin, $_list_pad_str;
275 $links = links_get_related($page);
284 foreach ($links as $page=>$lastmod) {
285 if (check_non_list($page)) continue;
287 $r_page = rawurlencode($page);
288 $s_page = htmlspecialchars($page);
289 $passage = get_passage($lastmod);
291 '<a href="' . $script . '?' . $r_page . '" title="' .
292 $s_page . ' ' . $passage . '">' . $s_page . '</a>' :
293 '<a href="' . $script . '?' . $r_page . '">' .
294 $s_page . '</a>' . $passage;
296 if (empty($_links)) return ''; // Nothing
298 if ($tag == 'p') { // From the line-head
299 $margin = $_ul_left_margin + $_ul_margin;
300 $style = sprintf($_list_pad_str, 1, $margin, $margin);
301 $retval = "\n" . '<ul' . $style . '>' . "\n" .
302 '<li>' . join($rule_related_str, $_links) . '</li>' . "\n" .
305 $retval = join($rule_related_str, $_links);
307 $retval = join($related_str, $_links);
313 // User-defined rules (convert without replacing source)
314 function make_line_rules($str)
317 static $pattern, $replace;
319 if (! isset($pattern)) {
320 $pattern = array_map(create_function('$a',
321 'return \'/\' . $a . \'/\';'), array_keys($line_rules));
322 $replace = array_values($line_rules);
326 return preg_replace($pattern, $replace, $str);
329 // Remove all HTML tags(or just anchor tags), and WikiName-speific decorations
330 function strip_htmltag($str, $all = TRUE)
332 global $_symbol_noexists;
333 static $noexists_pattern;
335 if (! isset($noexists_pattern))
336 $noexists_pattern = '#<span class="noexists">([^<]*)<a[^>]+>' .
337 preg_quote($_symbol_noexists, '#') . '</a></span>#';
339 // Strip Dagnling-Link decoration (Tags and "$_symbol_noexists")
340 $str = preg_replace($noexists_pattern, '$1', $str);
343 // All other HTML tags
344 return preg_replace('#<[^>]+>#', '', $str);
346 // All other anchor-tags only
347 return preg_replace('#<a[^>]+>|</a>#i', '', $str);
351 // Remove AutoLink marker with AutLink itself
352 function strip_autolink($str)
354 return preg_replace('#<!--autolink--><a [^>]+>|</a><!--/autolink-->#', '', $str);
357 // Make a backlink. searching-link of the page name, by the page name, for the page name
358 function make_search($page)
362 $s_page = htmlspecialchars($page);
363 $r_page = rawurlencode($page);
365 return '<a href="' . $script . '?plugin=related&page=' . $r_page .
366 '">' . $s_page . '</a> ';
369 // Make heading string (remove heading-related decorations from Wiki text)
370 function make_heading(& $str, $strip = TRUE)
374 // Cut fixed-heading anchors
377 if (preg_match('/^(\*{0,3})(.*?)\[#([A-Za-z][\w-]+)\](.*?)$/m', $str, $matches)) {
378 $str = $matches[2] . $matches[4];
381 $str = preg_replace('/^\*{0,3}/', '', $str);
384 // Cut footnotes and tags
386 $str = strip_htmltag(make_link(preg_replace($NotePattern, '', $str)));
391 // Separate a page-name(or URL or null string) and an anchor
392 // (last one standing) without sharp
393 function anchor_explode($page, $strict_editable = FALSE)
395 $pos = strrpos($page, '#');
396 if ($pos === FALSE) return array($page, '', FALSE);
398 // Ignore the last sharp letter
399 if ($pos + 1 == strlen($page)) {
400 $pos = strpos(substr($page, $pos + 1), '#');
401 if ($pos === FALSE) return array($page, '', FALSE);
404 $s_page = substr($page, 0, $pos);
405 $anchor = substr($page, $pos + 1);
407 if($strict_editable === TRUE && preg_match('/^[a-z][a-f0-9]{7}$/', $anchor)) {
408 return array ($s_page, $anchor, TRUE); // Seems fixed-anchor
410 return array ($s_page, $anchor, FALSE);
414 // Check HTTP header()s were sent already, or
415 // there're blank lines or something out of php blocks
416 function pkwk_headers_sent()
418 if (PKWK_OPTIMISE) return;
421 if (version_compare(PHP_VERSION, '4.3.0', '>=')) {
422 if (headers_sent($file, $line))
423 die('Headers already sent at ' .
424 htmlspecialchars($file) .
425 ' line ' . $line . '.');
428 die('Headers already sent.');
432 // Output common HTTP headers
433 function pkwk_common_headers()
435 if (! PKWK_OPTIMISE) pkwk_headers_sent();
437 if(defined('PKWK_ZLIB_LOADABLE_MODULE')) {
439 if(ini_get('zlib.output_compression') &&
440 preg_match('/\b(gzip|deflate)\b/i', $_SERVER['HTTP_ACCEPT_ENCODING'], $matches)) {
441 // Bug #29350 output_compression compresses everything _without header_ as loadable module
442 // http://bugs.php.net/bug.php?id=29350
443 header('Content-Encoding: ' . $matches[1]);
444 header('Vary: Accept-Encoding');
450 define('PKWK_DTD_XHTML_1_1', 17); // Strict only
451 define('PKWK_DTD_XHTML_1_0', 16); // Strict
452 define('PKWK_DTD_XHTML_1_0_STRICT', 16);
453 define('PKWK_DTD_XHTML_1_0_TRANSITIONAL', 15);
454 define('PKWK_DTD_XHTML_1_0_FRAMESET', 14);
455 define('PKWK_DTD_HTML_4_01', 3); // Strict
456 define('PKWK_DTD_HTML_4_01_STRICT', 3);
457 define('PKWK_DTD_HTML_4_01_TRANSITIONAL', 2);
458 define('PKWK_DTD_HTML_4_01_FRAMESET', 1);
460 define('PKWK_DTD_TYPE_XHTML', 1);
461 define('PKWK_DTD_TYPE_HTML', 0);
463 // Output HTML DTD, <html> start tag. Return content-type.
464 function pkwk_output_dtd($pkwk_dtd = PKWK_DTD_XHTML_1_1, $charset = CONTENT_CHARSET)
467 if (isset($called)) die('pkwk_output_dtd() already called. Why?');
470 $type = PKWK_DTD_TYPE_XHTML;
473 case PKWK_DTD_XHTML_1_1 :
475 $dtd = 'http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd';
477 case PKWK_DTD_XHTML_1_0_STRICT :
480 $dtd = 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd';
482 case PKWK_DTD_XHTML_1_0_TRANSITIONAL:
484 $option = 'Transitional';
485 $dtd = 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd';
488 case PKWK_DTD_HTML_4_01_STRICT :
489 $type = PKWK_DTD_TYPE_HTML;
491 $dtd = 'http://www.w3.org/TR/html4/strict.dtd';
493 case PKWK_DTD_HTML_4_01_TRANSITIONAL:
494 $type = PKWK_DTD_TYPE_HTML;
496 $option = 'Transitional';
497 $dtd = 'http://www.w3.org/TR/html4/loose.dtd';
500 default: die('DTD not specified or invalid DTD');
504 $charset = htmlspecialchars($charset);
507 if ($type == PKWK_DTD_TYPE_XHTML) echo '<?xml version="1.0" encoding="' . $charset . '" ?>' . "\n";
510 echo '<!DOCTYPE html PUBLIC "-//W3C//DTD ' .
511 ($type == PKWK_DTD_TYPE_XHTML ? 'XHTML' : 'HTML') . ' ' .
513 ($option != '' ? ' ' . $option : '') .
518 // Output <html> start tag
520 if ($type == PKWK_DTD_TYPE_XHTML) {
521 echo ' xmlns="http://www.w3.org/1999/xhtml"'; // dir="ltr" /* LeftToRight */
522 echo ' xml:lang="' . LANG . '"';
523 if ($version == '1.0') echo ' lang="' . LANG . '"'; // Only XHTML 1.0
525 echo ' lang="' . LANG . '"'; // HTML
527 echo '>' . "\n"; // <html>
529 // Return content-type (with MIME type)
530 if ($type == PKWK_DTD_TYPE_XHTML) {
531 // NOTE: XHTML 1.1 browser will ignore http-equiv
532 return '<meta http-equiv="content-type" content="application/xhtml+xml; charset=' . $charset . '" />' . "\n";
534 return '<meta http-equiv="content-type" content="text/html; charset=' . $charset . '" />' . "\n";