| 1 | package OpenInteract::Utility; | ||
| 2 | |||
| 3 | # $Id: Utility.pm,v 1.9 2003/03/25 16:32:48 lachoy Exp $ | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use Mail::Sendmail (); | ||
| 7 | use MIME::Lite (); | ||
| 8 | |||
| 9 | @OpenInteract::Utility::ISA = (); | ||
| 10 | $OpenInteract::Utility::VERSION = sprintf("%d.%02d", q$Revision: 1.9 $ =~ /(\d+)\.(\d+)/); | ||
| 11 | |||
| 12 | use constant DEFAULT_SUBJECT => 'Mail sent from OpenInteract'; | ||
| 13 | use constant DEFAULT_ATTACH_MESSAGE => 'Emailing attachments'; | ||
| 14 | |||
| 15 | # All other types except those listed here are 'base64' encoding | ||
| 16 | |||
| 17 | my %TEXT_ENCODINGS = map { $_ => '8bit' } | ||
| 18 | qw( text/csv text/html text/html text/plain text/xml | ||
| 19 | application/x-javascript application/x-perl ); | ||
| 20 | |||
| 21 | sub send_email { | ||
| 22 | my ( $class, $p ) = @_; | ||
| 23 | return $class->_send_email_attachment( $p ) if ( $p->{attach} ); | ||
| 24 | |||
| 25 | my $R = OpenInteract::Request->instance; | ||
| 26 | |||
| 27 | my %header_info = $class->_build_header_info( $p ); | ||
| 28 | my $smtp_host = $class->_get_smtp_host( $p ); | ||
| 29 | my %mail = ( | ||
| 30 | %header_info, | ||
| 31 | smtp => $smtp_host, | ||
| 32 | message => $p->{message}, | ||
| 33 | ); | ||
| 34 | $R->DEBUG && $R->scrib( 1, "Trying to send mail to [$mail{To}] via [$mail{smtp}]" ); | ||
| 35 | $R->DEBUG && $R->scrib( 2, "Message being sent:\n$mail{message}" ); | ||
| 36 | eval { Mail::Sendmail::sendmail( %mail ) || die $Mail::Sendmail::error }; | ||
| 37 | if ( $@ ) { | ||
| 38 | my $msg = "Cannot send email. Error: $@"; | ||
| 39 | OpenInteract::Error->set({ user_msg => $msg, | ||
| 40 | type => 'email', | ||
| 41 | system_msg => $@, | ||
| 42 | extra => \%mail }); | ||
| 43 | die $msg; | ||
| 44 | } | ||
| 45 | return 1; | ||
| 46 | } | ||
| 47 | |||
| 48 | |||
| 49 | sub _send_email_attachment { | ||
| 50 | my ( $class, $p ) = @_; | ||
| 51 | return $class->send_email( $p ) unless ( $p->{attach} ); | ||
| 52 | my $attachments = ( ref $p->{attach} eq 'ARRAY' ) ? $p->{attach} : [ $p->{attach} ]; | ||
| 53 | return $class->send_email( $p ) unless ( scalar @{ $attachments } > 0 ); | ||
| 54 | |||
| 55 | my $R = OpenInteract::Request->instance; | ||
| 56 | |||
| 57 | my %header_info = $class->_build_header_info( $p ); | ||
| 58 | my $initial_text = $p->{message} || DEFAULT_ATTACH_MESSAGE; | ||
| 59 | my $msg = new MIME::Lite( %header_info, | ||
| 60 | Data => $initial_text, | ||
| 61 | Type => 'text/plain' ); | ||
| 62 | foreach my $filename ( @{ $attachments } ) { | ||
| 63 | my $cleaned_name = $class->_clean_attachment_filename( $filename ); | ||
| 64 | next unless ( $cleaned_name ); | ||
| 65 | my ( $ext ) = $cleaned_name =~ /\.(\w+)$/; | ||
| 66 | my $mime_type = $R->content_type->mime_type_by_extension( lc $ext ); | ||
| 67 | my $encoding = $TEXT_ENCODINGS{ $mime_type } || 'base64'; | ||
| 68 | $msg->attach( Type => $mime_type, | ||
| 69 | Encoding => $encoding, | ||
| 70 | Path => $cleaned_name ); | ||
| 71 | } | ||
| 72 | |||
| 73 | my $smtp_host = $class->_get_smtp_host( $p ); | ||
| 74 | MIME::Lite->send( 'smtp', $smtp_host, Timeout => 10 ); | ||
| 75 | eval { $msg->send || die "Cannot send message: $!" }; | ||
| 76 | if ( $@ ) { | ||
| 77 | my $msg = "Cannot send email. Error: $@"; | ||
| 78 | OpenInteract::Error->set({ user_msg => $msg, | ||
| 79 | type => 'email', | ||
| 80 | system_msg => $@, | ||
| 81 | extra => { %header_info, attachments => $attachments } }); | ||
| 82 | die $msg; | ||
| 83 | } | ||
| 84 | |||
| 85 | } | ||
| 86 | |||
| 87 | |||
| 88 | sub _build_header_info { | ||
| 89 | my ( $class, $p ) = @_; | ||
| 90 | my $R = OpenInteract::Request->instance; | ||
| 91 | return ( To => $p->{to} || $p->{email}, | ||
| 92 | From => $p->{from} || $R->CONFIG->{mail}{admin_email} || $R->CONFIG->{admin_email}, | ||
| 93 | Subject => $p->{subject} || DEFAULT_SUBJECT ); | ||
| 94 | } | ||
| 95 | |||
| 96 | |||
| 97 | sub _get_smtp_host { | ||
| 98 | my ( $class, $p ) = @_; | ||
| 99 | my $R = OpenInteract::Request->instance; | ||
| 100 | return $p->{smtp} || $R->CONFIG->{mail}{smtp_host} || $R->CONFIG->{smtp_host}; | ||
| 101 | } | ||
| 102 | |||
| 103 | |||
| 104 | # Ensure that no absolute filenames are used, no directory traversals | ||
| 105 | # (../), and that the filename exists | ||
| 106 | |||
| 107 | sub _clean_attachment_filename { | ||
| 108 | my ( $class, $filename ) = @_; | ||
| 109 | my $R = OpenInteract::Request->instance; | ||
| 110 | |||
| 111 | $R->DEBUG && $R->scrib( 1, "Processing attachment filename [$filename]" ); | ||
| 112 | |||
| 113 | my $website_dir = $R->CONFIG->get_dir( 'base' ); | ||
| 114 | |||
| 115 | # Strip off the website root directory so we can ensure that the | ||
| 116 | # next check deals with relative files properly | ||
| 117 | |||
| 118 | $filename =~ s|^$website_dir||; | ||
| 119 | |||
| 120 | # First, see if they use an absolute. If so, strip off the leading | ||
| 121 | # '/' and assume they meant the absolute website directory | ||
| 122 | |||
| 123 | if ( $filename =~ s|^/+|| ) { | ||
| 124 | $R->DEBUG && $R->scrib( 1, "Attachment started with '/'. New: [($filename]" ); | ||
| 125 | } | ||
| 126 | |||
| 127 | if ( $filename =~ s|\.\.||g ) { | ||
| 128 | $R->DEBUG && $R->scrib( 1, "Attachment had '..' sequence. New: [$filename]" ); | ||
| 129 | } | ||
| 130 | |||
| 131 | my $cleaned_filename = join( '/', $website_dir, $filename ); | ||
| 132 | if ( -f $cleaned_filename ) { | ||
| 133 | $R->DEBUG && $R->scrib( 1, "Final filename exists: [$cleaned_filename]" ); | ||
| 134 | return $cleaned_filename; | ||
| 135 | } | ||
| 136 | $R->DEBUG && $R->scrib( 1, "Final filename does NOT EXIST: [$cleaned_filename]" ); | ||
| 137 | return undef; | ||
| 138 | } | ||
| 139 | |||
| 140 | 1; | ||
| 141 | |||
| 142 | __END__ |