Split link generation, table of contents, and image functions from the
[mediawiki.git] / includes / SpecialEmailuser.php
blob860d25b7f43e5b2664ad9099e9f550cf56d6dcea
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 ( 0 == $wgUser->getID() ||
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 ) { $f->showSuccess(); }
61 else if ( "submit" == $action && $wgRequest->wasPosted() ) { $f->doSubmit(); }
62 else { $f->showForm( "" ); }
65 /**
66 * @todo document
67 * @package MediaWiki
68 * @subpackage SpecialPage
70 class EmailUserForm {
72 var $mAddress;
73 var $target;
74 var $text, $subject;
76 function EmailUserForm( $addr, $target ) {
77 global $wgRequest;
78 $this->mAddress = $addr;
79 $this->target = $target;
80 $this->text = $wgRequest->getText( 'wpText' );
81 $this->subject = $wgRequest->getText( 'wpSubject' );
84 function showForm( $err ) {
85 global $wgOut, $wgUser, $wgLang;
87 $wgOut->setPagetitle( wfMsg( "emailpage" ) );
88 $wgOut->addWikiText( wfMsg( "emailpagetext" ) );
90 if ( $this->subject === "" ) {
91 $this->subject = wfMsg( "defemailsubject" );
94 $emf = wfMsg( "emailfrom" );
95 $sender = $wgUser->getName();
96 $emt = wfMsg( "emailto" );
97 $rcpt = str_replace( "_", " ", $this->target );
98 $emr = wfMsg( "emailsubject" );
99 $emm = wfMsg( "emailmessage" );
100 $ems = wfMsg( "emailsend" );
101 $encSubject = htmlspecialchars( $this->subject );
103 $titleObj = Title::makeTitle( NS_SPECIAL, "Emailuser" );
104 $action = $titleObj->escapeLocalURL( "target={$this->target}&action=submit" );
106 if ( "" != $err ) {
107 $wgOut->setSubtitle( wfMsg( "formerror" ) );
108 $wgOut->addHTML( "<p><font color='red' size='+1'>{$err}</font></p>\n" );
110 $wgOut->addHTML( "
111 <form id=\"emailuser\" method=\"post\" action=\"{$action}\">
112 <table border='0'><tr>
113 <td align='right'>{$emf}:</td>
114 <td align='left'><strong>{$sender}</strong></td>
115 </tr><tr>
116 <td align='right'>{$emt}:</td>
117 <td align='left'><strong>{$rcpt}</strong></td>
118 </tr><tr>
119 <td align='right'>{$emr}:</td>
120 <td align='left'>
121 <input type='text' name=\"wpSubject\" value=\"{$encSubject}\" />
122 </td>
123 </tr><tr>
124 <td align='right'>{$emm}:</td>
125 <td align='left'>
126 <textarea name=\"wpText\" rows='10' cols='60' wrap='virtual'>" . htmlspecialchars( $this->text ) .
127 "</textarea>
128 </td></tr><tr>
129 <td>&nbsp;</td><td align='left'>
130 <input type='submit' name=\"wpSend\" value=\"{$ems}\" />
131 </td></tr></table>
132 </form>\n" );
136 function doSubmit() {
137 global $wgOut, $wgUser, $wgLang, $wgOutputEncoding;
139 $from = wfQuotedPrintable( $wgUser->getName() ) . " <" . $wgUser->getEmail() . ">";
140 $subject = wfQuotedPrintable( $this->subject );
142 if (wfRunHooks('EmailUser', $this->mAddress, $from, $subject, $this->text)) {
144 $mailResult = userMailer( $this->mAddress, $from, $subject, $this->text );
146 if (!$mailResult) {
147 $titleObj = Title::makeTitle( NS_SPECIAL, "Emailuser" );
148 $encTarget = wfUrlencode( $this->target );
149 $wgOut->redirect( $titleObj->getFullURL( "target={$encTarget}&action=success" ) );
150 wfRunHooks('EmailUserComplete', $this->mAddress, $from, $subject, $this->text);
151 } else {
152 $wgOut->addHTML( wfMsg( "usermailererror" ) . $mailResult);
157 function showSuccess() {
158 global $wgOut, $wgUser;
160 $wgOut->setPagetitle( wfMsg( "emailsent" ) );
161 $wgOut->addHTML( wfMsg( "emailsenttext" ) );
163 $wgOut->returnToMain( false );