ApiEmailUser.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /*
  3. * Created on June 1, 2008
  4. * API for MediaWiki 1.8+
  5. *
  6. * Copyright (C) 2008 Bryan Tong Minh <Bryan.TongMinh@Gmail.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21. * http://www.gnu.org/copyleft/gpl.html
  22. */
  23. if (!defined('MEDIAWIKI')) {
  24. // Eclipse helper - will be ignored in production
  25. require_once ("ApiBase.php");
  26. }
  27. /**
  28. * @ingroup API
  29. */
  30. class ApiEmailUser extends ApiBase {
  31. public function __construct($main, $action) {
  32. parent :: __construct($main, $action);
  33. }
  34. public function execute() {
  35. global $wgUser;
  36. // Check whether email is enabled
  37. if ( !EmailUserForm::userEmailEnabled() )
  38. $this->dieUsageMsg( array( 'usermaildisabled' ) );
  39. $params = $this->extractRequestParams();
  40. // Check required parameters
  41. if ( !isset( $params['target'] ) )
  42. $this->dieUsageMsg( array( 'missingparam', 'target' ) );
  43. if ( !isset( $params['text'] ) )
  44. $this->dieUsageMsg( array( 'missingparam', 'text' ) );
  45. if ( !isset( $params['token'] ) )
  46. $this->dieUsageMsg( array( 'missingparam', 'token' ) );
  47. // Validate target
  48. $targetUser = EmailUserForm::validateEmailTarget( $params['target'] );
  49. if ( !( $targetUser instanceof User ) )
  50. $this->dieUsageMsg( array( $targetUser ) );
  51. // Check permissions
  52. $error = EmailUserForm::getPermissionsError( $wgUser, $params['token'] );
  53. if ( $error )
  54. $this->dieUsageMsg( array( $error ) );
  55. $form = new EmailUserForm( $targetUser, $params['text'], $params['subject'], $params['ccme'] );
  56. $retval = $form->doSubmit();
  57. if ( is_null( $retval ) )
  58. $result = array( 'result' => 'Success' );
  59. else
  60. $result = array( 'result' => 'Failure',
  61. 'message' => $retval->getMessage() );
  62. $this->getResult()->addValue( null, $this->getModuleName(), $result );
  63. }
  64. public function mustBePosted() { return true; }
  65. public function isWriteMode() {
  66. return true;
  67. }
  68. public function getAllowedParams() {
  69. return array (
  70. 'target' => null,
  71. 'subject' => null,
  72. 'text' => null,
  73. 'token' => null,
  74. 'ccme' => false,
  75. );
  76. }
  77. public function getParamDescription() {
  78. return array (
  79. 'target' => 'User to send email to',
  80. 'subject' => 'Subject header',
  81. 'text' => 'Mail body',
  82. 'token' => 'A token previously acquired via prop=info',
  83. 'ccme' => 'Send a copy of this mail to me',
  84. );
  85. }
  86. public function getDescription() {
  87. return array(
  88. 'Email a user.'
  89. );
  90. }
  91. protected function getExamples() {
  92. return array (
  93. 'api.php?action=emailuser&target=WikiSysop&text=Content'
  94. );
  95. }
  96. public function getVersion() {
  97. return __CLASS__ . ': $Id: ApiEmailUser.php 48091 2009-03-06 13:49:44Z catrope $';
  98. }
  99. }