#! c:/perl/bin/perl # # flist3.cgi # # 3.002 : 10/5/06 : ダウンロード合計ファイルサイズリミットを追加 # 3.001 : 6/11/06 : ボタン名を修正 # 3.0 : 12/19/05 : Upgreaded flist2.cgi # # Upgreaded parts from flist2.cgi. # Added English mode. # Added page mode # # Need Archive::Zip # # $Id: flist3.cgi,v 1.8 2006/10/05 04:50:45 Hideki Kanayama Exp $ # Copyright(c) 2005-2006, Hideki Kanayama, All rights reserved. use Archive::Zip; use CGI::Carp qw(fatalsToBrowser); use Cwd; use File::Basename; use CGI qw(:cgi-lib); use strict; my $script= basename($0); my $version = "3.002"; my $updatedyear = "2006"; my $lang = 0; my $charset = ('Shift_JIS', 'ISO-8859-1')[$lang]; my $prefix = 'flist'; my $zipfile = "$prefix$$.zip"; my $zipdir = cwd(); my $setupfile = "flist3_setup.pl"; #### Environment setup from here #################### # 公開するファイルがあるディレクトリ # A directory to be disclosed our $dldir = "files"; # $zipdirにあるテンポラリ.zipファイルを削除するまでの時間(分) # Expiratoin time to delete zip files in the temporary file our $expire = 60; # タイトル # title our $title = 'file list 3'; # 1ページ表示行数 # number to be displayed in one page our $num_in_a_page = 20; # 戻るリンク # back link our $back = ('戻る','Back')[$lang]; our $backlink = ".."; # ダウンロードするファイルの合計サイズのリミット(MB) our $size_limit = 20; #### Environment setup till here #################### require "$setupfile" if (-e "$setupfile"); my $q = new CGI; my $cgierror = $q->cgi_error; &error($cgierror) if ($cgierror); my %in = $q->Vars; &error(("ディレクトリ$zipdirが存在しません","Cannot open directory:$zipdir")[$lang]) if (! -d $zipdir); opendir(DLDIR, "$dldir") or &error(("ディレクトリ$dldirが存在しません","Cannot open directory:$dldir")[$lang]); my @filelist = sort grep !/^\./, readdir DLDIR; closedir(DLDIR); unshift(@filelist,'dummy'); if ($in{mode} eq 'download'){ &makezip; } else { &display; } exit; sub makezip { my $zip = Archive::Zip->new(); my $member; chdir($dldir); my $i=0; my $eachfile; my $totalsize; foreach (keys(%in)){ if ($in{$_} eq 'on'){ next if ($_ eq 'allon' || $_ eq 'alloff'); /check_(\d\d*)/; $eachfile = $filelist[$1]; my ($d_dev,$d_ino,$d_mode,$d_nlink,$d_uid,$d_gid,$d_rdev,$d_size,$d_atime,$d_mtime,$d_ctime,$d_blksize,$d_blocks)=stat("$eachfile"); $totalsize += $d_size; if ($totalsize > $size_limit * 1048576){ undef $zip; &error("ダウンロードしようとしている合計ファイルサイズが${size_limit}MBを超えています。"); } $member = $zip->addFile("$eachfile"); } } my $status = $zip->writeToFileNamed("$zipfile"); if ($status != 'AZ_OK') { unlink("$zipfile") if (-e "$zipfile"); &error(("$zipfileが作成されません","Cannot create $zipfile")[$lang]) } rename "$zipfile", "$zipdir/$zipfile"; chdir($zipdir); print "Location: $zipfile\n\n"; } sub display { opendir(ZIPDIR, "$zipdir") or &error(("ディレクトリ$zipdirが開けません","Cannot open $zipdir")[$lang]); my @ziplist = grep /^$prefix.*\.zip$/, readdir ZIPDIR; closedir(ZIPDIR); my $zipfile; my $now = time; my ($d_dev,$d_ino,$d_mode,$d_nlink,$d_uid,$d_gid,$d_rdev,$d_size,$d_atime,$d_mtime,$d_ctime,$d_blksize,$d_blocks); foreach $zipfile (@ziplist){ ($d_dev,$d_ino,$d_mode,$d_nlink,$d_uid,$d_gid,$d_rdev,$d_size,$d_atime,$d_mtime,$d_ctime,$d_blksize,$d_blocks)=stat("$zipdir/$zipfile"); if ($now > $d_mtime + $expire * 60){ unlink("$zipdir/$zipfile"); } } my $from; if (! exists $in{from}) { $from = 1; } else { $from = $in{from}; } my $to; if (! exists $in{to}){ $to = $num_in_a_page; } else { $to = $in{to}; } &beginning($from,$to); print qq($back\n); print "
\n"; print qq|
\n|; print qq|\n|; my $alldownload_name = ('チェックした分をまとめてダウンロード','Download all')[$lang]; my $allon_name = ('全部オン','All on')[$lang]; my $alloff_name = ('全部オフ','All off')[$lang]; print qq|

