package SPLMget; use strict; use warnings; use Data::Dumper; use LWP::UserAgent; my $ua = LWP::UserAgent->new(); $ua->agent('Perl LWP-UserAgent'); sub login { my $class = shift; my $logindata = shift; my $response = sendGETrequest($class, $logindata->{gtacURL}); return 0 if (!$response->is_error); my $postData = 'credential_0=' . "$logindata->{gtacUsername}" .'&credential_1=' . "$logindata->{gtacPassword}" . '&destination=%2F&protocol=http'; $response = sendPOSTrequest($class, $logindata->{sgtacURL}, $postData); return 0 if (!$response->is_redirect); $class->{COOKIE} = $response->header('Set-Cookie'); $response = sendGETrequest($class, $logindata->{gtacURL}); return 1; } sub readFilesAndDirs { my $class = shift; my $loc = shift; my $items = []; my $response = sendGETrequest($class, $loc); return 0 if (!$response->is_success); my $content = $response->content; my ($h,$b) = split('', $content); my ($table, $c) = split('', $b); my @rows = $table =~ /\(.+?)\<\/tr\>/gs; foreach my $row (@rows) { $row =~ s/\n//g; next if ($row !~ /\(.*?)\<\/td\>/g; if (defined $path) { next if ($path =~ /\.\./); ($link, $linkDesc) = $path =~ /^\(\w+)\/\<\/a\>$/; if (defined $link) { if ($type eq 'Directory') { $type = 1; } else { $type = 0; } my $item = { DATE => $date, isDIR => $type, LINK => $link, DESCRIPTION => $linkDesc }; push $items, $item; } } } print Dumper $items; return $items } sub sendGETrequest { my $class = shift; my $url = shift; my $getRequest = HTTP::Request->new(GET => $url); $getRequest->push_header('Accept-Encoding' => 'gzip, deflate'); $getRequest->push_header('Connection' => 'keep-alive'); $getRequest->push_header('Accept' => 'image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*'); $getRequest->push_header('Cookie' => $class->{COOKIE}) unless ($class->{COOKIE} eq ''); my $response = $ua->request($getRequest); return $response; } sub sendPOSTrequest { my $class = shift; my $url = shift; my $postData = shift; my $postRequest = HTTP::Request->new(POST => $url); $postRequest->content_type('application/x-www-form-urlencoded'); $postRequest->push_header('Accept-Encoding' => 'gzip, deflate'); $postRequest->push_header('Connection' => 'keep-alive'); $postRequest->push_header('Accept' => 'image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*'); $postRequest->content($postData); my $response = $ua->request($postRequest); return $response; } sub new { my $class = shift; my $self = bless {}, $class; $self->{COOKIE} = ''; return $self; } 1;