package WebGUI::Wobject::HttpProxy; #------------------------------------------------------------------- # WebGUI is Copyright 2001-2003 Plain Black LLC. #------------------------------------------------------------------- # Please read the legal notices (docs/legal.txt) and the license # (docs/license.txt) that came with this distribution before using # this software. #------------------------------------------------------------------- # http://www.plainblack.com info@plainblack.com #------------------------------------------------------------------- use strict; use URI; use LWP; use HTTP::Cookies; use HTTP::Request::Common; use HTML::Entities; use WebGUI::HTMLForm; use WebGUI::Icon; use WebGUI::International; use WebGUI::Privilege; use WebGUI::Session; use WebGUI::Wobject; use WebGUI::Wobject::HttpProxy::Parse; our @ISA = qw(WebGUI::Wobject); #------------------------------------------------------------------- sub name { return WebGUI::International::get(3,$_[0]->get("namespace")); } #------------------------------------------------------------------- sub new { my $class = shift; my $property = shift; my $self = WebGUI::Wobject->new( -properties=>$property, -extendedProperties=>{ proxiedUrl=>{ defaultValue=>'http://' }, timeout=>{ defaultValue=>30 }, removeStyle=>{ defaultValue=>1 }, filterHtml=>{ defaultValue=>"javascript" }, followExternal=>{ defaultValue=>1 }, rewriteUrls=>{ defaultValue=>1 }, followRedirect=>{ defaultValue=>0 } } ); bless $self, $class; } #------------------------------------------------------------------- sub uiLevel { return 5; } #------------------------------------------------------------------- sub www_edit { my %hash; tie %hash, 'Tie::IxHash'; %hash=(5=>5,10=>10,20=>20,30=>30,60=>60); my $privileges = WebGUI::HTMLForm->new; my $properties = WebGUI::HTMLForm->new; my $layout = WebGUI::HTMLForm->new; $properties->url( -name=>"proxiedUrl", -label=>WebGUI::International::get(1,$_[0]->get("namespace")), -value=>$_[0]->getValue("proxiedUrl") ); $privileges->yesNo( -name=>"followExternal", -label=>WebGUI::International::get(5,$_[0]->get("namespace")), -value=>$_[0]->getValue("followExternal") ); $properties->yesNo( -name=>"followRedirect", -label=>WebGUI::International::get(8,$_[0]->get("namespace")), -value=>$_[0]->getValue("followRedirect") ); $properties->yesNo( -name=>"rewriteUrls", -label=>WebGUI::International::get(12,$_[0]->get("namespace")), -value=>$_[0]->getValue("rewriteUrls") ); $layout->yesNo( -name=>"removeStyle", -label=>WebGUI::International::get(6,$_[0]->get("namespace")), -value=>$_[0]->getValue("removeStyle") ); $layout->filterContent( -name=>"filterHtml", -value=>$_[0]->getValue("filterHtml") ); $properties->select( -name=>"timeout", -options=>\%hash, -label=>WebGUI::International::get(4,$_[0]->get("namespace")), -value=>[$_[0]->getValue("timeout")] ); return $_[0]->SUPER::www_edit( -properties=>$properties->printRowsOnly, -layout=>$layout->printRowsOnly, -privileges=>$privileges->printRowsOnly, -helpId=>1, -headingId=>2 ); } #------------------------------------------------------------------- sub www_view { my (%formdata, @formUpload, $redirect, $response, $header, $userAgent, $proxiedUrl, $request, $content); my $output = $_[0]->displayTitle; $output .= $_[0]->description; my $node = WebGUI::Node->new("temp",$_[0]->get("namespace")."_cookies"); $node->create; my $cookiebox = WebGUI::URL::escape($session{var}{sessionId}); $cookiebox =~ s/[^A-Za-z0-9\-\.\_]//g; #removes all funky characters $cookiebox .= '.cookie'; $cookiebox = $node->getPath.$session{os}{slash}.$cookiebox; my $jar = HTTP::Cookies->new(File => $cookiebox, AutoSave => 1, Ignore_Discard => 1); if($session{form}{wid} == $_[0]->get("wobjectId") && $session{form}{func}!~/editSave/i) { $proxiedUrl = $session{form}{FormAction} || $session{form}{proxiedUrl} || $_[0]->get("proxiedUrl") ; } else { $proxiedUrl = $_[0]->get("proxiedUrl"); $session{env}{REQUEST_METHOD}='GET'; } $redirect=0; return $output unless ($proxiedUrl ne ""); until($redirect == 5) { # We follow max 5 redirects to prevent bouncing/flapping $userAgent = new LWP::UserAgent; $userAgent->agent($session{env}{HTTP_USER_AGENT}); $userAgent->timeout($_[0]->get("timeout")); $userAgent->env_proxy; $proxiedUrl = URI->new($proxiedUrl); #my $allowed_url = URI->new($_[0]->get('proxiedUrl'))->abs;; #if ($_[0]->get("followExternal")==0 && $proxiedUrl !~ /\Q$allowed_url/i) { if ($_[0]->get("followExternal")==0 && (URI->new($_[0]->get('proxiedUrl'))->host) ne (URI->new($proxiedUrl)->host) ) { return "
'.HTML::Entities::encode($response->content).''; } elsif ($response->content_type =~ /image\//i) { $content = '
GET status line: ".$response->status_line.""; } return $output.$content; } 1;