* XHTML fix: encoding of usernames
[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 ( !$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 ) { $f->showSuccess(); }
61 else if ( "submit" == $action && $wgRequest->wasPosted() ) { $f->doSubmit(); }
62 else { $f->showForm(); }
63 }
64
65 /**
66 * @todo document
67 * @package MediaWiki
68 * @subpackage SpecialPage
69 */
70 class EmailUserForm {
71
72 var $mAddress;
73 var $target;
74 var $text, $subject;
75
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' );
82 }
83
84 function showForm() {
85 global $wgOut, $wgUser, $wgLang;
86
87 $wgOut->setPagetitle( wfMsg( "emailpage" ) );
88 $wgOut->addWikiText( wfMsg( "emailpagetext" ) );
89
90 if ( $this->subject === "" ) {
91 $this->subject = wfMsg( "defemailsubject" );
92 }
93
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 );
102
103 $titleObj = Title::makeTitle( NS_SPECIAL, "Emailuser" );
104 $action = $titleObj->escapeLocalURL( "target=" .
105 urlencode( $this->target ) . "&action=submit" );
106
107 $wgOut->addHTML( "
108 <form id=\"emailuser\" method=\"post\" action=\"{$action}\">
109 <table border='0'><tr>
110 <td align='right'>{$emf}:</td>
111 <td align='left'><strong>" . htmlspecialchars( $sender ) . "</strong></td>
112 </tr><tr>
113 <td align='right'>{$emt}:</td>
114 <td align='left'><strong>" . htmlspecialchars( $rcpt ) . "</strong></td>
115 </tr><tr>
116 <td align='right'>{$emr}:</td>
117 <td align='left'>
118 <input type='text' name=\"wpSubject\" value=\"{$encSubject}\" />
119 </td>
120 </tr><tr>
121 <td align='right'>{$emm}:</td>
122 <td align='left'>
123 <textarea name=\"wpText\" rows='10' cols='60' wrap='virtual'>" . htmlspecialchars( $this->text ) .
124 "</textarea>
125 </td></tr><tr>
126 <td>&nbsp;</td><td align='left'>
127 <input type='submit' name=\"wpSend\" value=\"{$ems}\" />
128 </td></tr></table>
129 </form>\n" );
130
131 }
132
133 function doSubmit() {
134 global $wgOut, $wgUser, $wgLang, $wgOutputEncoding;
135
136 $from = wfQuotedPrintable( $wgUser->getName() ) . " <" . $wgUser->getEmail() . ">";
137 $subject = wfQuotedPrintable( $this->subject );
138
139 if (wfRunHooks('EmailUser', $this->mAddress, $from, $subject, $this->text)) {
140
141 $mailResult = userMailer( $this->mAddress, $from, $subject, $this->text );
142
143 if (!$mailResult) {
144 $titleObj = Title::makeTitle( NS_SPECIAL, "Emailuser" );
145 $encTarget = wfUrlencode( $this->target );
146 $wgOut->redirect( $titleObj->getFullURL( "target={$encTarget}&action=success" ) );
147 wfRunHooks('EmailUserComplete', $this->mAddress, $from, $subject, $this->text);
148 } else {
149 $wgOut->addHTML( wfMsg( "usermailererror" ) . $mailResult);
150 }
151 }
152 }
153
154 function showSuccess() {
155 global $wgOut, $wgUser;
156
157 $wgOut->setPagetitle( wfMsg( "emailsent" ) );
158 $wgOut->addHTML( wfMsg( "emailsenttext" ) );
159
160 $wgOut->returnToMain( false );
161 }
162 }
163 ?>