#!/usr/bin/perl ############################################################################################### use strict; use HTML::Template; use CGI; use Data::Dumper; use Net::SMTP; use BB::Util; use BB::Database; use Mail::Verify; ############################################################################################### ## CGI my $cgi = new CGI; my %vars = $cgi->Vars; ## HTML Template my $tmpl = HTML::Template->new(filename => 'email_story.tmpl',die_on_bad_params => 0); ## Link to email my($referer) = $vars{referer} || $ENV{'REFERER'} || $ENV{'HTTP_REFERER'}; $referer = BB::Util::strip_nasties($referer); $referer =~ s/\.\./_/g; # Prevent using ../../.. to read other files ## Check to make sure its from this site (i.e. no tampering) $referer = 'UNKNOWN' if $referer !~ m|^http://\w+.breitbart.com/.*\.html|i; $tmpl->param(REFERER => $referer); ## Database Handle my($dbh) = BB::Database::mysql_connect('db_w'); ## Email Address my($from_email) = $vars{from_email}; my($from_email_ck) = Mail::Verify::CheckAddress($from_email); ## Open data file (history, who we have sent to and what) #my $list; #my $dat = "email.dat"; #open DAT, '<', $dat; #my $data = ; #eval $data; #close DAT; ## Trying to send the story if ($vars{action} eq "send") { my $error; ## Sender email look ok? $error .= "
  • No email address was supplied" if($from_email_ck == 1); $error .= "
  • There is a syntaxical error in the email address: $from_email" if($from_email_ck == 2); $error .= "
  • Problem with your email address: There are no DNS entries for the host in question (no MX records or A records): $from_email" if($from_email_ck == 3); $error .= "
  • Problem with your email address: There are no live SMTP servers accepting connections for this address: $from_email" if($from_email_ck == 4); $error .= "
  • The email address $from_email does not appear to be valid." if $from_email !~ /^.*\@.*\..*$/; ## Receipiant emails look ok? my @send_to; my @tmp_send_to = split(/\,/,$vars{to_email}); foreach (@tmp_send_to) { my($to_email_ck) = Mail::Verify::CheckAddress($_); $error .= "
  • No email address was supplied" if($to_email_ck == 1); $error .= "
  • There is a syntaxical error in the email address: $_" if($to_email_ck == 2); $error .= "
  • Problem with your email address: There are no DNS entries for the host in question (no MX records or A records): $_" if($to_email_ck == 3); $error .= "
  • Problem with your email address: There are no live SMTP servers accepting connections for this address: $_" if($to_email_ck == 4); $error .= "
  • The recipient email $_ does not appear valid." if $_ !~ /^.*\@.*\..*$/; $_ =~ s/\s//g; push(@send_to, $_); } $error .= "
  • You must supply at least one recipient email address." if !$vars{to_email}; ## Sending URL valid? $error .= "
  • Unable to email story because story was not found" if !$referer; $error .= "
  • Unable to email story because story was not found (unknown)" if($referer eq 'UNKNOWN'); ## Ok, lets do it if (!$error) { my $count = 0; ## Send one for each recipients foreach (@send_to) { my $skip = already_emailed_this_article($_,$referer); ## Has this person already been emailed this story? If so, skip #foreach my $url (@{$list->{$_}}) { # $skip = 1 if $url eq $referer; #} ## Ok to email unless ($skip) { ## Save this so we dont send again #push(@{$list->{$_}},$referer); record_emailed_this_article($_,$referer); ## Send it here my $smtp = Net::SMTP->new('127.0.0.1'); $smtp->mail($from_email); $smtp->to($_); $smtp->data(); $smtp->datasend("To: $_\n"); $smtp->datasend("From: $from_email\n"); $smtp->datasend("Subject: I saw this story at breitbart.com\n"); $smtp->datasend("\n"); $smtp->datasend("\nI saw this story at breitbart.com\n\n$referer\n\n"); $smtp->datasend("$vars{message}\n\n") if $vars{message}; $smtp->datasend("http://www.breitbart.com\n"); $smtp->dataend(); $smtp->quit; ## Count it $count++; } } ## Finish, change template $tmpl = HTML::Template->new(filename => 'email_story_fin.tmpl',die_on_bad_params => 0); $tmpl->param(COUNT => $count); ## Errors } else { $tmpl->param(MSG => "Please correct the following errors:
    $error"); while(my($key,$val) = each %vars) { $tmpl->param(uc($key) => $val); } } } ### Saving data #open DAT, '>', $dat; #my $dumper = Data::Dumper->new([$list], ['list']); # $dumper->Indent(0); #print DAT $dumper->Dump; #close DAT; ## Finish up print $cgi->header(-type=>'text/html',-expires=>'-1d'); $tmpl->param('SCRIPT' => '/index.cgi'); print $tmpl->output; exit; ############################################################################################### sub already_emailed_this_article { my($email) = shift(@_); my($url) = shift(@_); my($already_emailed) = 0; my($sql) = " SELECT email_log_id FROM email_log WHERE email=? AND url=? LIMIT 1"; my($sth) = $dbh->prepare($sql); my($rv) = $sth->execute($email,$url); die "Database query failed: $DBI::errstr" unless($rv); $already_emailed = 1 if($sth->rows > 0); $sth->finish; return($already_emailed); } sub record_emailed_this_article { my($email) = shift(@_); my($url) = shift(@_); my($already_emailed) = 0; my($sql) = " INSERT INTO email_log SET email=?,url=?"; my($sth) = $dbh->prepare($sql); my($rv) = $sth->execute($email,$url); die "Database query failed: $DBI::errstr" unless($rv); $sth->finish; }