8 |
use HTTP::Daemon; |
use HTTP::Daemon; |
9 |
use HTTP::Status; |
use HTTP::Status; |
10 |
use IO::String; |
use IO::String; |
11 |
use CGI 2.50 qw/:standard :cgi-lib/; |
use CGI::Lite; |
12 |
|
use Template; |
13 |
|
use MWS; |
14 |
|
use URI::Escape; |
15 |
|
|
16 |
|
use Data::Dumper; |
17 |
|
|
18 |
|
my $debug = 1; |
19 |
|
|
20 |
my $d = HTTP::Daemon->new( Reuse => 1, LocalPort => 6969 ) || die; |
my $d = HTTP::Daemon->new( Reuse => 1, LocalPort => 6969 ) || die; |
21 |
print "Please contact me at: <URL:", $d->url, ">\n"; |
my $cgi = new CGI::Lite; |
22 |
|
my $mws = MWS->new('global.conf'); |
23 |
|
my $tt = Template->new({ |
24 |
|
INCLUDE_PATH => $mws->{config}->val('global', 'templates'), |
25 |
|
FILTERS => { |
26 |
|
'body5' => \&body5_filter, |
27 |
|
}, |
28 |
|
EVAL_PERL => 1, |
29 |
|
}); |
30 |
|
|
31 |
|
my $static_html = $mws->{config}->val('global', 'static_html'); |
32 |
|
|
33 |
|
print "Web server ready at: ", $d->url, "\n"; |
34 |
|
|
35 |
|
|
36 |
while ( my $c = $d->accept ) { |
while ( my $c = $d->accept ) { |
37 |
while ( my $r = $c->get_request ) { |
while ( my $r = $c->get_request ) { |
42 |
$ENV{'SERVER_PROTOCOL'} = $r->protocol; |
$ENV{'SERVER_PROTOCOL'} = $r->protocol; |
43 |
$ENV{'CONTENT_TYPE'} = $r->content_type; |
$ENV{'CONTENT_TYPE'} = $r->content_type; |
44 |
|
|
45 |
my $form_parameters; # GET/POST storage. |
# this part is based on CGI::Lite |
46 |
|
|
47 |
|
$cgi->close_all_files(); |
48 |
|
$cgi->{web_data} = {}; |
49 |
|
$cgi->{ordered_keys} = []; |
50 |
|
$cgi->{all_handles} = []; |
51 |
|
$cgi->{error_status} = 0; |
52 |
|
$cgi->{error_message} = undef; |
53 |
|
|
|
# is this a happy GET? |
|
54 |
if ( $r->method eq 'GET' || $r->uri =~ /\?/ ) { |
if ( $r->method eq 'GET' || $r->uri =~ /\?/ ) { |
55 |
$form_parameters = $r->uri; |
my $query_string = $r->uri; |
56 |
$form_parameters =~ s/[^\?]+\?(.*)/$1/; |
$query_string =~ s/[^\?]+\?(.*)/$1/; |
57 |
$CGI::Q = new CGI($form_parameters); |
$cgi->_decode_url_encoded_data (\$query_string, 'form'); |
58 |
} |
|
59 |
|
} elsif ( $r->method eq 'POST' ) { |
60 |
# possibly POST? |
|
61 |
if ( $r->method eq 'POST' ) { |
if ($r->content_type eq 'application/x-www-form-urlencoded') { |
62 |
|
# local $^W = 0; |
63 |
# now decide how we want to turn the parameters |
$cgi->_decode_url_encoded_data (\$r->content, 'form'); |
64 |
# over to CGI.pm. note that this will cause |
} elsif ($r->content_type =~ /multipart\/form-data/) { |
65 |
# problems with your STDIN with multipart forms. |
my ($boundary) = $r->content_type =~ /boundary=(\S+)$/; |
66 |
my $form_parameters = $r->content; |
$cgi->_parse_multipart_data ($r->content_length, $boundary); |
|
$ENV{'CONTENT_LENGTH'} = $r->content_length || 0; |
|
|
|
|
|
# sounds like multipart. |
|
|
if ( $form_parameters =~ /^--/ ) { |
|
|
|
|
|
my ($boundary) = split ( /\n/, $form_parameters ); |
|
|
chop($boundary); |
|
|
substr( $boundary, 0, 2 ) = ''; # delete the leading "--" !!! |
|
|
$ENV{'CONTENT_TYPE'} = |
|
|
$r->content_type . "; boundary=$boundary"; |
|
|
|
|
|
# this breaks STDIN forever. I've yet to discover |
|
|
# how to properly save and reassign STDIN after |
|
|
# we're done breaking things horrifically here. |
|
|
close STDIN; |
|
|
my $t = tie *STDIN, 'IO::String'; |
|
|
$t->open($form_parameters); |
|
|
$CGI::Q = new CGI(); |
|
67 |
} |
} |
68 |
|
} else { |
69 |
|
$c->send_error(RC_FORBIDDEN); |
70 |
|
} |
71 |
|
|
72 |
|
my $param = $cgi->{web_data}; |
73 |
|
my $url = $r->url->path; |
74 |
|
|
75 |
else { $CGI::Q = new CGI($form_parameters); } |
# XXX LOG |
76 |
|
print $r->method," ",$url,"\n",Dumper($param),"\n" if ($debug); |
77 |
|
|
78 |
|
# is this static page? |
79 |
|
if ($static_html && -f "$static_html/$url") { |
80 |
|
print "static file: $static_html/$url\n" if ($debug); |
81 |
|
$c->send_file_response("$static_html/$url"); |
82 |
|
$c->close; |
83 |
|
next; |
84 |
} |
} |
85 |
|
|
86 |
if (! $CGI::Q) { |
# template file name (use ?format=html as default) |
87 |
$c->send_error(RC_FORBIDDEN); |
my $tpl_file = 'master.'; |
88 |
|
$tpl_file .= $param->{'format'} || 'html'; |
89 |
|
|
90 |
|
# parse date from url |
91 |
|
my ($yyyy,$mm,$dd) = $mws->yyyymmdd; |
92 |
|
|
93 |
|
my $yyyymm; |
94 |
|
|
95 |
|
my $date_limit; |
96 |
|
|
97 |
|
if ($url =~ m,^/(\d{4})[/-](\d+)[/-](\d+),) { |
98 |
|
($yyyy, $mm, $dd) = $mws->fmtdate($1,$2,$3); |
99 |
|
$date_limit = "$yyyy-$mm-$dd"; |
100 |
|
} elsif ($url =~ m,^/(\d{4})[/-](\d+),) { |
101 |
|
($yyyy,$mm) = $mws->fmtdate($1,$2); |
102 |
|
$date_limit = "$yyyy-$mm"; |
103 |
|
} elsif ($url =~ m,^/(\d{4}),) { |
104 |
|
$date_limit = $mws->fmtdate($1); |
105 |
} |
} |
106 |
|
|
107 |
my $param = param("hello") || ''; |
# |
108 |
my $url = $r->url->path; |
# implement functionality and generate HTML |
109 |
|
# |
110 |
|
my $html; |
111 |
|
|
112 |
|
if ($param->{'search_val'} && $param->{'search_fld'} && !$param->{'search'}) { |
113 |
|
$param->{'search'} = $param->{'search_fld'}.":".$param->{'search_val'}; |
114 |
|
} elsif ($param->{'search'}) { |
115 |
|
($param->{'search_fld'}, $param->{'search_val'}) = split(/:/,$param->{'search'},2); |
116 |
|
} |
117 |
|
|
118 |
print "I saw: $param\n"; |
my $tpl_var = { |
119 |
|
param => $param, |
120 |
|
yyyy => $yyyy, |
121 |
|
mm => $mm, |
122 |
|
dd => $dd, |
123 |
|
date_limit => $date_limit, |
124 |
|
}; |
125 |
|
|
126 |
|
# |
127 |
|
# ?show_id=XXXXxxxx___message_id___xxxxXXXX |
128 |
|
if ($param->{'show_id'}) { |
129 |
|
|
130 |
|
$mws->reset_counters; |
131 |
|
my $row = $mws->fetch_result_by_id($param->{'show_id'}); |
132 |
|
$tpl_var->{message} = $row; |
133 |
|
} elsif ($param->{'search'} || $date_limit) { |
134 |
|
|
135 |
|
# show search results |
136 |
|
# ?search=foo:bar |
137 |
|
|
138 |
|
my @search = ( $param->{'search'} ); |
139 |
|
|
140 |
|
if ($date_limit) { |
141 |
|
push @search, "and" if (@search); |
142 |
|
push @search, "date:\"$date_limit\""; |
143 |
|
} |
144 |
|
|
145 |
# my $f = param("thefile"); |
if ($param->{sort_by}) { |
146 |
# print "thefile filename: $f\n"; |
push @search, "sort:".$param->{sort_by}; |
147 |
# { undef $/; print "thefile size: ", length(<$f>), "\n" } |
} |
148 |
|
|
149 |
my $res = HTTP::Response->new(RC_OK); |
print STDERR "search: ",join(" ",@search),"\n"; |
150 |
$res->content( qq{ |
|
151 |
<html> |
my $results = $mws->search(@search); |
152 |
hello = $param<br> |
my @res = $mws->fetch_all_results(); |
|
URL: $url |
|
|
</html> |
|
|
} |
|
|
); |
|
153 |
|
|
154 |
|
$tpl_var->{results} = \@res if (@res); |
155 |
|
$tpl_var->{total_hits} = $mws->{total_hits} || 0; |
156 |
|
|
157 |
|
} |
158 |
|
|
159 |
|
|
160 |
|
# push counters to template |
161 |
|
foreach my $f (qw(from to cc bcc)) { |
162 |
|
my $h = $mws->counter($f) || next; |
163 |
|
my @a; |
164 |
|
foreach my $k (sort { $h->{$b}->{usage} <=> $h->{$a}->{usage} } keys %$h) { |
165 |
|
push @a, $h->{$k}; |
166 |
|
} |
167 |
|
$tpl_var->{counters}->{$f} = [ @a ] if (@a); |
168 |
|
} |
169 |
|
|
170 |
|
# push calendar in template |
171 |
|
$tpl_var->{calendar} = $mws->counter('calendar'); |
172 |
|
|
173 |
|
$tt->process($tpl_file, $tpl_var, \$html) || die $tt->error(); |
174 |
|
|
175 |
|
# |
176 |
|
# send HTMLto client |
177 |
|
# |
178 |
|
|
179 |
|
my $res = HTTP::Response->new(RC_OK); |
180 |
|
$res->header( 'Content-type' => 'text/html; charset=ISO-8859-2' ); |
181 |
|
$res->content($html); |
182 |
$c->send_response($res); |
$c->send_response($res); |
183 |
|
|
184 |
$c->close; |
$c->close; |
185 |
} |
} |
186 |
undef($c); |
undef($c); |
187 |
} |
} |
188 |
|
|
189 |
|
# template toolkit filter |
190 |
|
|
191 |
|
#use Text::Context::EitherSide; |
192 |
|
|
193 |
|
sub body5_filter { |
194 |
|
my $text = shift; |
195 |
|
$text =~ s/^\s+//gs; |
196 |
|
$text =~ s/^[\>:\|=]+\s*.*?$//msg; # remove quoted text |
197 |
|
$text =~ s/[\n\r]+/\n/gs; # compress cr/lf |
198 |
|
if ($text =~ s,^((?:.*?[\n\r]){5}).*$,$1,s) { |
199 |
|
$text =~ s/[\n\r]*$/ .../; |
200 |
|
} |
201 |
|
$text =~ s/[\n\r]+--\s*[\n\r]+.*$//s; |
202 |
|
|
203 |
|
# my $context = Text::Context::EitherSide->new($text, context => 5); |
204 |
|
# return $context->as_string("perl"); |
205 |
|
|
206 |
|
return $text; |
207 |
|
} |
208 |
|
|