Remove double globals.
[lhc/web/wiklou.git] / includes / api / ApiEmailUser.php
1 <?php
2
3 /**
4 * Created on June 1, 2008
5 * API for MediaWiki 1.8+
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
25 if ( !defined( 'MEDIAWIKI' ) ) {
26 // Eclipse helper - will be ignored in production
27 require_once( "ApiBase.php" );
28 }
29
30 /**
31 * API Module to facilitate sending of emails to users
32 * @ingroup API
33 */
34 class ApiEmailUser extends ApiBase {
35
36 public function __construct( $main, $action ) {
37 parent::__construct( $main, $action );
38 }
39
40 public function execute() {
41 global $wgUser;
42
43 $params = $this->extractRequestParams();
44 // Check required parameters
45 if ( !isset( $params['target'] ) ) {
46 $this->dieUsageMsg( array( 'missingparam', 'target' ) );
47 }
48 if ( !isset( $params['text'] ) ) {
49 $this->dieUsageMsg( array( 'missingparam', 'text' ) );
50 }
51
52 // Validate target
53 $targetUser = SpecialEmailuser::getTarget( $params['target'] );
54 if ( !( $targetUser instanceof User ) ) {
55 $this->dieUsageMsg( array( $targetUser ) );
56 }
57
58 // Check permissions and errors
59 $error = SpecialEmailuser::getPermissionsError( $wgUser, $params['token'] );
60 if ( $error ) {
61 $this->dieUsageMsg( array( $error ) );
62 }
63
64 $data = array(
65 'Target' => $targetUser->getName(),
66 'Text' => $params['text'],
67 'Subject' => $params['subject'],
68 'CCMe' => $params['ccme'],
69 );
70 $retval = SpecialEmailuser::submit( $data );
71 if ( $retval === true ) {
72 $result = array( 'result' => 'Success' );
73 } else {
74 $result = array(
75 'result' => 'Failure',
76 'message' => $retval
77 );
78 }
79
80 $this->getResult()->addValue( null, $this->getModuleName(), $result );
81 }
82
83 public function mustBePosted() {
84 return true;
85 }
86
87 public function isWriteMode() {
88 return true;
89 }
90
91 public function getAllowedParams() {
92 return array(
93 'target' => null,
94 'subject' => null,
95 'text' => null,
96 'token' => null,
97 'ccme' => false,
98 );
99 }
100
101 public function getParamDescription() {
102 return array(
103 'target' => 'User to send email to',
104 'subject' => 'Subject header',
105 'text' => 'Mail body',
106 'token' => 'A token previously acquired via prop=info',
107 'ccme' => 'Send a copy of this mail to me',
108 );
109 }
110
111 public function getDescription() {
112 return 'Email a user.';
113 }
114
115 public function getPossibleErrors() {
116 return array_merge( parent::getPossibleErrors(), array(
117 array( 'usermaildisabled' ),
118 array( 'missingparam', 'target' ),
119 array( 'missingparam', 'text' ),
120 ) );
121 }
122
123 public function getTokenSalt() {
124 return '';
125 }
126
127 protected function getExamples() {
128 return array(
129 'api.php?action=emailuser&target=WikiSysop&text=Content'
130 );
131 }
132
133 public function getVersion() {
134 return __CLASS__ . ': $Id$';
135 }
136 }