OSDN Git Service

初回コミット(v2.6.17.1)
[magic3/magic3.git] / widgets / wiki_main / include / lib / html.php
1 <?php
2 /**
3  * HTML作成ライブラリ
4  *
5  * PHP versions 5
6  *
7  * LICENSE: This source file is licensed under the terms of the GNU General Public License.
8  *
9  * @package    Magic3 Framework
10  * @author     平田直毅(Naoki Hirata) <naoki@aplo.co.jp>
11  * @copyright  Copyright 2006-2010 Magic3 Project.
12  * @license    http://www.gnu.org/copyleft/gpl.html  GPL License
13  * @version    SVN: $Id: html.php 3474 2010-08-13 10:36:48Z fishbone $
14  * @link       http://www.magic3.org
15  */
16 // Copyright (C)
17 //   2002-2006 PukiWiki Developers Team
18 //   2001-2002 Originally written by yu-ji
19 // License: GPL v2 or (at your option) any later version
20 //
21 // HTML-publishing related functions
22
23 // Show page-content
24 // modified for Magic3 by naoki on 2008/9/29
25 //function catbody($title, $page, $body)
26 function catbody($body)
27 {
28         // modified for Magic3 by naoki on 2008/9/26
29         //global $script, $vars, $arg, $defaultpage, $whatsnew, $help_page, $hr;
30         global $script, $defaultpage, $whatsnew, $help_page, $hr;
31         global $attach_link, $related_link, $cantedit, $function_freeze;
32         global $search_word_color, $_msg_word, $foot_explain, $note_hr, $head_tags;
33         global $trackback, $trackback_javascript, $referer, $javascript;
34         global $nofollow;
35         //global $_LANG, $_LINK, $_IMAGE;
36
37         global $pkwk_dtd;     // XHTML 1.1, XHTML1.0, HTML 4.01 Transitional...
38         global $page_title;   // Title of this site
39         global $do_backup;    // Do backup or not
40         global $modifier;     // Site administrator's  web page
41         global $modifierlink; // Site administrator's name
42
43 // modified for Magic3 by naoki on 2008/9/22
44 //      if (! file_exists(SKIN_FILE) || ! is_readable(SKIN_FILE))
45 //              die_message('SKIN_FILE is not found');
46 //
47 //      $_LINK = $_IMAGE = array();
48
49         // Add JavaScript header when ...
50         if ($trackback && $trackback_javascript) $javascript = 1; // Set something If you want
51         if (! PKWK_ALLOW_JAVASCRIPT) unset($javascript);
52 }
53
54 // Show 'edit' form
55 //function edit_form($page, $postdata, $digest = FALSE, $b_template = TRUE)
56 function edit_form($page, $postdata, $digest = FALSE, $b_template = TRUE, $cmd='')
57 {
58         //global $script, $vars, $rows, $cols, $hr, $function_freeze;
59         global $script, $rows, $cols, $hr, $function_freeze;
60         global $_btn_preview, $_btn_repreview, $_btn_update, $_btn_cancel, $_msg_help;
61         global $whatsnew, $_btn_template, $_btn_load, $load_template_func;
62         global $notimeupdate;
63
64         // Newly generate $digest or not
65         //if ($digest === FALSE) $digest = md5(join('', get_source($page)));
66         if ($digest === FALSE) $digest = md5(get_source($page, true));
67
68         $refer = $template = '';
69  
70         // Add plugin
71         $addtag = $add_top = '';
72         //if(isset($vars['add'])) {
73         if ($cmd == 'add'){
74                 global $_btn_addtop;
75                 $addtag  = '<input type="hidden" name="add"    value="true" />';
76                 //$add_top = isset($vars['add_top']) ? ' checked="checked"' : '';
77                 $add_top = (WikiParam::getVar('add_top') != '') ? ' checked="checked"' : '';
78                 $add_top = '<input type="checkbox" name="add_top" ' .
79                         'id="_edit_form_add_top" value="true"' . $add_top . ' />' . "\n" .
80                         '  <label for="_edit_form_add_top">' .
81                                 '<span class="small">' . $_btn_addtop . '</span>' .
82                         '</label>';
83         }
84
85         if($load_template_func && $b_template) {
86                 $pages  = array();
87                 foreach(get_existpages() as $_page) {
88                         if ($_page == $whatsnew || check_non_list($_page))
89                                 continue;
90                         $s_page = htmlspecialchars($_page);
91                         $pages[$_page] = '   <option value="' . $s_page . '">' .
92                                 $s_page . '</option>';
93                 }
94                 ksort($pages);
95                 $s_pages  = join("\n", $pages);
96                 $template = <<<EOD
97   <select name="template_page">
98    <option value="">-- $_btn_template --</option>
99 $s_pages
100   </select>
101   <input type="submit" name="template" class="button" value="$_btn_load" accesskey="r" />
102   <br />
103 EOD;
104
105                 /*if (isset($vars['refer']) && $vars['refer'] != '')
106                         $refer = '[[' . strip_bracket($vars['refer']) . ']]' . "\n\n";*/
107                 $referValue = WikiParam::getVar('refer');
108                 if ($referValue != '') $refer = '[[' . strip_bracket($referValue) . ']]' . "\n\n";
109         }
110
111         $r_page      = rawurlencode($page);
112         $s_page      = htmlspecialchars($page);
113         $s_digest    = htmlspecialchars($digest);
114         $s_postdata  = htmlspecialchars($refer . $postdata);
115         /*$s_original  = isset($vars['original']) ? htmlspecialchars($vars['original']) : $s_postdata;
116         $b_preview   = isset($vars['preview']); // TRUE when preview*/
117         $s_original  = (WikiParam::getVar('original') != '') ? htmlspecialchars(WikiParam::getVar('original')) : $s_postdata;
118         $b_preview   = (WikiParam::getVar('preview') != ''); // TRUE when preview
119         $btn_preview = $b_preview ? $_btn_repreview : $_btn_preview;
120
121         // Checkbox 'do not change timestamp'
122         $add_notimestamp = '';
123         if ($notimeupdate != 0) {
124                 global $_btn_notchangetimestamp;
125                 //$checked_time = isset($vars['notimestamp']) ? ' checked="checked"' : '';
126                 $checked_time = (WikiParam::getVar('notimestamp') != '') ? ' checked="checked"' : '';
127                 // Only for administrator
128                 if ($notimeupdate == 2) {
129                         $add_notimestamp = '   ' .
130                                 '<input type="password" name="pass" size="12" />' . "\n";
131                 }
132                 $add_notimestamp = '<input type="checkbox" name="notimestamp" ' .
133                         'id="_edit_form_notimestamp" value="true"' . $checked_time . ' />' . "\n" .
134                         '   ' . '<label for="_edit_form_notimestamp"><span class="small">' .
135                         $_btn_notchangetimestamp . '</span></label>' . "\n" .
136                         $add_notimestamp .
137                         '&nbsp;';
138         }
139
140         // 'margin-bottom', 'float:left', and 'margin-top'
141         // are for layout of 'cancel button'
142         // modified for Magic3 by naoki on 2008/10/6
143         $postScript = $script . WikiParam::convQuery("?");
144         $body = <<<EOD
145 <div class="edit_form">
146  <form action="$postScript" method="post" style="margin-bottom:0px;" class="form">
147 $template
148   $addtag
149   <input type="hidden" name="wcmd"    value="edit" />
150   <input type="hidden" name="page"   value="$s_page" />
151   <input type="hidden" name="digest" value="$s_digest" />
152   <textarea name="msg" class="wiki_edit" rows="$rows" cols="$cols">$s_postdata</textarea>
153   <br />
154   <div style="float:left;">
155    <input type="submit" name="preview" class="button" value="$btn_preview" accesskey="p" />
156    <input type="submit" name="write"   class="button" value="$_btn_update" accesskey="s" />
157    $add_top
158    $add_notimestamp
159   </div>
160   <textarea name="original" rows="1" cols="1" style="display:none">$s_original</textarea>
161  </form>
162  <form action="$postScript" method="post" style="margin-top:0px;" class="form">
163   <input type="hidden" name="wcmd"    value="edit" />
164   <input type="hidden" name="page"   value="$s_page" />
165   <input type="submit" name="cancel" class="button" value="$_btn_cancel" accesskey="c" />
166  </form>
167 </div>
168 EOD;
169
170         //if (isset($vars['help'])) {
171         if (WikiParam::getVar('help') != ''){
172                 $body .= $hr . catrule();
173         } else {
174                 // modified for Magic3 by naoki on 2008/10/6
175                 /*$body .= '<ul><li><a href="' .
176                         $script . '?cmd=edit&amp;help=true&amp;page=' . $r_page .
177                         '">' . $_msg_help . '</a></li></ul>';*/
178                 $body .= '<ul><li><a href="' .
179                         $script . WikiParam::convQuery("?cmd=edit&amp;help=true&amp;page=$r_page") .
180                         '">' . $_msg_help . '</a></li></ul>';
181         }
182
183         return $body;
184 }
185
186 // Related pages
187 function make_related($page, $tag = '')
188 {
189         //global $script, $vars, $rule_related_str, $related_str;
190         global $script, $rule_related_str, $related_str;
191         global $_ul_left_margin, $_ul_margin, $_list_pad_str;
192
193         $links = links_get_related($page);
194
195         if ($tag) {
196                 ksort($links);
197         } else {
198                 arsort($links);
199         }
200
201         $_links = array();
202         foreach ($links as $page=>$lastmod) {
203                 if (check_non_list($page)) continue;
204
205                 $r_page   = rawurlencode($page);
206                 $s_page   = htmlspecialchars($page);
207                 $passage  = get_passage($lastmod);
208                 // modified for Magic3 by naoki on 2008/10/6
209                 /*$_links[] = $tag ?
210                         '<a href="' . $script . '?' . $r_page . '" title="' .
211                         $s_page . ' ' . $passage . '">' . $s_page . '</a>' :
212                         '<a href="' . $script . '?' . $r_page . '">' .
213                         $s_page . '</a>' . $passage;*/
214                 $_links[] = $tag ?
215                         '<a href="' . $script . WikiParam::convQuery("?$r_page") . '" title="' .
216                         $s_page . ' ' . $passage . '">' . $s_page . '</a>' :
217                         '<a href="' . $script . WikiParam::convQuery("?$r_page") . '">' .
218                         $s_page . '</a>' . $passage;
219         }
220         if (empty($_links)) return ''; // Nothing
221
222         if ($tag == 'p') { // From the line-head
223                 $margin = $_ul_left_margin + $_ul_margin;
224                 $style  = sprintf($_list_pad_str, 1, $margin, $margin);
225                 $retval =  "\n" . '<ul' . $style . '>' . "\n" .
226                         '<li>' . join($rule_related_str, $_links) . '</li>' . "\n" .
227                         '</ul>' . "\n";
228         } else if ($tag) {
229                 $retval = join($rule_related_str, $_links);
230         } else {
231                 $retval = join($related_str, $_links);
232         }
233
234         return $retval;
235 }
236
237 // User-defined rules (convert without replacing source)
238 function make_line_rules($str)
239 {
240         global $line_rules;
241         static $pattern, $replace;
242
243         if (! isset($pattern)) {
244                 $pattern = array_map(create_function('$a',
245                         'return \'/\' . $a . \'/\';'), array_keys($line_rules));
246                 $replace = array_values($line_rules);
247                 unset($line_rules);
248         }
249
250         return preg_replace($pattern, $replace, $str);
251 }
252
253 // Remove all HTML tags(or just anchor tags), and WikiName-speific decorations
254 function strip_htmltag($str, $all = TRUE)
255 {
256         global $_symbol_noexists;
257         static $noexists_pattern;
258
259         if (! isset($noexists_pattern))
260                 $noexists_pattern = '#<span class="noexists">([^<]*)<a[^>]+>' .
261                         preg_quote($_symbol_noexists, '#') . '</a></span>#';
262
263         // Strip Dagnling-Link decoration (Tags and "$_symbol_noexists")
264         $str = preg_replace($noexists_pattern, '$1', $str);
265
266         if ($all) {
267                 // All other HTML tags
268                 return preg_replace('#<[^>]+>#',        '', $str);
269         } else {
270                 // All other anchor-tags only
271                 return preg_replace('#<a[^>]+>|</a>#i', '', $str);
272         }
273 }
274
275 // Remove AutoLink marker with AutLink itself
276 function strip_autolink($str)
277 {
278         return preg_replace('#<!--autolink--><a [^>]+>|</a><!--/autolink-->#', '', $str);
279 }
280
281 // Make a backlink. searching-link of the page name, by the page name, for the page name
282 function make_search($page)
283 {
284         global $script;
285
286         $s_page = htmlspecialchars($page);
287         $r_page = rawurlencode($page);
288
289         // modified for Magic3 by naoki on 2008/10/6
290 /*      return '<a href="' . $script . '?plugin=related&amp;page=' . $r_page .
291                 '">' . $s_page . '</a> ';*/
292         return '<a href="' . $script . WikiParam::convQuery("?plugin=related&amp;page=$r_page") . '">' . $s_page . '</a> ';
293 }
294
295 // Make heading string (remove heading-related decorations from Wiki text)
296 function make_heading(& $str, $strip = TRUE)
297 {
298         global $NotePattern;
299
300         // Cut fixed-heading anchors
301         $id = '';
302         $matches = array();
303         if (preg_match('/^(\*{0,3})(.*?)\[#([A-Za-z][\w-]+)\](.*?)$/m', $str, $matches)) {
304                 $str = $matches[2] . $matches[4];
305                 $id  = $matches[3];
306         } else {
307                 $str = preg_replace('/^\*{0,3}/', '', $str);
308         }
309
310         // Cut footnotes and tags
311         if ($strip === TRUE)
312                 $str = strip_htmltag(make_link(preg_replace($NotePattern, '', $str)));
313
314         return $id;
315 }
316
317 // Separate a page-name(or URL or null string) and an anchor
318 // (last one standing) without sharp
319 function anchor_explode($page, $strict_editable = FALSE)
320 {
321         $pos = strrpos($page, '#');
322         if ($pos === FALSE) return array($page, '', FALSE);
323
324         // Ignore the last sharp letter
325         if ($pos + 1 == strlen($page)) {
326                 $pos = strpos(substr($page, $pos + 1), '#');
327                 if ($pos === FALSE) return array($page, '', FALSE);
328         }
329
330         $s_page = substr($page, 0, $pos);
331         $anchor = substr($page, $pos + 1);
332
333         if($strict_editable === TRUE &&  preg_match('/^[a-z][a-f0-9]{7}$/', $anchor)) {
334                 return array ($s_page, $anchor, TRUE); // Seems fixed-anchor
335         } else {
336                 return array ($s_page, $anchor, FALSE);
337         }
338 }
339
340 // Check HTTP header()s were sent already, or
341 // there're blank lines or something out of php blocks
342 function pkwk_headers_sent()
343 {
344         if (PKWK_OPTIMISE) return;
345
346         $file = $line = '';
347         if (version_compare(PHP_VERSION, '4.3.0', '>=')) {
348                 if (headers_sent($file, $line))
349                     die('Headers already sent at ' .
350                         htmlspecialchars($file) .
351                         ' line ' . $line . '.');
352         } else {
353                 if (headers_sent())
354                         die('Headers already sent.');
355         }
356 }
357
358 // Output common HTTP headers
359 function pkwk_common_headers()
360 {
361         if (! PKWK_OPTIMISE) pkwk_headers_sent();
362
363         if(defined('PKWK_ZLIB_LOADABLE_MODULE')) {
364                 $matches = array();
365                 if(ini_get('zlib.output_compression') &&
366                     preg_match('/\b(gzip|deflate)\b/i', $_SERVER['HTTP_ACCEPT_ENCODING'], $matches)) {
367                         // Bug #29350 output_compression compresses everything _without header_ as loadable module
368                         // http://bugs.php.net/bug.php?id=29350
369                         header('Content-Encoding: ' . $matches[1]);
370                         header('Vary: Accept-Encoding');
371                 }
372         }
373 }
374
375 // DTD definitions
376 define('PKWK_DTD_XHTML_1_1',              17); // Strict only
377 define('PKWK_DTD_XHTML_1_0',              16); // Strict
378 define('PKWK_DTD_XHTML_1_0_STRICT',       16);
379 define('PKWK_DTD_XHTML_1_0_TRANSITIONAL', 15);
380 define('PKWK_DTD_XHTML_1_0_FRAMESET',     14);
381 define('PKWK_DTD_HTML_4_01',               3); // Strict
382 define('PKWK_DTD_HTML_4_01_STRICT',        3);
383 define('PKWK_DTD_HTML_4_01_TRANSITIONAL',  2);
384 define('PKWK_DTD_HTML_4_01_FRAMESET',      1);
385
386 define('PKWK_DTD_TYPE_XHTML',  1);
387 define('PKWK_DTD_TYPE_HTML',   0);
388
389 // Output HTML DTD, <html> start tag. Return content-type.
390 function pkwk_output_dtd($pkwk_dtd = PKWK_DTD_XHTML_1_1, $charset = CONTENT_CHARSET)
391 {
392         static $called;
393         if (isset($called)) die('pkwk_output_dtd() already called. Why?');
394         $called = TRUE;
395
396         $type = PKWK_DTD_TYPE_XHTML;
397         $option = '';
398         switch($pkwk_dtd){
399         case PKWK_DTD_XHTML_1_1             :
400                 $version = '1.1' ;
401                 $dtd     = 'http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd';
402                 break;
403         case PKWK_DTD_XHTML_1_0_STRICT      :
404                 $version = '1.0' ;
405                 $option  = 'Strict';
406                 $dtd     = 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd';
407                 break;
408         case PKWK_DTD_XHTML_1_0_TRANSITIONAL:
409                 $version = '1.0' ;
410                 $option  = 'Transitional';
411                 $dtd     = 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd';
412                 break;
413
414         case PKWK_DTD_HTML_4_01_STRICT      :
415                 $type    = PKWK_DTD_TYPE_HTML;
416                 $version = '4.01';
417                 $dtd     = 'http://www.w3.org/TR/html4/strict.dtd';
418                 break;
419         case PKWK_DTD_HTML_4_01_TRANSITIONAL:
420                 $type    = PKWK_DTD_TYPE_HTML;
421                 $version = '4.01';
422                 $option  = 'Transitional';
423                 $dtd     = 'http://www.w3.org/TR/html4/loose.dtd';
424                 break;
425
426         default: die('DTD not specified or invalid DTD');
427                 break;
428         }
429
430         $charset = htmlspecialchars($charset);
431
432         // Output XML or not
433         if ($type == PKWK_DTD_TYPE_XHTML) echo '<?xml version="1.0" encoding="' . $charset . '" ?>' . "\n";
434
435         // Output doctype
436         echo '<!DOCTYPE html PUBLIC "-//W3C//DTD ' .
437                 ($type == PKWK_DTD_TYPE_XHTML ? 'XHTML' : 'HTML') . ' ' .
438                 $version .
439                 ($option != '' ? ' ' . $option : '') .
440                 '//EN" "' .
441                 $dtd .
442                 '">' . "\n";
443
444         // Output <html> start tag
445         echo '<html';
446         if ($type == PKWK_DTD_TYPE_XHTML) {
447                 echo ' xmlns="http://www.w3.org/1999/xhtml"'; // dir="ltr" /* LeftToRight */
448                 echo ' xml:lang="' . LANG . '"';
449                 if ($version == '1.0') echo ' lang="' . LANG . '"'; // Only XHTML 1.0
450         } else {
451                 echo ' lang="' . LANG . '"'; // HTML
452         }
453         echo '>' . "\n"; // <html>
454
455         // Return content-type (with MIME type)
456         if ($type == PKWK_DTD_TYPE_XHTML) {
457                 // NOTE: XHTML 1.1 browser will ignore http-equiv
458                 return '<meta http-equiv="content-type" content="application/xhtml+xml; charset=' . $charset . '" />' . "\n";
459         } else {
460                 return '<meta http-equiv="content-type" content="text/html; charset=' . $charset . '" />' . "\n";
461         }
462 }
463 ?>