OSDN Git Service

BugTrack2/106: Only variables can be passed by reference from PHP 5.0.5 (Patch from...
[pukiwiki/pukiwiki.git] / lib / html.php
1 <?php
2 // PukiWiki - Yet another WikiWikiWeb clone.
3 // $Id: html.php,v 1.44 2005/10/02 16:23:59 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 ?  format_date(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
123                 // Only variables can be passed by reference from PHP 5.0.5
124                 // with array_splice(), array_flip()
125                 $tmp_array = preg_split('/\s+/', $vars['word'], -1, PREG_SPLIT_NO_EMPTY);
126                 $tmp_array = array_splice($tmp_array, 0, 10);
127
128                 $words = array_flip($tmp_array);
129                 $keys = array();
130                 foreach ($words as $word=>$id) $keys[$word] = strlen($word);
131                 arsort($keys, SORT_NUMERIC);
132                 $keys = get_search_words(array_keys($keys), TRUE);
133                 $id = 0;
134                 foreach ($keys as $key=>$pattern) {
135                         $s_key    = htmlspecialchars($key);
136                         $pattern  = '/<[^>]*>|(' . $pattern . ')|&[^;]+;/';
137                         $callback = create_function(
138                                 '$arr',
139                                 'return (count($arr) > 1) ? \'<strong class="word' .
140                                         $id++ . '">\' . $arr[1] . \'</strong>\' : $arr[0];'
141                         );
142                         $body  = preg_replace_callback($pattern, $callback, $body);
143                         $notes = preg_replace_callback($pattern, $callback, $notes);
144                 }
145         }
146
147         $longtaketime = getmicrotime() - MUTIME;
148         $taketime     = sprintf('%01.03f', $longtaketime);
149
150         require(SKIN_FILE);
151 }
152
153 // Show 'edit' form
154 function edit_form($page, $postdata, $digest = FALSE, $b_template = TRUE)
155 {
156         global $script, $vars, $rows, $cols, $hr, $function_freeze;
157         global $_btn_addtop, $_btn_preview, $_btn_repreview, $_btn_update, $_btn_cancel,
158                 $_msg_help, $_btn_notchangetimestamp;
159         global $whatsnew, $_btn_template, $_btn_load, $non_list, $load_template_func;
160         global $notimeupdate;
161
162         // Newly generate $digest or not
163         if ($digest === FALSE) $digest = md5(join('', get_source($page)));
164
165         $refer = $template = $addtag = $add_top = '';
166
167         $checked_top  = isset($vars['add_top'])     ? ' checked="checked"' : '';
168         $checked_time = isset($vars['notimestamp']) ? ' checked="checked"' : '';
169
170         if(isset($vars['add'])) {
171                 $addtag  = '<input type="hidden" name="add" value="true" />';
172                 $add_top = '<input type="checkbox" name="add_top" value="true"' .
173                         $checked_top . ' /><span class="small">' .
174                         $_btn_addtop . '</span>';
175         }
176
177         if($load_template_func && $b_template) {
178                 $pages  = array();
179                 $non_list_pattern = '/' . $non_list . '/';
180                 foreach(get_existpages() as $_page) {
181                         if ($_page == $whatsnew || preg_match($non_list_pattern, $_page))
182                                 continue;
183                         $s_page = htmlspecialchars($_page);
184                         $pages[$_page] = '   <option value="' . $s_page . '">' .
185                                 $s_page . '</option>';
186                 }
187                 ksort($pages);
188                 $s_pages  = join("\n", $pages);
189                 $template = <<<EOD
190   <select name="template_page">
191    <option value="">-- $_btn_template --</option>
192 $s_pages
193   </select>
194   <input type="submit" name="template" value="$_btn_load" accesskey="r" />
195   <br />
196 EOD;
197
198                 if (isset($vars['refer']) && $vars['refer'] != '')
199                         $refer = '[[' . strip_bracket($vars['refer']) . ']]' . "\n\n";
200         }
201
202         $r_page      = rawurlencode($page);
203         $s_page      = htmlspecialchars($page);
204         $s_digest    = htmlspecialchars($digest);
205         $s_postdata  = htmlspecialchars($refer . $postdata);
206         $s_original  = isset($vars['original']) ? htmlspecialchars($vars['original']) : $s_postdata;
207         $b_preview   = isset($vars['preview']); // TRUE when preview
208         $btn_preview = $b_preview ? $_btn_repreview : $_btn_preview;
209
210         $add_notimestamp = '';
211         if ( $notimeupdate != 0 ) {
212                 // enable 'do not change timestamp'
213                 $add_notimestamp = <<<EOD
214   <input type="checkbox" name="notimestamp" id="_edit_form_notimestamp" value="true"$checked_time />
215   <label for="_edit_form_notimestamp"><span class="small">$_btn_notchangetimestamp</span></label>
216 EOD;
217                 if ( $notimeupdate == 2 ) {
218                         // enable only administrator
219                         $add_notimestamp .= <<<EOD
220   <input type="password" name="pass" size="12" />
221 EOD;
222                 }
223                 $add_notimestamp .= '&nbsp;';
224         }
225
226         $body = <<<EOD
227 <form action="$script" method="post">
228  <div class="edit_form">
229 $template
230   $addtag
231   <input type="hidden" name="cmd"    value="edit" />
232   <input type="hidden" name="page"   value="$s_page" />
233   <input type="hidden" name="digest" value="$s_digest" />
234   <textarea name="msg" rows="$rows" cols="$cols">$s_postdata</textarea>
235   <br />
236   <input type="submit" name="preview" value="$btn_preview" accesskey="p" />
237   <input type="submit" name="write"   value="$_btn_update" accesskey="s" />
238   $add_top
239   $add_notimestamp
240   <input type="submit" name="cancel"  value="$_btn_cancel" accesskey="c" />
241   <textarea name="original" rows="1" cols="1" style="display:none">$s_original</textarea>
242  </div>
243 </form>
244 EOD;
245
246         if (isset($vars['help'])) {
247                 $body .= $hr . catrule();
248         } else {
249                 $body .= '<ul><li><a href="' .
250                         $script . '?cmd=edit&amp;help=true&amp;page=' . $r_page .
251                         '">' . $_msg_help . '</a></li></ul>';
252         }
253
254         return $body;
255 }
256
257 // Related pages
258 function make_related($page, $tag = '')
259 {
260         global $script, $vars, $rule_related_str, $related_str, $non_list;
261         global $_ul_left_margin, $_ul_margin, $_list_pad_str;
262
263         $links = links_get_related($page);
264
265         if ($tag) {
266                 ksort($links);
267         } else {
268                 arsort($links);
269         }
270
271         $_links = array();
272         $non_list_pattern = '/' . $non_list . '/';
273         foreach ($links as $page=>$lastmod) {
274                 if (preg_match($non_list_pattern, $page)) continue;
275
276                 $r_page   = rawurlencode($page);
277                 $s_page   = htmlspecialchars($page);
278                 $passage  = get_passage($lastmod);
279                 $_links[] = $tag ?
280                         '<a href="' . $script . '?' . $r_page . '" title="' .
281                         $s_page . ' ' . $passage . '">' . $s_page . '</a>' :
282                         '<a href="' . $script . '?' . $r_page . '">' .
283                         $s_page . '</a>' . $passage;
284         }
285         if (empty($_links)) return ''; // Nothing
286
287         if ($tag == 'p') { // From the line-head
288                 $margin = $_ul_left_margin + $_ul_margin;
289                 $style  = sprintf($_list_pad_str, 1, $margin, $margin);
290                 $retval =  "\n" . '<ul' . $style . '>' . "\n" .
291                         '<li>' . join($rule_related_str, $_links) . '</li>' . "\n" .
292                         '</ul>' . "\n";
293         } else if ($tag) {
294                 $retval = join($rule_related_str, $_links);
295         } else {
296                 $retval = join($related_str, $_links);
297         }
298
299         return $retval;
300 }
301
302 // User-defined rules (convert without replacing source)
303 function make_line_rules($str)
304 {
305         global $line_rules;
306         static $pattern, $replace;
307
308         if (! isset($pattern)) {
309                 $pattern = array_map(create_function('$a',
310                         'return \'/\' . $a . \'/\';'), array_keys($line_rules));
311                 $replace = array_values($line_rules);
312                 unset($line_rules);
313         }
314
315         return preg_replace($pattern, $replace, $str);
316 }
317
318 // Remove all HTML tags(or just anchor tags), and WikiName-speific decorations
319 function strip_htmltag($str, $all = TRUE)
320 {
321         global $_symbol_noexists;
322         static $noexists_pattern;
323
324         if (! isset($noexists_pattern))
325                 $noexists_pattern = '#<span class="noexists">([^<]*)<a[^>]+>' .
326                         preg_quote($_symbol_noexists, '#') . '</a></span>#';
327
328         // Strip Dagnling-Link decoration (Tags and "$_symbol_noexists")
329         $str = preg_replace($noexists_pattern, '$1', $str);
330
331         if ($all) {
332                 // All other HTML tags
333                 return preg_replace('#<[^>]+>#',        '', $str);
334         } else {
335                 // All other anchor-tags only
336                 return preg_replace('#<a[^>]+>|</a>#i', '', $str);
337         }
338 }
339
340 // Remove AutoLink marker with AutLink itself
341 function strip_autolink($str)
342 {
343         return preg_replace('#<!--autolink--><a [^>]+>|</a><!--/autolink-->#', '', $str);
344 }
345
346 // Make a backlink. searching-link of the page name, by the page name, for the page name
347 function make_search($page)
348 {
349         global $script;
350
351         $s_page = htmlspecialchars($page);
352         $r_page = rawurlencode($page);
353
354         return '<a href="' . $script . '?plugin=related&amp;page=' . $r_page .
355                 '">' . $s_page . '</a> ';
356 }
357
358 // Make heading string (remove heading-related decorations from Wiki text)
359 function make_heading(& $str, $strip = TRUE)
360 {
361         global $NotePattern;
362
363         // Cut fixed-heading anchors
364         $id = '';
365         $matches = array();
366         if (preg_match('/^(\*{0,3})(.*?)\[#([A-Za-z][\w-]+)\](.*?)$/m', $str, $matches)) {
367                 $str = $matches[2] . $matches[4];
368                 $id  = & $matches[3];
369         } else {
370                 $str = preg_replace('/^\*{0,3}/', '', $str);
371         }
372
373         // Cut footnotes and tags
374         if ($strip === TRUE)
375                 $str = strip_htmltag(make_link(preg_replace($NotePattern, '', $str)));
376
377         return $id;
378 }
379
380 // Separate a page-name(or URL or null string) and an anchor
381 // (last one standing) without sharp
382 function anchor_explode($page, $strict_editable = FALSE)
383 {
384         $pos = strrpos($page, '#');
385         if ($pos === FALSE) return array($page, '', FALSE);
386
387         // Ignore the last sharp letter
388         if ($pos + 1 == strlen($page)) {
389                 $pos = strpos(substr($page, $pos + 1), '#');
390                 if ($pos === FALSE) return array($page, '', FALSE);
391         }
392
393         $s_page = substr($page, 0, $pos);
394         $anchor = substr($page, $pos + 1);
395
396         if($strict_editable === TRUE &&  preg_match('/^[a-z][a-f0-9]{7}$/', $anchor)) {
397                 return array ($s_page, $anchor, TRUE); // Seems fixed-anchor
398         } else {
399                 return array ($s_page, $anchor, FALSE);
400         }
401 }
402
403 // Check HTTP header()s were sent already, or
404 // there're blank lines or something out of php blocks
405 function pkwk_headers_sent()
406 {
407         if (PKWK_OPTIMISE) return;
408
409         $file = $line = '';
410         if (version_compare(PHP_VERSION, '4.3.0', '>=')) {
411                 if (headers_sent($file, $line))
412                     die('Headers already sent at ' .
413                         htmlspecialchars($file) .
414                         ' line ' . $line . '.');
415         } else {
416                 if (headers_sent())
417                         die('Headers already sent.');
418         }
419 }
420
421 // Output common HTTP headers
422 function pkwk_common_headers()
423 {
424         if (! PKWK_OPTIMISE) pkwk_headers_sent();
425
426         if(defined('PKWK_ZLIB_LOADABLE_MODULE')) {
427                 $matches = array();
428                 if(ini_get('zlib.output_compression') &&
429                     preg_match('/\b(gzip|deflate)\b/i', $_SERVER['HTTP_ACCEPT_ENCODING'], $matches)) {
430                         // Bug #29350 output_compression compresses everything _without header_ as loadable module
431                         // http://bugs.php.net/bug.php?id=29350
432                         header('Content-Encoding: ' . $matches[1]);
433                         header('Vary: Accept-Encoding');
434                 }
435         }
436 }
437
438 // DTD definitions
439 define('PKWK_DTD_XHTML_1_1',              17); // Strict only
440 define('PKWK_DTD_XHTML_1_0',              16); // Strict
441 define('PKWK_DTD_XHTML_1_0_STRICT',       16);
442 define('PKWK_DTD_XHTML_1_0_TRANSITIONAL', 15);
443 define('PKWK_DTD_XHTML_1_0_FRAMESET',     14);
444 define('PKWK_DTD_HTML_4_01',               3); // Strict
445 define('PKWK_DTD_HTML_4_01_STRICT',        3);
446 define('PKWK_DTD_HTML_4_01_TRANSITIONAL',  2);
447 define('PKWK_DTD_HTML_4_01_FRAMESET',      1);
448
449 define('PKWK_DTD_TYPE_XHTML',  1);
450 define('PKWK_DTD_TYPE_HTML',   0);
451
452 // Output HTML DTD, <html> start tag. Return content-type.
453 function pkwk_output_dtd($pkwk_dtd = PKWK_DTD_XHTML_1_1, $charset = CONTENT_CHARSET)
454 {
455         static $called;
456         if (isset($called)) die('pkwk_output_dtd() already called. Why?');
457         $called = TRUE;
458
459         $type = PKWK_DTD_TYPE_XHTML;
460         $option = '';
461         switch($pkwk_dtd){
462         case PKWK_DTD_XHTML_1_1             :
463                 $version = '1.1' ;
464                 $dtd     = 'http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd';
465                 break;
466         case PKWK_DTD_XHTML_1_0_STRICT      :
467                 $version = '1.0' ;
468                 $option  = 'Strict';
469                 $dtd     = 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd';
470                 break;
471         case PKWK_DTD_XHTML_1_0_TRANSITIONAL:
472                 $version = '1.0' ;
473                 $option  = 'Transitional';
474                 $dtd     = 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd';
475                 break;
476
477         case PKWK_DTD_HTML_4_01_STRICT      :
478                 $type    = PKWK_DTD_TYPE_HTML;
479                 $version = '4.01';
480                 $dtd     = 'http://www.w3.org/TR/html4/strict.dtd';
481                 break;
482         case PKWK_DTD_HTML_4_01_TRANSITIONAL:
483                 $type    = PKWK_DTD_TYPE_HTML;
484                 $version = '4.01';
485                 $option  = 'Transitional';
486                 $dtd     = 'http://www.w3.org/TR/html4/loose.dtd';
487                 break;
488
489         default: die('DTD not specified or invalid DTD');
490                 break;
491         }
492
493         $charset = htmlspecialchars($charset);
494
495         // Output XML or not
496         if ($type == PKWK_DTD_TYPE_XHTML) echo '<?xml version="1.0" encoding="' . $charset . '" ?>' . "\n";
497
498         // Output doctype
499         echo '<!DOCTYPE html PUBLIC "-//W3C//DTD ' .
500                 ($type == PKWK_DTD_TYPE_XHTML ? 'XHTML' : 'HTML') . ' ' .
501                 $version .
502                 ($option != '' ? ' ' . $option : '') .
503                 '//EN" "' .
504                 $dtd .
505                 '">' . "\n";
506
507         // Output <html> start tag
508         echo '<html';
509         if ($type == PKWK_DTD_TYPE_XHTML) {
510                 echo ' xmlns="http://www.w3.org/1999/xhtml"'; // dir="ltr" /* LeftToRight */
511                 echo ' xml:lang="' . LANG . '"';
512                 if ($version == '1.0') echo ' lang="' . LANG . '"'; // Only XHTML 1.0
513         } else {
514                 echo ' lang="' . LANG . '"'; // HTML
515         }
516         echo '>' . "\n"; // <html>
517
518         // Return content-type (with MIME type)
519         if ($type == PKWK_DTD_TYPE_XHTML) {
520                 // NOTE: XHTML 1.1 browser will ignore http-equiv
521                 return '<meta http-equiv="content-type" content="application/xhtml+xml; charset=' . $charset . '" />' . "\n";
522         } else {
523                 return '<meta http-equiv="content-type" content="text/html; charset=' . $charset . '" />' . "\n";
524         }
525 }
526 ?>