Index: trunk/phase3/includes/SpecialEmailuser.php |
— | — | @@ -8,52 +8,23 @@ |
9 | 9 | * @todo document |
10 | 10 | */ |
11 | 11 | function wfSpecialEmailuser( $par ) { |
12 | | - global $wgUser, $wgOut, $wgRequest, $wgEnableEmail, $wgEnableUserEmail; |
| 12 | + global $wgRequest, $wgUser, $wgOut; |
13 | 13 | |
14 | | - if( !( $wgEnableEmail && $wgEnableUserEmail ) ) { |
15 | | - $wgOut->showErrorPage( "nosuchspecialpage", "nospecialpagetext" ); |
| 14 | + $target = isset($par) ? $par : $wgRequest->getVal( 'target' ); |
| 15 | + $error = EmailUserForm::getPermissionsError( $target ); |
| 16 | + if ( $error ) { |
| 17 | + $wgOut->showErrorPage( $error[0], $error[1] ); |
16 | 18 | return; |
17 | 19 | } |
18 | 20 | |
19 | | - if( !$wgUser->canSendEmail() ) { |
20 | | - wfDebug( "User can't send.\n" ); |
21 | | - $wgOut->showErrorPage( "mailnologin", "mailnologintext" ); |
22 | | - return; |
23 | | - } |
24 | | - |
| 21 | + $form = EmailUserForm::newFromURL( $target, |
| 22 | + $wgRequest->getText( 'wpText' ), |
| 23 | + $wgRequest->getText( 'wpSubject' ), |
| 24 | + $wgRequest->getBool( 'wpCCMe' ) ); |
| 25 | + |
25 | 26 | $action = $wgRequest->getVal( 'action' ); |
26 | | - $target = isset($par) ? $par : $wgRequest->getVal( 'target' ); |
27 | | - if ( "" == $target ) { |
28 | | - wfDebug( "Target is empty.\n" ); |
29 | | - $wgOut->showErrorPage( "notargettitle", "notargettext" ); |
30 | | - return; |
31 | | - } |
32 | | - |
33 | | - $nt = Title::newFromURL( $target ); |
34 | | - if ( is_null( $nt ) ) { |
35 | | - wfDebug( "Target is invalid title.\n" ); |
36 | | - $wgOut->showErrorPage( "notargettitle", "notargettext" ); |
37 | | - return; |
38 | | - } |
39 | | - |
40 | | - $nu = User::newFromName( $nt->getText() ); |
41 | | - if( is_null( $nu ) || !$nu->canReceiveEmail() ) { |
42 | | - wfDebug( "Target is invalid user or can't receive.\n" ); |
43 | | - $wgOut->showErrorPage( "noemailtitle", "noemailtext" ); |
44 | | - return; |
45 | | - } |
46 | | - |
47 | | - if ( $wgUser->isBlockedFromEmailUser() ) { |
48 | | - // User has been blocked from sending e-mail. Show the std blocked form. |
49 | | - wfDebug( "User is blocked from sending e-mail.\n" ); |
50 | | - $wgOut->blockedPage(); |
51 | | - return; |
52 | | - } |
53 | | - |
54 | | - $f = new EmailUserForm( $nu ); |
55 | | - |
56 | 27 | if ( "success" == $action ) { |
57 | | - $f->showSuccess( $nu ); |
| 28 | + $form->showSuccess(); |
58 | 29 | } else if ( "submit" == $action && $wgRequest->wasPosted() && |
59 | 30 | $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) ) ) |
60 | 31 | { |
— | — | @@ -63,9 +34,18 @@ |
64 | 35 | return; |
65 | 36 | } |
66 | 37 | |
67 | | - $f->doSubmit(); |
| 38 | + $result = $form->doSubmit(); |
| 39 | + |
| 40 | + if ( !is_null( $result ) ) { |
| 41 | + $wgOut->addHTML( wfMsg( "usermailererror" ) . |
| 42 | + ' ' . htmlspecialchars( $result->getMessage() ) ); |
| 43 | + } else { |
| 44 | + $titleObj = SpecialPage::getTitleFor( "Emailuser" ); |
| 45 | + $encTarget = wfUrlencode( $form->getTarget()->getName() ); |
| 46 | + $wgOut->redirect( $titleObj->getFullURL( "target={$encTarget}&action=success" ) ); |
| 47 | + } |
68 | 48 | } else { |
69 | | - $f->showForm(); |
| 49 | + $form->showForm(); |
70 | 50 | } |
71 | 51 | } |
72 | 52 | |
— | — | @@ -82,12 +62,11 @@ |
83 | 63 | /** |
84 | 64 | * @param User $target |
85 | 65 | */ |
86 | | - function EmailUserForm( $target ) { |
87 | | - global $wgRequest; |
| 66 | + function EmailUserForm( $target, $text, $subject, $cc_me ) { |
88 | 67 | $this->target = $target; |
89 | | - $this->text = $wgRequest->getText( 'wpText' ); |
90 | | - $this->subject = $wgRequest->getText( 'wpSubject' ); |
91 | | - $this->cc_me = $wgRequest->getBool( 'wpCCMe' ); |
| 68 | + $this->text = $text; |
| 69 | + $this->subject = $subject; |
| 70 | + $this->cc_me = $cc_me; |
92 | 71 | } |
93 | 72 | |
94 | 73 | function showForm() { |
— | — | @@ -143,8 +122,13 @@ |
144 | 123 | |
145 | 124 | } |
146 | 125 | |
| 126 | + /* |
| 127 | + * Really send a mail. Permissions should have been checked using |
| 128 | + * EmailUserForm::getPermissionsError. It is probably also a good idea to |
| 129 | + * check the edit token and ping limiter in advance. |
| 130 | + */ |
147 | 131 | function doSubmit() { |
148 | | - global $wgOut, $wgUser, $wgUserEmailUseReplyTo; |
| 132 | + global $wgUser, $wgUserEmailUseReplyTo; |
149 | 133 | |
150 | 134 | $to = new MailAddress( $this->target ); |
151 | 135 | $from = new MailAddress( $wgUser ); |
— | — | @@ -183,8 +167,8 @@ |
184 | 168 | $mailResult = UserMailer::send( $to, $mailFrom, $subject, $this->text, $replyTo ); |
185 | 169 | |
186 | 170 | if( WikiError::isError( $mailResult ) ) { |
187 | | - $wgOut->addHTML( wfMsg( "usermailererror" ) . |
188 | | - ' ' . htmlspecialchars( $mailResult->getMessage() ) ); |
| 171 | + return $mailResult; |
| 172 | + |
189 | 173 | } else { |
190 | 174 | |
191 | 175 | // if the user requested a copy of this mail, do this now, |
— | — | @@ -199,27 +183,68 @@ |
200 | 184 | // We can either show them an error, or we can say everything was fine, |
201 | 185 | // or we can say we sort of failed AND sort of succeeded. Of these options, |
202 | 186 | // simply saying there was an error is probably best. |
203 | | - $wgOut->addHTML( wfMsg( "usermailererror" ) . |
204 | | - ' ' . htmlspecialchars( $ccResult->getMessage() ) ); |
205 | | - return; |
| 187 | + return $ccResult->getMessage(); |
206 | 188 | } |
207 | 189 | } |
208 | 190 | } |
209 | 191 | |
210 | | - $titleObj = SpecialPage::getTitleFor( "Emailuser" ); |
211 | | - $encTarget = wfUrlencode( $this->target->getName() ); |
212 | | - $wgOut->redirect( $titleObj->getFullURL( "target={$encTarget}&action=success" ) ); |
213 | 192 | wfRunHooks( 'EmailUserComplete', array( $to, $from, $subject, $this->text ) ); |
| 193 | + return; |
214 | 194 | } |
215 | 195 | } |
216 | 196 | } |
217 | 197 | |
218 | | - function showSuccess( &$user ) { |
| 198 | + function showSuccess( &$user = null ) { |
219 | 199 | global $wgOut; |
| 200 | + |
| 201 | + if ( is_null($user) ) |
| 202 | + $user = $this->target; |
220 | 203 | |
221 | 204 | $wgOut->setPagetitle( wfMsg( "emailsent" ) ); |
222 | 205 | $wgOut->addHTML( wfMsg( "emailsenttext" ) ); |
223 | 206 | |
224 | 207 | $wgOut->returnToMain( false, $user->getUserPage() ); |
225 | 208 | } |
| 209 | + |
| 210 | + function getTarget() { |
| 211 | + return $this->target; |
| 212 | + } |
| 213 | + |
| 214 | + static function getPermissionsError ( $target ) { |
| 215 | + global $wgUser, $wgRequest, $wgEnableEmail, $wgEnableUserEmail; |
| 216 | + |
| 217 | + if( !( $wgEnableEmail && $wgEnableUserEmail ) ) |
| 218 | + return array( "nosuchspecialpage", "nospecialpagetext" ); |
| 219 | + |
| 220 | + if( !$wgUser->canSendEmail() ) { |
| 221 | + wfDebug( "User can't send.\n" ); |
| 222 | + return array( "mailnologin", "mailnologintext" ); |
| 223 | + } |
| 224 | + |
| 225 | + if ( "" == $target ) { |
| 226 | + wfDebug( "Target is empty.\n" ); |
| 227 | + return array( "notargettitle", "notargettext" ); |
| 228 | + } |
| 229 | + |
| 230 | + $nt = Title::newFromURL( $target ); |
| 231 | + if ( is_null( $nt ) ) { |
| 232 | + wfDebug( "Target is invalid title.\n" ); |
| 233 | + return array( "notargettitle", "notargettext" ); |
| 234 | + } |
| 235 | + |
| 236 | + $nu = User::newFromName( $nt->getText() ); |
| 237 | + if( is_null( $nu ) || !$nu->canReceiveEmail() ) { |
| 238 | + wfDebug( "Target is invalid user or can't receive.\n" ); |
| 239 | + return array( "noemailtitle", "noemailtext" ); |
| 240 | + } |
| 241 | + |
| 242 | + return; |
| 243 | + } |
| 244 | + |
| 245 | + static function newFromURL( $target, $text, $subject, $cc_me ) |
| 246 | + { |
| 247 | + $nt = Title::newFromURL( $target ); |
| 248 | + $nu = User::newFromName( $nt->getText() ); |
| 249 | + return new EmailUserForm( $nu, $text, $subject, $cc_me ); |
| 250 | + } |
226 | 251 | } |