#!/usr/bin/perl

use LWP::UserAgent;
use HTML::LinkExtor;
use URI::URL;
use List::Compare;
use DBI;
use HTML::Form;

my $dbh = DBI->connect("dbi:SQLite:dbname=/opt/INTRINsec/vultureng/sql/db","","");
$sql = "SELECT url, remoteproxy FROM app WHERE id=?";
$sth = $dbh->prepare($sql);
$sth->execute($ARGV[4]);
my ($url, $remoteproxy) = $sth->fetchrow;

#$url = "http://www.caplorient.com/";
my $fqdn_source = URI->new($url);

$ua = LWP::UserAgent->new;
$ua->agent('Mozilla/4.0 (compatible)');
if ( $remoteproxy ne '') {
     $ua->proxy(['http', 'https'], $remoteproxy);
}
my @links = ();
&getlinks($url);

sub callback {
   my($tag, %attr) = @_;
   return if $tag ne 'a'; #A retravailler
   push(@links, values %attr);
}

sub getlinks (%) {
$lien=$_[0];
#print "$lien\n";
$p = HTML::LinkExtor->new(\&callback);
$res = $ua->request(HTTP::Request->new(GET => $lien),
                    sub {$p->parse($_[0])});
my $base = $res->base;
@links = map { $_ = url($_, $base)->abs; } @links;
#print join("\n", @links), "\n";

@links = sort @links;
for (my $i = 0; $i< $#links; $i++) {
    $links[$i] = "'REMOVE'" if $links[$i] eq $links[$i +1];
}
@links = grep { $_ ne "'REMOVE'" } @links;
$lc = List::Compare->new(\@links, \@links_off);
@links_on = $lc->get_unique;
}

while (my $link = shift @links_on) {
    print "$link\n";
    if ($link =~ /^http:\/\//) {
        my $fqdn_dest = URI->new($link);
        print $fqdn_source->host();
        print "\t";
        print $fqdn_dest->host();
        print "\n";
        if ($fqdn_source->host() eq $fqdn_dest->host()) {
        push(@links_off, $link);
        &getlinks($link);
}
}
}
push(@links_off, $url);
foreach my $url (@links_off)
  {
     open(F, '>form.txt');
     my $ub = LWP::UserAgent->new;
        $ub->agent('Mozilla/4.0 (compatible)');
     if ( $remoteproxy ne '') {
                $ub->proxy(['http', 'https'], $remoteproxy);
        }
        my $response = $ub->get($url);
     my @forms = HTML::Form->parse($response);

        foreach my $inputs (@forms)
	  {
            print F $inputs->action;
            print F "\nNom;";
            print F "Valeur;";
            print F "Type\n";
            foreach my $start_link ($inputs->inputs)
	      {
                print F $start_link->name;
                print F ";";
                print F $start_link->value;
                print F ";";
                print F $start_link->type;
                print F "\n";
	}
    }
}
