1 ############################################################
3 # źÉÕ¥Õ¥¡¥¤¥ë¤Î¥¢¥¯¥·¥ç¥ó¥Ï¥ó¥É¥é¡£
5 ############################################################
6 package plugin::attach::AttachHandler;
8 use plugin::attach::Files;
9 #===========================================================
11 #===========================================================
15 return bless $self,$class;
18 #===========================================================
20 #===========================================================
24 my $cgi = $wiki->get_CGI;
26 my $pagename = $cgi->param("page");
28 $pagename = $wiki->config("frontpage");
31 $wiki->set_title("¥Õ¥¡¥¤¥ë¤ÎźÉÕ",1);
33 if($cgi->param("UPLOAD") ne "" || $cgi->param("CONFIRM") ne "" || $cgi->param("DELETE") ne ""){
34 unless($wiki->can_modify_page($pagename)){
35 return $wiki->error("ÊÔ½¸¤Ï¶Ø»ß¤µ¤ì¤Æ¤¤¤Þ¤¹¡£");
39 if($cgi->param("DELETE") ne ""){
40 unless(&plugin::attach::Files::can_attach_delete($wiki, $pagename)){
41 return $wiki->error("¥Õ¥¡¥¤¥ë¤Îºï½ü¤Ïµö²Ä¤µ¤ì¤Æ¤¤¤Þ¤»¤ó¡£");
45 #-------------------------------------------------------
47 if($cgi->param("UPLOAD") ne ""){
48 my $filename = $cgi->param("file");
49 $filename =~ s/\\/\//g;
50 $filename = substr($filename,rindex($filename,"/")+1);
51 $filename =~ tr/";\x00-\x1f/': /;
52 &Jcode::convert(\$filename,'euc');
55 return $wiki->error("¥Õ¥¡¥¤¥ë¤¬»ØÄꤵ¤ì¤Æ¤¤¤Þ¤»¤ó¡£");
58 my $hundle = $cgi->upload("file");
60 return $wiki->error("¥Õ¥¡¥¤¥ë¤¬Æɤ߹þ¤á¤Þ¤»¤ó¤Ç¤·¤¿¡£");
63 my $uploadfile = $wiki->config('attach_dir')."/".&Util::url_encode($pagename).".".&Util::url_encode($filename);
64 if(-e $uploadfile && !&plugin::attach::Files::can_attach_update($wiki, $pagename)){
65 return $wiki->error("¥Õ¥¡¥¤¥ë¤Î¾å½ñ¤¤Ïµö²Ä¤µ¤ì¤Æ¤¤¤Þ¤»¤ó¡£");
68 open(DATA,">$uploadfile") or die $!;
70 while(read($hundle,$_,16384)){ print DATA $_; }
73 # attach¥×¥é¥°¥¤¥ó¤«¤éźÉÕ¤µ¤ì¤¿¾ì¹ç
74 if(defined($cgi->param("count"))){
75 my @lines = split(/\n/,$wiki->get_page($pagename));
78 my $count=$cgi->param("count");
81 if(index($_," ")==0||index($_,"\t")==0){
85 if(index($_,"{{attach}}")!=-1 && $flag==0){
86 if($form_count==$count){
87 $content = $content."{{ref ".$filename."}}\n";
93 $content = $content.$_."\n";
96 $wiki->save_page($pagename,$content);
101 &write_log($wiki,"UPLOAD",$pagename,$filename);
103 $wiki->redirect($pagename);
105 #-------------------------------------------------------
107 } elsif($cgi->param("CONFIRM") ne ""){
108 my $file = $cgi->param("file");
110 return $wiki->error("¥Õ¥¡¥¤¥ë¤¬»ØÄꤵ¤ì¤Æ¤¤¤Þ¤»¤ó¡£");
115 $buf .= "<a href=\"".$wiki->create_page_url($pagename)."\">".
116 Util::escapeHTML($pagename)."</a>¤«¤é".Util::escapeHTML($file)."¤òºï½ü¤·¤Æ¤è¤í¤·¤¤¤Ç¤¹¤«¡©\n".
117 "<form action=\"".$wiki->create_url()."\" method=\"POST\">\n".
118 " <input type=\"submit\" name=\"DELETE\" value=\"ºï ½ü\">".
119 " <input type=\"hidden\" name=\"action\" value=\"ATTACH\">".
120 " <input type=\"hidden\" name=\"page\" value=\"".Util::escapeHTML($pagename)."\">".
121 " <input type=\"hidden\" name=\"file\" value=\"".Util::escapeHTML($file)."\">".
125 #-------------------------------------------------------
127 } elsif($cgi->param("DELETE") ne ""){
128 my $file = $cgi->param("file");
130 return $wiki->error("¥Õ¥¡¥¤¥ë¤¬»ØÄꤵ¤ì¤Æ¤¤¤Þ¤»¤ó¡£");
134 &write_log($wiki,"DELETE",$pagename,$file);
136 unlink($wiki->config('attach_dir')."/".&Util::url_encode($pagename).".".&Util::url_encode($file));
137 $wiki->redirect($pagename);
139 #-------------------------------------------------------
142 my $file = $cgi->param("file");
144 return $wiki->error("¥Õ¥¡¥¤¥ë¤¬»ØÄꤵ¤ì¤Æ¤¤¤Þ¤»¤ó¡£");
146 unless($wiki->page_exists($pagename)){
147 return $wiki->error("¥Ú¡¼¥¸¤¬Â¸ºß¤·¤Þ¤»¤ó¡£");
149 unless($wiki->can_show($pagename)){
150 return $wiki->error("¥Ú¡¼¥¸¤Î»²¾È¸¢¸Â¤¬¤¢¤ê¤Þ¤»¤ó¡£");
152 my $filepath = $wiki->config('attach_dir')."/".&Util::url_encode($pagename).".".&Util::url_encode($file);
153 unless(-e $filepath){
154 return $wiki->error("¥Õ¥¡¥¤¥ë¤¬¤ß¤Ä¤«¤ê¤Þ¤»¤ó¡£");
157 my $contenttype = &get_mime_type($wiki,$file);
158 my $ua = $ENV{"HTTP_USER_AGENT"};
159 my $disposition = ($contenttype =~ /^image\// && $ua !~ /MSIE/ ? "inline" : "attachment");
161 open(DATA, $filepath) or die $!;
162 print "Content-Type: $contenttype\n";
163 print Util::make_content_disposition($file, $disposition);
165 while(read(DATA,$_,16384)){ print $_; }
169 &write_log($wiki,"DOWNLOAD",$pagename,$file);
170 &count_up($wiki,$pagename,$file);
176 #===========================================================
177 # ¥À¥¦¥ó¥í¡¼¥É¥«¥¦¥ó¥È¤ò¥¤¥ó¥¯¥ê¥á¥ó¥È
178 #===========================================================
184 Util::sync_update_config(undef,$wiki->config('log_dir')."/".$wiki->config('download_count_file'),
187 unless(defined($hash->{$page."::".$file})){
188 $hash->{$page."::".$file} = 1;
190 $hash->{$page."::".$file}++;
197 #===========================================================
199 #===========================================================
205 if($wiki->config('log_dir') eq "" || $wiki->config('attach_log_file') eq ""){
208 my $ip = $ENV{"REMOTE_ADDR"};
209 my $ref = $ENV{"HTTP_REFERER"};
210 my $ua = $ENV{"HTTP_USER_AGENT"};
211 if($ip eq ""){ $ip = "-"; }
212 if($ref eq ""){ $ref = "-"; }
213 if($ua eq ""){ $ua = "-"; }
214 my ($sec, $min, $hour, $mday, $mon, $year) = localtime(time());
215 my $date = sprintf("%04d/%02d/%02d %02d:%02d:%02d",$year+1900,$mon+1,$mday,$hour,$min,$sec);
217 my $logfile = $wiki->config('log_dir')."/".$wiki->config('attach_log_file');
218 Util::file_lock($logfile);
219 open(LOG,">>$logfile") or die $!;
221 print LOG $mode." ".&Util::url_encode($page)." ".&Util::url_encode($file)." ".
222 $date." ".$ip." ".$ref." ".$ua."\n";
224 Util::file_unlock($logfile);
227 #===========================================================
228 # MIME¥¿¥¤¥×¤ò¼èÆÀ¤·¤Þ¤¹
229 #===========================================================
233 my $type = lc(substr($file,rindex($file,".")+1));
235 my $hash = &Util::load_config_hash($wiki,$wiki->config('mime_file'));
236 my $ctype = $hash->{$type};
239 $ctype = "application/octet-stream";