Added the ability to do an authenticated SMTP login to send mail.
[lhc/web/wiklou.git] / includes / SpecialEmailuser.php
1 <?php
2
3 require_once('UserMailer.php');
4
5 function wfSpecialEmailuser()
6 {
7 global $wgUser, $wgOut, $action, $target;
8
9 if ( 0 == $wgUser->getID() ||
10 ( false === strpos( $wgUser->getEmail(), "@" ) ) ) {
11 $wgOut->errorpage( "mailnologin", "mailnologintext" );
12 return;
13 }
14 $target = wfCleanQueryVar( $target );
15 if ( "" == $target ) {
16 $wgOut->errorpage( "notargettitle", "notargettext" );
17 return;
18 }
19 $nt = Title::newFromURL( $target );
20 $nu = User::newFromName( $nt->getText() );
21 $id = $nu->idForName();
22
23 if ( 0 == $id ) {
24 $wgOut->errorpage( "noemailtitle", "noemailtext" );
25 return;
26 }
27 $nu->setID( $id );
28 $address = $nu->getEmail();
29
30 if ( ( false === strpos( $address, "@" ) ) ||
31 ( 1 == $nu->getOption( "disablemail" ) ) ) {
32 $wgOut->errorpage( "noemailtitle", "noemailtext" );
33 return;
34 }
35 $fields = array( "wpSubject", "wpText" );
36 wfCleanFormFields( $fields );
37
38 $f = new EmailUserForm( $nu->getName() . " <{$address}>" );
39
40 if ( "success" == $action ) { $f->showSuccess(); }
41 else if ( "submit" == $action ) { $f->doSubmit(); }
42 else { $f->showForm( "" ); }
43 }
44
45 class EmailUserForm {
46
47 var $mAddress;
48
49 function EmailUserForm( $addr )
50 {
51 $this->mAddress = $addr;
52 }
53
54 function showForm( $err )
55 {
56 global $wgOut, $wgUser, $wgLang;
57 global $wpSubject, $wpText, $target;
58
59 $wgOut->setPagetitle( wfMsg( "emailpage" ) );
60 $wgOut->addWikiText( wfMsg( "emailpagetext" ) );
61
62 if ( ! $wpSubject ) { $wpSubject = wfMsg( "defemailsubject" ); }
63
64 $emf = wfMsg( "emailfrom" );
65 $sender = $wgUser->getName();
66 $emt = wfMsg( "emailto" );
67 $rcpt = str_replace( "_", " ", urldecode( $target ) );
68 $emr = wfMsg( "emailsubject" );
69 $emm = wfMsg( "emailmessage" );
70 $ems = wfMsg( "emailsend" );
71
72 $action = wfLocalUrlE( $wgLang->specialPage( "Emailuser" ),
73 "target={$target}&action=submit" );
74
75 if ( "" != $err ) {
76 $wgOut->setSubtitle( wfMsg( "formerror" ) );
77 $wgOut->addHTML( "<p><font color='red' size='+1'>{$err}</font>\n" );
78 }
79 $wgOut->addHTML( "<p>
80 <form id=\"emailuser\" method=\"post\" action=\"{$action}\">
81 <table border=0><tr>
82 <td align=right>{$emf}:</td>
83 <td align=left><strong>{$sender}</strong></td>
84 </tr><tr>
85 <td align=right>{$emt}:</td>
86 <td align=left><strong>{$rcpt}</strong></td>
87 </tr><tr>
88 <td align=right>{$emr}:</td>
89 <td align=left>
90 <input type=text name=\"wpSubject\" value=\"{$wpSubject}\">
91 </td>
92 </tr><tr>
93 <td align=right>{$emm}:</td>
94 <td align=left>
95 <textarea name=\"wpText\" rows=10 cols=60 wrap=virtual>
96 {$wpText}
97 </textarea>
98 </td></tr><tr>
99 <td>&nbsp;</td><td align=left>
100 <input type=submit name=\"wpSend\" value=\"{$ems}\">
101 </td></tr></table>
102 </form>\n" );
103
104 }
105
106 function doSubmit()
107 {
108 global $wgOut, $wgUser, $wgLang, $wgOutputEncoding;
109 global $wpSubject, $wpText, $target;
110
111 $from = wfQuotedPrintable( $wgUser->getName() ) . " <" . $wgUser->getEmail() . ">";
112
113 $mailResult = userMailer( $this->mAddress, $from, wfQuotedPrintable( $wpSubject ), $wpText );
114
115 if (! $mailResult)
116 {
117 $success = wfLocalUrl( $wgLang->specialPage( "Emailuser" ),
118 "target={$target}&action=success" );
119 $wgOut->redirect( $success );
120 }
121 else
122 $wgOut->addHTML( wfMsg( "usermailererror" ) . $mailResult);
123 }
124
125 function showSuccess()
126 {
127 global $wgOut, $wgUser;
128
129 $wgOut->setPagetitle( wfMsg( "emailsent" ) );
130 $wgOut->addHTML( wfMsg( "emailsenttext" ) );
131
132 $wgOut->returnToMain( false );
133 }
134 }
135 ?>