OSDN Git Service

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