« auto/share/dist/Nes/cgi-bin/obj/location.pl
↑ Summary
auto/share/dist/Nes/cgi-bin/obj/session.pl »
| 1 | #!/usr/bin/perl | ||
| 2 | |||
| 3 | # ----------------------------------------------------------------------------- | ||
| 4 | # | ||
| 5 | # Nes by Skriptke | ||
| 6 | # Copyright 2009 - 2010 Enrique F. Castañón Barbero | ||
| 7 | # Licensed under the GNU GPL. | ||
| 8 | # | ||
| 9 | # CPAN: | ||
| 10 | # http://search.cpan.org/dist/Nes/ | ||
| 11 | # | ||
| 12 | # Sample: | ||
| 13 | # http://nes.sourceforge.net/ | ||
| 14 | # | ||
| 15 | # Repository: | ||
| 16 | # http://github.com/Skriptke/nes | ||
| 17 | # | ||
| 18 | # Version 1.03 | ||
| 19 | # | ||
| 20 | # lwp.pl | ||
| 21 | # | ||
| 22 | # ----------------------------------------------------------------------------- | ||
| 23 | |||
| 24 | use Nes; | ||
| 25 | my $nes = Nes::Singleton->new('./lwp.nhtml'); | ||
| 26 | my $obj = $nes->{'query'}->{'q'}{'obj_param_0'}; | ||
| 27 | my $url = $nes->{'query'}->{'q'}{$obj.'_param_1'}; | ||
| 28 | my $extrac = $nes->{'query'}->{'q'}{$obj.'_param_2'} || 'content'; | ||
| 29 | my $method = $nes->{'query'}->{'q'}{$obj.'_param_3'} || 'GET'; | ||
| 30 | my $query = $nes->{'query'}->{'q'}{$obj.'_param_4'}; | ||
| 31 | my $charset = $nes->{'query'}->{'q'}{$obj.'_param_5'}; | ||
| 32 | my $uagent = $nes->{'query'}->{'q'}{$obj.'_param_6'} || 'Nes/1.0'; | ||
| 33 | my $uamail = $nes->{'query'}->{'q'}{$obj.'_param_6'} || $ENV{'SERVER_ADMIN'}; | ||
| 34 | $uamail = 'no-mail@example.com' if $uamail !~ /[^@]?+\@[^@]?+\..?+/; | ||
| 35 | my %tags; | ||
| 36 | |||
| 37 | my $call_from_nhtml = 1 if $nes->{'this_template_name'} =~ /\.?html$/; | ||
| 38 | my $call_from_pl = 1 if $nes->{'this_template_name'} =~ /\.pl$/; | ||
| 39 | |||
| 40 | use LWP::RobotUA; | ||
| 41 | my $ua = LWP::RobotUA->new($uagent,$uamail); | ||
| 42 | $ua->delay(0); | ||
| 43 | |||
| 44 | use HTTP::Request::Common; | ||
| 45 | my $req = HTTP::Request->new(); | ||
| 46 | $req->method($method); | ||
| 47 | $url .= '?'.$query if $method eq 'GET' && $query; | ||
| 48 | $req->uri($url); | ||
| 49 | $req->referer("http://$ENV{'SERVER_NAME'}$ENV{'PATH_INFO'}"); | ||
| 50 | $req->content_type('application/x-www-form-urlencoded') if $method eq 'POST'; | ||
| 51 | $req->content($query) if $method eq 'POST' && $query; | ||
| 52 | my $response = $ua->request($req); | ||
| 53 | |||
| 54 | $tags{'status'} = $response->status_line; | ||
| 55 | $tags{'request'} = $req->as_string; | ||
| 56 | $tags{'request'} =~ s/\n/<br>\n/g; | ||
| 57 | $tags{'Content-Type'} = $response->header('Content-Type'); | ||
| 58 | $tags{'content'} = $response->content; | ||
| 59 | |||
| 60 | if ( $charset eq 'UTF-8' ) { | ||
| 61 | use utf8; | ||
| 62 | utf8::decode($tags{'content'}); | ||
| 63 | utf8::encode($tags{'content'}); | ||
| 64 | } | ||
| 65 | |||
| 66 | if ( $charset eq 'ISO' ) { | ||
| 67 | use utf8; | ||
| 68 | utf8::decode($tags{'content'}); | ||
| 69 | } | ||
| 70 | |||
| 71 | ( $tags{'title'} ) = $tags{'content'} =~ /<title>(.*)<\/title>/si; | ||
| 72 | ( $tags{'head'} ) = $tags{'content'} =~ /<head>(.*)<\/head>/si; | ||
| 73 | ( $tags{'body'} ) = $tags{'content'} =~ /<body>(.*)<\/body>/si; | ||
| 74 | |||
| 75 | if ( $extrac =~ /:-:/ ) { | ||
| 76 | |||
| 77 | my $include_tag = $extrac =~ s/^:-://; | ||
| 78 | $extrac =~ s/:-:$//; | ||
| 79 | my ( $star, $end ) = split (':-:', $extrac); | ||
| 80 | if ( $include_tag ) { | ||
| 81 | ( $tags{'extrac'} ) = $tags{'content'} =~ /(\Q$star\E.*\Q$end\E)/si; | ||
| 82 | } else { | ||
| 83 | ( $tags{'extrac'} ) = $tags{'content'} =~ /\Q$star\E(.*)\Q$end\E/si; | ||
| 84 | } | ||
| 85 | |||
| 86 | } else { | ||
| 87 | |||
| 88 | $tags{'extrac'} = $tags{$extrac}; | ||
| 89 | |||
| 90 | } | ||
| 91 | |||
| 92 | $tags{'extrac'} = $tags{'status'} if !$response->is_success; | ||
| 93 | |||
| 94 | print $tags{'extrac'} if $call_from_pl; | ||
| 95 | $nes->out(%tags) if $call_from_nhtml; | ||
| 96 | |||
| 97 | 1; |