\n};
# 出力メッセージ
if ($response) {
chomp $response;
$response =~ s{\n}{ }g;
} else {
$response = 'ready to upload file.';
}
}
# アップロード処理
sub upload_file {
# ディレクトリが無かったら作成
unless (-d $dir) {
require File::Path;
unless (File::Path::mkpath($dir)) {
&err_msg("cannot make dir: $!.");
return;
}
}
my $outfh = FileHandle->new;
foreach my $upfh (param($cn_upload)) {
$upfh or next;
# ファイル名の設定
my $fn = "$upfh";
$fn =~ s{^.*[/:>\]\\]}{}; # cf. File::Basename.pm
if ($fn =~ m{[^\w.-]}) {
&err_msg('bad filename (only 0-9 A-Z a-z ._-).');
next;
}
my $file = "$dir/$fn";
# フォームデータをファイルに書き込み
unless ($outfh->open(">$file")) {
&err_msg("cannot write $fn: $!.");
next;
}
my $buffer;
while ( read($upfh, $buffer, 1024) ) {
print $outfh $buffer;
}
$outfh->close;
$response .= "'$fn' has been uploaded successfully.\n";
}
}
# ファイル削除処理
sub remove_file {
my @file = param($cn_remove);
foreach my $fn (@file) {
my $file = "$dir/$fn";
if (-f $file and unlink $file) {
$response .= "'$fn' has been removed successfully.\n";
} else {
&err_msg("cannot remove $fn: $!.");
}
}
# ディレクトリが空ならディレクトリを削除
my $dh = DirHandle->new($dir);
unless ($dh) {
&err_msg("cannot opendir: $!.");
return;
}
unless (grep { !/^\.\.?$/ } $dh->read) {
$dh->close;
rmdir $dir or &err_msg("cannot rmdir: $!.");
}
}
# エラーメッセージ
sub err_msg {
my $msg = $_[0];
$response .= "$msg\n";
$debug and warn "upload: $msg\n";
1;
}
1;
__END__
=encoding utf8
=head1 NAME
Blosxom Plug-in: upload
=head1 SYNOPSIS
目的: 記事ごとにディレクトリを作成し、
そこへファイルをアップロードする機能を加えます。
=head1 INSTALLATION
このプラグインファイルをプラグインディレクトリ($plugin_dir)に
置いて下さい。
=head1 CONFIGURATION
$base_dir, $base_url, $upload_flavour, $password を
環境に合わせて設定して下さい。
また、以下のようなアップロードフォームを、使用するフレーバーの
footテンプレートファイルに加えます。
上記例はHTMLの場合です。
XHTMLの場合は空要素を">"ではなく" />"で閉じます。
ファイル選択用のinput要素を複数並べることにより、複数のファイルを
同時にアップロードすることも可能です。
アップロードされたファイルの一覧は定義リストの形式で表示されます。
uploaded files
aaaa.txt (remove[ ])
bbbb.txt (remove[ ])
cccc.txt (remove[ ])
=head1 NOTICE
アップロードするファイルの名前には、英数字及び . _ - のみが
使用できます。
=head1 VERSION
2005-11-19
=head1 AUTHOR
北村曉 (KITAMURA Akatsuki)
,
http://www.akatsukinishisu.net/
=head1 SEE ALSO
Blosxom Home/Docs/Licensing: http://www.blosxom.com/
Blosxom Plugin Docs: http://www.blosxom.com/plugins/
=head1 LICENSE
This Blosxom Plug-in Copyright (c) 2005, KITAMURA Akatsuki
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.