\n|; &listlink($from,$to); print "

\n"; print qq|$allon_name\n|; print qq|$alloff_name
\n|; my $i; for ($i=1;$i<=$#filelist;$i++){ if ($i < $from or $i > $to) { next; } my $dlfile = $filelist[$i]; chomp($dlfile); my $dllistfile = "$dldir/$dlfile"; ($d_dev,$d_ino,$d_mode,$d_nlink,$d_uid,$d_gid,$d_rdev,$d_size,$d_atime,$d_mtime,$d_ctime,$d_blksize,$d_blocks)=stat("$dllistfile"); my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime($d_mtime); my $update; if ($lang) { $update = sprintf("%02s:%02s %s/%s/%s",$hour,$min,$mon+1,$mday,$year+1900); } else { $update = sprintf("%s年%s月%s日%02s時%02s分",$year+1900,$mon+1,$mday,$hour,$min); } my $size; if ($d_size > 1048576){ $size = sprintf("%.1fMB",$d_size/1048576); } elsif ($d_size > 1024){ $size = sprintf("%.1fkB",$d_size/1024); } else { $size = sprintf("%dB",$d_size); } my $checkname = "check_$i"; print qq||; print "$i, $dlfile ($size)"; print " .......... $update
\n"; } print "

\n"; &listlink($from,$to); print qq|

\n|; print "

\n"; &ending; } sub beginning { my ($from,$to) = @_; print $q->header(-type=>'text/html', -charset=>"$charset"); print ""; print < $title HEADPRINT &jsset($from,$to) if ($in{mode} eq ''); print "\n"; print "\n"; } sub ending { my $mysite = ('http://www.hidekik.com/','http://www.hidekik.com/en/')[$lang]; print "
\n"; print qq|
flist3.cgi Ver. $version
Copyright(c) 2005-$updatedyear, hidekik.com
\n|; print ""; print ""; } sub error { &beginning; print "
$_[0]
\n"; &ending; exit; } sub jsset { my ($from,$to) = @_; my (@locallist) = @filelist; shift(@locallist); print < function allcheck(){ if (document.selectfile.allon.checked==1){ document.selectfile.alloff.checked=0; JSDISP1 my $localfile; my $i=1; my $check; foreach $localfile (@locallist){ if ($i >= $from and $i <= $to) { $check = "check_$i"; print " document.selectfile.$check.checked=1;\n"; } $i++; } print " }\n"; print "}\n"; print <= $from and $i <= $to) { $check = "check_$i"; print " document.selectfile.$check.checked=0;\n"; } $i++; } print " }\n"; print "}\n"; print < JSDISP3 } sub listlink { my ($from, $to) = @_; my $i; for ($i=1;$i<=$#filelist;$i+=$num_in_a_page){ my $end = $i + $num_in_a_page - 1; if ($end > $#filelist) { $end = $#filelist; } print qq(); print $i . '-' . $end; print " "; } print "
\n"; }