2 // PukiWiki - Yet another WikiWikiWeb clone.
3 // $Id: html.php,v 1.43 2002/12/02 03:08:47 panda Exp $
4 /////////////////////////////////////////////////
6 // ËÜʸ¤ò¥Ú¡¼¥¸Ì¾¤«¤é½ÐÎÏ
7 function catbodyall($page,$title="",$pg="")
9 if($title === "") $title = strip_bracket($page);
10 if($pg === "") $pg = make_search($page);
12 $body = join("",get_source($page));
13 $body = convert_html($body);
15 header_lastmod($vars["page"]);
16 catbody($title,$pg,$body);
21 function catbody($title,$page,$body)
23 global $script,$vars,$arg,$do_backup,$modifier,$modifierlink,$defaultpage,$whatsnew,$hr;
24 global $date_format,$weeklabels,$time_format,$related_link;
25 global $HTTP_SERVER_VARS,$cantedit;
27 global $foot_explain, $note_hr;
29 if($vars["page"] && !arg_check("backup") && $vars["page"] != $whatsnew)
34 $link_add = "$script?cmd=add&page=".rawurlencode($vars["page"]);
35 $link_edit = "$script?cmd=edit&page=".rawurlencode($vars["page"]);
36 $link_diff = "$script?cmd=diff&page=".rawurlencode($vars["page"]);
37 $link_top = "$script?$defaultpage";
38 $link_list = "$script?cmd=list";
39 $link_filelist = "$script?cmd=filelist";
40 $link_search = "$script?cmd=search";
41 $link_whatsnew = "$script?$whatsnew";
42 $link_backup = "$script?cmd=backup&page=".rawurlencode($vars["page"]);
43 $link_help = "$script?cmd=help";
45 if(is_page($vars["page"]) && $is_page)
47 $fmt = @filemtime(get_filename(encode($vars["page"])));
50 if(is_page($vars["page"]) && $related_link && $is_page && !arg_check("edit") && !arg_check("freeze") && !arg_check("unfreeze"))
52 $related = make_related($vars["page"],FALSE);
55 if(is_page($vars["page"]) && !in_array($vars["page"],$cantedit) && !arg_check("backup") && !arg_check("edit") && !$vars["preview"])
60 $longtaketime = getmicrotime() - MUTIME;
61 $taketime = sprintf("%01.03f",$longtaketime);
64 $body .= "\n$note_hr\n".join("\n",inline2($foot_explain));
66 if(!file_exists(SKIN_FILE)||!is_readable(SKIN_FILE))
67 die_message(SKIN_FILE."(skin file) is not found.");
71 // ¥Æ¥¥¹¥ÈËÜÂΤòHTML¤ËÊÑ´¹¤¹¤ë
72 function convert_html($string)
74 global $hr,$script,$page,$vars,$top;
75 global $note_id,$foot_explain,$digest,$note_hr;
76 global $user_rules,$str_rules,$line_rules,$strip_link_wall;
77 global $InterWikiName, $BracketName;
80 $content_id_local = ++$content_id;
83 $string = rtrim($string);
84 $string = preg_replace("/((\x0D\x0A)|(\x0D)|(\x0A))/","\n",$string);
86 $start_mtime = getmicrotime();
88 $digest = md5(@join("",get_source($vars["page"])));
92 $arycontents = array();
94 $string = preg_replace("/^#freeze\n/","",$string);
96 $lines = split("\n", $string);
97 // ³Æ¹Ô¤Î¹ÔƬ½ñ¼°¤ò³ÊǼ
99 // ¸½ºß¤Î¹Ô¿ô¤òÆþ¤ì¤Æ¤ª¤³¤¦
101 // ¥Ö¥í¥Ã¥¯¤ÎȽÄê¥Õ¥é¥°
107 if(preg_match("/#contents/",$string))
108 $top_link = "<a href=\"#contents_$content_id_local\">$top</a>";
110 foreach ($lines as $line)
112 if(!preg_match("/^\/\/(.*)/",$line,$comment_out) && $table != 0)
114 if(!preg_match("/^\|(.+)\|$/",$line,$out) or
115 $table != count(explode("|",$out[1])))
118 array_push($result, "</table></div>");
122 $comment_out = $comment_out[1];
124 // ¹ÔƬ½ñ¼°¤«¤É¤¦¤«¤ÎȽÄê
125 $line_head = substr($line,0,1);
126 if( $line_head == ' ' ||
136 if($headform[$_cnt-1] == '' && $_p){
137 array_push($result, "</p>");
140 if($line_head != '>' && $_bq){
141 array_push($result, "</p>");
145 if(preg_match("/^\#([^\(]+)(.*)$/",$line,$out)){
146 if(exist_plugin_convert($out[1])) {
147 $result = array_merge($result,$saved); $saved = array();
150 $_plugin = preg_replace("/^\#([^\(]+)\((.*)\)$/ex","do_plugin_convert('$1','$2')",$line);
152 $_plugin = preg_replace("/^\#([^\(]+)$/ex","do_plugin_convert('$1','$2')",$line);
154 // ÀèƬ¤Ë¶õÇò¤òÆþ¤ì¤ë¤³¤È¤Ë¤è¤ê¤È¤ê¤¢¤¨¤ºpre¤Î°·¤¤¤ÈƱÍͤËinline2¤ÎƯ¤¤òÍÞ¤¨¤ë¡¢¤¦¡Á¤ó¡¢ÌµÃã¡£
155 array_push($result,"\t$_plugin");
157 array_push($result, htmlspecialchars($line));
160 else if(preg_match("/^(\*{1,3})(.*)/",$line,$out))
162 $result = array_merge($result,$saved); $saved = array();
163 $headform[$_cnt] = $out[1];
164 $str = inline($out[2]);
166 $level = strlen($out[1]) + 1;
168 array_push($result, "<h$level><a name=\"content_{$content_id_local}_$content_count\"></a>$str $top_link</h$level>");
169 $arycontents[] = str_repeat("-",$level-1)."<a href=\"#content_{$content_id_local}_$content_count\">".strip_htmltag(make_user_rules(inline($out[2],TRUE)))."</a>\n";
172 else if(preg_match("/^(-{1,4})(.*)/",$line,$out))
174 $headform[$_cnt] = $out[1];
175 if(strlen($out[1]) == 4)
177 $result = array_merge($result,$saved); $saved = array();
178 array_push($result, $hr);
182 list_push($result,$saved,'ul', strlen($out[1]));
183 array_push($result, '<li>'.inline($out[2]));
186 else if(preg_match("/^(\+{1,3})(.*)/",$line,$out))
188 $headform[$_cnt] = $out[1];
189 list_push($result,$saved,'ol', strlen($out[1]));
190 array_push($result, '<li>'.inline($out[2]));
192 else if (preg_match("/^:([^:]+):(.*)/",$line,$out))
194 $headform[$_cnt] = ':'.$out[1].':';
195 back_push($result,$saved,'dl', 1);
196 array_push($result, '<dt>' . inline($out[1]) . '</dt>', '<dd>' . inline($out[2]) . '</dd>');
198 else if(preg_match("/^(>{1,3})(.*)/",$line,$out))
200 $headform[$_cnt] = $out[1];
201 back_push($result,$saved,'blockquote', strlen($out[1]));
202 // ¤³¤³¤Î¤¢¤¿¤ê¤Ç¼«Á°¤Çback_push¤«¤±¤Æ¤ë´¶¤¸¡£ÌµÃã¶ìÃã¡Ä
203 if($headform[$_cnt-1] != $headform[$_cnt] ) {
205 array_push($result, "<p class=\"quotation\">");
208 else if(substr($headform[$_cnt-1],0,1) == '>'){
209 $_level_diff = abs( strlen($out[1]) - strlen($headform[$_cnt-1]) );
210 if( $_level_diff == 1 ){
211 $i = array_pop($result);
212 array_push($result, "</p>");
213 array_push($result,$i);
214 array_push($result, "<p class=\"quotation\">");
218 $i[] = array_pop($result);
219 $i[] = array_pop($result);
220 array_push($result, "</p>");
221 $result = array_merge($result,$i);
222 array_push($result, "<p class=\"quotation\">");
227 array_push($result, ltrim(inline($out[2])));
229 else if(preg_match("/^(\s+.*)/",$line,$out))
231 $headform[$_cnt] = ' ';
232 back_push($result,$saved,'pre', 1);
233 array_push($result, htmlspecialchars($out[1],ENT_NOQUOTES));
235 else if(preg_match("/^\|(.+)\|$/",$line,$out))
237 $headform[$_cnt] = '|';
238 $arytable = explode("|",$out[1]);
242 $result = array_merge($result,$saved); $saved = array();
243 array_push($result,"<div class=\"ie5\"><table class=\"style_table\" cellspacing=\"1\" border=\"0\">");
244 $table = count($arytable);
247 array_push($result,"<tr>");
248 foreach($arytable as $td)
250 if (preg_match("/^(LEFT|CENTER|RIGHT):(.*)$/",$td,$tmp)) {
251 $style = ' align="'.strtolower($tmp[1]).'"';
256 array_push($result,"<td class=\"style_td\"$style>");
257 array_push($result,ltrim(inline($td)));
258 array_push($result,"</td>");
260 array_push($result,"</tr>");
263 else if(strlen($comment_out) != 0)
265 $headform[$_cnt] = '//';
266 # array_push($result," <!-- ".htmlspecialchars($comment_out)." -->");
271 $headform[$_cnt] = '';
272 if($headform[$_cnt-1] != $headform[$_cnt]){
273 if(array_values($saved)){
275 array_unshift($saved, "</p>");
278 $i = array_pop($saved);
279 array_push($saved,$i);
280 $result = array_merge($result,$saved); $saved = array();
282 if( substr($line,0,1) == '' && !$_p){
283 array_push($result, "<p>");
286 else if( substr($line,0,1) != '' && $_p){
287 array_push($result, "</p>");
292 if (preg_match("/^(LEFT|CENTER|RIGHT):(.*)$/",$line,$tmp)) {
294 array_push($result,"</p>");
295 array_push($result,'<p align="'.strtolower($tmp[1]).'">');
296 array_push($result,inline($tmp[2]));
297 array_push($result,"</p>");
301 if( substr($line,0,1) == '' && $_p){
302 $_tmp = array_pop($result);
304 $_tmp = '<p class="empty">';
306 array_push($result, $_tmp, "</p>");
309 else if( substr($line,0,1) != '' && !$_p) {
310 array_push($result, "<p>");
313 if( substr($line,0,1) != '' ){
314 array_push($result, inline($line));
322 if($_p) array_push($result, "</p>");
324 array_push($result, "</p>");
326 if($table) array_push($result, "</table></div>");
328 $result_last = $result = array_merge($result,$saved); $saved = array();
330 if($content_count != 0)
335 foreach($arycontents as $line)
337 if(preg_match("/^(-{1,3})(.*)/",$line,$out))
339 list_push($result,$saved,'ul', strlen($out[1]));
340 array_push($result, '<li>'.$out[2]);
343 $result = array_merge($result,$saved); $saved = array();
345 $contents = "<a name=\"contents_$content_id_local\"></a>\n";
346 $contents .= join("\n",$result);
349 $contents = preg_replace("/\[\[([^\]:]+):(.+)\]\]/","$1",$contents);
350 $contents = preg_replace("/\[\[([^\]]+)\]\]/","$1",$contents);
354 $result_last = inline2($result_last);
356 $result_last = preg_replace("/^#contents/",$contents,$result_last);
358 $str = join("\n", $result_last);
360 # $str = preg_replace("/&((amp)|(quot)|(nbsp)|(lt)|(gt));/","&$1;",$str);
365 // $tag¤Î¥¿¥°¤ò$level¥ì¥Ù¥ë¤Þ¤ÇµÍ¤á¤ë¡£
366 function back_push(&$result,&$saved,$tag, $level)
368 while (count($saved) > $level) {
369 array_push($result, array_shift($saved));
371 if ($saved[0] != "</$tag>") {
372 $result = array_merge($result,$saved); $saved = array();
374 while (count($saved) < $level) {
375 array_unshift($saved, "</$tag>");
376 array_push($result, "<$tag>");
380 function list_push(&$result,&$saved,$tag,$level) {
381 global $_list_left_margin, $_list_margin, $_list_pad_str;
384 $close = "</li></$tag>";
386 while (count($saved) > $level or
387 (count($saved) > 0 and $saved[0] != $close)) {
388 array_push($result, array_shift($saved));
391 $margin = $level - count($saved);
393 while (count($saved) < ($level - 1)) {
394 array_unshift($saved, ''); //count($saved)¤òÁý¤ä¤¹¤¿¤á¤Îdummy
397 if (count($saved) < $level) {
399 array_unshift($saved, $close);
401 $left = $margin * $_list_margin;
402 if ($level == $margin) $left += $_list_left_margin;
403 $str = sprintf($_list_pad_str, $level, $left, $left);
404 array_push($result, sprintf($open, $str));
408 array_push($result, '</li>');
411 // ¥¤¥ó¥é¥¤¥óÍ×ÁǤΥѡ¼¥¹ (Ãí¼á)
412 function inline($line,$remove=FALSE)
414 $line = htmlspecialchars($line);
416 $replace = $remove ? '' : 'make_note("$1")';
417 $line = preg_replace("/\(\(((?:(?!\)\)).)*)\)\)/ex",$replace,$line);
422 // ¥¤¥ó¥é¥¤¥óÍ×ÁǤΥѡ¼¥¹ (¥ê¥ó¥¯¡¢´ØÏ¢°ìÍ÷¡¢¸«½Ð¤·°ìÍ÷)
423 function inline2($str)
425 global $WikiName,$BracketName,$InterWikiName,$vars,$related,$related_link,$script;
426 $cnts_plain = array();
429 for($cnt=0;$cnt<count($str);$cnt++)
431 if(preg_match("/^(\s)/",$str[$cnt]))
433 $arykeep[$cnt] = $str[$cnt];
435 $cnts_plain[] = $cnt;
439 $str = make_link($str);
440 $str = preg_replace("/#related/e",'make_related($vars["page"],TRUE)',$str);
441 $str = make_user_rules($str);
444 $str = preg_replace("/^#norelated$/","",$str);
448 foreach($cnts_plain as $cnt)
449 $str[$cnt] = $arykeep[$cnt];
455 function get_list($withfilename)
457 global $script,$list_index,$top,$non_list,$whatsnew;
458 global $_msg_symbol,$_msg_other;
461 $files = get_existpages();
462 foreach($files as $page) {
463 if(preg_match("/$non_list/",$page) && !$withfilename) continue;
464 if($page == $whatsnew) continue;
465 $page_url = rawurlencode($page);
466 $page2 = strip_bracket($page);
467 $pg_passage = get_pg_passage($page);
468 $file = encode($page).".txt";
469 $retval[$page2] .= "<li><a href=\"$script?$page_url\">".htmlspecialchars($page2,ENT_QUOTES)."</a>$pg_passage";
472 $retval[$page2] .= "<ul><li>$file</li></ul>\n";
474 $retval[$page2] .= "</li>\n";
477 $retval = list_sort($retval);
486 foreach($retval as $page => $link)
488 $head = substr($page,0,1);
489 if($head_str != $head && !$etc_sw)
491 $retval2[$page] = "";
493 if(preg_match("/([A-Z])|([a-z])/",$head,$match))
496 $head_nm = "High_$head";
498 $head_nm = "Low_$head";
500 if($head_str) $retval2[$page] = "</ul></li>\n";
501 $retval2[$page] .= "<li><a href=\"#top_$head_nm\" name=\"$head_nm\"><strong>$head</strong></a>\n<ul>\n";
503 if($link_counter) $top_link .= "|";
504 $link_counter = $link_counter + 1;
505 $top_link .= "<a href=\"#$head_nm\" name=\"top_$head_nm\"><strong> ".$head." </strong></a>";
506 if($link_counter==16) {
507 $top_link .= "<br />";
511 else if(preg_match("/[ -~]/",$head))
515 if($head_str) $retval2[$page] = "</ul></li>\n";
516 $retval2[$page] .= "<li><a href=\"#top_symbol\" name=\"symbol\"><strong>$_msg_symbol</strong></a>\n<ul>\n";
518 if($link_counter) $top_link .= "|";
519 $link_counter = $link_counter + 1;
520 $top_link .= "<a href=\"#symbol\" name=\"top_symbol\"><strong>$_msg_symbol</strong></a>";
526 if($head_str) $retval2[$page] = "</ul></li>\n";
527 $retval2[$page] .= "<li><a href=\"#top_etc\" name=\"etc\"><strong>$_msg_other</strong></a>\n<ul>\n";
529 if($link_counter) $top_link .= "|";
530 $link_counter = $link_counter + 1;
531 $top_link .= "<a href=\"#etc\" name=\"top_etc\"><strong>$_msg_other</strong></a>";
534 $retval2[$page] .= $link;
536 $retval2[] = "</ul></li>\n";
538 $top_link = "<div style=\"text-align:center\"><a name=\"top\"></a>$top_link</div><br />\n<ul>";
540 array_unshift($retval2,$top_link);
548 array_unshift($retval2,$top_link);
551 return join("",$retval2)."</ul>";
554 // ÊÔ½¸¥Õ¥©¡¼¥à¤Îɽ¼¨
555 function edit_form($postdata,$page,$add=0)
557 global $script,$rows,$cols,$hr,$vars,$function_freeze;
558 global $_btn_addtop,$_btn_preview,$_btn_update,$_btn_freeze,$_msg_help,$_btn_notchangetimestamp;
559 global $whatsnew,$_btn_template,$_btn_load,$non_list,$load_template_func;
561 $digest = md5(@join("",get_source($page)));
565 $addtag = '<input type="hidden" name="add" value="true" />';
566 $add_top = '<input type="checkbox" name="add_top" value="true" /><span class="small">'.$_btn_addtop.'</span>';
569 if($vars["help"] == "true")
570 $help = $hr.catrule();
572 $help = "<br />\n<ul><li><a href=\"$script?cmd=edit&help=true&page=".rawurlencode($page)."\">$_msg_help</a></ul></li>\n";
575 $str_freeze = '<input type="submit" name="freeze" value="'.$_btn_freeze.'" accesskey="f" />';
577 if($load_template_func)
581 $files = get_existpages();
582 foreach($files as $pg_org) {
583 if($pg_org == $whatsnew) continue;
584 if(preg_match("/$non_list/",$pg_org)) continue;
585 $s_name = htmlspecialchars(strip_bracket($pg_org));
586 $s_org = htmlspecialchars($pg_org);
587 $vals[$name] = " <option value=\"$s_org\">$s_name</option>";
591 $template = " <select name=\"template_page\">\n"
592 ." <option value=\"\">-- $_btn_template --</option>\n"
595 ." <input type=\"submit\" name=\"template\" value=\"$_btn_load\" accesskey=\"r\" /><br />\n";
597 if($vars["refer"]) $refer = $vars["refer"]."\n\n";
601 <form action="'.$script.'" method="post">
603 <table cellspacing="3" cellpadding="0" border="0">
611 <input type="hidden" name="page" value="'.htmlspecialchars($page).'" />
612 <input type="hidden" name="digest" value="'.htmlspecialchars($digest).'" />
613 <textarea name="msg" rows="'.$rows.'" cols="'.$cols.'" wrap="virtual">
614 '.htmlspecialchars($refer.$postdata).'</textarea>
619 <input type="submit" name="preview" value="'.$_btn_preview.'" accesskey="p" />
620 <input type="submit" name="write" value="'.$_btn_update.'" accesskey="s" />
622 <input type="checkbox" name="notimestamp" value="true" /><span style="small">'.$_btn_notchangetimestamp.'</span>
628 <form action="'.$script.'?cmd=freeze" method="post">
630 <input type="hidden" name="page" value="'.htmlspecialchars($vars["page"]).'" />
639 function make_related($page,$_isrule)
641 global $related_str,$rule_related_str,$related,$_make_related,$vars;
643 $page_name = strip_bracket($vars["page"]);
645 if(!is_array($_make_related))
647 $aryrelated = do_search($page,"OR",1);
649 if(is_array($aryrelated))
651 foreach($aryrelated as $key => $val)
653 $new_arylerated[$key.md5($val)] = $val;
657 if(is_array($related))
659 foreach($related as $key => $val)
661 $new_arylerated[$key.md5($val)] = $val;
665 @krsort($new_arylerated);
666 $_make_related = @array_unique($new_arylerated);
671 if(is_array($_make_related))
673 foreach($_make_related as $str)
675 preg_match("/<a\shref=\"([^\"]+)\">([^<]+)<\/a>(.*)/",$str,$out);
677 if($out[3]) $title = " title=\"$out[2] $out[3]\"";
679 $aryret[$out[2]] = "<a href=\"$out[1]\"$title>$out[2]</a>";
686 $aryret = $_make_related;
689 if($_isrule) $str = $rule_related_str;
690 else $str = $related_str;
692 return @join($str,$aryret);
696 function make_note($str)
698 global $note_id,$foot_explain;
700 $str = preg_replace("/^\(\(/","",$str);
701 $str = preg_replace("/\s*\)\)$/","",$str);
703 $str= str_replace("\\'","'",$str);
705 $str = make_user_rules($str);
707 $foot_explain[] = "<a name=\"notefoot_$note_id\" href=\"#notetext_$note_id\" class=\"note_super\">*$note_id</a> <span class=\"small\">$str</span><br />\n";
708 $note = "<a name=\"notetext_$note_id\" href=\"#notefoot_$note_id\" class=\"note_super\">*$note_id</a>";
715 function make_link($name,$page = '')
717 return p_make_link($name,$page);
720 // ¥æ¡¼¥¶ÄêµÁ¥ë¡¼¥ë(¥½¡¼¥¹¤òÃÖ´¹¤¹¤ë)
721 function user_rules_str($str)
725 $arystr = split("\n",$str);
727 // ÆüÉÕ¡¦»þ¹ïÃÖ´¹½èÍý
728 foreach($arystr as $str)
730 if(substr($str,0,1) != " ")
732 foreach($str_rules as $rule => $replace)
734 $str = preg_replace("/$rule/",$replace,$str);
740 return join("\n",$retvars);
743 // ¥æ¡¼¥¶ÄêµÁ¥ë¡¼¥ë(¥½¡¼¥¹¤ÏÃÖ´¹¤»¤º¥³¥ó¥Ð¡¼¥È)
744 function make_user_rules($str)
748 foreach($user_rules as $rule => $replace)
750 $str = preg_replace("/$rule/",$replace,$str);
756 // HTML¥¿¥°¤ò¼è¤ê½ü¤¯
757 function strip_htmltag($str)
759 //$str = preg_replace("/<a[^>]+>\?<\/a>/","",$str);
760 return preg_replace("/<[^>]+>/","",$str);
763 // ¥Ú¡¼¥¸Ì¾¤«¤é¥Ú¡¼¥¸Ì¾¤ò¸¡º÷¤¹¤ë¥ê¥ó¥¯¤òºîÀ®
764 function make_search($page)
766 global $script,$WikiName;
768 $name = strip_bracket($page);
769 $url = rawurlencode($page);
771 //WikiWikiWeb like...
772 //if(preg_match("/^$WikiName$/",$page))
773 // $name = preg_replace("/([A-Z][a-z]+)/","$1 ",$name);
775 return "<a href=\"$script?cmd=search&word=$url\">".htmlspecialchars($name)."</a> ";