ApiEmailUser.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. /**
  3. * Copyright © 2008 Bryan Tong Minh <Bryan.TongMinh@Gmail.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. */
  22. /**
  23. * API Module to facilitate sending of emails to users
  24. * @ingroup API
  25. */
  26. class ApiEmailUser extends ApiBase {
  27. public function execute() {
  28. $params = $this->extractRequestParams();
  29. // Validate target
  30. $targetUser = SpecialEmailUser::getTarget( $params['target'], $this->getUser() );
  31. if ( !( $targetUser instanceof User ) ) {
  32. switch ( $targetUser ) {
  33. case 'notarget':
  34. $this->dieWithError( 'apierror-notarget' );
  35. case 'noemail':
  36. $this->dieWithError( [ 'noemail', $params['target'] ] );
  37. case 'nowikiemail':
  38. $this->dieWithError( 'nowikiemailtext', 'nowikiemail' );
  39. default:
  40. $this->dieWithError( [ 'apierror-unknownerror', $targetUser ] );
  41. }
  42. }
  43. // Check permissions and errors
  44. $error = SpecialEmailUser::getPermissionsError(
  45. $this->getUser(),
  46. $params['token'],
  47. $this->getConfig()
  48. );
  49. if ( $error ) {
  50. $this->dieWithError( $error );
  51. }
  52. $data = [
  53. 'Target' => $targetUser->getName(),
  54. 'Text' => $params['text'],
  55. 'Subject' => $params['subject'],
  56. 'CCMe' => $params['ccme'],
  57. ];
  58. $retval = SpecialEmailUser::submit( $data, $this->getContext() );
  59. if ( !$retval instanceof Status ) {
  60. // This is probably the reason
  61. $retval = Status::newFatal( 'hookaborted' );
  62. }
  63. $result = array_filter( [
  64. 'result' => $retval->isGood() ? 'Success' : ( $retval->isOK() ? 'Warnings' : 'Failure' ),
  65. 'warnings' => $this->getErrorFormatter()->arrayFromStatus( $retval, 'warning' ),
  66. 'errors' => $this->getErrorFormatter()->arrayFromStatus( $retval, 'error' ),
  67. ] );
  68. $this->getResult()->addValue( null, $this->getModuleName(), $result );
  69. }
  70. public function mustBePosted() {
  71. return true;
  72. }
  73. public function isWriteMode() {
  74. return true;
  75. }
  76. public function getAllowedParams() {
  77. return [
  78. 'target' => [
  79. ApiBase::PARAM_TYPE => 'string',
  80. ApiBase::PARAM_REQUIRED => true
  81. ],
  82. 'subject' => null,
  83. 'text' => [
  84. ApiBase::PARAM_TYPE => 'text',
  85. ApiBase::PARAM_REQUIRED => true
  86. ],
  87. 'ccme' => false,
  88. ];
  89. }
  90. public function needsToken() {
  91. return 'csrf';
  92. }
  93. protected function getExamplesMessages() {
  94. return [
  95. 'action=emailuser&target=WikiSysop&text=Content&token=123ABC'
  96. => 'apihelp-emailuser-example-email',
  97. ];
  98. }
  99. public function getHelpUrls() {
  100. return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Email';
  101. }
  102. }