OSDN Git Service

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