From c4115cc82ad5e85358a0e3609783bb40dc7d669a Mon Sep 17 00:00:00 2001 From: Martin Kamerbeek Date: Fri, 7 May 2010 11:49:54 +0200 Subject: [PATCH] Adding link rewriting so that uri are complete. --- lib/WebGUI/Mailing/Email.pm | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/lib/WebGUI/Mailing/Email.pm b/lib/WebGUI/Mailing/Email.pm index 6ea4207..a6abc27 100644 --- a/lib/WebGUI/Mailing/Email.pm +++ b/lib/WebGUI/Mailing/Email.pm @@ -3,6 +3,8 @@ package WebGUI::Mailing::Email; use strict; use warnings; +use HTML::TreeBuilder; + use base 'WebGUI::Crud'; sub crud_definition { @@ -83,6 +85,32 @@ sub error { return; } +sub absolutifyURIs { + my $self = shift; + my $content = shift; + my $pageUrl = shift; + my $siteUrl = $self->session->url->getSiteURL; + + my $tb = HTML::TreeBuilder->new; + my $root = $tb->parse( $content ); + + foreach my $link ( @{ $root->extract_links } ) { + my ($uri, $element, $attr, $tag) = @{ $link }; + + if ( $uri !~ m{ ^ [a-z]+:// }xmsi ) { + my $new = + ( $uri =~ m{ ^ / }xmsi ) # Is url absolute? + ? $siteUrl . $uri + : $siteUrl . $pageUrl . '/' . $uri + ; + + # replace attribute + $element->attr( $attr, $new ); + } + } + + return $tb->as_HTML; +} sub send { my $self = shift; @@ -90,7 +118,9 @@ sub send { #### TODO: Error checking my $mailing = $self->getMailing; - my $body = $mailing->getAsset->processContentAsUser( $mailing->get('issueId'), $self->get('userId') ); + my $asset = $mailing->getAsset; + my $content = $asset->processContentAsUser( $mailing->get('issueId'), $self->get('userId') ); + my $body = $self->absolutifyURIs( $content, $asset->getUrl ); # Mail is a test mail but there is noone to send the result to if ( $self->get( 'isTest' ) && !$self->get('recipientEmail') ) {