Various additions and fixes
[mediawiki.git] / includes / SpecialEmailuser.php
blob8ae02b2800cb9e3a2c90f575ecbc4fba512113f9
1 <?
3 function wfSpecialEmailuser()
5 global $wgUser, $wgOut, $action, $target;
7 if ( 0 == $wgUser->getID() ||
8 ( false === strpos( $wgUser->getEmail(), "@" ) ) ) {
9 $wgOut->errorpage( "mailnologin", "mailnologintext" );
10 return;
12 $target = wfCleanQueryVar( $target );
13 if ( "" == $target ) {
14 $wgOut->errorpage( "notargettitle", "notargettext" );
15 return;
17 $nt = Title::newFromURL( $target );
18 $nu = User::newFromName( $nt->getText() );
19 $id = $nu->idForName();
21 if ( 0 == $id ) {
22 $wgOut->errorpage( "noemailtitle", "noemailtext" );
23 return;
25 $nu->setID( $id );
26 $address = $nu->getEmail();
28 if ( ( false === strpos( $address, "@" ) ) ||
29 ( 1 == $nu->getOption( "disablemail" ) ) ) {
30 $wgOut->errorpage( "noemailtitle", "noemailtext" );
31 return;
33 $fields = array( "wpSubject", "wpText" );
34 wfCleanFormFields( $fields );
36 $f = new EmailUserForm( $nu->getName() . " <{$address}>" );
38 if ( "success" == $action ) { $f->showSuccess(); }
39 else if ( "submit" == $action ) { $f->doSubmit(); }
40 else { $f->showForm( "" ); }
43 class EmailUserForm {
45 var $mAddress;
47 function EmailUserForm( $addr )
49 $this->mAddress = $addr;
52 function showForm( $err )
54 global $wgOut, $wgUser, $wgLang;
55 global $wpSubject, $wpText, $target;
57 $wgOut->setPagetitle( wfMsg( "emailpage" ) );
58 $wgOut->addWikiText( wfMsg( "emailpagetext" ) );
60 if ( ! $wpSubject ) { $wpSubject = "Wikipedia e-mail"; }
62 $emf = wfMsg( "emailfrom" );
63 $sender = $wgUser->getName();
64 $emt = wfMsg( "emailto" );
65 $rcpt = str_replace( "_", " ", urldecode( $target ) );
66 $emr = wfMsg( "emailsubject" );
67 $emm = wfMsg( "emailmessage" );
68 $ems = wfMsg( "emailsend" );
70 $action = wfLocalUrlE( $wgLang->specialPage( "Emailuser" ),
71 "target={$target}&action=submit" );
73 if ( "" != $err ) {
74 $wgOut->setSubtitle( wfMsg( "formerror" ) );
75 $wgOut->addHTML( "<p><font color='red' size='+1'>{$err}</font>\n" );
77 $wgOut->addHTML( "<p>
78 <form id=\"emailuser\" method=\"post\" action=\"{$action}\">
79 <table border=0><tr>
80 <td align=right>{$emf}:</td>
81 <td align=left><strong>{$sender}</strong></td>
82 </tr><tr>
83 <td align=right>{$emt}:</td>
84 <td align=left><strong>{$rcpt}</strong></td>
85 </tr><tr>
86 <td align=right>{$emr}:</td>
87 <td align=left>
88 <input type=text name=\"wpSubject\" value=\"{$wpSubject}\">
89 </td>
90 </tr><tr>
91 <td align=right>{$emm}:</td>
92 <td align=left>
93 <textarea name=\"wpText\" rows=10 cols=60 wrap=virtual>
94 {$wpText}
95 </textarea>
96 </td></tr><tr>
97 <td>&nbsp;</td><td align=left>
98 <input type=submit name=\"wpSend\" value=\"{$ems}\">
99 </td></tr></table>
100 </form>\n" );
104 function doSubmit()
106 global $wgOut, $wgUser, $wgLang, $wgOutputEncoding;
107 global $wpSubject, $wpText, $target;
109 $from = $wgUser->getName() . " <" . $wgUser->getEmail() . ">";
110 $headers =
111 "MIME-Version: 1.0\r\n" .
112 "Content-type: text/plain; charset={$wgOutputEncoding}\r\n" .
113 "Content-transfer-encoding: 8bit\r\n" .
114 "From: {$from}\r\n" .
115 "Reply-To: {$from}\r\n" .
116 "To: {$this->mAddress}\r\n" .
117 "X-Mailer: Pediawiki interuser e-mailer";
118 mail( $this->mAddress, $wpSubject, $wpText, $headers );
120 $success = wfLocalUrl( $wgLang->specialPage( "Emailuser" ),
121 "target={$target}&action=success" );
122 $wgOut->redirect( $success );
125 function showSuccess()
127 global $wgOut, $wgUser;
129 $wgOut->setPagetitle( wfMsg( "emailsent" ) );
130 $wgOut->addHTML( wfMsg( "emailsenttext" ) );
132 $wgOut->returnToMain( false );