886e3e170f3338a077e9c3a0d3d807a2709419e8
[lhc/web/wiklou.git] / includes / api / ApiEmailUser.php
1 <?php
2 /**
3 * API for MediaWiki 1.8+
4 *
5 * Created on June 1, 2008
6 *
7 * Copyright © 2008 Bryan Tong Minh <Bryan.TongMinh@Gmail.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
23 *
24 * @file
25 */
26
27 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( "ApiBase.php" );
30 }
31
32 /**
33 * API Module to facilitate sending of emails to users
34 * @ingroup API
35 */
36 class ApiEmailUser extends ApiBase {
37
38 public function __construct( $main, $action ) {
39 parent::__construct( $main, $action );
40 }
41
42 public function execute() {
43 global $wgUser;
44
45 $params = $this->extractRequestParams();
46
47 // Validate target
48 $targetUser = SpecialEmailUser::getTarget( $params['target'] );
49 if ( !( $targetUser instanceof User ) ) {
50 $this->dieUsageMsg( array( $targetUser ) );
51 }
52
53 // Check permissions and errors
54 $error = SpecialEmailUser::getPermissionsError( $wgUser, $params['token'] );
55 if ( $error ) {
56 $this->dieUsageMsg( array( $error ) );
57 }
58
59 $data = array(
60 'Target' => $targetUser->getName(),
61 'Text' => $params['text'],
62 'Subject' => $params['subject'],
63 'CCMe' => $params['ccme'],
64 );
65 $retval = SpecialEmailUser::submit( $data );
66 if ( $retval === true ) {
67 $result = array( 'result' => 'Success' );
68 } else {
69 $result = array(
70 'result' => 'Failure',
71 'message' => $retval
72 );
73 }
74
75 $this->getResult()->addValue( null, $this->getModuleName(), $result );
76 }
77
78 public function mustBePosted() {
79 return true;
80 }
81
82 public function isWriteMode() {
83 return true;
84 }
85
86 public function getAllowedParams() {
87 return array(
88 'target' => array(
89 ApiBase::PARAM_TYPE => 'string',
90 ApiBase::PARAM_REQUIRED => true
91 ),
92 'subject' => null,
93 'text' => array(
94 ApiBase::PARAM_TYPE => 'string',
95 ApiBase::PARAM_REQUIRED => true
96 ),
97 'token' => null,
98 'ccme' => false,
99 );
100 }
101
102 public function getParamDescription() {
103 return array(
104 'target' => 'User to send email to',
105 'subject' => 'Subject header',
106 'text' => 'Mail body',
107 'token' => 'A token previously acquired via prop=info',
108 'ccme' => 'Send a copy of this mail to me',
109 );
110 }
111
112 public function getDescription() {
113 return 'Email a user.';
114 }
115
116 public function getPossibleErrors() {
117 return array_merge( parent::getPossibleErrors(), array(
118 array( 'usermaildisabled' ),
119 ) );
120 }
121
122 public function getTokenSalt() {
123 return '';
124 }
125
126 protected function getExamples() {
127 return array(
128 'api.php?action=emailuser&target=WikiSysop&text=Content'
129 );
130 }
131
132 public function getVersion() {
133 return __CLASS__ . ': $Id$';
134 }
135 }