OSDN Git Service

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