Cruft war continues: more uses of isAnon() and isLoggedIn()
[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 ( $wgUser->isAnon() ||
22 ( !$wgUser->isValidEmailAddr( $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 ( ( !$nu->isValidEmailAddr( $address ) ) ||
52 ( 1 == $nu->getOption( "disablemail" ) ) ||
53 ( 0 == $nu->getEmailauthenticationtimestamp() ) ) {
54 $wgOut->errorpage( "noemailtitle", "noemailtext" );
55 return;
56 }
57
58 $f = new EmailUserForm( $nu->getName() . " <{$address}>", $target );
59
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();
67 }
68 }
69
70 /**
71 * @todo document
72 * @package MediaWiki
73 * @subpackage SpecialPage
74 */
75 class EmailUserForm {
76
77 var $mAddress;
78 var $target;
79 var $text, $subject;
80
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' );
87 }
88
89 function showForm() {
90 global $wgOut, $wgUser, $wgLang;
91
92 $wgOut->setPagetitle( wfMsg( "emailpage" ) );
93 $wgOut->addWikiText( wfMsg( "emailpagetext" ) );
94
95 if ( $this->subject === "" ) {
96 $this->subject = wfMsg( "defemailsubject" );
97 }
98
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 );
107
108 $titleObj = Title::makeTitle( NS_SPECIAL, "Emailuser" );
109 $action = $titleObj->escapeLocalURL( "target=" .
110 urlencode( $this->target ) . "&action=submit" );
111 $token = $wgUser->editToken();
112
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" );
137
138 }
139
140 function doSubmit() {
141 global $wgOut, $wgUser, $wgLang, $wgOutputEncoding;
142
143 $from = wfQuotedPrintable( $wgUser->getName() ) . " <" . $wgUser->getEmail() . ">";
144 $subject = wfQuotedPrintable( $this->subject );
145
146 if (wfRunHooks('EmailUser', $this->mAddress, $from, $subject, $this->text)) {
147
148 $mailResult = userMailer( $this->mAddress, $from, $subject, $this->text );
149
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', $this->mAddress, $from, $subject, $this->text);
155 } else {
156 $wgOut->addHTML( wfMsg( "usermailererror" ) . $mailResult);
157 }
158 }
159 }
160
161 function showSuccess() {
162 global $wgOut, $wgUser;
163
164 $wgOut->setPagetitle( wfMsg( "emailsent" ) );
165 $wgOut->addHTML( wfMsg( "emailsenttext" ) );
166
167 $wgOut->returnToMain( false );
168 }
169 }
170 ?>