OSDN Git Service

ソフト404等のレスポンスエラーコード対応
[fswiki/fswiki.git] / plugin / comment / CommentHandler.pm
1 ############################################################
2
3 # Comment¥×¥é¥°¥¤¥ó¤Î¥¢¥¯¥·¥ç¥ó¥Ï¥ó¥É¥é¡£
4
5 ############################################################
6 package plugin::comment::CommentHandler;
7 use strict;
8 use HTTP::Status;
9 #===========================================================
10 # ¥³¥ó¥¹¥È¥é¥¯¥¿
11 #===========================================================
12 sub new {
13         my $class = shift;
14         my $self = {};
15         return bless $self,$class;
16 }
17
18 #===========================================================
19 # ¥³¥á¥ó¥È¤Î½ñ¤­¹þ¤ß
20 #===========================================================
21 sub do_action {
22         my $self = shift;
23         my $wiki = shift;
24         my $cgi  = $wiki->get_CGI;
25         
26         my $name    = $cgi->param("name");
27         my $message = $cgi->param("message");
28         my $count   = $cgi->param("count");
29         my $page    = $cgi->param("page");
30         my $option  = $cgi->param("option");
31         
32         if(!$wiki->can_show($page)){
33                 return $wiki->error(RC_FORBIDDEN, "¥Ú¡¼¥¸¤Î»²¾È¸¢¸Â¤¬¤¢¤ê¤Þ¤»¤ó¡£");
34         }
35         if($name eq ""){
36                 $name = "̵̾¤·¤µ¤ó";
37         } else {
38                 # fswiki_post_name¤È¤¤¤¦¥­¡¼¤Ç¥¯¥Ã¥­¡¼¤ò¥»¥Ã¥È¤¹¤ë
39                 my $path   = &Util::cookie_path($wiki);
40                 my $cookie = $cgi->cookie(-name=>'fswiki_post_name',-value=>Util::url_encode($name),-expires=>'+1M',-path=>$path);
41                 print "Set-Cookie: ",$cookie->as_string,"\n";
42         }
43         
44         # ¥Õ¥©¡¼¥Þ¥Ã¥È¥×¥é¥°¥¤¥ó¤Ø¤ÎÂбþ
45         my $format = $wiki->get_edit_format();
46         $name    = $wiki->convert_to_fswiki($name   ,$format,1);
47         $message = $wiki->convert_to_fswiki($message,$format,1);
48         
49         if($page ne "" && $message ne "" && $count ne ""){
50                 
51                 my @lines = split(/\n/,$wiki->get_page($page));
52                 my $flag       = 0;
53                 my $form_count = 1;
54                 my $content    = "";
55                 
56                 foreach(@lines){
57                         # ¿·Ãå½ç¤Î¾ì¹ç
58                         if($option eq "reverse"){
59                                 $content = $content.$_."\n";
60                                 if(/^\{\{comment\s*.*\}\}$/ && $flag==0){
61                                         if($form_count==$count){
62                                                 $content = $content."*$message - $name (".Util::format_date(time()).")\n";
63                                                 $flag = 1;
64                                         } else {
65                                                 $form_count++;
66                                         }
67                                 }
68                         # ¥Ú¡¼¥¸ËöÈø¤ËÄɲäξì¹ç
69                         } elsif($option eq "tail"){
70                                 $content = $content.$_."\n";
71                                 
72                         # Åê¹Æ½ç¤Î¾ì¹ç
73                         } else {
74                                 if(/^\{\{comment\s*.*\}\}$/ && $flag==0){
75                                         if($form_count==$count){
76                                                 $content = $content."*$message - $name (".Util::format_date(time()).")\n";
77                                                 $flag = 1;
78                                         } else {
79                                                 $form_count++;
80                                         }
81                                 }
82                                 $content = $content.$_."\n";
83                         }
84                 }
85                 
86                 # ¥Ú¡¼¥¸ËöÈø¤ËÄɲäξì¹ç¤ÏºÇ¸å¤ËÄɲÃ
87                 if($option eq "tail" && check_comment($wiki, 'Footer')){
88                         $content = $content."*$message - $name (".Util::format_date(time()).")\n";
89                         $flag = 1;
90                 }
91                 
92                 if($flag==1){
93                         $wiki->save_page($page,$content);
94                 }
95         }
96         
97         # ¸µ¤Î¥Ú¡¼¥¸¤Ë¥ê¥À¥¤¥ì¥¯¥È
98         $wiki->redirect($page);
99 }
100
101 #==================================================================
102 # ¥Ú¡¼¥¸¤Ëcomment¥×¥é¥°¥¤¥ó¤¬´Þ¤Þ¤ì¤Æ¤¤¤ë¤«¤É¤¦¤«¤ò¥Á¥§¥Ã¥¯
103 #==================================================================
104 sub check_comment {
105         my $wiki = shift;
106         my $page = shift;
107         my @lines = split(/\n/,$wiki->get_page($page));
108         foreach(@lines){
109                 if(/^\{\{comment\s*.*\}\}$/){
110                         return 1;
111                 }
112         }
113         return 0;
114 }
115
116 1;