use
vars
qw($VERSION @ISA @EXPORT @EXPORT_OK)
;
@ISA
=
qw(Exporter)
;
@EXPORT
=
qw()
;
$VERSION
= (
'$Revision: 0.5 $ '
=~ /(\d+\.\d+)/)[0];
sub
new {
my
$class
=
shift
;
my
$self
= {};
bless
$self
,
$class
;
my
%param
=
@_
;
my
$mail
= new MIME::Lite
From
=>
$param
{
'From'
},
To
=>
$param
{
'To'
},
Subject
=>
$param
{
'Subject'
},
Type
=>
'multipart/related'
;
$self
->{_MAIL} =
$mail
;
$self
->{_DEBUG} = 1
if
$param
{
'Debug'
};
print
"Set proxy for http : "
,
$param
{
'Proxy'
},
"\n"
if
(
$self
->{_DEBUG} &&
$param
{
'Proxy'
});
$self
->{_AGENT} = new LWP::UserAgent
'MIME-Lite'
,
'alian@alianwebserver.com'
;
$self
->{_AGENT}->proxy(
'http'
,
$param
{
'Proxy'
})
if
$param
{
'Proxy'
};
$self
->{_HASH_TEMPLATE}=
$param
{
'HashTemplate'
}
if
$param
{
'HashTemplate'
};
return
$self
;
}
sub
parse
{
my
(
$self
,
$url_page
)=
@_
;
my
(
$type
,
@mail
,
$gabarit
);
print
"Get "
,
$url_page
,
"\n"
if
$self
->{_DEBUG};
my
$req
= new HTTP::Request(
'GET'
=>
$url_page
);
my
$res
=
$self
->{_AGENT}->request(
$req
);
if
(!
$res
->is_success) {
die
"$url_page n'est pas accessible"
;}
else
{
$gabarit
=
$res
->content;}
my
$analyseur
= HTML::LinkExtor->new;
$analyseur
->parse(
$gabarit
);
my
@l
=
$analyseur
->links;
$gabarit
=
$self
->include_css(
$gabarit
,
$res
->base);
$gabarit
=
$self
->include_javascript(
$gabarit
,
$res
->base);
(
$gabarit
,
@mail
) =
$self
->input_image(
$gabarit
,
$res
->base);
$gabarit
=
$self
->link_form(
$gabarit
,
$res
->base);
my
(
%images_read
,
%url_remplace
);
foreach
my
$url
(
@l
)
{
my
$urlAbs
= URI::WithBase->new(
$$url
[2],
$res
->base)->
abs
;
if
( (
$$url
[0] eq
'a'
)
&& (
$$url
[1] eq
'href'
)
&& (!
$url_remplace
{
$urlAbs
}) )
{
$gabarit
=~s/href=
"?'?$$url[2]("
?|'?)/href=
"$urlAbs"
/gim;
print
"Replace "
,
$$url
[2],
" with "
,
$urlAbs
,
"\n"
if
$self
->{_DEBUG};
$url_remplace
{
$urlAbs
}=1;
}
elsif
((
$$url
[0] eq
'body'
) && (
$$url
[1] eq
'background'
))
{
$gabarit
=~s/background=
"?'?$$url[2]("
?|'?)/background=
"cid:$urlAbs"
/gim;
print
"Get "
,
$urlAbs
,
"\n"
if
$self
->{_DEBUG};
my
$res2
=
$self
->{_AGENT}->request(new HTTP::Request(
'GET'
=>
$urlAbs
));
if
(
lc
(
$urlAbs
)=~/gif$/) {
$type
=
"image/gif"
;}
else
{
$type
=
"image/jpg"
;}
my
$mail
= new MIME::Lite
Data
=>
$res2
->content,
Encoding
=>
'base64'
;
$mail
->attr(
"Content-type"
=>
$type
);
$mail
->attr(
'Content-ID'
=>
$urlAbs
);
push
(
@mail
,
$mail
);
}
next
if
((
lc
(
$$url
[0]) ne
'img'
)
&& (
lc
(
$$url
[0]) ne
'src'
)
|| (
$images_read
{
$urlAbs
}) );
if
(
lc
(
$urlAbs
)=~/gif$/) {
$type
=
"image/gif"
;}
else
{
$type
=
"image/jpg"
;}
print
"Get "
,
$urlAbs
,
"\n"
if
$self
->{_DEBUG};
$images_read
{
$urlAbs
}=1;
my
$res2
=
$self
->{_AGENT}->request(new HTTP::Request(
'GET'
=>
$urlAbs
));
if
(!
$res2
->is_success) {
print
"Attention:$urlAbs n'est pas accessible"
;}
my
$mail
= new MIME::Lite
Data
=>
$res2
->content,
Encoding
=>
'base64'
;
$mail
->attr(
"Content-type"
=>
$type
);
$mail
->attr(
'Content-ID'
=>
$urlAbs
);
push
(
@mail
,
$mail
);
}
sub
pattern_image {
return
'<img '
.
$_
[0].
'src="cid:'
.URI::WithBase->new(
$_
[1],
$_
[2])->
abs
.
'"'
;}
$gabarit
=~s/<img([^<>]*)src=([
"']?)([^"
'> ]*)(["']?)/pattern_image($1,$3,
$res
->base)/ieg;
if
(
$self
->{_HASH_TEMPLATE}) {
$gabarit
=
$self
->fill_template(
$gabarit
,
$self
->{_HASH_TEMPLATE});}
my
$part
= new MIME::Lite
'Type'
=>
'TEXT'
,
'Data'
=>
$gabarit
;
$part
->attr(
"content-type"
=>
"text/html; charset=iso-8859-1"
);
unshift
(
@mail
,
$part
);
foreach
(
@mail
) {
$self
->{_MAIL}->attach(
$_
);}
return
$self
->{_MAIL};
}
sub
include_css
{
my
(
$self
,
$gabarit
,
$root
)=
@_
;
sub
pattern_css
{
my
(
$self
,
$url
,
$milieu
,
$fin
,
$root
)=
@_
;
my
$ur
= URI::URL->new(
$url
,
$root
)->
abs
;
print
"Include CSS file $ur\n"
if
$self
->{_DEBUG};
my
$res2
=
$self
->{_AGENT}->request(new HTTP::Request(
'GET'
=>
$ur
));
print
"Ok file downloaded\n"
if
$self
->{_DEBUG};
return
'<style type="text/css">'
.
"\n"
.
'<!--'
.
"\n"
.
$res2
->content.
"\n-->\n</style>\n"
;
}
$gabarit
=~s/<
link
([^<>]*?)href=
"?([^"
]
*css
)"?([^>]*)>/
$self
->pattern_css($2,$1,$3,
$root
)/iegm;
print
"Done CSS\n"
if
$self
->{_DEBUG};
return
$gabarit
;
}
sub
include_javascript
{
my
(
$self
,
$gabarit
,
$root
)=
@_
;
sub
pattern_js
{
my
(
$self
,
$url
,
$milieu
,
$fin
,
$root
)=
@_
;
my
$ur
= URI::URL->new(
$url
,
$root
)->
abs
;
print
"Include Javascript file $ur\n"
if
$self
->{_DEBUG};
my
$res2
=
$self
->{_AGENT}->request(new HTTP::Request(
'GET'
=>
$ur
));
my
$content
=
$res2
->content;
print
"Ok file downloaded\n"
if
$self
->{_DEBUG};
return
"\n"
.
"<!-- $ur -->\n"
.
'<script '
.
$milieu
.
$fin
.
">\n"
.
'<!--'
.
"\n"
.
$content
.
"\n-->\n</script>\n"
;
}
$gabarit
=~s/<script([^>]*)src=
"?([^"
]
*js
)"?([^>]*)>/
$self
->pattern_js($2,$1,$3,
$root
)/iegm;
print
"Done Javascript\n"
if
$self
->{_DEBUG};
return
$gabarit
;
}
sub
input_image
{
my
(
$self
,
$gabarit
,
$root
)=
@_
;
my
@mail
;
sub
pattern_input_image
{
my
(
$self
,
$deb
,
$url
,
$fin
,
$base
,
$ref_tab_mail
)=
@_
;
my
$type
;
my
$ur
= URI::URL->new(
$url
,
$base
)->
abs
;
if
(
lc
(
$ur
)=~/gif$/) {
$type
=
"image/gif"
;}
else
{
$type
=
"image/jpg"
;}
my
$res
=
$self
->{_AGENT}->request(new HTTP::Request(
'GET'
=>
$ur
));
my
$mail
= new MIME::Lite
Data
=>
$res
->content,
Encoding
=>
'base64'
;
$mail
->attr(
"Content-type"
=>
$type
);
$mail
->attr(
'Content-ID'
=>
$ur
);
push
(
@$ref_tab_mail
,
$mail
);
return
'<input '
.
$deb
.
' src="cid:'
.
$ur
.
'"'
.
$fin
;
}
$gabarit
=~s/<input([^<>]*)src=
"?([^"
'> ]*)"?([^>]*)>/
$self
->pattern_input_image($1,$2,$3,
$root
,\
@mail
)/iegm;
print
"Done input image\n"
if
$self
->{_DEBUG};
return
(
$gabarit
,
@mail
);
}
sub
link_form
{
my
(
$self
,
$gabarit
,
$root
)=
@_
;
my
@mail
;
sub
pattern_link_form
{
my
(
$self
,
$deb
,
$url
,
$fin
,
$base
)=
@_
;
my
$type
;
my
$ur
= URI::URL->new(
$url
,
$base
)->
abs
;
return
'<form '
.
$deb
.
' action="'
.
$ur
.
'"'
.
$fin
.
'>'
;
}
$gabarit
=~s/<form([^<>]*)action=
"?([^"
'> ]*)"?([^>]*)>/
$self
->pattern_link_form($1,$2,$3,
$root
)/iegm;
print
"Done form\n"
if
$self
->{_DEBUG};
return
$gabarit
;
}
sub
fill_template
{
my
(
$self
,
$masque
,
$vars
)=
@_
;
my
@buf
=
split
(/\n/,
$masque
);
my
$i
=0;
while
(
my
(
$n
,
$v
)=
each
(
%$vars
))
{
if
(
$v
) {
map
{s/<\?\s\
$$n
\s\?>/
$v
/gm}
@buf
;}
else
{
map
{s/<\?\s\
$$n
\s\?>//gm}
@buf
;}
$i
++;
}
print
"<b>Attention</b>: pas de variables à substituer dans $masque<br>\n"
if
(
$i
==0);
return
join
(
"\n"
,
@buf
);
}