Use Doxygen @addtogroup instead of phpdoc @package && @subpackage
[lhc/web/wiklou.git] / includes / SpecialEmailuser.php
1 <?php
2 /**
3 *
4 * @addtogroup SpecialPage
5 */
6
7 /**
8 *
9 */
10 require_once('UserMailer.php');
11
12 function wfSpecialEmailuser( $par ) {
13 global $wgUser, $wgOut, $wgRequest, $wgEnableEmail, $wgEnableUserEmail;
14
15 if( !( $wgEnableEmail && $wgEnableUserEmail ) ) {
16 $wgOut->showErrorPage( "nosuchspecialpage", "nospecialpagetext" );
17 return;
18 }
19
20 if( !$wgUser->canSendEmail() ) {
21 wfDebug( "User can't send.\n" );
22 $wgOut->showErrorPage( "mailnologin", "mailnologintext" );
23 return;
24 }
25
26 $action = $wgRequest->getVal( 'action' );
27 $target = isset($par) ? $par : $wgRequest->getVal( 'target' );
28 if ( "" == $target ) {
29 wfDebug( "Target is empty.\n" );
30 $wgOut->showErrorPage( "notargettitle", "notargettext" );
31 return;
32 }
33
34 $nt = Title::newFromURL( $target );
35 if ( is_null( $nt ) ) {
36 wfDebug( "Target is invalid title.\n" );
37 $wgOut->showErrorPage( "notargettitle", "notargettext" );
38 return;
39 }
40
41 $nu = User::newFromName( $nt->getText() );
42 if( is_null( $nu ) || !$nu->canReceiveEmail() ) {
43 wfDebug( "Target is invalid user or can't receive.\n" );
44 $wgOut->showErrorPage( "noemailtitle", "noemailtext" );
45 return;
46 }
47
48 $f = new EmailUserForm( $nu );
49
50 if ( "success" == $action ) {
51 $f->showSuccess( $nu );
52 } else if ( "submit" == $action && $wgRequest->wasPosted() &&
53 $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) ) ) {
54 $f->doSubmit();
55 } else {
56 $f->showForm();
57 }
58 }
59
60 /**
61 * @todo document
62 * @addtogroup SpecialPage
63 */
64 class EmailUserForm {
65
66 var $target;
67 var $text, $subject;
68 var $cc_me; // Whether user requested to be sent a separate copy of their email.
69
70 /**
71 * @param User $target
72 */
73 function EmailUserForm( $target ) {
74 global $wgRequest;
75 $this->target = $target;
76 $this->text = $wgRequest->getText( 'wpText' );
77 $this->subject = $wgRequest->getText( 'wpSubject' );
78 $this->cc_me = $wgRequest->getBool( 'wpCCMe' );
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 $emc = wfMsg( "emailccme" );
99 $encSubject = htmlspecialchars( $this->subject );
100
101 $titleObj = SpecialPage::getTitleFor( "Emailuser" );
102 $action = $titleObj->escapeLocalURL( "target=" .
103 urlencode( $this->target->getName() ) . "&action=submit" );
104 $token = $wgUser->editToken();
105
106 $wgOut->addHTML( "
107 <form id=\"emailuser\" method=\"post\" action=\"{$action}\">
108 <table border='0' id='mailheader'><tr>
109 <td align='right'>{$emf}:</td>
110 <td align='left'><strong>" . htmlspecialchars( $sender ) . "</strong></td>
111 </tr><tr>
112 <td align='right'>{$emt}:</td>
113 <td align='left'><strong>" . htmlspecialchars( $rcpt ) . "</strong></td>
114 </tr><tr>
115 <td align='right'>{$emr}:</td>
116 <td align='left'>
117 <input type='text' size='60' maxlength='200' name=\"wpSubject\" value=\"{$encSubject}\" />
118 </td>
119 </tr>
120 </table>
121 <span id='wpTextLabel'><label for=\"wpText\">{$emm}:</label><br /></span>
122 <textarea name=\"wpText\" rows='20' cols='80' wrap='virtual' style=\"width: 100%;\">" . htmlspecialchars( $this->text ) .
123 "</textarea>
124 " . wfCheckLabel( $emc, 'wpCCMe', 'wpCCMe', $wgUser->getBoolOption( 'ccmeonemails' ) ) . "<br />
125 <input type='submit' name=\"wpSend\" value=\"{$ems}\" />
126 <input type='hidden' name='wpEditToken' value=\"$token\" />
127 </form>\n" );
128
129 }
130
131 function doSubmit() {
132 global $wgOut, $wgUser;
133
134 $to = new MailAddress( $this->target );
135 $from = new MailAddress( $wgUser );
136 $subject = $this->subject;
137
138 if( wfRunHooks( 'EmailUser', array( &$to, &$from, &$subject, &$this->text ) ) ) {
139
140 $mailResult = userMailer( $to, $from, $subject, $this->text );
141
142 if( WikiError::isError( $mailResult ) ) {
143 $wgOut->addHTML( wfMsg( "usermailererror" ) . $mailResult);
144 } else {
145
146 // if the user requested a copy of this mail, do this now,
147 // unless they are emailing themselves, in which case one copy of the message is sufficient.
148 if ($this->cc_me && $to != $from) {
149 $cc_subject = wfMsg('emailccsubject', $this->target->getName(), $subject);
150 if( wfRunHooks( 'EmailUser', array( &$from, &$from, &$cc_subject, &$this->text ) ) ) {
151 $ccResult = userMailer( $from, $from, $cc_subject, $this->text );
152 if( WikiError::isError( $ccResult ) ) {
153 // At this stage, the user's CC mail has failed, but their
154 // original mail has succeeded. It's unlikely, but still, what to do?
155 // We can either show them an error, or we can say everything was fine,
156 // or we can say we sort of failed AND sort of succeeded. Of these options,
157 // simply saying there was an error is probably best.
158 $wgOut->addHTML( wfMsg( "usermailererror" ) . $ccResult);
159 return;
160 }
161 }
162 }
163
164 $titleObj = SpecialPage::getTitleFor( "Emailuser" );
165 $encTarget = wfUrlencode( $this->target->getName() );
166 $wgOut->redirect( $titleObj->getFullURL( "target={$encTarget}&action=success" ) );
167 wfRunHooks( 'EmailUserComplete', array( $to, $from, $subject, $this->text ) );
168 }
169 }
170 }
171
172 function showSuccess( &$user ) {
173 global $wgOut;
174
175 $wgOut->setPagetitle( wfMsg( "emailsent" ) );
176 $wgOut->addHTML( wfMsg( "emailsenttext" ) );
177
178 $wgOut->returnToMain( false, $user->getUserPage() );
179 }
180 }
181 ?>