* 0 => NS_MAIN
[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->canSendEmail() ) {
22 wfDebug( "User can't send.\n" );
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 wfDebug( "Target is empty.\n" );
35 $wgOut->errorpage( "notargettitle", "notargettext" );
36 return;
37 }
38
39 $nt = Title::newFromURL( $target );
40 if ( is_null( $nt ) ) {
41 wfDebug( "Target is invalid title.\n" );
42 $wgOut->errorpage( "notargettitle", "notargettext" );
43 return;
44 }
45
46 $nu = User::newFromName( $nt->getText() );
47 if( is_null( $nu ) || !$nu->canReceiveEmail() ) {
48 wfDebug( "Target is invalid user or can't receive.\n" );
49 $wgOut->errorpage( "noemailtitle", "noemailtext" );
50 return;
51 }
52
53 $address = $nu->getEmail();
54 $f = new EmailUserForm( $nu->getName() . " <{$address}>", $target );
55
56 if ( "success" == $action ) {
57 $f->showSuccess();
58 } else if ( "submit" == $action && $wgRequest->wasPosted() &&
59 $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) ) ) {
60 $f->doSubmit();
61 } else {
62 $f->showForm();
63 }
64 }
65
66 /**
67 * @todo document
68 * @package MediaWiki
69 * @subpackage SpecialPage
70 */
71 class EmailUserForm {
72
73 var $mAddress;
74 var $target;
75 var $text, $subject;
76
77 function EmailUserForm( $addr, $target ) {
78 global $wgRequest;
79 $this->mAddress = $addr;
80 $this->target = $target;
81 $this->text = $wgRequest->getText( 'wpText' );
82 $this->subject = $wgRequest->getText( 'wpSubject' );
83 }
84
85 function showForm() {
86 global $wgOut, $wgUser, $wgLang;
87
88 $wgOut->setPagetitle( wfMsg( "emailpage" ) );
89 $wgOut->addWikiText( wfMsg( "emailpagetext" ) );
90
91 if ( $this->subject === "" ) {
92 $this->subject = wfMsg( "defemailsubject" );
93 }
94
95 $emf = wfMsg( "emailfrom" );
96 $sender = $wgUser->getName();
97 $emt = wfMsg( "emailto" );
98 $rcpt = str_replace( "_", " ", $this->target );
99 $emr = wfMsg( "emailsubject" );
100 $emm = wfMsg( "emailmessage" );
101 $ems = wfMsg( "emailsend" );
102 $encSubject = htmlspecialchars( $this->subject );
103
104 $titleObj = Title::makeTitle( NS_SPECIAL, "Emailuser" );
105 $action = $titleObj->escapeLocalURL( "target=" .
106 urlencode( $this->target ) . "&action=submit" );
107 $token = $wgUser->editToken();
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>" . htmlspecialchars( $sender ) . "</strong></td>
114 </tr><tr>
115 <td align='right'>{$emt}:</td>
116 <td align='left'><strong>" . htmlspecialchars( $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 <input type='hidden' name='wpEditToken' value=\"$token\" />
132 </form>\n" );
133
134 }
135
136 function doSubmit() {
137 global $wgOut, $wgUser, $wgLang, $wgOutputEncoding;
138
139 $from = wfQuotedPrintable( $wgUser->getName() ) . " <" . $wgUser->getEmail() . ">";
140 $subject = wfQuotedPrintable( $this->subject );
141
142 if (wfRunHooks('EmailUser', array(&$this->mAddress, &$from, &$subject, &$this->text))) {
143
144 $mailResult = userMailer( $this->mAddress, $from, $subject, $this->text );
145
146 if( WikiError::isError( $mailResult ) ) {
147 $wgOut->addHTML( wfMsg( "usermailererror" ) . $mailResult);
148 } else {
149 $titleObj = Title::makeTitle( NS_SPECIAL, "Emailuser" );
150 $encTarget = wfUrlencode( $this->target );
151 $wgOut->redirect( $titleObj->getFullURL( "target={$encTarget}&action=success" ) );
152 wfRunHooks('EmailUserComplete', array($this->mAddress, $from, $subject, $this->text));
153 }
154 }
155 }
156
157 function showSuccess() {
158 global $wgOut, $wgUser;
159
160 $wgOut->setPagetitle( wfMsg( "emailsent" ) );
161 $wgOut->addHTML( wfMsg( "emailsenttext" ) );
162
163 $wgOut->returnToMain( false );
164 }
165 }
166 ?>