Completed
Pull Request — master (#526)
by Michael
16:45 queued 06:57
created

RequestValidationHelper::validateName()   C

Complexity

Conditions 9
Paths 256

Size

Total Lines 53
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 14.184

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 19
nc 256
nop 0
dl 0
loc 53
ccs 12
cts 20
cp 0.6
crap 14.184
rs 6.5222
c 1
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * Performs the validation of an incoming request.
5
 */
6
class RequestValidationHelper
7
{
8
	private $banHelper;
9
	private $request;
10
	private $emailConfirmation;
11
12
	/**
13
	 * Summary of __construct
14
	 * @param IBanHelper $banHelper
15
	 * @param Request $request
16
	 * @param string $emailConfirmation
17
	 */
18 1
	public function __construct(IBanHelper $banHelper, Request $request, $emailConfirmation)
19
	{
20 1
		$this->banHelper = $banHelper;
21 1
		$this->request = $request;
22 1
		$this->emailConfirmation = $emailConfirmation;
23 1
	}
24
25
	/**
26
	 * Summary of validateName
27
	 * @return ValidationError[]
28
	 */
29 1
	public function validateName()
30
	{
31 1
		$errorList = array();
32
33
		// ERRORS
34
		// name is empty
35 1
		if (trim($this->request->getName()) == "") {
36
			$errorList[ValidationError::NAME_EMPTY] = new ValidationError(ValidationError::NAME_EMPTY);
37
		}
38
39
		// name is banned
40 1
		$ban = $this->banHelper->nameIsBanned($this->request->getName());
41 1
		if ($ban != false) {
0 ignored issues
show
introduced by
The condition $ban != false is always true.
Loading history...
42
			$errorList[ValidationError::BANNED] = new ValidationError(ValidationError::BANNED);
43
		}
44
45
		// username already exists
46
		// TODO: implement
47 1
		if ($this->userExists()) {
48
			$errorList[ValidationError::NAME_EXISTS] = new ValidationError(ValidationError::NAME_EXISTS);
49
		}
50
51
		// username part of SUL account
52
		// TODO: implement
53 1
		if ($this->userSulExists()) {
54
			// using same error slot as name exists - it's the same sort of error, and we probably only want to show one.
55
			$errorList[ValidationError::NAME_EXISTS] = new ValidationError(ValidationError::NAME_EXISTS_SUL);
56
		}
57
58
		// username is numbers
59 1
		if (preg_match("/^[0-9]+$/", $this->request->getName()) === 1) {
60
			$errorList[ValidationError::NAME_NUMONLY] = new ValidationError(ValidationError::NAME_NUMONLY);
61
		}
62
63
		// username can't contain #@/<>[]|{}
64 1
		if (preg_match("/[" . preg_quote("#@/<>[]|{}", "/") . "]/", $this->request->getName()) === 1) {
65
			$errorList[ValidationError::NAME_INVALIDCHAR] = new ValidationError(ValidationError::NAME_INVALIDCHAR);
66
		}
67
68
		// existing non-closed request for this name
69
		// TODO: implement
70 1
		if ($this->nameRequestExists()) {
71
			$errorList[ValidationError::OPEN_REQUEST_NAME] = new ValidationError(ValidationError::OPEN_REQUEST_NAME);
72
		}
73
74
		// WARNINGS
75
		// name has to be sanitised
76
		// TODO: implement
77 1
		if (false) {
78
			$errorList[ValidationError::NAME_SANITISED] = new ValidationError(ValidationError::NAME_SANITISED, false);
79
		}
80
81 1
		return $errorList;
82
	}
83
84
	/**
85
	 * Summary of validateEmail
86
	 * @return ValidationError[]
87
	 */
88
	public function validateEmail()
89
	{
90
		$errorList = array();
91
92
		// ERRORS
93
94
		// Email is banned
95
		$ban = $this->banHelper->emailIsBanned($this->request->getEmail());
96
		if ($ban != false) {
0 ignored issues
show
introduced by
The condition $ban != false is always true.
Loading history...
97
			$errorList[ValidationError::BANNED] = new ValidationError(ValidationError::BANNED);
98
		}
99
100
		// email addresses must match
101
		if ($this->request->getEmail() != $this->emailConfirmation) {
102
			$errorList[ValidationError::EMAIL_MISMATCH] = new ValidationError(ValidationError::EMAIL_MISMATCH);
103
		}
104
105
		// email address must be validly formed
106
		if (trim($this->request->getEmail()) == "") {
107
			$errorList[ValidationError::EMAIL_EMPTY] = new ValidationError(ValidationError::EMAIL_EMPTY);
108
		}
109
110
		// email address must be validly formed
111
		if (!filter_var($this->request->getEmail(), FILTER_VALIDATE_EMAIL)) {
112
			if (trim($this->request->getEmail()) != "") {
113
				$errorList[ValidationError::EMAIL_INVALID] = new ValidationError(ValidationError::EMAIL_INVALID);
114
			}
115
		}
116
117
		// email address can't be wikimedia/wikipedia .com/org
118
		if (preg_match('/.*@.*wiki(m.dia|p.dia)\.(org|com)/i', $this->request->getEmail()) === 1) {
119
			$errorList[ValidationError::EMAIL_WIKIMEDIA] = new ValidationError(ValidationError::EMAIL_WIKIMEDIA);
120
		}
121
122
		// WARNINGS
123
124
		return $errorList;
125
	}
126
127
	/**
128
	 * Summary of validateOther
129
	 * @return ValidationError[]
130
	 */
131
	public function validateOther()
132
	{
133
		$errorList = array();
134
135
		// ERRORS
136
137
		// TOR nodes
138
		// TODO: Implement
139
		if (false) {
140
			$errorList[ValidationError::BANNED] = new ValidationError(ValidationError::BANNED_TOR);
141
		}
142
143
		// IP banned
144
		$ban = $this->banHelper->ipIsBanned($this->request->getTrustedIp());
145
		if ($ban != false) {
0 ignored issues
show
introduced by
The condition $ban != false is always true.
Loading history...
146
			$errorList[ValidationError::BANNED] = new ValidationError(ValidationError::BANNED);
147
		}
148
149
		// WARNINGS
150
151
		// Antispoof check
152
		$this->checkAntiSpoof();
153
154
		// Blacklist check
155
		$this->checkTitleBlacklist();
156
157
		return $errorList;
158
	}
159
160
	private function checkAntiSpoof()
161
	{
162
		global $antispoofProvider;
163
		try {
164
			if (count($antispoofProvider->getSpoofs($this->request->getName())) > 0) {
165
				// If there were spoofs an Admin should handle the request.
166
				$this->request->setStatus("Flagged users");
167
			}
168
		}
169
		catch (Exception $ex) {
170
			// hrm.
171
			// TODO: log this?
172
		}
173
	}
174
175
	private function checkTitleBlacklist()
176
	{
177
		global $enableTitleblacklist;
178
		if ($enableTitleblacklist == 1) {
179
			$apiResult = file_get_contents("https://en.wikipedia.org/w/api.php?action=titleblacklist&tbtitle=" . urlencode($this->request->getName()) . "&tbaction=new-account&tbnooverride&format=php");
180
181
			$data = unserialize($apiResult);
182
183
			$requestIsOk = $data['titleblacklist']['result'] == "ok";
184
185
			if (!$requestIsOk) {
186
				$this->request->setStatus("Flagged users");
187
			}
188
		}
189
	}
190
191 1
	private function userExists()
192
	{
193 1
		global $mediawikiWebServiceEndpoint;
194
195 1
		$userexist = file_get_contents($mediawikiWebServiceEndpoint . "?action=query&list=users&ususers=" . urlencode($this->request->getName()) . "&format=php");
196 1
		$ue = unserialize($userexist);
197 1
		if (!isset ($ue['query']['users']['0']['missing']) && isset ($ue['query']['users']['0']['userid'])) {
198
			return true;
199
		}
200
201 1
		return false;
202
	}
203
204 1
	private function userSulExists()
205
	{
206 1
		global $mediawikiWebServiceEndpoint;
207
208 1
		$reqname = str_replace("_", " ", $this->request->getName());
209 1
		$userexist = file_get_contents($mediawikiWebServiceEndpoint . "?action=query&meta=globaluserinfo&guiuser=" . urlencode($reqname) . "&format=php");
210 1
		$ue = unserialize($userexist);
211 1
		if (isset ($ue['query']['globaluserinfo']['id'])) {
212
			return true;
213
		}
214
215 1
		return false;
216
	}
217
218 1
	private function nameRequestExists()
219
	{
220 1
		$query = "SELECT COUNT(id) FROM request WHERE status != 'Closed' AND name = :name;";
221 1
		$statement = gGetDb()->prepare($query);
222 1
		$statement->execute(array(':name' => $this->request->getName()));
223
224 1
		if (!$statement) {
0 ignored issues
show
introduced by
$statement is of type PDOStatement, thus it always evaluated to true.
Loading history...
225
			return false;
226
		}
227
228 1
		return $statement->fetchColumn() > 0;
229
	}
230
}
231