#!/usr/bin/perl -w # # file : index.pl # author : Bas Couwenberg # license : GPL # use strict qw(subs vars); use Cwd; use File::Basename; use Date::Calc qw(:all); my $apache2 = 0; # assume apache 1.3 by default # detect apache version of ServerSignature in environment if($ENV{SERVER_SOFTWARE} =~ /^Apache\/2/i) { $apache2 = 1; } elsif($ENV{SERVER_SOFTWARE} =~ /^Apache\/1/i) { $apache2 = 0; } # Apache Request objects my ($r, $c, $s); if($apache2 != 1) { # Running on Apache 1.3 with mod_perl probably require Apache::Request; import Apache::Request; $r = Apache->request; } else { # Running on Apache 2.x with mod_perl2 probabl require Apache2::Request; require Apache2::Upload; require Apache2::compat; import Apache2::Request; import Apache2::Upload; import Apache2::compat; $r = Apache2::RequestUtil->request; } $c = $r->connection; $s = $r->server; # Initialize global variables my %args = &get_args($r, $c, $s, $apache2); $args{version} = '0.1'; # send http header and page content $r->header_out('Pragma' => 'no-cache'); $r->header_out('Cache-control' => 'no-cache'); $r->header_out('Connection' => 'close'); $r->header_out('X-Fortune' => "This webpage has Super Cow Powers"); $r->content_type('text/html'); $r->send_http_header; # If the UserAgent only requested the HTTP header (using HEAD perhaps), # we should send it and then exit, skipping the rest of the HTML document. return 'OK' if $r->header_only; # print the site HTML htmlheader( title => 'HTML to RML converter for Marcelloz DPCH stats', doctype => 'HTML', doctype_dtd => 'HTML 4.01 Transitional', doctype_dtd_url => 'http://www.w3.org/TR/html4/loose.dtd' ); $args{cols} = 100; $args{rows} = 12; if($args{html} ne '') { $args{rml} = html2rml($args{html}); } print "
\n"; print "Convert HTML to RML\n"; print "

\n"; print "HTML:
\n"; print "
\n"; print "
\n"; print "RML:
\n"; print "
\n"; print "
\n"; print "\n"; print "
\n"; print "

