Make User::newFromName set the user object's ID attribute, so it can
[lhc/web/wiklou.git] / includes / SpecialEmailuser.php
1 <?php
2 /**
3 *
4 * @package MediaWiki
5 * @subpackage SpecialPage
6 */
7
8 /**
9 *
10 */
11 require_once('UserMailer.php');
12
13 function wfSpecialEmailuser( $par ) {
14 global $wgUser, $wgOut, $wgRequest, $wgEnableEmail, $wgEnableUserEmail;
15
16 if( !( $wgEnableEmail && $wgEnableUserEmail ) ) {
17 $wgOut->errorpage( "nosuchspecialpage", "nospecialpagetext" );
18 return;
19 }
20
21 if ( 0 == $wgUser->getID() ||
22 ( false === strpos( $wgUser->getEmail(), "@" ) ) ) {
23 $wgOut->errorpage( "mailnologin", "mailnologintext" );
24 return;
25 }
26
27 $action = $wgRequest->getVal( 'action' );
28 if( empty( $par ) ) {
29 $target = $wgRequest->getVal( 'target' );
30 } else {
31 $target = $par;
32 }
33 if ( "" == $target ) {
34 $wgOut->errorpage( "notargettitle", "notargettext" );
35 return;
36 }
37 $nt = Title::newFromURL( $target );
38 if ( is_null( $nt ) ) {
39 $wgOut->errorpage( "notargettitle", "notargettext" );
40 return;
41 }
42 $nu = User::newFromName( $nt->getText() );
43
44 if ( 0 == $nu->getID() ) {
45 $wgOut->errorpage( "noemailtitle", "noemailtext" );
46 return;
47 }
48
49 $address = $nu->getEmail();
50
51 if ( ( false === strpos( $address, "@" ) ) ||
52 ( 1 == $nu->getOption( "disablemail" ) ) ) {
53 $wgOut->errorpage( "noemailtitle", "noemailtext" );
54 return;
55 }
56
57 $f = new EmailUserForm( $nu->getName() . " <{$address}>", $target );
58
59 if ( "success" == $action ) { $f->showSuccess(); }
60 else if ( "submit" == $action && $wgRequest->wasPosted() ) { $f->doSubmit(); }
61 else { $f->showForm( "" ); }
62 }
63
64 /**
65 * @todo document
66 * @package MediaWiki
67 * @subpackage SpecialPage
68 */
69 class EmailUserForm {
70
71 var $mAddress;
72 var $target;
73 var $text, $subject;
74
75 function EmailUserForm( $addr, $target ) {
76 global $wgRequest;
77 $this->mAddress = $addr;
78 $this->target = $target;
79 $this->text = $wgRequest->getText( 'wpText' );
80 $this->subject = $wgRequest->getText( 'wpSubject' );
81 }
82
83 function showForm( $err ) {
84 global $wgOut, $wgUser, $wgLang;
85
86 $wgOut->setPagetitle( wfMsg( "emailpage" ) );
87 $wgOut->addWikiText( wfMsg( "emailpagetext" ) );
88
89 if ( $this->subject === "" ) {
90 $this->subject = wfMsg( "defemailsubject" );
91 }
92
93 $emf = wfMsg( "emailfrom" );
94 $sender = $wgUser->getName();
95 $emt = wfMsg( "emailto" );
96 $rcpt = str_replace( "_", " ", $this->target );
97 $emr = wfMsg( "emailsubject" );
98 $emm = wfMsg( "emailmessage" );
99 $ems = wfMsg( "emailsend" );
100 $encSubject = htmlspecialchars( $this->subject );
101
102 $titleObj = Title::makeTitle( NS_SPECIAL, "Emailuser" );
103 $action = $titleObj->escapeLocalURL( "target={$this->target}&action=submit" );
104
105 if ( "" != $err ) {
106 $wgOut->setSubtitle( wfMsg( "formerror" ) );
107 $wgOut->addHTML( "<p><font color='red' size='+1'>{$err}</font></p>\n" );
108 }
109 $wgOut->addHTML( "
110 <form id=\"emailuser\" method=\"post\" action=\"{$action}\">
111 <table border='0'><tr>
112 <td align='right'>{$emf}:</td>
113 <td align='left'><strong>{$sender}</strong></td>
114 </tr><tr>
115 <td align='right'>{$emt}:</td>
116 <td align='left'><strong>{$rcpt}</strong></td>
117 </tr><tr>
118 <td align='right'>{$emr}:</td>
119 <td align='left'>
120 <input type='text' name=\"wpSubject\" value=\"{$encSubject}\" />
121 </td>
122 </tr><tr>
123 <td align='right'>{$emm}:</td>
124 <td align='left'>
125 <textarea name=\"wpText\" rows='10' cols='60' wrap='virtual'>" . htmlspecialchars( $this->text ) .
126 "</textarea>
127 </td></tr><tr>
128 <td>&nbsp;</td><td align='left'>
129 <input type='submit' name=\"wpSend\" value=\"{$ems}\" />
130 </td></tr></table>
131 </form>\n" );
132
133 }
134
135 function doSubmit() {
136 global $wgOut, $wgUser, $wgLang, $wgOutputEncoding;
137
138 $from = wfQuotedPrintable( $wgUser->getName() ) . " <" . $wgUser->getEmail() . ">";
139 $subject = wfQuotedPrintable( $this->subject );
140
141 if (wfRunHooks('EmailUser', $this->mAddress, $from, $subject, $this->text)) {
142
143 $mailResult = userMailer( $this->mAddress, $from, $subject, $this->text );
144
145 if (!$mailResult) {
146 $titleObj = Title::makeTitle( NS_SPECIAL, "Emailuser" );
147 $encTarget = wfUrlencode( $this->target );
148 $wgOut->redirect( $titleObj->getFullURL( "target={$encTarget}&action=success" ) );
149 wfRunHooks('EmailUserComplete', $this->mAddress, $from, $subject, $this->text);
150 } else {
151 $wgOut->addHTML( wfMsg( "usermailererror" ) . $mailResult);
152 }
153 }
154 }
155
156 function showSuccess() {
157 global $wgOut, $wgUser;
158
159 $wgOut->setPagetitle( wfMsg( "emailsent" ) );
160 $wgOut->addHTML( wfMsg( "emailsenttext" ) );
161
162 $wgOut->returnToMain( false );
163 }
164 }
165 ?>