Per 46512#c1554, moving check to before required params, returns a result rather...
[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 (C) 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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 /**
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 // Check whether email is enabled
44 if ( !EmailUserForm::userEmailEnabled() )
45 $this->dieUsageMsg( array( 'usermaildisabled' ) );
46
47 $this->getMain()->requestWriteMode();
48 $params = $this->extractRequestParams();
49
50 // Check required parameters
51 if ( !isset( $params['target'] ) )
52 $this->dieUsageMsg( array( 'missingparam', 'target' ) );
53 // Validate target
54 $targetUser = EmailUserForm::validateEmailTarget( $params['target'] );
55 if( isset( $params['check'] ) )
56 if($targetUser instanceof User) {
57 $result = array( 'result' => 'Enabled' );
58 $this->getResult()->addValue( null, $this->getModuleName(), $result );
59 return;
60 }
61 else {
62 $result = array( 'result' => 'Disabled' );
63 $this->getResult()->addValue( null, $this->getModuleName(), $result );
64 return;
65 } //$this->dieUsageMsg( array( 'usermailenabled' ) ) : $this->dieUsageMsg( array( 'usermaildisabled' ) );
66 if ( !( $targetUser instanceof User ) )
67 $this->dieUsageMsg( array( $targetUser ) );
68
69 //Check more parameters
70 if ( !isset( $params['text'] ) )
71 $this->dieUsageMsg( array( 'missingparam', 'text' ) );
72 if ( !isset( $params['token'] ) )
73 $this->dieUsageMsg( array( 'missingparam', 'token' ) );
74
75
76 // Check permissions
77 $error = EmailUserForm::getPermissionsError( $wgUser, $params['token'] );
78 if ( $error )
79 $this->dieUsageMsg( array( $error ) );
80
81
82 $form = new EmailUserForm( $targetUser, $params['text'], $params['subject'], $params['ccme'] );
83 $retval = $form->doSubmit();
84 if ( is_null( $retval ) )
85 $result = array( 'result' => 'Success' );
86 else
87 $result = array( 'result' => 'Failure',
88 'message' => $retval->getMessage() );
89
90 $this->getResult()->addValue( null, $this->getModuleName(), $result );
91 }
92
93 public function mustBePosted() { return true; }
94
95 public function getAllowedParams() {
96 return array (
97 'target' => null,
98 'subject' => null,
99 'text' => null,
100 'token' => null,
101 'ccme' => false,
102 'check' => null,
103 );
104 }
105
106 public function getParamDescription() {
107 return array (
108 'target' => 'User to send email to',
109 'subject' => 'Subject header',
110 'text' => 'Mail body',
111 'token' => 'A token previously acquired via prop=info',
112 'ccme' => 'Send a copy of this mail to me',
113 'check' => 'Check if the user has email enabled',
114 );
115 }
116
117 public function getDescription() {
118 return array(
119 'Email a user.'
120 );
121 }
122
123 protected function getExamples() {
124 return array (
125 'api.php?action=emailuser&target=WikiSysop&text=Content',
126 'api.php?action=emailuser&target=WikiSysop&check=yes',
127 );
128 }
129
130 public function getVersion() {
131 return __CLASS__ . ': $Id$';
132 }
133 }
134