<?_code

 use strict;
 use vars qw(%FORM $title $body);

 LJ::set_active_crumb('tellfriend');
 
 $title = "Tell a Friend!";
 $body = "";

 my $remote = LJ::get_remote();
 my $sth;

 if ($LJ::DISABLED{'tellafriend'}) {
     $body .= "This feature is disabled.";
     return;
 }

 unless ($remote) {
     $body .= "<?h1 Sorry... h1?><?p To use the \"Tell a Friend\" feature, you must first <a href=\"/login.bml?ret=1\">login</a>.  p?><?p If we didn't require you to login for this, we'd be essentially providing an anonymous email gateway and there'd be tons of people abusing it and harassing each other.  (We tried this on another website once...)  p?>";
     return;
 }

 my $u = LJ::load_userid($remote->{'userid'});
 $u->{'emailpref'} = $u->{'email'};
 if ($LJ::USER_EMAIL && LJ::get_cap($u, "useremail")) {
     $u->{'emailpref'} = $u->{'user'} . '@' . $LJ::USER_DOMAIN;
 }
 
 unless ($u->{'status'} eq "A") {
     $body .= "<?h1 Sorry... h1?><?p Your current email address ($u->{'email'}) isn't validated, so we can't let you use the \"Tell a Friend\" feature.  To validate your email address, have the confirmation email resent at the <a href=\"/lostinfo.bml\">lost information</a> page, and then follow the instructions in it when you get it. p?>";
     return;
 }

 my $err = sub {
     $title = "Error";
     $body = "<?h1 Error h1?><?p $_[0] p?>";
     return;
 };
 
 if ($FORM{'mode'} eq "mail") 
 {
     my $email = $FORM{'toemail'};
     my @errors;
     LJ::check_email($email, \@errors);
     push @errors, "<?requirepost?>" unless LJ::did_post();
     if (@errors) {
         $title = "Error";
         $body = LJ::bad_input(@errors);
         return;
     }

     LJ::send_mail({
         'to' => $FORM{'toemail'},
         'toname' => $FORM{'toname'},
         'from' => $u->{'emailpref'},
         'fromname' => $FORM{'fromname'},
         'charset' => 'utf-8',
         'subject' => $FORM{'subject'},
         'body' => $FORM{'body'},
     });

     $body .= "<?h1 Sent! h1?><?p $FORM{'toemail'} has been mailed. p?>";    
     return;
 }

 $body .= "<form method='post' action='tellafriend.bml'><input type=hidden name=mode value=mail>";
 $body .= "<table cellpadding=3 border=0>";
 
 $body .= "<tr><td align=right nowrap><b>From:</b></td><td><input name=fromname size=30 value=\"" . LJ::ehtml($u->{'name'} || "$LJ::SITENAME User") . "\"></td></tr>\n";
 $body .= "<tr><td align=right nowrap><b>&nbsp;</b></td><td>$u->{'emailpref'}</td></tr>\n"; 

 $body .= "<tr><td align=right nowrap><b>To (Name):</b></td><td><input name=toname size=30></td></tr>\n";
 $body .= "<tr><td align=right nowrap><b>To (Email):</b></td><td><input name=toemail size=30></td></tr>\n";

 my ($subject, $msg);
 $subject = "Check this out...";
 if ($FORM{'itemid'} =~ /^\d+$/) 
 {
     my $journal = $FORM{'journal'};
     my $itemid = $FORM{'itemid'}+0;
     my $ditemid = $itemid;
     my $uj;

     $itemid = int($itemid / 256);
         
     $uj = LJ::load_user($journal);
     return $err->("Unknown journal") unless $uj;

     my $dbcr = LJ::get_cluster_reader($uj);
     my ($posterid, $subject) = $dbcr->selectrow_array("SELECT l.posterid, ls.subject ".
                                                       "FROM log2 l, logtext2 ls ".
                                                       "WHERE l.journalid=? AND ls.journalid=l.journalid ".
                                                       "AND l.jitemid=? AND ls.jitemid=l.jitemid",
                                                       undef, $uj->{'userid'}, $itemid);
     my $up;
     LJ::load_userids_multiple([ $posterid => \$up ], [ $uj, $u ]);

     my $base = LJ::journal_base($uj);
     my $url = "$base/$ditemid.html";

     if ($up->{'user'} eq $u->{'user'}) {
         $msg .= "Hey,\n\nCheck out this journal entry of mine:\n\n";
     } else {
         $msg .= "Hey,\n\nCheck this out... I saw this journal entry while reading " . $up->{'user'} . "'s journal:\n\n";
     }
     $msg .= " $subject\n" if $subject;
     $msg .= " $url\n\n";
     $msg .= "I thought you might be interested.\n\n";
 }

 if ($FORM{'user'} =~ /^\w{1,15}$/) {
     my $user = $FORM{'user'};
     my $uj = LJ::load_user($user);
     my $url;
     if (LJ::get_cap($uj, "userdomain")) {
         my $sub = $user;
         $sub =~ s/_/-/g;
         $url .= "http://$sub.$LJ::USER_DOMAIN/";
     } else {
         $url .= "$LJ::SITEROOT/users/$user/";
     }
     
     if ($user eq $u->{'user'}) {
         $msg .= "Hey,\n\nCheck out my journal that I'm keeping online:\n\n";
         $msg .= "   $url\n\n";
         my $name = $u->{'name'};
         $name =~ s/\s.+//;
         $msg .= "- $name\n";
     } else {
         $msg .= "Hey,\n\nCheck out my friend's journal online:\n\n";
         $msg .= "  $url\n\n";
         my $name = $u->{'name'};
         $name =~ s/\s.+//;
         $msg .= "- $name\n";
     }
 }

 my $subject = "Check it out...";
 $body .= "<tr><td align=right nowrap><b>Subject:</b></td><td><input name=subject size=40 maxlength=60 value=\"" . LJ::ehtml($subject) . "\"></td></tr>\n";

 $body .= "<tr><td align=right nowrap valign=top><b>Body:</b></td><td><textarea name=body rows=15 cols=67 wrap=soft>" . LJ::ehtml($msg) . "</textarea></td></tr>\n";
 
 $body .= "<tr><td>&nbsp;</td><td><input type=submit value=\"Send email\"></td></tr>\n";
 $body .= "</table>";
 $body .= "</form>";

 return;

_code?><?page
title=><?_code return $title; _code?>
body=><?_code return $body; _code?>
page?><?_c <LJDEP>
lib: cgi-bin/emailcheck.pl
link: htdocs/login.bml, htdocs/lostinfo.bml, htdocs/users, htdocs/talkread.bml
form: htdocs/tools/tellafriend.bml
</LJDEP> _c?>
