OSDN Git Service

Compat: Skins for 1.4.4 and before
[pukiwiki/pukiwiki.git] / lib / html.php
1 <?php
2 /////////////////////////////////////////////////
3 // PukiWiki - Yet another WikiWikiWeb clone.
4 //
5 // $Id: html.php,v 1.7 2004/10/16 01:18:58 henoheno Exp $
6 //
7
8 // ËÜʸ¤ò½ÐÎÏ
9 function catbody($title,$page,$body)
10 {
11         global $script, $vars, $arg, $defaultpage, $whatsnew, $help_page, $hr;
12         global $related_link, $cantedit, $function_freeze, $search_word_color, $_msg_word;
13         global $foot_explain, $note_hr, $head_tags, $trackback, $referer;
14         global $_LANG, $_LINK, $_IMAGE;
15
16         global $html_transitional; // FALSE:XHTML1.1 TRUE:XHTML1.0 Transitional
17         global $page_title;        // ¥Û¡¼¥à¥Ú¡¼¥¸¤Î¥¿¥¤¥È¥ë
18         global $do_backup;         // ¥Ð¥Ã¥¯¥¢¥Ã¥×¤ò¹Ô¤¦¤«¤É¤¦¤«
19         global $modifier;          // ÊÔ½¸¼Ô¤Î¥Û¡¼¥à¥Ú¡¼¥¸
20         global $modifierlink;      // ÊÔ½¸¼Ô¤Î̾Á°
21
22         if (! file_exists(SKIN_FILE) || ! is_readable(SKIN_FILE))
23                 die_message('SKIN_FILE is not found');
24
25         $_LANG = $_LINK = $_IMAGE = array();
26
27         $_page  = isset($vars['page']) ? $vars['page'] : '';
28         $r_page = rawurlencode($_page);
29
30         // Set $_LINK for skin
31         $_LINK['add']      = "$script?cmd=add&amp;page=$r_page";
32         $_LINK['backup']   = "$script?cmd=backup&amp;page=$r_page";
33         $_LINK['copy']     = "$script?plugin=template&amp;refer=$r_page";
34         $_LINK['diff']     = "$script?cmd=diff&amp;page=$r_page";
35         $_LINK['edit']     = "$script?cmd=edit&amp;page=$r_page";
36         $_LINK['filelist'] = "$script?cmd=filelist";
37         $_LINK['freeze']   = "$script?cmd=freeze&amp;page=$r_page";
38         $_LINK['help']     = "$script?" . rawurlencode($help_page);
39         $_LINK['list']     = "$script?cmd=list";
40         $_LINK['new']      = "$script?plugin=newpage&amp;refer=$r_page";
41         $_LINK['recent']   = "$script?" . rawurlencode($whatsnew);
42         $_LINK['refer']    = "$script?plugin=referer&amp;page=$r_page";
43         $_LINK['reload']   = "$script?$r_page";
44         $_LINK['rename']   = "$script?plugin=rename&amp;refer=$r_page";
45         $_LINK['rss']      = "$script?cmd=rss10";
46         $_LINK['search']   = "$script?cmd=search";
47         $_LINK['top']      = "$script?" . rawurlencode($defaultpage);
48         if ($trackback) {
49                 $tb_id = tb_get_id($_page);
50                 $_LINK['trackback'] = "$script?plugin=tb&amp;__mode=view&amp;tb_id=$tb_id";
51         }
52         $_LINK['unfreeze'] = "$script?cmd=unfreeze&amp;page=$r_page";
53         $_LINK['upload']   = "$script?plugin=attach&amp;pcmd=upload&amp;page=$r_page";
54
55         // Compat: Skins for 1.4.4 and before
56         $link_add       = & $_LINK['add'];
57         $link_new       = & $_LINK['new'];      // New!
58         $link_edit      = & $_LINK['edit'];
59         $link_diff      = & $_LINK['diff'];
60         $link_top       = & $_LINK['top'];
61         $link_list      = & $_LINK['list'];
62         $link_filelist  = & $_LINK['filelist'];
63         $link_search    = & $_LINK['search'];
64         $link_whatsnew  = & $_LINK['recent'];
65         $link_backup    = & $_LINK['backup'];
66         $link_help      = & $_LINK['help'];
67         $link_trackback = & $_LINK['trackback'];        // New!
68         $link_rss       = & $_LINK['rss'];
69         $link_freeze    = & $_LINK['freeze'];
70         $link_unfreeze  = & $_LINK['unfreeze'];
71         $link_upload    = & $_LINK['upload'];
72         $link_template  = & $_LINK['copy'];
73         $link_refer     = & $_LINK['refer'];    // New!
74         $link_rename    = & $_LINK['rename'];
75
76         // ¥Ú¡¼¥¸¤Îɽ¼¨»þTRUE(¥Ð¥Ã¥¯¥¢¥Ã¥×¤Îɽ¼¨¡¢RecentChanges¤Îɽ¼¨¤ò½ü¤¯)
77         $is_page = (is_pagename($_page) && ! arg_check('backup') && $_page != $whatsnew);
78
79         // ¥Ú¡¼¥¸¤ÎÆɤ߽Ф·»þTRUE
80         $is_read = (arg_check('read') && is_page($_page));
81
82         // ¥Ú¡¼¥¸¤¬Åà·ë¤µ¤ì¤Æ¤¤¤ë¤È¤­TRUE
83         $is_freeze = is_freeze($_page);
84
85         // ¥Ú¡¼¥¸¤ÎºÇ½ª¹¹¿·»þ¹ï(ʸ»úÎó)
86         $lastmodified = $is_read ?  get_date('D, d M Y H:i:s T', get_filetime($_page)) .
87                 ' ' . get_pg_passage($_page, FALSE) : '';
88
89         // ´ØÏ¢¤¹¤ë¥Ú¡¼¥¸¤Î¥ê¥¹¥È
90         $related = ($is_read && $related_link) ? make_related($_page) : '';
91
92         // ÅºÉÕ¥Õ¥¡¥¤¥ë¤Î¥ê¥¹¥È
93         $attaches = ($is_read && exist_plugin_action('attach')) ? attach_filelist() : '';
94
95         // Ãí¼á¤Î¥ê¥¹¥È
96         ksort($foot_explain, SORT_NUMERIC);
97         $notes = ! empty($foot_explain) ? $note_hr . join("\n", $foot_explain) : '';
98
99         // <head>Æâ¤ËÄɲ乤륿¥°
100         $head_tag = ! empty($head_tags) ? join("\n", $head_tags) ."\n" : '';
101
102         // 1.3.x compat
103         // ¥Ú¡¼¥¸¤ÎºÇ½ª¹¹¿·»þ¹ï(UNIX timestamp)
104         $fmt = $is_read ? get_filetime($_page) + LOCALZONE : 0;
105
106         //ñ¸ì¸¡º÷
107         if ($search_word_color && isset($vars['word'])) {
108                 $body = '<div class="small">' . $_msg_word . htmlspecialchars($vars['word']) .
109                         "</div>$hr\n$body";
110                 $words = array_flip(array_splice(
111                         preg_split('/\s+/', $vars['word'], -1, PREG_SPLIT_NO_EMPTY),
112                         0, 10));
113                 $keys = array();
114                 foreach ($words as $word=>$id) {
115                         $keys[$word] = strlen($word);
116                 }
117                 arsort($keys, SORT_NUMERIC);
118                 $keys = get_search_words(array_keys($keys), TRUE);
119                 $id = 0;
120                 foreach ($keys as $key=>$pattern)
121                 {
122                         $s_key    = htmlspecialchars($key);
123                         $pattern  = "/<[^>]*>|($pattern)|&[^;]+;/";
124                         $callback = create_function(
125                                 '$arr',
126                                 'return (count($arr) > 1) ? "<strong class=\"word' . $id++ . '\">{$arr[1]}</strong>" : $arr[0];'
127                         );
128                         $body  = preg_replace_callback($pattern, $callback, $body);
129                         $notes = preg_replace_callback($pattern, $callback, $notes);
130                 }
131         }
132
133         $longtaketime = getmicrotime() - MUTIME;
134         $taketime     = sprintf('%01.03f', $longtaketime);
135
136         require(SKIN_FILE);
137 }
138
139 // ¥¤¥ó¥é¥¤¥óÍ×ÁǤΥѡ¼¥¹ (obsolete)
140 function inline($line, $remove = FALSE)
141 {
142         global $NotePattern;
143
144         $line = htmlspecialchars($line);
145         if ($remove) 
146                 $line = preg_replace($NotePattern, '', $line);
147
148         return $line;
149 }
150
151 // ¥¤¥ó¥é¥¤¥óÍ×ÁǤΥѡ¼¥¹ (¥ê¥ó¥¯¡¢¸«½Ð¤·°ìÍ÷) (obsolete)
152 function inline2($str)
153 {
154         return make_link($str);
155 }
156
157 // ÊÔ½¸¥Õ¥©¡¼¥à¤Îɽ¼¨
158 function edit_form($page, $postdata, $digest = 0, $b_template = TRUE)
159 {
160         global $script, $vars, $rows, $cols, $hr, $function_freeze;
161         global $_btn_addtop, $_btn_preview, $_btn_repreview, $_btn_update, $_btn_cancel,
162                 $_btn_freeze, $_msg_help, $_btn_notchangetimestamp;
163         global $whatsnew, $_btn_template, $_btn_load, $non_list, $load_template_func;
164
165         $refer = $template = $addtag = $add_top = '';
166
167         if ($digest == 0) $digest = md5(join('', get_source($page)));
168
169         $checked_top  = isset($vars['add_top'])     ? ' checked="checked"' : '';
170         $checked_time = isset($vars['notimestamp']) ? ' checked="checked"' : '';
171
172         if(isset($vars['add'])) {
173                 $addtag  = '<input type="hidden" name="add" value="true" />';
174                 $add_top = "<input type=\"checkbox\" name=\"add_top\" value=\"true\"$checked_top /><span class=\"small\">$_btn_addtop</span>";
175         }
176
177         if($load_template_func && $b_template) {
178                 $_pages = get_existpages();
179                 $pages  = array();
180                 foreach($_pages as $_page) {
181                         if ($_page == $whatsnew || preg_match("/$non_list/", $_page))
182                                 continue;
183                         $s_page = htmlspecialchars($_page);
184                         $pages[$_page] = "   <option value=\"$s_page\">$s_page</option>";
185                 }
186                 ksort($pages);
187                 $s_pages  = join("\n", $pages);
188                 $template = <<<EOD
189   <select name="template_page">
190    <option value="">-- $_btn_template --</option>
191 $s_pages
192   </select>
193   <input type="submit" name="template" value="$_btn_load" accesskey="r" />
194   <br />
195 EOD;
196
197                 if (isset($vars['refer']) && $vars['refer'] != '')
198                         $refer = '[[' . strip_bracket($vars['refer']) ."]]\n\n";
199         }
200
201         $r_page      = rawurlencode($page);
202         $s_page      = htmlspecialchars($page);
203         $s_digest    = htmlspecialchars($digest);
204         $s_postdata  = htmlspecialchars($refer . $postdata);
205         $s_original  = isset($vars['original']) ? htmlspecialchars($vars['original']) : $s_postdata;
206         $b_preview   = isset($vars['preview']); // ¥×¥ì¥Ó¥å¡¼ÃæTRUE
207         $btn_preview = $b_preview ? $_btn_repreview : $_btn_preview;
208
209         $body = <<<EOD
210 <form action="$script" method="post">
211  <div class="edit_form">
212 $template
213   $addtag
214   <input type="hidden" name="cmd"    value="edit" />
215   <input type="hidden" name="page"   value="$s_page" />
216   <input type="hidden" name="digest" value="$s_digest" />
217   <textarea name="msg" rows="$rows" cols="$cols">$s_postdata</textarea>
218   <br />
219   <input type="submit" name="preview" value="$btn_preview" accesskey="p" />
220   <input type="submit" name="write"   value="$_btn_update" accesskey="s" />
221   $add_top
222   <input type="checkbox" name="notimestamp" value="true"$checked_time />
223   <span style="small">$_btn_notchangetimestamp</span> &nbsp;
224   <input type="submit" name="cancel"  value="$_btn_cancel" accesskey="c" />
225   <textarea name="original" rows="1" cols="1" style="display:none">$s_original</textarea>
226  </div>
227 </form>
228 EOD;
229
230         if (isset($vars['help'])) {
231                 $body .= $hr . catrule();
232         } else {
233                 $body .=
234                 "<ul><li><a href=\"$script?cmd=edit&amp;help=true&amp;page=$r_page\">$_msg_help</a></li></ul>";
235         }
236
237         return $body;
238 }
239
240 // ´ØÏ¢¤¹¤ë¥Ú¡¼¥¸
241 function make_related($page, $tag = '')
242 {
243         global $script, $vars, $related, $rule_related_str, $related_str, $non_list;
244         global $_ul_left_margin, $_ul_margin, $_list_pad_str;
245
246         $links = links_get_related($page);
247
248         if ($tag) {
249                 ksort($links);
250         } else {
251                 arsort($links);
252         }
253
254         $_links = array();
255         foreach ($links as $page=>$lastmod) {
256                 if (preg_match("/$non_list/", $page)) continue;
257
258                 $r_page   = rawurlencode($page);
259                 $s_page   = htmlspecialchars($page);
260                 $passage  = get_passage($lastmod);
261                 $_links[] = $tag ?
262                         "<a href=\"$script?$r_page\" title=\"$s_page $passage\">$s_page</a>" :
263                         "<a href=\"$script?$r_page\">$s_page</a>$passage";
264         }
265
266         if (empty($_links)) return '';
267
268         if ($tag == 'p') { // ¹ÔƬ¤«¤é
269                 $margin = $_ul_left_margin + $_ul_margin;
270                 $style  = sprintf($_list_pad_str, 1, $margin, $margin);
271                 $retval =  "\n<ul$style>\n<li>" . join($rule_related_str, $_links) . "</li>\n</ul>\n";
272         } else if ($tag) {
273                 $retval = join($rule_related_str, $_links);
274         } else {
275                 $retval = join($related_str, $_links);
276         }
277
278         return $retval;
279 }
280
281 // ¥æ¡¼¥¶ÄêµÁ¥ë¡¼¥ë(¥½¡¼¥¹¤ÏÃÖ´¹¤»¤º¥³¥ó¥Ð¡¼¥È)
282 function make_line_rules($str)
283 {
284         global $line_rules;
285         static $pattern, $replace;
286
287         if (! isset($pattern)) {
288                 $pattern = array_map(create_function('$a', 'return "/$a/";'), array_keys($line_rules));
289                 $replace = array_values($line_rules);
290                 unset($line_rules);
291         }
292
293         return preg_replace($pattern, $replace, $str);
294 }
295
296 // HTML¥¿¥°¤ò¼è¤ê½ü¤¯
297 function strip_htmltag($str)
298 {
299         global $_symbol_noexists;
300
301         $noexists_pattern = '#<span class="noexists">([^<]*)<a[^>]+>' .
302                 preg_quote($_symbol_noexists, '#') . '</a></span>#';
303
304         $str = preg_replace($noexists_pattern, '$1', $str);
305         //$str = preg_replace('/<a[^>]+>\?<\/a>/', '', $str);
306         return preg_replace('/<[^>]+>/', '', $str);
307 }
308
309 // ¥Ú¡¼¥¸Ì¾¤«¤é¥Ú¡¼¥¸Ì¾¤ò¸¡º÷¤¹¤ë¥ê¥ó¥¯¤òºîÀ®
310 function make_search($page)
311 {
312         global $script, $WikiName;
313
314         $s_page = htmlspecialchars($page);
315         $r_page = rawurlencode($page);
316
317         //WikiWikiWeb like...
318         //if(preg_match("/^$WikiName$/", $page))
319         //      $name = preg_replace("/([A-Z][a-z]+)/", "$1 ", $name);
320
321         return "<a href=\"$script?cmd=search&amp;word=$r_page\">$s_page</a> ";
322 }
323
324 // ¸«½Ð¤·¤òÀ¸À® (Ãí¼á¤äHTML¥¿¥°¤ò½üµî)
325 function make_heading(& $str, $strip = TRUE)
326 {
327         global $NotePattern;
328
329         // ¸«½Ð¤·¤Î¸ÇÍ­IDÉô¤òºï½ü
330         $id = '';
331         if (preg_match('/^(\*{0,3})(.*?)\[#([A-Za-z][\w-]+)\](.*?)$/m', $str, $matches)) {
332                 $str = $matches[2] . $matches[4];
333                 $id  = $matches[3];
334         } else {
335                 $str = preg_replace('/^\*{0,3}/', '', $str);
336         }
337
338         if ($strip === TRUE)
339                 $str = strip_htmltag(make_link(preg_replace($NotePattern, '', $str)));
340
341         return $id;
342 }
343 ?>