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