フォームからの入力(その3)

                             <p>ソースコード</p>
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/perl

# 初期設定
# require Jcode;
# $JcodeVer = "Jcode $Jcode::VERSION";
# *Jgetcode = &amp;Jcode::getcode;
# *Jconvert = &amp;Jcode::convert;

require "../../../cgi-bin/jcode.pl";
$JcodeVer = "jcode.pl $jcode::version";
*Jgetcode = &amp;jcode::getcode;
*Jconvert = sub { &amp;jcode::to($_[1], $_[0], $_[2]); };

@G_styles = ('../../ipp.css','../test.css');
$G_title = 'テスト18';
$G_myCode = &amp;Jgetcode('漢字');
$G_Code = 'jis';
$G_Charset = 'iso-2022-jp';
%G_form=();

$G_scrName = $ENV{'SCRIPT_NAME'};
if($G_scrName =~ /ts[0-9]{4}/){
    $G_scrName = $&amp;;
    $G_linkFile = "../$G_scrName.htm";
}

{
    &amp;printHeader;
    print "<div class=test>n";

    if($#ARGV == -1){
        if($ENV{'QUERY_STRING'}){
            &amp;formRead;
            &amp;formWrite;
        }else{
            &amp;printForm("get");
        }
    }else{
        &amp;error('<h2>無効なコマンドが渡されました。</h2>');
    }

    print "</div>n";
    &amp;printFooter;

    exit;
}

sub error{
    &amp;Jprint(@_);
    print "</div>n";
    &amp;printFooter;
    exit;
}

sub formWrite{
    local($name, $value);
    print "<table border=1>n";
    &amp;Jprint("<tr><th>エレメントの名前<th>エレメントの中身</tr>n");
    while (($name, $value) = each(%G_form)){
        &amp;JconvPrint("<tr><td>$name<td>$value</tr>n");
    }
    print "</table>n";
}

sub formRead{
    local($query_string, @elements, $elm, $name, $value, $code, $method);
    $method = $ENV{'REQUEST_METHOD'};
    if($method eq "POST"){
        read(STDIN, $query_string, $ENV{'CONTENT_LENGTH'});
    }elsif($method eq "GET"){
        $query_string = $ENV{'QUERY_STRING'};
    }else{
        &amp;error('methodは「post」か「get」にしてください。');
    }
    @elements = split(/&amp;/, $query_string);  # 中身はpostもgetも同じ
    foreach $elm (@elements){
        ($name, $value) = split(/=/, $elm);
        $value =~ s/+/ /g; # =~ tr/+/ /; と書くのが普通なのか?
        $value =~ s/%([0-9a-fA-F][0-9a-fA-F])/pack("C", hex($1))/eg;
        $code = &amp;Jgetcode($value);
        if($code ne 'euc'){ $value = &amp;Jconvert($value, 'euc', $code); } # とりあえずEUCに変換
        $value =~ s/&amp;/&amp;amp;/g;  # メタ文字(?)を無効化
        $value =~ s/</&amp;lt;/g;   # タグを無効化
        $value =~ s/>/&amp;gt;/g;   # タグを無効化
        $value =~ s/rn?/n/g; # 改行を統一
        $value =~ s/n/<br>/g;  # 改行を<br>に変換
        $G_form{$name} = $value;
    }
}

sub printForm{
    print "<form method=$_[0] action="$ENV{'SCRIPT_NAME'}?output">n";
    print "<input type=text name=tx1 value="tx1"><br>n";
    print "<input type=text name=tx2 value="tx2"><br>n";
    &amp;Jprint("<textarea name=tx3 cols=40 rows=4>tx3 日本語 英語 どちらでもnタグなどは無効になります。</textarea><br>n");
    print "<input type=submit value="Submit!">n";
    print "</form>n";
}

sub printHeader{
    if($G_Charset){
        print "Content-type: text/html; charset=$G_Charsetnn";
    }else{
        print "Content-type: text/htmlnn";
    }
    print '<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">'."n";
    print "<html lang=ja>n<head>n";
    if($G_Charset){ print "<meta http-equiv="Content-Type" content="text/html; charset=$G_Charset">n"; }
    print '<meta http-equiv="Content-Script-Type" content="text/javascript">',"n";
    print '<meta http-equiv="Content-Style-Type" content="text/css">'."n";
    &amp;Jprint ("<title>$G_title</title>n");
    foreach (@G_styles){ print "<link rel="stylesheet" type="text/css" href="$_">n"; }
    print "</head>n<body>n";
    print "<div class=head>n";
    &amp;Jprint ("<h1>$G_title</h1><hr>n");
    &amp;printlinks;
    print "<hr></div>n";
}

sub printFooter{
    print "<div class=foot><hr>n";
    &amp;printlinks;
    print "<hr>n";
    &amp;Jprint("漢字コード変換 : $JcodeVer<br>n");
    open(IN, '../../sig.txt');
    print while (<IN>);
    close(IN);
    print "</div>n";
    print "</body></html>n";
}

sub printlinks{
    print "<a href="../../../index.htm">Home</a>n";
    print "/n<a href="../../">Perl</a>n";
    print "/n<a href="../">TestCGI Index</a>n";
    if($G_linkFile){ &amp;Jprint ("/n<a href="$G_linkFile">$G_titleの解説</a>n"); }
}

sub Jprint{
    if($G_Code eq $G_myCode){
        foreach (@_){ print; }
    }else{
        foreach (@_){ print &amp;Jconvert($_, $G_Code, $G_myCode); }
    }
}

sub JconvPrint{
    foreach (@_){ print &amp;Jconvert($_, $G_Code, &amp;Jgetcode($_)); }
}
                          </div>

Comments

comments powered by Disqus