\n"; print "Source Code"; htmlfooter(); ################################[ Subroutines ]################################ sub html2rml { my $data = shift; # strip the [norml] RML tags if($data =~ s/\[\/?norml\]\r?\n?//ig) {} # strip the HTML tags while($data =~ /((.*?)<\/font>)/ig) { my $tag = $1; my $args = $2; my $text = $3; if($args =~ m/color='#(.*?)'/) { my $hex = $1; if($hex =~ /ff0000/) { $text = "[red]$text\[/red]"; } elsif($hex =~ /00ff00/) { $text = "[green]$text\[/green]"; } elsif($hex =~ /0000ff/) { $text = "[blue]$text\[/blue]"; } } $data =~ s/\Q$tag\E/$text/ig; } if($data =~ s/<\/font>//g) {} # convert HTML tags while($data =~ //ig) { my $s1 = $1; my $s2 = $1; if($s2 =~ s/'//g) {} if($s2 =~ s/"//g) {} $data =~ s//[table$s2]\n/ig; } if($data =~ s!
!\[/table\]\n!ig) {} # convert the HTML tags while($data =~ //i) { my $s1 = $1; my $s2 = $1; if($s2 =~ s/^ $//g) {} if($s2 =~ s/'//g) {} if($s2 =~ s/"//g) {} $data =~ s//[tr$s2]\n/ig; } if($data =~ s!![/tr]\n!ig) {} # convert the HTML tags while($data =~ //i) { my $s1 = $1; my $s2 = $1; if($s2 =~ s/^ $//g) {} if($s2 =~ s/'//g) {} if($s2 =~ s/"//g) {} $data =~ s//[td$s2]\n/ig; } if($data =~ s!!\n[/td]\n!ig) {} # convert the tags while($data =~ /((.*?)<\/a>)/i) { my $tag = $1; my $url = $2; my $name = $5; if($url =~ s/\[/\%5B/g){} if($url =~ s/\]/\%5D/g){} $data =~ s/\Q$tag\E/[url=$url]$name\[\/url]/ig; } # convert the tags while($data =~ /()/i) { my $tag = $1; my $url = $2; $data =~ s/\Q$tag\E/[img]$url\[\/img]/ig; } # convert the HTML tags if($data =~ s!<(\/?)b>![$1b]!ig) {} # convert the
HTML tags if($data =~ s!
![br]!ig) {} return $data; } ############## # # Routine: get_date() # Parameters: optionally a hash with the keys date and/or time set to something # Returns: the date in yyyy-mm-dd format, if only date is set # the time in hh:mm:ss format, if only time is set # the full date in yyyy-mm-dd hh:mm:ss format, if neither date and # time are set # both the date and the time, if they're both set # Description: Get the contents and other info of uploaded data # sub get_date { my %option = @_; my ($year,$month,$day,$hour,$min,$sec) = Date::Calc::Today_and_Now(); $month = '0'.$month if(length($month) == 1); $day = '0'.$day if(length($day) == 1); $hour = '0'.$hour if(length($hour) == 1); $min = '0'.$min if(length($min) == 1); $sec = '0'.$sec if(length($sec) == 1); my $date = "$year-$month-$day"; my $time = "$hour:$min:$sec"; if(!$option{date} && !$option{time}) { return "$date $time"; } elsif($option{time} && !$option{date}) { return $time; } elsif($option{date} && !$option{time}) { return $date; } else { return ($date, $time); } } ############## # # Routine: upload_properties() # Parameters: Apache::Upload object # Parameter name # %args # Returns: updated hash with HTTP headers, URL variables, and other global variables # Description: Get the contents and other info of uploaded data # sub upload_properties { my $upload = shift; # upload object my $param = shift; # parameter name my %args = @_; # Uploaded file properties $args{$param.'_Size'} = $upload->size; $args{$param.'_Type'} = $upload->type; $args{$param.'_Tempname'} = $upload->tempname; $args{$param.'_Filename'} = $upload->filename; # Save file content my $fh = $upload->fh; while (<$fh>) { $args{$param} .= $_; } # strip the filename from the start of the data if($args{$param} =~ s/^\Q$args{$param.'_Filename'}\E//) {} # handle files uploaded with Internet Explorer # instead of just the filename, IE sends the whole path # example: # X:\Porn\Pics\NakedLady.jpg if($args{$param.'_Filename'} =~ /^\w:.*\\(.*?)$/) { $args{$param.'_Filename'} = $1; } return \%args; } ############## # # Routine: get_args() # Parameters: Apache::Request object # Apache::Connection object # Apache::Server object # Apache2 flag # Returns: hash with HTTP headers, URL variables, and other global variables # Description: get the information from the HTTP request and Apache API, # and set global variables # sub get_args { my ($r, $c, $s, $apache2) = @_; my %args; # fill hash with URI parameters my $apr; if($apache2 != 1) { $apr = Apache::Request->new($r); } else { $apr = Apache2::Request->new($r); } # normal parameters foreach my $param ($apr->param) { my $i = 0; foreach my $value ($apr->param($param)) { # parameters can have multiple values, # number the ones afther the 1st $param .= $i if($i > 0); $args{$param} = $value; $i++; } } # handle POST requests if($r->method eq 'POST') { # uploaded files if($apache2 != 1) { for my $upload ($apr->upload) { my $param = $upload->name; foreach(keys %{$upload->info}) { $args{$param.'_Info_'.$_} = $upload->info($_); } %args = %{upload_properties($upload, $param, %args)}; } } else { my $upload_table_ref = $apr->upload; # APR::Table ref of uploads foreach my $name (keys %$upload_table_ref) { my $i = 0; foreach my $upload ($apr->upload($name)) { my $param = $upload->name; # parameters can have multiple values, # number the ones afther the 1st $param .= $i if($i > 0); foreach(keys %{$upload->info}) { $args{$param.'_Info_'.$_} = $upload->info->{$_}; } %args = %{upload_properties($upload, $param, %args)}; $i++; } } } } my ($date, $time) = get_date(date => 1, time => 1); $args{'Date'} = $date; $args{'Time'} = $time; $args{'User'} = $c->user if defined($c->user); $args{'Protocol'} = $r->protocol; $args{'Bytes'} = $r->bytes_sent if($r->bytes_sent); $args{'Method'} = $r->method; $args{'MethodNumber'} = $r->method_number; $args{'Request'} = $r->the_request; $args{'RequestTime'} = $r->request_time; $args{'URI'} = $r->uri; $args{'Filename'} = $r->filename; $args{'PathInfo'} = $r->path_info if($r->path_info); $args{'DocumentRoot'} = $r->document_root; $args{'UserAgent'} = $r->header_in('User-Agent'); # for compatibility with old scripts foreach(keys %{$r->headers_in()}) { $args{$_} = $r->headers_in->{$_}; } $args{'Hostname'} = $r->hostname; $args{'AuthName'} = $r->auth_name if($r->auth_name); $args{'AuthType'} = $c->auth_type if($c->auth_type); $args{'RemoteIP'} = $c->remote_ip; if($c->remote_host() eq '') { $args{'RemoteHostname'} = $r->get_remote_host; } else { $args{'RemoteHostname'} = $c->remote_host; } $args{'ServerName'} = $s->server_hostname; $args{'ServerAdmin'} = $s->server_admin; $args{'ServerPort'} = $s->port; $args{'ServerSoftware'} = $ENV{SERVER_SOFTWARE}; return %args; } ############## # # Routine: html_footer() # Parameters: hash of tags to generate # Returns: nothing # Description: print the tags of the header of the html page # sub htmlheader { my %args = @_; if($args{doctype}) { print "\n"; } print "\n"; print "\n"; print "\t$args{title}\n" if($args{title}); if($args{meta1_name}) { my $i = 1; while($args{'meta' . $i . '_name'}) { print "\t\n"; $i++; } } if($args{link1}) { my $i = 1; while($args{'link' . $i}) { print "\t\n"; $i++; } } if($args{script1}) { my $i = 1; while($args{'script' . $i}) { print "\t\n"; $i++; } } print "\t\n" if($args{style}); print "\n\n"; print "\n\n"; } ############## # # Routine: htmlfooter() # Parameters: none # Returns: nothing # Description: print the closing tags for the html page # sub htmlfooter { print "\n\n"; print "\n"; }