#!/usr/bin/perl
use LWP::Simple;

$dir='/grabber';#grabber directory
$limit=10;#limit of copyred html files

if(!opendir(D,"$dir")){mkpath("$dir", 1, 0755);}



use File::Path;
$ring=0;
$url= shift;
if($url!~m/.*\/$/){$url.="/";}
$base=$url;
$basen=$url;
%unic=();
%unic_img=();
print "Content-type: text/html\n\n";
print "Base URL-$url<br>\n";
 &get_url;


START:
foreach $url (keys %unic){
                   if($unic{$url}==1){
                               $unic{$url}=2;
                               $url=~m/$base/i;
                               $path=$';
                               @bd=split(/\//,$path);
                               $file_name=pop(@bd);
                               $x=@bd;
                               $path=join("\/",@bd);
                               if($path){$basen=$base.$path."/";}      
                               else{$basen=$base;} 
                               mkpath("$dir/$path", 1, 0755) if ($path);

                         if ($ring++<=$limit){

                                     &get_url;
                                     }
}}

foreach $value (values %unic){if ($value==1){goto START;}}
&save_img(1,%unic_img);#Сохранение изображений
#Site map
open (MAP,">map.txt");
print MAP "Pages in $base\n\n";
foreach $key (keys %unic_img){print MAP "Page=>$key\n";}
print MAP "\nImages\n\n";
foreach $key (keys %unic){print MAP "img=>$key\n";}
close (MAP);
M2:
print"For quit press enter";
<>;
exit;


sub get_url{
@a=();@links=();
$content=get($url);
while($content=~m/(<a.*?\n*?.*?>)/ig){push(@a,$1);}
foreach $l (@a){
$l=~s/<//g; $l=~s/>//g;
$l=~s/\n//g;#print "==>$l<br>";
$l=~s/'/ /g; $l=~s/"/ /g;
$l=~s/(href\s*?=\s*)/href=/gi;
$l=~m/(href=\S*)/i; $l=$1;
$l=~m/(href=)/i;
$n=$';
if($n=~/:\/\// || $n=~/mailto:/){
                  if($n=~/$base/i){push(@links,$n);}
                                }
else{push(@links,$n);}
}

$a=0;
foreach $sd (@links){$pod='';
                if($sd!~m/$base/i){
                                  if($base eq $basen){$links[$a]=$base.$links[$a];}
                                  else{
                                       if($sd!~/(\.\.\/)/){$links[$a]=$basen.$links[$a];}
                                                   else{$re=0;while($sd=~/(\.\.\/)/g){$re++;} $m=$x-$re;
                                                        for($d=0; $d<$m; $d++){ $pod.=$bd[$d]."/";} #print"pod=$pod";
                                                        $links[$a]=~s/(\.\.\/)*/$pod/;
                                                        $links[$a]=$base.$links[$a];}
                                      }
                                   }
              
                $a++;}
@new=();
foreach $lin (@links){
             if($lin=~m/($base)/i){                                        
                         unless($unic{$lin}){push(@new, $lin); $unic{$lin}=1;}
                                   }
                     }
##img
@img=();
while($content=~m/(<img.*?\n*?.*?>)/ig){push(@img,$1);}
foreach $im (@img){
                  $im=~s/\n//g;
                  $im=~s/<//g; $im=~s/>//g;# print"==>$im<br>";
                  $im=~s/'/ /g; $im=~s/"/ /g;
                  $im=~s/(src\s*?=\s*)/src=/gi;
                  $im=~m/(src=\S*)/i; $im=$1;
                  $im=~m/(src=)/i;
                  $im=$';
if($im=~m/$base/i || $im!~m/http:/i){if($im=~m/$base/i){push(@image, $im);}
                                      else{push(@image, $im);}}
                  }
$a=0;#goto M4;
foreach $sd (@image){$pod='';
                if($sd!~m/$base/i){
                                  if($base eq $basen){$image[$a]=$base.$image[$a];}
                                  else{
                                       if($sd!~/(\.\.\/)/){$image[$a]=$basen.$image[$a];}
                                                   else{$re=0;while($sd=~/(\.\.\/)/g){$re++;} $m=$x-$re;
                                                        for($d=0; $d<$m; $d++){ $pod.=$bd[$d]."/";} #print"pod=$pod";
                                                        $image[$a]=~s/(\.\.\/)*/$pod/;
                                                        $image[$a]=$base.$image[$a];}
                                      }
                                   }
              
                $a++;}

M4:

@uni_image=();
foreach $lin (@image){
             if($lin=~m/($base)/i){                                        
                         unless($unic_img{$lin}){push(@uni_image, $lin); $unic_img{$lin}=1;}
                                   }
                     }
  $ppt="file://$dir";
  $content=~s{$base}{$ppt}gi;
                               $namef="$dir";
                               $namef.="/".$path if ($path);
if($file_name eq ''){$namef.="/index.htm";}
                    else{           $namef.="/".$file_name;}

&save;


}

sub save{
open(F,">$namef");# || die "Can't open file";
print F $content;
close(F);
print "Save \"$file_name\" in $namef<br>\n";
}



sub save_img(\%){
($x,%uni)=@_;
foreach $key (sort keys %uni){
                               $key=~m/$base/i;
                               $path=$';
                               @di=split(/\//,$path);
                               $file_name=pop @di; 
                               $path=join("\/",@di);
                               mkpath("$dir/$path", 1, 0755) if ($path);
                               $name="$dir";
                               $name.="/".$path if ($path);
                               $name.="/".$file_name;
                          if(open(IMG,"$name")){print"File \"$url\" exist in $name\n"; return;}
                          else{
                               $content=get($key);
                               open (IMG,">$name") || die "Can't open file";
                               binmode IMG if($x==1);
                               print IMG $content; close(IMG);
                               print "save\"$file_name\" in $name<br>\n";
                               }
                               }

}