OSDN Git Service

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