| 1 |
#!/usr/bin/env perl
|
| 2 |
# $Id: formfind,v 1.6 2004/10/19 18:36:17 bagder Exp $
|
| 3 |
#
|
| 4 |
# formfind.pl
|
| 5 |
#
|
| 6 |
# This script gets a HTML page on stdin and presents form information on
|
| 7 |
# stdout.
|
| 8 |
#
|
| 9 |
# Author: Daniel Stenberg <daniel@haxx.se>
|
| 10 |
# Version: 0.2 Nov 18, 2002
|
| 11 |
#
|
| 12 |
# HISTORY
|
| 13 |
#
|
| 14 |
# 0.1 - Nov 12 1998 - Created now!
|
| 15 |
# 0.2 - Nov 18 2002 - Enhanced. Removed URL support, use only stdin.
|
| 16 |
#
|
| 17 |
|
| 18 |
$in="";
|
| 19 |
|
| 20 |
if($ARGV[0] eq "-h") {
|
| 21 |
print "Usage: $0 < HTML\n";
|
| 22 |
exit;
|
| 23 |
}
|
| 24 |
|
| 25 |
sub namevalue {
|
| 26 |
my ($tag)=@_;
|
| 27 |
my $name=$tag;
|
| 28 |
if($name =~ /name *=/i) {
|
| 29 |
if($name =~ /name *= *([^\"\']([^ \">]*))/i) {
|
| 30 |
$name = $1;
|
| 31 |
}
|
| 32 |
elsif($name =~ /name *= *(\"|\')([^\"\']*)(\"|\')/i) {
|
| 33 |
$name=$2;
|
| 34 |
}
|
| 35 |
else {
|
| 36 |
# there is a tag but we didn't find the contents
|
| 37 |
$name="[weird]";
|
| 38 |
}
|
| 39 |
|
| 40 |
}
|
| 41 |
else {
|
| 42 |
# no name given
|
| 43 |
$name="";
|
| 44 |
}
|
| 45 |
# get value tag
|
| 46 |
my $value= $tag;
|
| 47 |
if($value =~ /[^\.a-zA-Z0-9]value *=/i) {
|
| 48 |
if($value =~ /[^\.a-zA-Z0-9]value *= *([^\"\']([^ \">]*))/i) {
|
| 49 |
$value = $1;
|
| 50 |
}
|
| 51 |
elsif($value =~ /[^\.a-zA-Z0-9]value *= *(\"|\')([^\"\']*)(\"|\')/i) {
|
| 52 |
$value=$2;
|
| 53 |
}
|
| 54 |
else {
|
| 55 |
# there is a tag but we didn't find the contents
|
| 56 |
$value="[weird]";
|
| 57 |
}
|
| 58 |
}
|
| 59 |
else {
|
| 60 |
$value="";
|
| 61 |
}
|
| 62 |
return ($name, $value);
|
| 63 |
}
|
| 64 |
|
| 65 |
|
| 66 |
while(<STDIN>) {
|
| 67 |
$line = $_;
|
| 68 |
push @indoc, $line;
|
| 69 |
$line=~ s/\n//g;
|
| 70 |
$line=~ s/\r//g;
|
| 71 |
$in=$in.$line;
|
| 72 |
}
|
| 73 |
|
| 74 |
while($in =~ /[^<]*(<[^>]+>)/g ) {
|
| 75 |
# we have a tag in $1
|
| 76 |
$tag = $1;
|
| 77 |
|
| 78 |
if($tag =~ /^<!--/) {
|
| 79 |
# this is a comment tag, ignore it
|
| 80 |
}
|
| 81 |
else {
|
| 82 |
if(!$form &&
|
| 83 |
($tag =~ /^< *form/i )) {
|
| 84 |
$method= $tag;
|
| 85 |
if($method =~ /method *=/i) {
|
| 86 |
$method=~ s/.*method *= *(\"|)([^ \">]*).*/$2/gi;
|
| 87 |
}
|
| 88 |
else {
|
| 89 |
$method="get"; # default method
|
| 90 |
}
|
| 91 |
$action= $tag;
|
| 92 |
$action=~ s/.*action *= *(\'|\"|)([^ \"\'>]*).*/$2/gi;
|
| 93 |
|
| 94 |
$method=uc($method);
|
| 95 |
|
| 96 |
$enctype=$tag;
|
| 97 |
if ($enctype =~ /enctype *=/) {
|
| 98 |
$enctype=~ s/.*enctype *= *(\'|\"|)([^ \"\'>]*).*/$2/gi;
|
| 99 |
|
| 100 |
if($enctype eq "multipart/form-data") {
|
| 101 |
$enctype="multipart form upload [use -F]"
|
| 102 |
}
|
| 103 |
$enctype = "\n--- type: $enctype";
|
| 104 |
}
|
| 105 |
else {
|
| 106 |
$enctype="";
|
| 107 |
}
|
| 108 |
|
| 109 |
print "--- FORM report. Uses $method to URL \"$action\"$enctype\n";
|
| 110 |
$form=1;
|
| 111 |
}
|
| 112 |
elsif($form &&
|
| 113 |
($tag =~ /< *\/form/i )) {
|
| 114 |
|
| 115 |
print "--- end of FORM\n";
|
| 116 |
$form=0;
|
| 117 |
if( 0 ) {
|
| 118 |
print "*** Fill in all or any of these: (default assigns may be shown)\n";
|
| 119 |
for(@vars) {
|
| 120 |
$var = $_;
|
| 121 |
$def = $value{$var};
|
| 122 |
print "$var=$def\n";
|
| 123 |
}
|
| 124 |
print "*** Pick one of these:\n";
|
| 125 |
for(@alts) {
|
| 126 |
print "$_\n";
|
| 127 |
}
|
| 128 |
}
|
| 129 |
undef @vars;
|
| 130 |
undef @alts;
|
| 131 |
}
|
| 132 |
elsif($form &&
|
| 133 |
($tag =~ /^< *(input|select)/i)) {
|
| 134 |
$mtag = $1;
|
| 135 |
|
| 136 |
($name, $value)=namevalue($tag);
|
| 137 |
|
| 138 |
if($mtag =~ /select/i) {
|
| 139 |
print "Select: NAME=\"$name\"\n";
|
| 140 |
push @vars, "$name";
|
| 141 |
$select = 1;
|
| 142 |
}
|
| 143 |
else {
|
| 144 |
$type=$tag;
|
| 145 |
if($type =~ /type *=/i) {
|
| 146 |
$type =~ s/.*type *= *(\'|\"|)([^ \"\'>]*).*/$2/gi;
|
| 147 |
}
|
| 148 |
else {
|
| 149 |
$type="text"; # default type
|
| 150 |
}
|
| 151 |
$type=uc($type);
|
| 152 |
if(lc($type) eq "reset") {
|
| 153 |
# reset types are for UI only, ignore.
|
| 154 |
}
|
| 155 |
elsif($name eq "") {
|
| 156 |
# let's read the value parameter
|
| 157 |
|
| 158 |
print "Button: \"$value\" ($type)\n";
|
| 159 |
push @alts, "$value";
|
| 160 |
}
|
| 161 |
else {
|
| 162 |
print "Input: NAME=\"$name\"";
|
| 163 |
if($value ne "") {
|
| 164 |
print " VALUE=\"$value\"";
|
| 165 |
}
|
| 166 |
print " ($type)\n";
|
| 167 |
push @vars, "$name";
|
| 168 |
# store default value:
|
| 169 |
$value{$name}=$value;
|
| 170 |
}
|
| 171 |
}
|
| 172 |
}
|
| 173 |
elsif($form &&
|
| 174 |
($tag =~ /^< *textarea/i)) {
|
| 175 |
my ($name, $value)=namevalue($tag);
|
| 176 |
|
| 177 |
print "Textarea: NAME=\"$name\"\n";
|
| 178 |
}
|
| 179 |
elsif($select) {
|
| 180 |
if($tag =~ /^< *\/ *select/i) {
|
| 181 |
print "[end of select]\n";
|
| 182 |
$select = 0;
|
| 183 |
}
|
| 184 |
elsif($tag =~ /[^\/] *option/i ) {
|
| 185 |
my ($name, $value)=namevalue($tag);
|
| 186 |
my $s;
|
| 187 |
if($tag =~ /selected/i) {
|
| 188 |
$s= " (SELECTED)";
|
| 189 |
}
|
| 190 |
print " Option VALUE=\"$value\"$s\n";
|
| 191 |
}
|
| 192 |
}
|
| 193 |
}
|
| 194 |
}
|