PageRenderTime 21ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/mediawiki-integration/source/php/mediawiki/includes/SpecialEmailuser.php

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