#! c:/perl/bin/perl
#
# mlist2.cgi
# メールアドレスの表などCSVファイルのマトリックスを整理して表示
#
# 2.0 : 10/6/06 : maillist.cgiからユーザー編集モードを追加
#
# $Id: mlist2.cgi,v 1.1 2006/10/07 04:56:59 Hideki Kanayama Exp $
use strict;
use CGI qw(:cgi-lib);
use File::Basename;
use CGI::Carp qw(fatalsToBrowser);
my $lastupdatedyear = 2006;
my $version = "2.0";
my $script = basename($0);
my $admindat = "adminpwd.dat";
my $setupfile = "maillist_setup.pl";
my $lang = 0;
my $charset = "Shift_JIS";
#####################################
# ページタイトル
our $title = 'メールリスト2';
# データファイル名
our $datafile = "maillist.csv";
# 戻りリンク 1:on 0:off
our $back_en = 1;
our $back = "../maillist.html";
our $backname = "戻る";
# 編集リンク 1:on 0:off
our $edit_en = 1;
our $editname = "編集";
# 管理用リンク 1:on 0:off
our $setup_en = 1;
our $setupname = "管理用";
# バックグラウンド
our $bgcolor = 'white';
# 管理人オンリーモード 1:有効 0:無効
our $admin_only = 1;
# セパレーター
our $separator = ',';
# 最初に表示するカラム
# 名前のすぐ右のカラム:1, その右:2, ...
our $default_menu = 1;
# 一列に表示するメニューの数
our $menu_cr = 10;
# all1/all2の選択
our $all1_en = 'on';
our $all2_en = 'on';
our $all1_name = "全部1";
our $all2_name = "全部2";
# メニューの色
our $menu_back = "gray";
our $menu_front = "white";
#スタイルシート 1:on 0:off
our $style_sheet_en = 1;
our $style_sheet = '
A:link {text-decoration: none}
A:visited {text-decoration: none}
A:active {text-decoration: none}
';
#
〜内に挿入できる構文 1:on, 0:off
our $head_insert_en = 0;
our $head_insert = '\n";
print "$title \n";
print "$backname \n" if ($back_en == 1);
print "$editname \n" if ($edit_en == 1);
print "$setupname \n" if ($setup_en == 1);
my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks) = stat $datafile;
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($mtime);
printf("%4d年%d月%d日%2d時%02d分更新
\n",$year+1900,$mon+1,$mday,$hour,$min);
$in{mode} = --$default_menu unless (exists $in{mode});
my $title_flag = '';
my $link_flag = '';
my @titlelist;
my @linklist;
my $cols;
my $ref;
open(FILE,"< $datafile") || &error("データファイル$datafileが開けません");
while (){
chomp;
next if /^\s*#/;
next if /^\s*$/;
my @line = split /$separator/;
if (/^\s*$firsttitle/ and $title_flag ne 'done') {
shift @line;
$cols = $#line + 1;
print qq{\n} if ($title_flag ne 'done');
my $i = 0;
foreach (@line){
s/^\s*?$//;
if ($in{mode} !~ /^\d/ or $i ne $in{mode}) {
print qq{$_ \n};
} else {
print qq{$_ };
}
print " \n\n" if ($i != 0 and ($i+1) % $menu_cr == 0);
$i++;
}
if ($all1_en eq 'on'){
if ($in{mode} ne 'all1') {
print qq{$all1_name \n};
} else {
print qq{$all1_name \n};
}
print " \n\n" if ($i != 0 and ($i+1) % $menu_cr == 0);
}
if ($all2_en eq 'on'){
if ($in{mode} ne 'all2') {
print qq{$all2_name \n};
} else {
print qq{$all2_name \n};
}
print qq{ } if ($title_flag ne 'done');
}
print qq{
} if ($title_flag ne 'done');
@titlelist = @line;
$title_flag = 'done';
} elsif (/^\s*$firstlink/ and $link_flag ne 'done'){
shift @line;
foreach (@line){s/^\s*//; s/\s*$//;}
@linklist = @line;
$link_flag = 'done';
}
last if ($title_flag eq 'done' and $link_flag eq 'done');
}
close(FILE);
print "\n";
if ($in{mode} =~ /^\d/ or $in{mode} eq 'all1') {
print "
\n";
} else {
$cols=$#titlelist+1;
print "\n";
print "\n";
my $i;
for ($i=0;$i<=$cols;$i++){
if ($i == 0){
print " \n";
} else {
print "$titlelist[$i-1] \n";
}
}
print " \n";
}
open(FILE,"< $datafile") || &error("データファイル$datafileが開けません");
while (){
chomp;
next if /^\s*#/;
next if /^\s*$/;
my @line = split /$separator/;
foreach (@line) {s/^\s*?$//;}
my $addr = '';
if (!/^\s*$firsttitle/ and !/^\s*$firstlink/) {
my $name = shift @line;
$name =~ s/^\s*//; $name =~ s/\s*$//;
next if ($#line == -1);
if ($in{mode} eq 'all1'){
print "\n";
print "$name \n";
print "\n";
print "\n";
my $wrote = 0;
my $i;
for ($i=0;$i<=$#line;$i++){
if ($line[$i] =~ /^(\d+)$/){
$ref = $1 - 1;
} else {
$ref = $i;
}
$addr = $line[$ref];
next if ($addr eq '');
print "\n";
print "$titlelist[$i]: \n";
if ($linklist[$i] eq 'none' or $linklist[$i] eq ''){
print "$addr \n";
} else {
print "$addr \n";
}
print " \n";
$wrote = 1;
}
print " \n" if ($wrote == 0);
print "
\n";
print " \n";
} elsif ($in{mode} eq 'all2'){
print " \n";
print "$name \n";
my $i;
for ($i=0;$i<=$cols-1;$i++){
if ($line[$i] =~ /^(\d+)$/){
$ref = $1 - 1;
} else {
$ref = $i;
}
$addr = $line[$ref];
if ($addr eq '' or $#line == -1){
print " \n";
} elsif ($linklist[$i] eq 'none' or $linklist[$i] eq ''){
print "$addr \n";
} else {
print "$addr \n";
}
}
print " \n";
} else {
if ($line[$in{mode}] =~ /^(\d+)$/){
$ref = $1 - 1;
} else {
$ref = $in{mode};
}
$addr = $line[$ref];
next if ($addr eq '');
print "\n";
print "$name \n";
if ($linklist[$in{mode}] eq 'none' or $linklist[$in{mode}] eq ''){
print "$addr \n";
} else {
print "$addr \n";
}
print " \n";
}
}
}
close(FILE);
print "
\n";
print "\n";
&htmltail;
}
sub htmlhead {
my $title = shift;
my $bgimage = "bgcolor=\"$bgcolor\"";
print "Content-type:text/html\n\n";
print "\n";
print "\n";
print " \n";
print "$title \n";
if ($head_insert_en == 1){
print "$head_insert\n";
}
if ($style_sheet_en == 1){
print "\n";
}
print "\n";
print "\n";
}
sub htmltail {
my $mysite = ('http://www.hidekik.com/','http://www.hidekik.com/en/')[$lang];
print "mlist2.cgi Ver. $version
\n";
print "\n";
print "\n";
exit;
}
sub error {
my ($msg) = shift;
&htmlhead($msg);
print "$msg \n";
&htmltail;
exit;
}
sub fileedit {
if ($in{pwd} eq '' and $admin_only){
&enteradmin;
}
if (! &checkadmin($in{pwd}) and $admin_only) {
&error("パスワードが違います。");
}
&htmlhead($title);
print <
データファイル編集
END2
&htmltail;
}
sub filewrite {
if ($admin_only and !&checkadmin($in{pwd})){
&error("管理人パスワードが違います。");
}
open (FILE, "> $datafile") || "データファイル$in{datafile}を作成できません。\n";
print FILE $in{contents};
close(FILE);
print "Location: $script\n\n";
}
sub makecrypt {
my $plain = shift;
my $salt = join "", ('.', '/', 0..9, 'A'..'Z', 'a'..'z')[rand 64, rand 64];
my $result = crypt($plain,$salt) or crypt($plain,'$1$'.$salt.'$');
return $result;
}
sub setadminpwd {
print $q->header({-type=>'text/html',-charset=>$charset});
print "\n";
print "\n";
print " \n";
print " \n";
print "管理者用パスワード \n";
print "\n";
print "\n";
print "\n";
print "管理者用パスワードを設定してください。 \n";
print " \n";
print " \n";
print " \n";
print " \n";
print "\n";
exit;
}
sub wradminpwd {
my $passwd = &makecrypt($in{pwd});
if (open(FILE,"> $admindat")){
print FILE "$passwd";
close(FILE);
} else {
print $q->header({-type=>'text/html',-charset=>$charset});
print "\n";
print "\n";
print " \n";
print " \n";
print "パスワードファイル作成に失敗しました \n";
print "\n";
print "\n";
print "パスワードファイル作成に失敗しました。";
print "\n";
exit;
}
}
sub mbdecode {
my (%in) = @_;
my ($key,$value);
while (($key,$value)=each %in){
$value =~ s/\r\n/\n/g if ($key eq 'contents');
$in{"$key"}=$value;
}
return(%in);
}
############################## セットアップ ################################
sub setup {
if ($in{'pwd'} eq "") {
&enteradmin;
}
if (!&checkadmin($in{pwd})){
&error("パスワードが違います。");
}
&htmlhead($title);
my @backlink_check;
my @edit_check;
my @setup_check;
my @admin_only_check;
my %all1_check;
my %all2_check;
my @style_sheet_check;
my @head_insert_check;
$backlink_check[$back_en] = "checked";
$edit_check[$edit_en] = "checked";
$setup_check[$setup_en] = "checked";
$admin_only_check[$admin_only] = "checked";
$all1_check{'on'} = "checked";
$all2_check{'on'} = "checked";
$style_sheet_check[$style_sheet_en] = "checked";
$head_insert_check[$head_insert_en] = "checked";
print <
SETUPWIN
&htmltail;
}
############################## セットアップ作成 ############################
sub wrsetup {
if ($in{'pwd'} eq "") {
&error("管理用パスワードを正しく入れてください。");
}
if (! &checkadmin($in{pwd})){
&error("パスワードが違います。");
}
my @nodecode=('style_sheet',
'head_insert',
);
foreach (@nodecode){
$in{$_} =~ s/ /\n/g;
$in{$_} =~ s/<//g;
$in{$_} =~ s/&\#44;/,/g;
}
open(FILE,"> $setupfile") || error('$セットアップファイルを作成できません。$setupfileのディレクトリのパーミッションを確認してください。');
print FILE <〜内に挿入できる構文 1:on, 0:off
\$head_insert_en = $in{head_insert_en};
\$head_insert = '$in{head_insert}';
1;
END
close(FILE);
print "Location: $script\n\n";
}
sub checkadmin {
my $pwd = shift;
if (open(FILE,"< $admindat")){
my $filepwd = ;
close(FILE);
my $inpwd = crypt($pwd,$filepwd);
return ("$inpwd" eq "$filepwd");
} else {
&error('パスワードファイルが存在しません');
}
}
sub enteradmin {
&htmlhead($title);
print <
管理人パスワードを入力してください。
END
&htmltail;
}