Completed
Branch master (939199)
by
unknown
39:35
created

includes/api/ApiEmailUser.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 *
4
 *
5
 * Created on June 1, 2008
6
 *
7
 * Copyright © 2008 Bryan Tong Minh <[email protected]>
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
/**
28
 * API Module to facilitate sending of emails to users
29
 * @ingroup API
30
 */
31
class ApiEmailUser extends ApiBase {
32
33
	public function execute() {
34
		$params = $this->extractRequestParams();
35
36
		// Validate target
37
		$targetUser = SpecialEmailUser::getTarget( $params['target'] );
38
		if ( !( $targetUser instanceof User ) ) {
39
			$this->dieUsageMsg( [ $targetUser ] );
40
		}
41
42
		// Check permissions and errors
43
		$error = SpecialEmailUser::getPermissionsError(
44
			$this->getUser(),
45
			$params['token'],
46
			$this->getConfig()
47
		);
48
		if ( $error ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $error of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
49
			$this->dieUsageMsg( [ $error ] );
50
		}
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
60
		if ( $retval instanceof Status ) {
61
			// SpecialEmailUser sometimes returns a status
62
			// sometimes it doesn't.
63
			if ( $retval->isGood() ) {
64
				$retval = true;
65
			} else {
66
				$retval = $retval->getErrorsArray();
67
			}
68
		}
69
70
		if ( $retval === true ) {
71
			$result = [ 'result' => 'Success' ];
72
		} else {
73
			$result = [
74
				'result' => 'Failure',
75
				'message' => $retval
76
			];
77
		}
78
79
		$this->getResult()->addValue( null, $this->getModuleName(), $result );
80
	}
81
82
	public function mustBePosted() {
83
		return true;
84
	}
85
86
	public function isWriteMode() {
87
		return true;
88
	}
89
90 View Code Duplication
	public function getAllowedParams() {
91
		return [
92
			'target' => [
93
				ApiBase::PARAM_TYPE => 'string',
94
				ApiBase::PARAM_REQUIRED => true
95
			],
96
			'subject' => null,
97
			'text' => [
98
				ApiBase::PARAM_TYPE => 'text',
99
				ApiBase::PARAM_REQUIRED => true
100
			],
101
			'ccme' => false,
102
		];
103
	}
104
105
	public function needsToken() {
106
		return 'csrf';
107
	}
108
109
	protected function getExamplesMessages() {
110
		return [
111
			'action=emailuser&target=WikiSysop&text=Content&token=123ABC'
112
				=> 'apihelp-emailuser-example-email',
113
		];
114
	}
115
116
	public function getHelpUrls() {
117
		return 'https://www.mediawiki.org/wiki/API:Email';
118
	}
119
}
120