OSDN Git Service

[[pukiwiki:PukiWiki/1.3.2]] 過去のBugTrackの修正ミス
[pukiwiki/pukiwiki.git] / html.php
1 <?
2 // PukiWiki - Yet another WikiWikiWeb clone.
3 // $Id: html.php,v 1.23 2002/07/16 14:07:13 masui Exp $
4 /////////////////////////////////////////////////
5
6 // ËÜʸ¤ò¥Ú¡¼¥¸Ì¾¤«¤é½ÐÎÏ
7 function catbodyall($page,$title="",$pg="")
8 {
9         if($title === "") $title = strip_bracket($page);
10         if($pg === "") $pg = make_search($page);
11
12         $body = join("",get_source($page));
13         $body = convert_html($body);
14
15         header_lastmod($vars["page"]);
16         catbody($title,$pg,$body);
17         die();
18 }
19
20 // ËÜʸ¤ò½ÐÎÏ
21 function catbody($title,$page,$body)
22 {
23         global $script,$vars,$arg,$do_backup,$modifier,$modifierlink,$defaultpage,$whatsnew,$hr;
24         global $date_format,$weeklabels,$time_format,$longtaketime,$related_link;
25         global $HTTP_SERVER_VARS,$cantedit;
26
27         if($vars["page"] && !arg_check("backup") && $vars["page"] != $whatsnew)
28         {
29                 $is_page = 1;
30         }
31
32         $link_add = "$script?cmd=add&amp;page=".rawurlencode($vars["page"]);
33         $link_edit = "$script?cmd=edit&amp;page=".rawurlencode($vars["page"]);
34         $link_diff = "$script?cmd=diff&amp;page=".rawurlencode($vars["page"]);
35         $link_top = "$script?$defaultpage";
36         $link_list = "$script?cmd=list";
37         $link_filelist = "$script?cmd=filelist";
38         $link_search = "$script?cmd=search";
39         $link_whatsnew = "$script?$whatsnew";
40         $link_backup = "$script?cmd=backup&amp;page=".rawurlencode($vars["page"]);
41         $link_help = "$script?cmd=help";
42
43         if(is_page($vars["page"]) && $is_page)
44         {
45                 $fmt = @filemtime(get_filename(encode($vars["page"])));
46         }
47
48         if(is_page($vars["page"]) && $related_link && $is_page && !arg_check("edit") && !arg_check("freeze") && !arg_check("unfreeze"))
49         {
50                 $related = make_related($vars["page"],FALSE);
51         }
52
53         if(is_page($vars["page"]) && !in_array($vars["page"],$cantedit) && !arg_check("backup") && !arg_check("edit") && !$vars["preview"])
54         {
55                 $is_read = TRUE;
56         }
57
58         //if(!$longtaketime)
59                 $longtaketime = getmicrotime() - MUTIME;
60         $taketime = sprintf("%01.03f",$longtaketime);
61
62         require(SKIN_FILE);
63 }
64
65 // ¥Æ¥­¥¹¥ÈËÜÂΤòHTML¤ËÊÑ´¹¤¹¤ë
66 function convert_html($string)
67 {
68         global $result,$saved,$hr,$script,$page,$vars,$top;
69         global $note_id,$foot_explain,$digest,$note_hr;
70         global $user_rules,$str_rules,$line_rules,$strip_link_wall;
71         global $InterWikiName, $BracketName;
72
73         global $longtaketime;
74
75         $string = rtrim($string);
76         $string = preg_replace("/((\x0D\x0A)|(\x0D)|(\x0A))/","\n",$string);
77
78         $start_mtime = getmicrotime();
79
80         $digest = md5(@join("",get_source($vars["page"])));
81
82         $content_id = 0;
83         $user_rules = array_merge($str_rules,$line_rules);
84
85         $result = array();
86         $saved = array();
87         $arycontents = array();
88
89         $string = preg_replace("/^#freeze\n/","",$string);
90
91         $lines = split("\n", $string);
92         $note_id = 1;
93         $foot_explain = array();
94         // ³Æ¹Ô¤Î¹ÔƬ½ñ¼°¤ò³ÊǼ
95         $headform = array();
96         // ¸½ºß¤Î¹Ô¿ô¤òÆþ¤ì¤Æ¤ª¤³¤¦
97         $_cnt = 0;
98         // ¥Ö¥í¥Ã¥¯¤ÎȽÄê¥Õ¥é¥°
99         $_p = FALSE;
100         $_bq = FALSE;
101
102         $table = 0;
103
104         if(preg_match("/#contents/",$string))
105                 $top_link = "<a href=\"#contents\">$top</a>";
106
107         foreach ($lines as $line)
108         {
109                 if(!preg_match("/^\/\/(.*)/",$line,$comment_out) && $table != 0)
110                 {
111                         if(!preg_match("/^\|(.+)\|$/",$line,$out))
112                                 array_push($result, "</table></div>");
113                         if(!$out[1] || $table != count(explode("|",$out[1])))
114                                 $table = 0;
115                 }
116
117                 $comment_out = $comment_out[1];
118
119                 // ¹ÔƬ½ñ¼°¤«¤É¤¦¤«¤ÎȽÄê
120                 $line_head = substr($line,0,1);
121                 if(     $line_head == ' ' || 
122                         $line_head == ':' || 
123                         $line_head == '>' || 
124                         $line_head == '-' || 
125                         $line_head == '|' || 
126                         $line_head == '*' || 
127                         $line_head == '#' || 
128                         $line_head == '/'
129                 ) {
130                         if($headform[$_cnt-1] == '' && $_p){
131                                 array_push($result, "</p>");
132                                 $_p = FALSE;
133                         }
134                         if($line_head != '>' && $_bq){
135                                 array_push($result, "</p>");
136                                 $_bq = FALSE;
137                         }
138
139                         if(preg_match("/^\#([^\(]+)(.*)$/",$line,$out)){
140                                 if(exist_plugin_convert($out[1])) {
141                                         $result = array_merge($result,$saved); $saved = array();
142                                         
143                                         if($out[2]) {
144                                                 $_plugin = preg_replace("/^\#([^\(]+)\((.*)\)$/ex","do_plugin_convert('$1','$2')",$line);
145                                         } else {
146                                                 $_plugin = preg_replace("/^\#([^\(]+)$/ex","do_plugin_convert('$1','$2')",$line);
147                                         }
148                                         // ÀèƬ¤Ë¶õÇò¤òÆþ¤ì¤ë¤³¤È¤Ë¤è¤ê¤È¤ê¤¢¤¨¤ºpre¤Î°·¤¤¤ÈƱÍͤËinline2¤ÎƯ¤­¤òÍÞ¤¨¤ë¡¢¤¦¡Á¤ó¡¢ÌµÃã¡£
149                                         array_push($result,"\t$_plugin");
150                                 } else {
151                                         array_push($result, htmlspecialchars($line));
152                                 }
153                         }
154                         else if(preg_match("/^(\*{1,3})(.*)/",$line,$out))
155                         {
156                                 $result = array_merge($result,$saved); $saved = array();
157                                 $headform[$_cnt] = $out[1];
158                                 $str = inline($out[2]);
159                                 
160                                 $level = strlen($out[1]) + 1;
161
162                                 array_push($result, "<h$level><a name=\"content:$content_id\"></a>$str $top_link</h$level>");
163                                 $arycontents[] = str_repeat("-",$level-1)."<a href=\"#content:$content_id\">".strip_htmltag(make_user_rules($str))."</a>\n";
164                                 $content_id++;
165                         }
166                         else if(preg_match("/^(-{1,4})(.*)/",$line,$out))
167                         {
168                                 $headform[$_cnt] = $out[1];
169                                 if(strlen($out[1]) == 4)
170                                 {
171                                         $result = array_merge($result,$saved); $saved = array();
172                                         array_push($result, $hr);
173                                 }
174                                 else
175                                 {
176                                         back_push('ul', strlen($out[1]));
177                                         array_push($result, '<li>' . inline($out[2]) . '</li>');
178                                 }
179                         }
180                         else if (preg_match("/^:([^:]+):(.*)/",$line,$out))
181                         {
182                                 $headform[$_cnt] = ':'.$out[1].':';
183                                 back_push('dl', 1);
184                                 array_push($result, '<dt>' . inline($out[1]) . '</dt>', '<dd>' . inline($out[2]) . '</dd>');
185                         }
186                         else if(preg_match("/^(>{1,3})(.*)/",$line,$out))
187                         {
188                                 $headform[$_cnt] = $out[1];
189                                 back_push('blockquote', strlen($out[1]));
190                                 // ¤³¤³¤Î¤¢¤¿¤ê¤Ç¼«Á°¤Çback_push¤«¤±¤Æ¤ë´¶¤¸¡£ÌµÃã¶ìÃã¡Ä
191                                 if($headform[$_cnt-1] != $headform[$_cnt] ) {
192                                         if(!$_bq) {
193                                                 array_push($result, "<p class=\"quotation\">");
194                                                 $_bq = TRUE;
195                                         }
196                                         else if(substr($headform[$_cnt-1],0,1) == '>'){
197                                                 $_level_diff = abs( strlen($out[1]) - strlen($headform[$_cnt-1]) );
198                                                 if( $_level_diff == 1 ){
199                                                         $i = array_pop($result);
200                                                         array_push($result, "</p>");
201                                                         array_push($result,$i);
202                                                         array_push($result, "<p class=\"quotation\">");
203                                                         $_bq = TRUE;
204                                                 } else {
205                                                         $i = array();
206                                                         $i[] = array_pop($result);
207                                                         $i[] = array_pop($result);
208                                                         array_push($result, "</p>");
209                                                         $result = array_merge($result,$i);
210                                                         array_push($result, "<p class=\"quotation\">");
211                                                         $_bq = TRUE;
212                                                 }
213                                         }
214                                 }
215                                 array_push($result, ltrim(inline($out[2])));
216                         }
217                         else if(preg_match("/^(\s+.*)/",$line,$out))
218                         {
219                                 $headform[$_cnt] = ' ';
220                                 back_push('pre', 1);
221                                 array_push($result, htmlspecialchars($out[1],ENT_NOQUOTES));
222                         }
223                         else if(preg_match("/^\|(.+)\|$/",$line,$out))
224                         {
225                                 $headform[$_cnt] = '|';
226                                 $arytable = explode("|",$out[1]);
227
228                                 if(!$table)
229                                 {
230                                         $result = array_merge($result,$saved); $saved = array();
231                                         array_push($result,"<div class=\"ie5\"><table class=\"style_table\" cellspacing=\"1\" border=\"0\">");
232                                         $table = count($arytable);
233                                 }
234
235                                 array_push($result,"<tr>");
236                                 foreach($arytable as $td)
237                                 {
238                                         array_push($result,"<td class=\"style_td\">");
239                                         array_push($result,ltrim(inline($td)));
240                                         array_push($result,"</td>");
241                                 }
242                                 array_push($result,"</tr>");
243
244                         }
245                         else if(strlen($comment_out) != 0)
246                         {
247                                 $headform[$_cnt] = '//';
248                                 array_push($result," <!-- ".htmlspecialchars($comment_out)." -->");
249                         }
250
251                 } else {
252
253                         $headform[$_cnt] = '';
254                         if($headform[$_cnt-1] != $headform[$_cnt]){
255                                 if(array_values($saved)){
256                                         if( $_bq ){
257                                                 array_unshift($saved, "</p>");
258                                                 $_bq = FALSE;
259                                         }
260                                         $i = array_pop($saved);
261                                         array_push($saved,$i);
262                                         $result = array_merge($result,$saved); $saved = array();
263                                 }
264                                 if( substr($line,0,1) == '' && !$_p){
265                                         array_push($result, "<p>");
266                                         $_p = TRUE;
267                                 }
268                                 else if( substr($line,0,1) != '' && $_p){
269                                         array_push($result, "</p>");
270                                         $_p = FALSE;
271                                 }
272                         }
273                         
274                         if( substr($line,0,1) == '' && $_p){
275                                 $_tmp = array_pop($result);
276                                 if($_tmp == "<p>") {
277                                         $_tmp = '<p class="empty">';
278                                 }
279                                 array_push($result, $_tmp, "</p>");
280                                 $_p = FALSE;
281                         }
282                         else if( substr($line,0,1) != '' && !$_p) {
283                                 array_push($result, "<p>");
284                                 $_p = TRUE;
285                         }
286                         if( substr($line,0,1) != '' ){
287                                 array_push($result, inline($line));
288                         }
289
290                 }
291
292                 $_cnt++;
293         }
294
295         if($_p) array_push($result, "</p>");
296         if($_bq) {
297                 array_push($result, "</p>");
298                 array_push($result, "</blockquote>");
299                 $saved = array();
300         }
301         if($table) array_push($result, "</table></div>");
302         
303         $result_last = $result = array_merge($result,$saved); $saved = array();
304
305         if($content_id != 0)
306         {
307                 $result = array();
308                 $saved = array();
309
310                 foreach($arycontents as $line)
311                 {
312                         if(preg_match("/^(-{1,3})(.*)/",$line,$out))
313                         {
314                                 back_push('ul', strlen($out[1]));
315                                 array_push($result, '<li>'.$out[2].'</li>');
316                         }
317                 }
318                 $result = array_merge($result,$saved); $saved = array();
319                 
320                 $contents = "<a name=\"contents\"></a>\n";
321                 $contents .= join("\n",$result);
322                 if($strip_link_wall)
323                 {
324                         $contents = preg_replace("/\[\[([^\]:]+):(.+)\]\]/","$1",$contents);
325                         $contents = preg_replace("/\[\[([^\]]+)\]\]/","$1",$contents);
326                 }
327         }
328
329         $result_last = inline2($result_last);
330         
331         $result_last = preg_replace("/^#contents/",$contents,$result_last);
332
333         $str = join("\n", $result_last);
334
335         if($foot_explain)
336         {
337                 $str .= "\n";
338                 $str .= "$note_hr\n";
339                 $str .= join("\n",inline2($foot_explain));
340         }
341
342         $longtaketime = getmicrotime() - $start_mtime;
343
344 #       $str = preg_replace("/&((amp)|(quot)|(nbsp)|(lt)|(gt));/","&$1;",$str);
345
346         return $str;
347 }
348
349 // $tag¤Î¥¿¥°¤ò$level¥ì¥Ù¥ë¤Þ¤ÇµÍ¤á¤ë¡£
350 function back_push($tag, $level)
351 {
352         global $result,$saved;
353         
354         while (count($saved) > $level) {
355                 array_push($result, array_shift($saved));
356         }
357         if ($saved[0] != "</$tag>") {
358                 $result = array_merge($result,$saved); $saved = array();
359         }
360         while (count($saved) < $level) {
361                 array_unshift($saved, "</$tag>");
362                 array_push($result, "<$tag>");
363         }
364 }
365
366 // ¥¤¥ó¥é¥¤¥óÍ×ÁǤΥѡ¼¥¹ (Ãí¼á)
367 function inline($line)
368 {
369         $line = htmlspecialchars($line);
370
371         $line = preg_replace("/\(\(((?:(?!\)\)).)*)\)\)/ex","make_note(\"$1\")",$line);
372
373         return $line;
374 }
375
376 // ¥¤¥ó¥é¥¤¥óÍ×ÁǤΥѡ¼¥¹ (¥ê¥ó¥¯¡¢´ØÏ¢°ìÍ÷¡¢¸«½Ð¤·°ìÍ÷)
377 function inline2($str)
378 {
379         global $WikiName,$BracketName,$InterWikiName,$vars,$related,$related_link,$script;
380         $cnts_plain = array();
381         $arykeep = array();
382
383         for($cnt=0;$cnt<count($str);$cnt++)
384         {
385                 if(preg_match("/^(\s)/",$str[$cnt]))
386                 {
387                         $arykeep[$cnt] = $str[$cnt];
388                         $str[$cnt] = "";
389                         $cnts_plain[] = $cnt;
390                 }
391         }
392
393         $str = preg_replace("/
394                 (
395                         (\[\[([^\]]+)\:(https?|ftp|news)(:\/\/[-_.!~*'()a-zA-Z0-9;\/?:\@&=+\$,%#]+)\]\])
396                         |
397                         (\[(https?|ftp|news)(:\/\/[-_.!~*'()a-zA-Z0-9;\/?:\@&=+\$,%#]+)\s([^\]]+)\])
398                         |
399                         (https?|ftp|news)(:\/\/[-_.!~*'()a-zA-Z0-9;\/?:\@&=+\$,%#]+)
400                         |
401                         ([[:alnum:]\-_.]+@[[:alnum:]\-_]+\.[[:alnum:]\-_\.]+)
402                         |
403                         (\[\[([^\]]+)\:([[:alnum:]\-_.]+@[[:alnum:]\-_]+\.[[:alnum:]\-_\.]+)\]\])
404                         |
405                         ($InterWikiName)
406                         |
407                         ($BracketName)
408                         |
409                         ($WikiName)
410                 )/ex","make_link('$1')",$str);
411
412         $str = preg_replace("/#related/",make_related($vars["page"],TRUE),$str);
413
414         $str = make_user_rules($str);
415
416         $tmp = $str;
417         $str = preg_replace("/^#norelated$/","",$str);
418         if($tmp != $str)
419                 $related_link = 0;
420
421         foreach($cnts_plain as $cnt)
422                 $str[$cnt] = $arykeep[$cnt];
423
424         return $str;
425 }
426
427 // °ìÍ÷¤Î¼èÆÀ
428 function get_list($withfilename)
429 {
430         global $script,$list_index,$top,$non_list,$whatsnew;
431         global $_msg_symbol,$_msg_other;
432         
433         $retval = array();
434         $files = get_existpages();
435         foreach($files as $page) {
436                 if(preg_match("/$non_list/",$page) && !$withfilename) continue;
437                 if($page == $whatsnew) continue;
438                 $page_url = rawurlencode($page);
439                 $page2 = strip_bracket($page);
440                 $pg_passage = get_pg_passage($page);
441                 $file = encode($page).".txt";
442                 $retval[$page2] .= "<li><a href=\"$script?$page_url\">".htmlspecialchars($page2,ENT_QUOTES)."</a>$pg_passage</li>\n";
443                 if($withfilename)
444                 {
445                         $retval[$page2] .= "<ul><li>$file</li></ul>\n";
446                 }
447         }
448         
449         $retval = list_sort($retval);
450         
451         if($list_index)
452         {
453                 $head_str = "";
454                 $etc_sw = 0;
455                 $symbol_sw = 0;
456                 $top_link = "";
457                 $link_counter = 0;
458                 foreach($retval as $page => $link)
459                 {
460                         $head = substr($page,0,1);
461                         if($head_str != $head && !$etc_sw)
462                         {
463                                 $retval2[$page] = "";
464                                 
465                                 if(preg_match("/([A-Z])|([a-z])/",$head,$match))
466                                 {
467                                         if($match[1])
468                                                 $head_nm = "High:$head";
469                                         else
470                                                 $head_nm = "Low:$head";
471                                         
472                                         if($head_str) $retval2[$page] = "</ul>\n";
473                                         $retval2[$page] .= "<li><a href=\"#top:$head_nm\" name=\"$head_nm\"><strong>$head</strong></a></li>\n<ul>\n";
474                                         $head_str = $head;
475                                         if($link_counter) $top_link .= "|";
476                                         $link_counter = $link_counter + 1;
477                                         $top_link .= "<a href=\"#$head_nm\" name=\"top:$head_nm\"><strong>&nbsp;".$head."&nbsp;</strong></a>";
478                                         if($link_counter==16) {
479                                                 $top_link .= "<br />";
480                                                 $link_counter = 0;
481                                         }
482                                 }
483                                 else if(preg_match("/[ -~]/",$head))
484                                 {
485                                         if(!$symbol_sw)
486                                         {
487                                                 if($head_str) $retval2[$page] = "</ul>\n";
488                                                 $retval2[$page] .= "<li><a href=\"#top:symbol\" name=\"symbol\"><strong>$_msg_symbol</strong></a></li>\n<ul>\n";
489                                                 $head_str = $head;
490                                                 if($link_counter) $top_link .= "|";
491                                                 $link_counter = $link_counter + 1;
492                                                 $top_link .= "<a href=\"#symbol\" name=\"top:symbol\"><strong>$_msg_symbol</strong></a>";
493                                                 $symbol_sw = 1;
494                                         }
495                                 }
496                                 else
497                                 {
498                                         if($head_str) $retval2[$page] = "</ul>\n";
499                                         $retval2[$page] .= "<li><a href=\"#top:etc\" name=\"etc\"><strong>$_msg_other</strong></a></li>\n<ul>\n";
500                                         $etc_sw = 1;
501                                         if($link_counter) $top_link .= "|";
502                                         $link_counter = $link_counter + 1;
503                                         $top_link .= "<a href=\"#etc\" name=\"top:etc\"><strong>$_msg_other</strong></a>";
504                                 }
505                         }
506                         $retval2[$page] .= $link;
507                 }
508                 $retval2[] = "</ul>\n";
509                 
510                 $top_link = "<div style=\"text-align:center\"><a name=\"top\">$top_link</a></div><br />\n";
511                 
512                 array_unshift($retval2,$top_link);
513         }
514         else
515         {
516                 $retval2 = $retval;
517         }
518         
519         return join("",$retval2);
520 }
521
522 // ÊÔ½¸¥Õ¥©¡¼¥à¤Îɽ¼¨
523 function edit_form($postdata,$page,$add=0)
524 {
525         global $script,$rows,$cols,$hr,$vars,$function_freeze;
526         global $_btn_addtop,$_btn_preview,$_btn_update,$_btn_freeze,$_msg_help,$_btn_notchangetimestamp;
527         global $whatsnew,$_btn_template,$_btn_load,$non_list,$load_template_func;
528
529         $digest = md5(@join("",get_source($page)));
530
531         if($add)
532         {
533                 $addtag = '<input type="hidden" name="add" value="true" />';
534                 $add_top = '<input type="checkbox" name="add_top" value="true" /><span class="small">'.$_btn_addtop.'</span>';
535         }
536
537         if($vars["help"] == "true")
538                 $help = $hr.catrule();
539         else
540                 $help = "<br />\n<ul><li><a href=\"$script?cmd=edit&amp;help=true&amp;page=".rawurlencode($page)."\">$_msg_help</a></ul></li>\n";
541
542         if($function_freeze)
543                 $str_freeze = '<input type="submit" name="freeze" value="'.$_btn_freeze.'" accesskey="f" />';
544
545         if($load_template_func)
546         {
547                 $vals = array();
548
549                 $files = get_existpages();
550                 foreach($files as $pg_org) {
551                         if($pg_org == $whatsnew) continue;
552                         if(preg_match("/$non_list/",$pg_org)) continue;
553                         $name = strip_bracket($pg_org);
554                         $vals[$name] = "    <option value=\"$pg_org\">$name</option>";
555                 }
556                 @ksort($vals);
557                 
558                 $template = "   <select name=\"template_page\">\n"
559                            ."    <option value=\"\">-- $_btn_template --</option>\n"
560                            .join("\n",$vals)
561                            ."   </select>\n"
562                            ."   <input type=\"submit\" name=\"template\" value=\"$_btn_load\" accesskey=\"r\" /><br />\n";
563
564                 if($vars["refer"]) $refer = $vars["refer"]."\n\n";
565         }
566
567 return '
568 <form action="'.$script.'" method="post">
569 '.$addtag.'
570 <table cellspacing="3" cellpadding="0" border="0">
571  <tr>
572   <td align="right">
573 '.$template.'
574   </td>
575  </tr>
576  <tr>
577   <td align="right">
578    <input type="hidden" name="page" value="'.htmlspecialchars($page).'" />
579    <input type="hidden" name="digest" value="'.htmlspecialchars($digest).'" />
580    <textarea name="msg" rows="'.$rows.'" cols="'.$cols.'" wrap="virtual">
581 '.htmlspecialchars($refer.$postdata).'</textarea>
582   </td>
583  </tr>
584  <tr>
585   <td>
586    <input type="submit" name="preview" value="'.$_btn_preview.'" accesskey="p" />
587    <input type="submit" name="write" value="'.$_btn_update.'" accesskey="s" />
588    '.$add_top.'
589    <input type="checkbox" name="notimestamp" value="true" /><span style="small">'.$_btn_notchangetimestamp.'</span>
590   </td>
591  </tr>
592 </table>
593 </form>
594
595 <form action="'.$script.'?cmd=freeze" method="post">
596 <div>
597 <input type="hidden" name="page" value="'.htmlspecialchars($vars["page"]).'" />
598 '.$str_freeze.'
599 </div>
600 </form>
601
602 ' . $help;
603 }
604
605 // ´ØÏ¢¤¹¤ë¥Ú¡¼¥¸
606 function make_related($page,$_isrule)
607 {
608         global $related_str,$rule_related_str,$related,$_make_related,$vars;
609
610         $page_name = strip_bracket($vars["page"]);
611
612         if(!is_array($_make_related))
613         {
614                 $aryrelated = do_search($page,"OR",1);
615
616                 if(is_array($aryrelated))
617                 {
618                         foreach($aryrelated as $key => $val)
619                         {
620                                 $new_arylerated[$key.md5($val)] = $val;
621                         }
622                 }
623
624                 if(is_array($related))
625                 {
626                         foreach($related as $key => $val)
627                         {
628                                 $new_arylerated[$key.md5($val)] = $val;
629                         }
630                 }
631
632                 @krsort($new_arylerated);
633                 $_make_related = @array_unique($new_arylerated);
634         }
635
636         if($_isrule)
637         {
638                 if(is_array($_make_related))
639                 {
640                         foreach($_make_related as $str)
641                         {
642                                 preg_match("/<a\shref=\"([^\"]+)\">([^<]+)<\/a>(.*)/",$str,$out);
643                                 
644                                 if($out[3]) $title = " title=\"$out[2] $out[3]\"";
645                                 
646                                 $aryret[$out[2]] = "<a href=\"$out[1]\"$title>$out[2]</a>";
647                         }
648                         @ksort($aryret);
649                 }
650         }
651         else
652         {
653                 $aryret = $_make_related;
654         }
655
656         if($_isrule) $str = $rule_related_str;
657         else         $str = $related_str;
658
659         return @join($str,$aryret);
660 }
661
662 // Ãí¼á½èÍý
663 function make_note($str)
664 {
665         global $note_id,$foot_explain;
666
667         $str = preg_replace("/^\(\(/","",$str);
668         $str = preg_replace("/\)\)$/","",$str);
669
670         $str= str_replace("\\'","'",$str);
671
672         $str = make_user_rules($str);
673
674         $foot_explain[] = "<a name=\"notefoot:$note_id\" href=\"#notetext:$note_id\" class=\"note_super\">*$note_id</a> <span class=\"small\">$str</span><br />\n";
675         $note =  "<a name=\"notetext:$note_id\" href=\"#notefoot:$note_id\" class=\"note_super\">*$note_id</a>";
676         $note_id++;
677
678         return $note;
679 }
680
681 // ¥ê¥ó¥¯¤òÉղ乤ë
682 function make_link($name)
683 {
684         global $BracketName,$WikiName,$InterWikiName,$InterWikiNameNoBracket,$script,$link_target,$interwiki_target;
685         global $related,$show_passage,$vars,$defaultpage;
686
687         $aryconv_htmlspecial = array("&amp;","&lt;","&gt;");
688         $aryconv_html = array("&","<",">");
689
690         $page = $name;
691
692         if(preg_match("/^\[\[([^\]]+)\:((https?|ftp|news)([^\]]+))\]\]$/",$name,$match))
693         {
694                 return "<a href=\"$match[2]\" target=\"$link_target\">$match[1]</a>";
695         }
696         else if(preg_match("/^\[((https?|ftp|news)([^\]\s]+))\s([^\]]+)\]$/",$name,$match))
697         {
698                 return "<a href=\"$match[1]\" target=\"$link_target\">$match[4]</a>";
699         }
700         else if(preg_match("/^(https?|ftp|news).*?(\.gif|\.png|\.jpeg|\.jpg)?$/",$name,$match))
701         {
702                 if($match[2])
703                         return "<a href=\"$name\" target=\"$link_target\"><img src=\"$name\" border=\"0\"></a>";
704                 else
705                         return "<a href=\"$name\" target=\"$link_target\">$page</a>";
706         }
707         else if(preg_match("/^\[\[([^\]]+)\:([[:alnum:]\-_.]+@[[:alnum:]\-_]+\.[[:alnum:]\-_\.]+)\]\]/",$name,$match))
708         {
709                 return "<a href=\"mailto:$match[2]\">$match[1]</a>";
710         }
711         else if(preg_match("/^([[:alnum:]\-_]+@[[:alnum:]\-_]+\.[[:alnum:]\-_\.]+)/",$name))
712         {
713                 return "<a href=\"mailto:$name\">$page</a>";
714         }
715         else if(preg_match("/^(.+?)&gt;($InterWikiNameNoBracket)$/",strip_bracket($name),$match))
716         {
717                 $page = $match[1];
718                 $name = '[['.$match[2].']]';
719                 $percent_name = str_replace($aryconv_htmlspecial,$aryconv_html,$name);
720                 $percent_name = rawurlencode($percent_name);
721
722                 return "<a href=\"$script?$percent_name\" target=\"$interwiki_target\">$page</a>";
723         }
724         else if(preg_match("/^($InterWikiName)$/",$name))
725         {
726                 $page = strip_bracket($page);
727                 $percent_name = str_replace($aryconv_htmlspecial,$aryconv_html,$name);
728                 $percent_name = rawurlencode($percent_name);
729
730                 return "<a href=\"$script?$percent_name\" target=\"$interwiki_target\">$page</a>";
731         }
732         else if(preg_match("/^($BracketName)|($WikiName)$/",$name))
733         {
734                 if(preg_match("/^(.+?)&gt;(.+)$/",strip_bracket($name),$match))
735                 {
736
737                         $page = $match[1];
738                         $name = $match[2];
739                         if(!preg_match("/^($BracketName)|($WikiName)$/",$page))
740                                 $page = "[[$page]]";
741                         if(!preg_match("/^($BracketName)|($WikiName)$/",$name))
742                                 $name = "[[$name]]";
743                 }
744                 if(preg_match("/^\[\[\.\/([^\]]*)\]\]/",$name,$match))
745                 {
746                         if(!$match[1])
747                                 $name = $vars["page"];
748                         else
749                                 $name = "[[".strip_bracket($vars["page"])."/$match[1]]]";
750                 }
751                 else if(preg_match("/^\[\[\..\/([^\]]+)\]\]/",$name,$match))
752                 {
753                         for($i=0;$i<substr_count($name,"../");$i++)
754                                 $name = preg_replace("/(.+)\/([^\/]+)$/","$1",strip_bracket($vars["page"]));
755
756                         if(!preg_match("/^($BracketName)|($WikiName)$/",$name))
757                                 $name = "[[$name]]";
758                         
759                         if($vars["page"]==$name)
760                                 $name = "[[$match[1]]]";
761                         else
762                                 $name = "[[".strip_bracket($name)."/$match[1]]]";
763                 }
764                 else if($name == "[[../]]")
765                 {
766                         $name = preg_replace("/(.+)\/([^\/]+)$/","$1",strip_bracket($vars["page"]));
767                         
768                         if(!preg_match("/^($BracketName)|($WikiName)$/",$name))
769                                 $name = "[[$name]]";
770                         if($vars["page"]==$name)
771                                 $name = $defaultpage;
772                 }
773                 
774                 $page = strip_bracket($page);
775                 $pagename = htmlspecialchars(strip_bracket($name));
776                 $percent_name = str_replace($aryconv_htmlspecial,$aryconv_html,$name);
777                 $percent_name = rawurlencode($percent_name);
778
779                 $refer = rawurlencode($vars["page"]);
780                 if(is_page($name))
781                 {
782                         $str = get_pg_passage($name,FALSE);
783                         $tm = @filemtime(get_filename(encode($name)));
784                         if($vars["page"] != $name)
785                                 $related[$tm] = "<a href=\"$script?$percent_name\">$pagename</a>$str";
786                         if($show_passage)
787                         {
788                                 $str_title = "title=\"$pagename $str\"";
789                         }
790                         return "<a href=\"$script?$percent_name\" $str_title>$page</a>";
791                 }
792                 else
793                         return "<span class=\"noexists\">$page<a href=\"$script?cmd=edit&amp;page=$percent_name&amp;refer=$refer\">?</a></span>";
794         }
795         else
796         {
797                 return $page;
798         }
799 }
800
801 // ¥æ¡¼¥¶ÄêµÁ¥ë¡¼¥ë(¥½¡¼¥¹¤òÃÖ´¹¤¹¤ë)
802 function user_rules_str($str)
803 {
804         global $str_rules;
805
806         $arystr = split("\n",$str);
807
808         // ÆüÉÕ¡¦»þ¹ïÃÖ´¹½èÍý
809         foreach($arystr as $str)
810         {
811                 if(substr($str,0,1) != " ")
812                 {
813                         foreach($str_rules as $rule => $replace)
814                         {
815                                 $str = preg_replace("/$rule/",$replace,$str);
816                         }
817                 }
818                 $retvars[] = $str;
819         }
820
821         return join("\n",$retvars);
822 }
823
824 // ¥æ¡¼¥¶ÄêµÁ¥ë¡¼¥ë(¥½¡¼¥¹¤ÏÃÖ´¹¤»¤º¥³¥ó¥Ð¡¼¥È)
825 function make_user_rules($str)
826 {
827         global $user_rules;
828
829         foreach($user_rules as $rule => $replace)
830         {
831                 $str = preg_replace("/$rule/",$replace,$str);
832         }
833
834         return $str;
835 }
836
837 // HTML¥¿¥°¤ò¼è¤ê½ü¤¯
838 function strip_htmltag($str)
839 {
840         //$str = preg_replace("/<a[^>]+>\?<\/a>/","",$str);
841         return preg_replace("/<[^>]+>/","",$str);
842 }
843
844 // ¥Ú¡¼¥¸Ì¾¤«¤é¥Ú¡¼¥¸Ì¾¤ò¸¡º÷¤¹¤ë¥ê¥ó¥¯¤òºîÀ®
845 function make_search($page)
846 {
847         global $script,$WikiName;
848
849         $name = strip_bracket($page);
850         $url = rawurlencode($page);
851
852         //WikiWikiWeb like...
853         //if(preg_match("/^$WikiName$/",$page))
854         //      $name = preg_replace("/([A-Z][a-z]+)/","$1 ",$name);
855
856         return "<a href=\"$script?cmd=search&amp;word=$url\">".htmlspecialchars($name)."</a> ";
857 }
858
859 ?>