OSDN Git Service

BugTrack2/62: Do remove the whole design, 'Showing TrackBack-ping list by html'.
[pukiwiki/pukiwiki.git] / lib / html.php
1 <?php
2 // PukiWiki - Yet another WikiWikiWeb clone.
3 // $Id: html.php,v 1.38 2005/06/12 03:03:05 henoheno Exp $
4 // Copyright (C)
5 //   2002-2005 PukiWiki Developers 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 $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;
18         global $nofollow;
19         global $_LANG, $_LINK, $_IMAGE;
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         if (! file_exists(SKIN_FILE) || ! is_readable(SKIN_FILE))
28                 die_message('SKIN_FILE is not found');
29
30         $_LINK = $_IMAGE = array();
31
32         // Add JavaScript header when ...
33         if ($trackback && $trackback_javascript) $javascript = 1; // Set something If you want
34         if (! PKWK_ALLOW_JAVASCRIPT) unset($javascript);
35
36         $_page  = isset($vars['page']) ? $vars['page'] : '';
37         $r_page = rawurlencode($_page);
38
39         // Set $_LINK for skin
40         $_LINK['add']      = "$script?cmd=add&amp;page=$r_page";
41         $_LINK['backup']   = "$script?cmd=backup&amp;page=$r_page";
42         $_LINK['copy']     = "$script?plugin=template&amp;refer=$r_page";
43         $_LINK['diff']     = "$script?cmd=diff&amp;page=$r_page";
44         $_LINK['edit']     = "$script?cmd=edit&amp;page=$r_page";
45         $_LINK['filelist'] = "$script?cmd=filelist";
46         $_LINK['freeze']   = "$script?cmd=freeze&amp;page=$r_page";
47         $_LINK['help']     = "$script?" . rawurlencode($help_page);
48         $_LINK['list']     = "$script?cmd=list";
49         $_LINK['new']      = "$script?plugin=newpage&amp;refer=$r_page";
50         $_LINK['rdf']      = "$script?cmd=rss&amp;ver=1.0";
51         $_LINK['recent']   = "$script?" . rawurlencode($whatsnew);
52         $_LINK['refer']    = "$script?plugin=referer&amp;page=$r_page";
53         $_LINK['reload']   = "$script?$r_page";
54         $_LINK['rename']   = "$script?plugin=rename&amp;refer=$r_page";
55         $_LINK['rss']      = "$script?cmd=rss";
56         $_LINK['rss10']    = "$script?cmd=rss&amp;ver=1.0"; // Same as 'rdf'
57         $_LINK['rss20']    = "$script?cmd=rss&amp;ver=2.0";
58         $_LINK['search']   = "$script?cmd=search";
59         $_LINK['top']      = "$script?" . rawurlencode($defaultpage);
60         if ($trackback) {
61                 $tb_id = tb_get_id($_page);
62                 $_LINK['trackback'] = "$script?plugin=tb&amp;__mode=view&amp;tb_id=$tb_id";
63         }
64         $_LINK['unfreeze'] = "$script?cmd=unfreeze&amp;page=$r_page";
65         $_LINK['upload']   = "$script?plugin=attach&amp;pcmd=upload&amp;page=$r_page";
66
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'];
90
91         // Init flags
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);
95
96         // Last modification date (string) of the page
97         $lastmodified = $is_read ?  get_date('D, d M Y H:i:s T', get_filetime($_page)) .
98                 ' ' . get_pg_passage($_page, FALSE) : '';
99
100         // List of attached files to the page
101         $attaches = ($attach_link && $is_read && exist_plugin_action('attach')) ?
102                 attach_filelist() : '';
103
104         // List of related pages
105         $related  = ($related_link && $is_read) ? make_related($_page) : '';
106
107         // List of footnotes
108         ksort($foot_explain, SORT_NUMERIC);
109         $notes = ! empty($foot_explain) ? $note_hr . join("\n", $foot_explain) : '';
110
111         // Tags will be inserted into <head></head>
112         $head_tag = ! empty($head_tags) ? join("\n", $head_tags) ."\n" : '';
113
114         // 1.3.x compat
115         // Last modification date (UNIX timestamp) of the page
116         $fmt = $is_read ? get_filetime($_page) + LOCALZONE : 0;
117
118         // Search words
119         if ($search_word_color && isset($vars['word'])) {
120                 $body = '<div class="small">' . $_msg_word . htmlspecialchars($vars['word']) .
121                         '</div>' . $hr . "\n" . $body;
122                 $words = array_flip(array_splice(
123                         preg_split('/\s+/', $vars['word'], -1, PREG_SPLIT_NO_EMPTY),
124                         0, 10));
125                 $keys = array();
126                 foreach ($words as $word=>$id) $keys[$word] = strlen($word);
127                 arsort($keys, SORT_NUMERIC);
128                 $keys = get_search_words(array_keys($keys), TRUE);
129                 $id = 0;
130                 foreach ($keys as $key=>$pattern) {
131                         $s_key    = htmlspecialchars($key);
132                         $pattern  = '/<[^>]*>|(' . $pattern . ')|&[^;]+;/';
133                         $callback = create_function(
134                                 '$arr',
135                                 'return (count($arr) > 1) ? \'<strong class="word' .
136                                         $id++ . '">\' . $arr[1] . \'</strong>\' : $arr[0];'
137                         );
138                         $body  = preg_replace_callback($pattern, $callback, $body);
139                         $notes = preg_replace_callback($pattern, $callback, $notes);
140                 }
141         }
142
143         $longtaketime = getmicrotime() - MUTIME;
144         $taketime     = sprintf('%01.03f', $longtaketime);
145
146         require(SKIN_FILE);
147 }
148
149 // Show 'edit' form
150 function edit_form($page, $postdata, $digest = FALSE, $b_template = TRUE)
151 {
152         global $script, $vars, $rows, $cols, $hr, $function_freeze;
153         global $_btn_addtop, $_btn_preview, $_btn_repreview, $_btn_update, $_btn_cancel,
154                 $_msg_help, $_btn_notchangetimestamp;
155         global $whatsnew, $_btn_template, $_btn_load, $non_list, $load_template_func;
156         global $notimeupdate;
157
158         // Newly generate $digest or not
159         if ($digest === FALSE) $digest = md5(join('', get_source($page)));
160
161         $refer = $template = $addtag = $add_top = '';
162
163         $checked_top  = isset($vars['add_top'])     ? ' checked="checked"' : '';
164         $checked_time = isset($vars['notimestamp']) ? ' checked="checked"' : '';
165
166         if(isset($vars['add'])) {
167                 $addtag  = '<input type="hidden" name="add" value="true" />';
168                 $add_top = '<input type="checkbox" name="add_top" value="true"' .
169                         $checked_top . ' /><span class="small">' .
170                         $_btn_addtop . '</span>';
171         }
172
173         if($load_template_func && $b_template) {
174                 $pages  = array();
175                 $non_list_pattern = '/' . $non_list . '/';
176                 foreach(get_existpages() as $_page) {
177                         if ($_page == $whatsnew || preg_match($non_list_pattern, $_page))
178                                 continue;
179                         $s_page = htmlspecialchars($_page);
180                         $pages[$_page] = '   <option value="' . $s_page . '">' .
181                                 $s_page . '</option>';
182                 }
183                 ksort($pages);
184                 $s_pages  = join("\n", $pages);
185                 $template = <<<EOD
186   <select name="template_page">
187    <option value="">-- $_btn_template --</option>
188 $s_pages
189   </select>
190   <input type="submit" name="template" value="$_btn_load" accesskey="r" />
191   <br />
192 EOD;
193
194                 if (isset($vars['refer']) && $vars['refer'] != '')
195                         $refer = '[[' . strip_bracket($vars['refer']) . ']]' . "\n\n";
196         }
197
198         $r_page      = rawurlencode($page);
199         $s_page      = htmlspecialchars($page);
200         $s_digest    = htmlspecialchars($digest);
201         $s_postdata  = htmlspecialchars($refer . $postdata);
202         $s_original  = isset($vars['original']) ? htmlspecialchars($vars['original']) : $s_postdata;
203         $b_preview   = isset($vars['preview']); // TRUE when preview
204         $btn_preview = $b_preview ? $_btn_repreview : $_btn_preview;
205
206         $add_notimestamp = '';
207         if ( $notimeupdate != 0 ) {
208                 // enable 'do not change timestamp'
209                 $add_notimestamp = <<<EOD
210   <input type="checkbox" name="notimestamp" id="_edit_form_notimestamp" value="true"$checked_time />
211   <label for="_edit_form_notimestamp"><span class="small">$_btn_notchangetimestamp</span></label>
212 EOD;
213                 if ( $notimeupdate == 2 ) {
214                         // enable only administrator
215                         $add_notimestamp .= <<<EOD
216   <input type="password" name="pass" size="12" />
217 EOD;
218                 }
219                 $add_notimestamp .= '&nbsp;';
220         }
221
222         $body = <<<EOD
223 <form action="$script" method="post">
224  <div class="edit_form">
225 $template
226   $addtag
227   <input type="hidden" name="cmd"    value="edit" />
228   <input type="hidden" name="page"   value="$s_page" />
229   <input type="hidden" name="digest" value="$s_digest" />
230   <textarea name="msg" rows="$rows" cols="$cols">$s_postdata</textarea>
231   <br />
232   <input type="submit" name="preview" value="$btn_preview" accesskey="p" />
233   <input type="submit" name="write"   value="$_btn_update" accesskey="s" />
234   $add_top
235   $add_notimestamp
236   <input type="submit" name="cancel"  value="$_btn_cancel" accesskey="c" />
237   <textarea name="original" rows="1" cols="1" style="display:none">$s_original</textarea>
238  </div>
239 </form>
240 EOD;
241
242         if (isset($vars['help'])) {
243                 $body .= $hr . catrule();
244         } else {
245                 $body .= '<ul><li><a href="' .
246                         $script . '?cmd=edit&amp;help=true&amp;page=' . $r_page .
247                         '">' . $_msg_help . '</a></li></ul>';
248         }
249
250         return $body;
251 }
252
253 // Related pages
254 function make_related($page, $tag = '')
255 {
256         global $script, $vars, $rule_related_str, $related_str, $non_list;
257         global $_ul_left_margin, $_ul_margin, $_list_pad_str;
258
259         $links = links_get_related($page);
260
261         if ($tag) {
262                 ksort($links);
263         } else {
264                 arsort($links);
265         }
266
267         $_links = array();
268         $non_list_pattern = '/' . $non_list . '/';
269         foreach ($links as $page=>$lastmod) {
270                 if (preg_match($non_list_pattern, $page)) continue;
271
272                 $r_page   = rawurlencode($page);
273                 $s_page   = htmlspecialchars($page);
274                 $passage  = get_passage($lastmod);
275                 $_links[] = $tag ?
276                         '<a href="' . $script . '?' . $r_page . '" title="' .
277                         $s_page . ' ' . $passage . '">' . $s_page . '</a>' :
278                         '<a href="' . $script . '?' . $r_page . '">' .
279                         $s_page . '</a>' . $passage;
280         }
281         if (empty($_links)) return ''; // Nothing
282
283         if ($tag == 'p') { // From the line-head
284                 $margin = $_ul_left_margin + $_ul_margin;
285                 $style  = sprintf($_list_pad_str, 1, $margin, $margin);
286                 $retval =  "\n" . '<ul' . $style . '>' . "\n" .
287                         '<li>' . join($rule_related_str, $_links) . '</li>' . "\n" .
288                         '</ul>' . "\n";
289         } else if ($tag) {
290                 $retval = join($rule_related_str, $_links);
291         } else {
292                 $retval = join($related_str, $_links);
293         }
294
295         return $retval;
296 }
297
298 // User-defined rules (convert without replacing source)
299 function make_line_rules($str)
300 {
301         global $line_rules;
302         static $pattern, $replace;
303
304         if (! isset($pattern)) {
305                 $pattern = array_map(create_function('$a',
306                         'return \'/\' . $a . \'/\';'), array_keys($line_rules));
307                 $replace = array_values($line_rules);
308                 unset($line_rules);
309         }
310
311         return preg_replace($pattern, $replace, $str);
312 }
313
314 function strip_htmltag($str)
315 {
316         global $_symbol_noexists;
317         static $noexists_pattern;
318
319         if (! isset($noexists_pattern))
320                 $noexists_pattern = '#<span class="noexists">([^<]*)<a[^>]+>' .
321                         preg_quote($_symbol_noexists, '#') . '</a></span>#';
322
323         $str = preg_replace($noexists_pattern, '$1', $str);
324         //$str = preg_replace('/<a[^>]+>\?<\/a>/', '', $str);
325
326         return preg_replace('/<[^>]+>/', '', $str);
327 }
328
329 // Make a backlink. searching-link of the page name, by the page name, for the page name
330 function make_search($page)
331 {
332         global $script;
333
334         $s_page = htmlspecialchars($page);
335         $r_page = rawurlencode($page);
336
337         return '<a href="' . $script . '?plugin=related&amp;page=' . $r_page .
338                 '">' . $s_page . '</a> ';
339 }
340
341 // Make heading (remove footnotes and HTML tags)
342 function make_heading(& $str, $strip = TRUE)
343 {
344         global $NotePattern;
345
346         // Cut fixed-anchors
347         $id = '';
348         $matches = array();
349         if (preg_match('/^(\*{0,3})(.*?)\[#([A-Za-z][\w-]+)\](.*?)$/m', $str, $matches)) {
350                 $str = $matches[2] . $matches[4];
351                 $id  = $matches[3];
352         } else {
353                 $str = preg_replace('/^\*{0,3}/', '', $str);
354         }
355
356         // Cut footnotes and tags
357         if ($strip === TRUE)
358                 $str = strip_htmltag(make_link(preg_replace($NotePattern, '', $str)));
359
360         return $id;
361 }
362
363 // Separate a page-name(or URL or null string) and an anchor
364 // (last one standing) without sharp
365 function anchor_explode($page, $strict_editable = FALSE)
366 {
367         $pos = strrpos($page, '#');
368         if ($pos === FALSE) return array($page, '', FALSE);
369
370         // Ignore the last sharp letter
371         if ($pos + 1 == strlen($page)) {
372                 $pos = strpos(substr($page, $pos + 1), '#');
373                 if ($pos === FALSE) return array($page, '', FALSE);
374         }
375
376         $s_page = substr($page, 0, $pos);
377         $anchor = substr($page, $pos + 1);
378
379         if($strict_editable === TRUE &&  preg_match('/^[a-z][a-f0-9]{7}$/', $anchor)) {
380                 return array ($s_page, $anchor, TRUE); // Seems fixed-anchor
381         } else {
382                 return array ($s_page, $anchor, FALSE);
383         }
384 }
385
386 // Check HTTP header()s were sent already, or
387 // there're blank lines or something out of php blocks
388 function pkwk_headers_sent()
389 {
390         if (PKWK_OPTIMISE) return;
391
392         $file = $line = '';
393         if (version_compare(PHP_VERSION, '4.3.0', '>=')) {
394                 if (headers_sent($file, $line))
395                     die('Headers already sent at ' .
396                         htmlspecialchars($file) .
397                         ' line ' . $line . '.');
398         } else {
399                 if (headers_sent())
400                         die('Headers already sent.');
401         }
402 }
403
404 // Output common HTTP headers
405 function pkwk_common_headers()
406 {
407         if (! PKWK_OPTIMISE) pkwk_headers_sent();
408
409         if(defined('PKWK_ZLIB_LOADABLE_MODULE')) {
410                 $matches = array();
411                 if(ini_get('zlib.output_compression') &&
412                     preg_match('/\b(gzip|deflate)\b/i', $_SERVER['HTTP_ACCEPT_ENCODING'], $matches)) {
413                         // Bug #29350 output_compression compresses everything _without header_ as loadable module
414                         // http://bugs.php.net/bug.php?id=29350
415                         header('Content-Encoding: ' . $matches[1]);
416                         header('Vary: Accept-Encoding');
417                 }
418         }
419 }
420
421 // DTD definitions
422 define('PKWK_DTD_XHTML_1_1',              17); // Strict only
423 define('PKWK_DTD_XHTML_1_0',              16); // Strict
424 define('PKWK_DTD_XHTML_1_0_STRICT',       16);
425 define('PKWK_DTD_XHTML_1_0_TRANSITIONAL', 15);
426 define('PKWK_DTD_XHTML_1_0_FRAMESET',     14);
427 define('PKWK_DTD_HTML_4_01',               3); // Strict
428 define('PKWK_DTD_HTML_4_01_STRICT',        3);
429 define('PKWK_DTD_HTML_4_01_TRANSITIONAL',  2);
430 define('PKWK_DTD_HTML_4_01_FRAMESET',      1);
431
432 define('PKWK_DTD_TYPE_XHTML',  1);
433 define('PKWK_DTD_TYPE_HTML',   0);
434
435 // Output HTML DTD, <html> start tag. Return content-type.
436 function pkwk_output_dtd($pkwk_dtd = PKWK_DTD_XHTML_1_1, $charset = CONTENT_CHARSET)
437 {
438         static $called;
439         if (isset($called)) die('pkwk_output_dtd() already called. Why?');
440         $called = TRUE;
441
442         $type = PKWK_DTD_TYPE_XHTML;
443         $option = '';
444         switch($pkwk_dtd){
445         case PKWK_DTD_XHTML_1_1             :
446                 $version = '1.1' ;
447                 $dtd     = 'http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd';
448                 break;
449         case PKWK_DTD_XHTML_1_0_STRICT      :
450                 $version = '1.0' ;
451                 $option  = 'Strict';
452                 $dtd     = 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd';
453                 break;
454         case PKWK_DTD_XHTML_1_0_TRANSITIONAL:
455                 $version = '1.0' ;
456                 $option  = 'Transitional';
457                 $dtd     = 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd';
458                 break;
459
460         case PKWK_DTD_HTML_4_01_STRICT      :
461                 $type    = PKWK_DTD_TYPE_HTML;
462                 $version = '4.01';
463                 $dtd     = 'http://www.w3.org/TR/html4/strict.dtd';
464                 break;
465         case PKWK_DTD_HTML_4_01_TRANSITIONAL:
466                 $type    = PKWK_DTD_TYPE_HTML;
467                 $version = '4.01';
468                 $option  = 'Transitional';
469                 $dtd     = 'http://www.w3.org/TR/html4/loose.dtd';
470                 break;
471
472         default: die('DTD not specified or invalid DTD');
473                 break;
474         }
475
476         $charset = htmlspecialchars($charset);
477
478         // Output XML or not
479         if ($type == PKWK_DTD_TYPE_XHTML) echo '<?xml version="1.0" encoding="' . $charset . '" ?>' . "\n";
480
481         // Output doctype
482         echo '<!DOCTYPE html PUBLIC "-//W3C//DTD ' .
483                 ($type == PKWK_DTD_TYPE_XHTML ? 'XHTML' : 'HTML') . ' ' .
484                 $version .
485                 ($option != '' ? ' ' . $option : '') .
486                 '//EN" "' .
487                 $dtd .
488                 '">' . "\n";
489
490         // Output <html> start tag
491         echo '<html';
492         if ($type == PKWK_DTD_TYPE_XHTML) {
493                 echo ' xmlns="http://www.w3.org/1999/xhtml"'; // dir="ltr" /* LeftToRight */
494                 echo ' xml:lang="' . LANG . '"';
495                 if ($version == '1.0') echo ' lang="' . LANG . '"'; // Only XHTML 1.0
496         } else {
497                 echo ' lang="' . LANG . '"'; // HTML
498         }
499         echo '>' . "\n"; // <html>
500
501         // Return content-type (with MIME type)
502         if ($type == PKWK_DTD_TYPE_XHTML) {
503                 // NOTE: XHTML 1.1 browser will ignore http-equiv
504                 return '<meta http-equiv="content-type" content="application/xhtml+xml; charset=' . $charset . '" />' . "\n";
505         } else {
506                 return '<meta http-equiv="content-type" content="text/html; charset=' . $charset . '" />' . "\n";
507         }
508 }
509 ?>