| 1 | package FWS::V2::Net; | ||
| 2 | |||
| 3 | use 5.006; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | no warnings 'uninitialized'; | ||
| 7 | |||
| 8 | =head1 NAME | ||
| 9 | |||
| 10 | FWS::V2::Net - Framework Sites version 2 network access methods | ||
| 11 | |||
| 12 | =head1 VERSION | ||
| 13 | |||
| 14 | Version 1.13091122 | ||
| 15 | |||
| 16 | =cut | ||
| 17 | |||
| 18 | our $VERSION = '1.13091122'; | ||
| 19 | |||
| 20 | |||
| 21 | =head1 SYNOPSIS | ||
| 22 | |||
| 23 | use FWS::V2; | ||
| 24 | |||
| 25 | my $fws = FWS::V2->new(); | ||
| 26 | |||
| 27 | my $responseRef = $fws->HTTPRequest(url=>'http://www.thiswebsite.com'); | ||
| 28 | |||
| 29 | |||
| 30 | =head1 DESCRIPTION | ||
| 31 | |||
| 32 | FWS version 2 core network methods | ||
| 33 | |||
| 34 | =head1 METHODS | ||
| 35 | |||
| 36 | =head2 HTTPRequest | ||
| 37 | |||
| 38 | Post HTTP or HTTPS and return the result to a hash reference containing the results plus the parameters provided. | ||
| 39 | |||
| 40 | my $responseRef = $fws->HTTPRequest( | ||
| 41 | url => 'http://www.cpan.org'# only required parameter | ||
| 42 | type => 'get' # default is get [get|post] | ||
| 43 | user => 'theUser' # if needed for auth | ||
| 44 | password => 'thePass' # if needed for auth | ||
| 45 | noRedirect => 1 # do not follow redirects (defaults to 0) | ||
| 46 | timeout => '30' # fail if 30 seconds go by | ||
| 47 | expire => 30 # cache this for 30 minutes | ||
| 48 | # and return cache till it expires | ||
| 49 | ip =>'1.2.3.4' # show that I am from this ip | ||
| 50 | ); | ||
| 51 | |||
| 52 | print $responseRef->{url} . "\n"; # what was passed to HTTPRequest | ||
| 53 | print $responseRef->{success} . "\n"; # will be a 1 or a 0 | ||
| 54 | print $responseRef->{content} . "\n"; # the content returned | ||
| 55 | print $responseRef->{status} . "\n"; # the status returned | ||
| 56 | |||
| 57 | =cut | ||
| 58 | |||
| 59 | sub HTTPRequest { | ||
| 60 | my ( $self, %paramHash ) = @_; | ||
| 61 | |||
| 62 | # | ||
| 63 | # URL Hash caching if needed | ||
| 64 | # | ||
| 65 | use Digest::MD5 qw(md5_hex); | ||
| 66 | my $URLHash = md5_hex( $paramHash{url} ); | ||
| 67 | |||
| 68 | # | ||
| 69 | # check if we are cached, and if so lets return if we are still in time | ||
| 70 | # | ||
| 71 | if ( $paramHash{expire} ) { | ||
| 72 | $paramHash{content} = $self->cacheValue( 'FWSHTTP_' . $URLHash ); | ||
| 73 | if ( $paramHash{content} ne '' ) { | ||
| 74 | $paramHash{success} = 1; | ||
| 75 | return \%paramHash; | ||
| 76 | } | ||
| 77 | } | ||
| 78 | |||
| 79 | # | ||
| 80 | # lets use the LWP to get this done | ||
| 81 | # | ||
| 82 | require LWP::UserAgent; | ||
| 83 | my $ua = LWP::UserAgent->new(); | ||
| 84 | |||
| 85 | # | ||
| 86 | # disable redirect if passed | ||
| 87 | # | ||
| 88 | if ( $paramHash{noRedirect} ) { $ua->requests_redirectable( [] ) } | ||
| 89 | |||
| 90 | # | ||
| 91 | # set the agent if we need to | ||
| 92 | # | ||
| 93 | if ( $paramHash{agent} ) { $ua->agent( $paramHash{agent} ) } | ||
| 94 | if ( $paramHash{timeout} ) { $ua->timeout( $paramHash{timeout} ) } | ||
| 95 | |||
| 96 | # | ||
| 97 | # lets get our request obj ready | ||
| 98 | # | ||
| 99 | my $req; | ||
| 100 | |||
| 101 | # | ||
| 102 | # force an IP if needed | ||
| 103 | # | ||
| 104 | if ( $paramHash{ip} ) { $ua->local_address( $paramHash{ip} ) } | ||
| 105 | |||
| 106 | # | ||
| 107 | # this is a post... but we get the stuff just like a get - but do the work | ||
| 108 | # | ||
| 109 | if ( $paramHash{type} =~ /post/i ) { | ||
| 110 | my ( $postURL, $content ) = split( /\?/, $paramHash{url} ); | ||
| 111 | $req = HTTP::Request->new( POST => $postURL ); | ||
| 112 | $req->content_type( 'application/x-www-form-urlencoded' ); | ||
| 113 | $req->content( $content ); | ||
| 114 | } | ||
| 115 | else { $req = HTTP::Request->new( GET => $paramHash{url} ) } | ||
| 116 | |||
| 117 | # | ||
| 118 | # if auth is set, lets set it! | ||
| 119 | # | ||
| 120 | if ( $paramHash{user} && $paramHash{password} ) { $req->authorization_basic( $paramHash{user}, $paramHash{password} ) } | ||
| 121 | |||
| 122 | # | ||
| 123 | # do the request and see what happens | ||
| 124 | # | ||
| 125 | my $response = $ua->request( $req ); | ||
| 126 | $paramHash{content} = $response->content; | ||
| 127 | if ( $response->is_success ) { | ||
| 128 | $paramHash{success} = 1 ; | ||
| 129 | |||
| 130 | # | ||
| 131 | # because we have success lets cache this if we are supposed to | ||
| 132 | # | ||
| 133 | if ( $paramHash{expire} ) { | ||
| 134 | $self->saveCache( key => 'FWSHTTP_' . $URLHash, expire => $paramHash{expire}, value => $paramHash{content} ); | ||
| 135 | } | ||
| 136 | |||
| 137 | } | ||
| 138 | else { | ||
| 139 | $paramHash{success} = 0; | ||
| 140 | } | ||
| 141 | |||
| 142 | # | ||
| 143 | # return the reference | ||
| 144 | # | ||
| 145 | return \%paramHash; | ||
| 146 | } | ||
| 147 | |||
| 148 | |||
| 149 | |||
| 150 | =head2 send | ||
| 151 | |||
| 152 | Send an email: Documentation needed. | ||
| 153 | |||
| 154 | =cut | ||
| 155 | |||
| 156 | sub send { | ||
| 157 | my ( $self, %paramHash ) = @_; | ||
| 158 | |||
| 159 | my @digitalAssets; | ||
| 160 | if ( $paramHash{digitalAssets} ) { | ||
| 161 | @digitalAssets = split( /\|/, $paramHash{digitalAssets} ); | ||
| 162 | } | ||
| 163 | |||
| 164 | # | ||
| 165 | # set the stuff if its not specified | ||
| 166 | # | ||
| 167 | $paramHash{characterSet} ||= 'utf-8'; | ||
| 168 | $paramHash{transferEncoding} ||= '7bit'; | ||
| 169 | $paramHash{mimeType} ||= 'text/html'; | ||
| 170 | $paramHash{from} ||= $self->{email}; | ||
| 171 | $paramHash{fromName} ||= $self->{email}; | ||
| 172 | $paramHash{type} ||= $self->{sendMethod}; | ||
| 173 | $paramHash{type} ||= 'sendmail'; | ||
| 174 | $paramHash{fromQueue} ||= 0; | ||
| 175 | |||
| 176 | # | ||
| 177 | # if this has a scheduled date, lets put it in the queue, instead of sending it | ||
| 178 | # | ||
| 179 | if ( ( ( $paramHash{scheduledDate} ) || ( $paramHash{type} && $paramHash{type} ne 'sendmail' ) ) && !$paramHash{fromQueue} ) { | ||
| 180 | $self->saveQueue(%paramHash); | ||
| 181 | } | ||
| 182 | elsif ( !$paramHash{draft} ) { | ||
| 183 | |||
| 184 | # | ||
| 185 | # Switch anything that could have been URIed and changed to html tags that will need to be put back to regular chars. | ||
| 186 | # | ||
| 187 | $paramHash{from} =~ s/( |;|\t|\n|,)/ /sg; | ||
| 188 | $paramHash{to} =~ s/( |;|\t|\n|,)/ /sg; | ||
| 189 | |||
| 190 | # | ||
| 191 | # only use the first one if there is more than one in a list. | ||
| 192 | # | ||
| 193 | my @mailFromSplit = split( ' ', $paramHash{from} ); | ||
| 194 | $paramHash{from} = $mailFromSplit[0]; | ||
| 195 | $paramHash{fromName} ||= $paramHash{from}; | ||
| 196 | |||
| 197 | my $evalEmail; | ||
| 198 | |||
| 199 | # | ||
| 200 | # convert the subject to utf-8 if it is | ||
| 201 | # | ||
| 202 | if ( $paramHash{characterSet} eq lc( 'utf-8' ) ) { | ||
| 203 | $paramHash{subject} = '=?utf-8?B?' . encode_base64( $paramHash{subject}, '' ).'?='; | ||
| 204 | } | ||
| 205 | |||
| 206 | |||
| 207 | # | ||
| 208 | # Split the emailTo's space delmited and process them one by one. | ||
| 209 | # | ||
| 210 | my @emailAccounts = split( / /,$paramHash{to} ); | ||
| 211 | while ( @emailAccounts ) { | ||
| 212 | |||
| 213 | |||
| 214 | # | ||
| 215 | # if this didn't come into the queue, lets just put it in the history now so we know this went down | ||
| 216 | # | ||
| 217 | $self->saveQueueHistory( %paramHash ); | ||
| 218 | |||
| 219 | $paramHash{to} = shift @emailAccounts; | ||
| 220 | |||
| 221 | # | ||
| 222 | # For security reasons lets get rid of all the stuff that could potentialy be dangerous | ||
| 223 | # | ||
| 224 | $paramHash{to} =~ s/(\>|\<|\`|\/)//sg; | ||
| 225 | |||
| 226 | if ( $self->{sendMethod} eq '' || $self->{sendMethod} eq 'sendmail' ) { | ||
| 227 | |||
| 228 | my $boundary = "_-------------" . $self->createPassword(composition=>'1234567890',lowLength=>16,highLength=>16); | ||
| 229 | |||
| 230 | # | ||
| 231 | # Make sure this email is cool. otherwise we might get interneral server errors | ||
| 232 | # | ||
| 233 | if ( $paramHash{to} =~ /^[^@]+@[^@]+.[a-z]{2,}$/i ) { | ||
| 234 | |||
| 235 | use MIME::Base64; | ||
| 236 | |||
| 237 | open ( my $SENDMAIL, "|-", $self->{sendmailBin} . " -t" ) || $self->FWSLog( "Sendmail execute failed: " . $self->{sendmailBin} ); | ||
| 238 | |||
| 239 | print $SENDMAIL "Reply-To: \"" . $paramHash{fromName} . "\" <" . $paramHash{from} . ">\n"; | ||
| 240 | print $SENDMAIL "From: \"" . $paramHash{fromName} . "\" <" . $paramHash{from} . ">\n"; | ||
| 241 | print $SENDMAIL "MIME-Version: 1.0\n"; | ||
| 242 | print $SENDMAIL "To: " . $paramHash{to} . "\n"; | ||
| 243 | print $SENDMAIL "Subject: " . $paramHash{subject} . "\n"; | ||
| 244 | print $SENDMAIL "Content-Type: multipart/mixed;\n"; | ||
| 245 | print $SENDMAIL "\tboundary=\"" . $boundary . "\"\n"; | ||
| 246 | print $SENDMAIL "\nThis is a multi-part message in MIME format.\n"; | ||
| 247 | print $SENDMAIL "\n--" . $boundary . "\n"; | ||
| 248 | print $SENDMAIL "Content-Type: " . $paramHash{mimeType} . "; charset=" . $paramHash{characterSet} . "\n"; | ||
| 249 | print $SENDMAIL "Content-Transfer-Encoding: " . $paramHash{transferEncoding} . "\n\n"; | ||
| 250 | print $SENDMAIL $paramHash{body}; | ||
| 251 | |||
| 252 | # | ||
| 253 | # Add the attachments | ||
| 254 | # | ||
| 255 | for my $fileName (@digitalAssets) { | ||
| 256 | if ( ( -e $fileName) && ( $fileName ) ) { | ||
| 257 | my $justFileName = $self->justFileName( $fileName ); | ||
| 258 | |||
| 259 | print $SENDMAIL "\n--" . $boundary . "\n"; | ||
| 260 | print $SENDMAIL "Content-Type: application/octet-stream;\n"; | ||
| 261 | print $SENDMAIL "\tname=\"" . $justFileName . "\"\n"; | ||
| 262 | print $SENDMAIL "Content-Transfer-Encoding: base64\n"; | ||
| 263 | print $SENDMAIL "Content-Disposition: attachment\n"; | ||
| 264 | print $SENDMAIL "\tfilename=\"" . $justFileName . "\"\n\n"; | ||
| 265 | print $SENDMAIL $self->getEncodedBinary( $fileName ); | ||
| 266 | } | ||
| 267 | } | ||
| 268 | print $SENDMAIL "\n--" . $boundary . "--\n\n"; | ||
| 269 | close $SENDMAIL; | ||
| 270 | } | ||
| 271 | } | ||
| 272 | |||
| 273 | } | ||
| 274 | } | ||
| 275 | return; | ||
| 276 | } | ||
| 277 | |||
| 278 | |||
| 279 | =head1 AUTHOR | ||
| 280 | |||
| 281 | Nate Lewis, C<< <nlewis at gnetworks.com> >> | ||
| 282 | |||
| 283 | =head1 BUGS | ||
| 284 | |||
| 285 | Please report any bugs or feature requests to C<bug-fws-v2 at rt.cpan.org>, or through | ||
| 286 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=FWS-V2>. I will be notified, and then you'll | ||
| 287 | automatically be notified of progress on your bug as I make changes. | ||
| 288 | |||
| 289 | |||
| 290 | |||
| 291 | |||
| 292 | =head1 SUPPORT | ||
| 293 | |||
| 294 | You can find documentation for this module with the perldoc command. | ||
| 295 | |||
| 296 | perldoc FWS::V2::Net | ||
| 297 | |||
| 298 | |||
| 299 | You can also look for information at: | ||
| 300 | |||
| 301 | =over 4 | ||
| 302 | |||
| 303 | =item * RT: CPAN's request tracker (report bugs here) | ||
| 304 | |||
| 305 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=FWS-V2> | ||
| 306 | |||
| 307 | =item * AnnoCPAN: Annotated CPAN documentation | ||
| 308 | |||
| 309 | L<http://annocpan.org/dist/FWS-V2> | ||
| 310 | |||
| 311 | =item * CPAN Ratings | ||
| 312 | |||
| 313 | L<http://cpanratings.perl.org/d/FWS-V2> | ||
| 314 | |||
| 315 | =item * Search CPAN | ||
| 316 | |||
| 317 | L<http://search.cpan.org/dist/FWS-V2/> | ||
| 318 | |||
| 319 | =back | ||
| 320 | |||
| 321 | |||
| 322 | =head1 LICENSE AND COPYRIGHT | ||
| 323 | |||
| 324 | Copyright 2013 Nate Lewis. | ||
| 325 | |||
| 326 | This program is free software; you can redistribute it and/or modify it | ||
| 327 | under the terms of either: the GNU General Public License as published | ||
| 328 | by the Free Software Foundation; or the Artistic License. | ||
| 329 | |||
| 330 | See http://dev.perl.org/licenses/ for more information. | ||
| 331 | |||
| 332 | |||
| 333 | =cut | ||
| 334 | |||
| 335 | 1; # End of FWS::V2::Net |