OSDN Git Service

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