Various live patches ported from REL1_4
[mediawiki.git] / includes / SpecialEmailuser.php
blob577a8e93663a01fa9b5123b36a8456f25ded6b85
1 <?php
2 /**
4 * @package MediaWiki
5 * @subpackage SpecialPage
6 */
8 /**
11 require_once('UserMailer.php');
13 function wfSpecialEmailuser( $par ) {
14 global $wgUser, $wgOut, $wgRequest, $wgEnableEmail, $wgEnableUserEmail;
16 if( !( $wgEnableEmail && $wgEnableUserEmail ) ) {
17 $wgOut->errorpage( "nosuchspecialpage", "nospecialpagetext" );
18 return;
21 if ( $wgUser->isAnon() ||
22 ( !$wgUser->isValidEmailAddr( $wgUser->getEmail() ) ) ) {
23 $wgOut->errorpage( "mailnologin", "mailnologintext" );
24 return;
27 $action = $wgRequest->getVal( 'action' );
28 if( empty( $par ) ) {
29 $target = $wgRequest->getVal( 'target' );
30 } else {
31 $target = $par;
33 if ( "" == $target ) {
34 $wgOut->errorpage( "notargettitle", "notargettext" );
35 return;
37 $nt = Title::newFromURL( $target );
38 if ( is_null( $nt ) ) {
39 $wgOut->errorpage( "notargettitle", "notargettext" );
40 return;
42 $nu = User::newFromName( $nt->getText() );
44 if ( 0 == $nu->getID() ) {
45 $wgOut->errorpage( "noemailtitle", "noemailtext" );
46 return;
49 $address = $nu->getEmail();
51 if ( ( !$nu->isValidEmailAddr( $address ) ) ||
52 ( 1 == $nu->getOption( "disablemail" ) ) ||
53 ( 0 == $nu->getEmailauthenticationtimestamp() ) ) {
54 $wgOut->errorpage( "noemailtitle", "noemailtext" );
55 return;
58 $f = new EmailUserForm( $nu->getName() . " <{$address}>", $target );
60 if ( "success" == $action ) {
61 $f->showSuccess();
62 } else if ( "submit" == $action && $wgRequest->wasPosted() &&
63 $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) ) ) {
64 $f->doSubmit();
65 } else {
66 $f->showForm();
70 /**
71 * @todo document
72 * @package MediaWiki
73 * @subpackage SpecialPage
75 class EmailUserForm {
77 var $mAddress;
78 var $target;
79 var $text, $subject;
81 function EmailUserForm( $addr, $target ) {
82 global $wgRequest;
83 $this->mAddress = $addr;
84 $this->target = $target;
85 $this->text = $wgRequest->getText( 'wpText' );
86 $this->subject = $wgRequest->getText( 'wpSubject' );
89 function showForm() {
90 global $wgOut, $wgUser, $wgLang;
92 $wgOut->setPagetitle( wfMsg( "emailpage" ) );
93 $wgOut->addWikiText( wfMsg( "emailpagetext" ) );
95 if ( $this->subject === "" ) {
96 $this->subject = wfMsg( "defemailsubject" );
99 $emf = wfMsg( "emailfrom" );
100 $sender = $wgUser->getName();
101 $emt = wfMsg( "emailto" );
102 $rcpt = str_replace( "_", " ", $this->target );
103 $emr = wfMsg( "emailsubject" );
104 $emm = wfMsg( "emailmessage" );
105 $ems = wfMsg( "emailsend" );
106 $encSubject = htmlspecialchars( $this->subject );
108 $titleObj = Title::makeTitle( NS_SPECIAL, "Emailuser" );
109 $action = $titleObj->escapeLocalURL( "target=" .
110 urlencode( $this->target ) . "&action=submit" );
111 $token = $wgUser->editToken();
113 $wgOut->addHTML( "
114 <form id=\"emailuser\" method=\"post\" action=\"{$action}\">
115 <table border='0'><tr>
116 <td align='right'>{$emf}:</td>
117 <td align='left'><strong>" . htmlspecialchars( $sender ) . "</strong></td>
118 </tr><tr>
119 <td align='right'>{$emt}:</td>
120 <td align='left'><strong>" . htmlspecialchars( $rcpt ) . "</strong></td>
121 </tr><tr>
122 <td align='right'>{$emr}:</td>
123 <td align='left'>
124 <input type='text' name=\"wpSubject\" value=\"{$encSubject}\" />
125 </td>
126 </tr><tr>
127 <td align='right'>{$emm}:</td>
128 <td align='left'>
129 <textarea name=\"wpText\" rows='10' cols='60' wrap='virtual'>" . htmlspecialchars( $this->text ) .
130 "</textarea>
131 </td></tr><tr>
132 <td>&nbsp;</td><td align='left'>
133 <input type='submit' name=\"wpSend\" value=\"{$ems}\" />
134 </td></tr></table>
135 <input type='hidden' name='wpEditToken' value=\"$token\" />
136 </form>\n" );
140 function doSubmit() {
141 global $wgOut, $wgUser, $wgLang, $wgOutputEncoding;
143 $from = wfQuotedPrintable( $wgUser->getName() ) . " <" . $wgUser->getEmail() . ">";
144 $subject = wfQuotedPrintable( $this->subject );
146 if (wfRunHooks('EmailUser', array(&$this->mAddress, &$from, &$subject, &$this->text))) {
148 $mailResult = userMailer( $this->mAddress, $from, $subject, $this->text );
150 if (!$mailResult) {
151 $titleObj = Title::makeTitle( NS_SPECIAL, "Emailuser" );
152 $encTarget = wfUrlencode( $this->target );
153 $wgOut->redirect( $titleObj->getFullURL( "target={$encTarget}&action=success" ) );
154 wfRunHooks('EmailUserComplete', array($this->mAddress, $from, $subject, $this->text));
155 } else {
156 $wgOut->addHTML( wfMsg( "usermailererror" ) . $mailResult);
161 function showSuccess() {
162 global $wgOut, $wgUser;
164 $wgOut->setPagetitle( wfMsg( "emailsent" ) );
165 $wgOut->addHTML( wfMsg( "emailsenttext" ) );
167 $wgOut->returnToMain( false );