(bug 7098) Add an option to disable/enable sending of HTTP ETag headers,
[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->showErrorPage( "nosuchspecialpage", "nospecialpagetext" );
18 return;
19 }
20
21 if( !$wgUser->canSendEmail() ) {
22 wfDebug( "User can't send.\n" );
23 $wgOut->showErrorPage( "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->showErrorPage( "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->showErrorPage( "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->showErrorPage( "noemailtitle", "noemailtext" );
46 return;
47 }
48
49 $f = new EmailUserForm( $nu );
50
51 if ( "success" == $action ) {
52 $f->showSuccess();
53 } else if ( "submit" == $action && $wgRequest->wasPosted() &&
54 $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) ) ) {
55 $f->doSubmit();
56 } else {
57 $f->showForm();
58 }
59 }
60
61 /**
62 * @todo document
63 * @package MediaWiki
64 * @subpackage SpecialPage
65 */
66 class EmailUserForm {
67
68 var $target;
69 var $text, $subject;
70
71 /**
72 * @param User $target
73 */
74 function EmailUserForm( $target ) {
75 global $wgRequest;
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;
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 = $this->target->getName();
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->getName() ) . "&action=submit" );
103 $token = $wgUser->editToken();
104
105 $wgOut->addHTML( "
106 <form id=\"emailuser\" method=\"post\" action=\"{$action}\">
107 <table border='0' id='mailheader'><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' size='60' maxlength='200' name=\"wpSubject\" value=\"{$encSubject}\" />
117 </td>
118 </tr>
119 </table>
120 <span id='wpTextLabel'><label for=\"wpText\">{$emm}:</label><br /></span>
121 <textarea name=\"wpText\" rows='20' cols='80' wrap='virtual' style=\"width: 100%;\">" . htmlspecialchars( $this->text ) .
122 "</textarea>
123 <input type='submit' name=\"wpSend\" value=\"{$ems}\" />
124 <input type='hidden' name='wpEditToken' value=\"$token\" />
125 </form>\n" );
126
127 }
128
129 function doSubmit() {
130 global $wgOut, $wgUser;
131
132 $to = new MailAddress( $this->target );
133 $from = new MailAddress( $wgUser );
134 $subject = $this->subject;
135
136 if( wfRunHooks( 'EmailUser', array( &$to, &$from, &$subject, &$this->text ) ) ) {
137
138 $mailResult = userMailer( $to, $from, $subject, $this->text );
139
140 if( WikiError::isError( $mailResult ) ) {
141 $wgOut->addHTML( wfMsg( "usermailererror" ) . $mailResult);
142 } else {
143 $titleObj = Title::makeTitle( NS_SPECIAL, "Emailuser" );
144 $encTarget = wfUrlencode( $this->target->getName() );
145 $wgOut->redirect( $titleObj->getFullURL( "target={$encTarget}&action=success" ) );
146 wfRunHooks( 'EmailUserComplete', array( $to, $from, $subject, $this->text ) );
147 }
148 }
149 }
150
151 function showSuccess() {
152 global $wgOut;
153
154 $wgOut->setPagetitle( wfMsg( "emailsent" ) );
155 $wgOut->addHTML( wfMsg( "emailsenttext" ) );
156
157 $wgOut->returnToMain( false );
158 }
159 }
160 ?>