1
|
|
|
<?php |
2
|
|
|
/****************************************************************************** |
3
|
|
|
* Wikipedia Account Creation Assistance tool * |
4
|
|
|
* * |
5
|
|
|
* All code in this file is released into the public domain by the ACC * |
6
|
|
|
* Development Team. Please see team.json for a list of contributors. * |
7
|
|
|
******************************************************************************/ |
8
|
|
|
|
9
|
|
|
namespace Waca\Pages\Request; |
10
|
|
|
|
11
|
|
|
use Exception; |
12
|
|
|
use Waca\DataObjects\Comment; |
13
|
|
|
use Waca\DataObjects\Request; |
14
|
|
|
use Waca\Exceptions\OptimisticLockFailedException; |
15
|
|
|
use Waca\Helpers\BanHelper; |
16
|
|
|
use Waca\SessionAlert; |
17
|
|
|
use Waca\Tasks\PublicInterfacePageBase; |
18
|
|
|
use Waca\Validation\RequestValidationHelper; |
19
|
|
|
use Waca\Validation\ValidationError; |
20
|
|
|
use Waca\WebRequest; |
21
|
|
|
|
22
|
|
|
class PageRequestAccount extends PublicInterfacePageBase |
23
|
|
|
{ |
24
|
|
|
/** @var RequestValidationHelper do not use directly. */ |
25
|
|
|
private $validationHelper; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Main function for this page, when no specific actions are called. |
29
|
|
|
* @return void |
30
|
|
|
* @throws OptimisticLockFailedException |
31
|
|
|
* @throws Exception |
32
|
|
|
*/ |
33
|
|
|
protected function main() |
34
|
|
|
{ |
35
|
|
|
// dual mode page |
36
|
|
|
if (WebRequest::wasPosted()) { |
37
|
|
|
$request = $this->createNewRequest(); |
38
|
|
|
$comment = $this->createComment(); |
39
|
|
|
|
40
|
|
|
$validationErrors = $this->validateRequest($request); |
41
|
|
|
|
42
|
|
|
if (count($validationErrors) > 0) { |
43
|
|
|
foreach ($validationErrors as $validationError) { |
44
|
|
|
SessionAlert::error($validationError->getErrorMessage()); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
// Preserve the data after an error |
48
|
|
|
WebRequest::setSessionContext('accountReq', |
49
|
|
|
array( |
50
|
|
|
'username' => WebRequest::postString('name'), |
51
|
|
|
'email' => WebRequest::postEmail('email'), |
52
|
|
|
'comments' => WebRequest::postString('comments'), |
53
|
|
|
) |
54
|
|
|
); |
55
|
|
|
|
56
|
|
|
// Validation error, bomb out early. |
57
|
|
|
$this->redirect(); |
58
|
|
|
|
59
|
|
|
return; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
// actually save the request to the database |
63
|
|
|
if ($this->getSiteConfiguration()->getEmailConfirmationEnabled()) { |
64
|
|
|
$this->saveAsEmailConfirmation($request, $comment); |
65
|
|
|
} |
66
|
|
|
else { |
67
|
|
|
$this->saveWithoutEmailConfirmation($request, $comment); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$this->getRequestValidationHelper()->postSaveValidations($request); |
71
|
|
|
} |
72
|
|
|
else { |
73
|
|
|
// set the form values from the session context |
74
|
|
|
$context = WebRequest::getSessionContext('accountReq'); |
75
|
|
|
if ($context !== null && is_array($context)) { |
76
|
|
|
$this->assign('username', $context['username']); |
77
|
|
|
$this->assign('email', $context['email']); |
78
|
|
|
$this->assign('comments', $context['comments']); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
// Clear it for a refresh |
82
|
|
|
WebRequest::setSessionContext('accountReq', null); |
83
|
|
|
|
84
|
|
|
$this->setTemplate('request/request-form.tpl'); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return Request |
90
|
|
|
*/ |
91
|
|
|
protected function createNewRequest() |
92
|
|
|
{ |
93
|
|
|
$request = new Request(); |
94
|
|
|
$request->setDatabase($this->getDatabase()); |
95
|
|
|
|
96
|
|
|
$request->setName(WebRequest::postString('name')); |
97
|
|
|
$request->setEmail(WebRequest::postEmail('email')); |
98
|
|
|
|
99
|
|
|
$request->setIp(WebRequest::remoteAddress()); |
100
|
|
|
$request->setForwardedIp(WebRequest::forwardedAddress()); |
101
|
|
|
|
102
|
|
|
$request->setUserAgent(WebRequest::userAgent()); |
103
|
|
|
|
104
|
|
|
return $request; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return Comment|null |
109
|
|
|
*/ |
110
|
|
|
private function createComment() |
111
|
|
|
{ |
112
|
|
|
$commentText = WebRequest::postString('comments'); |
113
|
|
|
if ($commentText === null || trim($commentText) === '') { |
114
|
|
|
return null; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$comment = new Comment(); |
118
|
|
|
$comment->setDatabase($this->getDatabase()); |
119
|
|
|
|
120
|
|
|
$comment->setVisibility('requester'); |
121
|
|
|
$comment->setUser(null); |
122
|
|
|
$comment->setComment($commentText); |
123
|
|
|
|
124
|
|
|
return $comment; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param Request $request |
129
|
|
|
* |
130
|
|
|
* @return ValidationError[] |
131
|
|
|
*/ |
132
|
|
|
protected function validateRequest($request) |
133
|
|
|
{ |
134
|
|
|
$validationHelper = $this->getRequestValidationHelper(); |
135
|
|
|
|
136
|
|
|
// These are arrays of ValidationError. |
137
|
|
|
$nameValidation = $validationHelper->validateName($request); |
138
|
|
|
$emailValidation = $validationHelper->validateEmail($request, WebRequest::postEmail('emailconfirm')); |
139
|
|
|
$otherValidation = $validationHelper->validateOther($request); |
140
|
|
|
|
141
|
|
|
$validationErrors = array_merge($nameValidation, $emailValidation, $otherValidation); |
142
|
|
|
|
143
|
|
|
return $validationErrors; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param Request $request |
148
|
|
|
* |
149
|
|
|
* @param Comment|null $comment |
150
|
|
|
* |
151
|
|
|
* @throws OptimisticLockFailedException |
152
|
|
|
* @throws Exception |
153
|
|
|
*/ |
154
|
|
|
protected function saveAsEmailConfirmation(Request $request, $comment) |
155
|
|
|
{ |
156
|
|
|
$request->generateEmailConfirmationHash(); |
157
|
|
|
$request->save(); |
158
|
|
|
|
159
|
|
|
if ($comment !== null) { |
160
|
|
|
$comment->setRequest($request->getId()); |
161
|
|
|
$comment->save(); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
$trustedIp = $this->getXffTrustProvider()->getTrustedClientIp( |
165
|
|
|
$request->getIp(), |
166
|
|
|
$request->getForwardedIp()); |
167
|
|
|
|
168
|
|
|
$this->assign("ip", $trustedIp); |
169
|
|
|
$this->assign("id", $request->getId()); |
170
|
|
|
$this->assign("hash", $request->getEmailConfirm()); |
171
|
|
|
|
172
|
|
|
// Sends the confirmation email to the user. |
173
|
|
|
$this->getEmailHelper()->sendMail( |
174
|
|
|
$request->getEmail(), |
175
|
|
|
"[ACC #{$request->getId()}] English Wikipedia Account Request", |
176
|
|
|
$this->fetchTemplate('request/confirmation-mail.tpl')); |
177
|
|
|
|
178
|
|
|
$this->redirect('emailConfirmationRequired'); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @param Request $request |
183
|
|
|
* |
184
|
|
|
* @param Comment|null $comment |
185
|
|
|
* |
186
|
|
|
* @throws OptimisticLockFailedException |
187
|
|
|
* @throws Exception |
188
|
|
|
*/ |
189
|
|
|
protected function saveWithoutEmailConfirmation(Request $request, $comment) |
190
|
|
|
{ |
191
|
|
|
$request->setEmailConfirm(0); // fixme Since it can't be null |
192
|
|
|
$request->save(); |
193
|
|
|
|
194
|
|
|
if ($comment !== null) { |
195
|
|
|
$comment->setRequest($request->getId()); |
196
|
|
|
$comment->save(); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
$this->getNotificationHelper()->requestReceived($request); |
200
|
|
|
|
201
|
|
|
$this->redirect('requestSubmitted'); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @return RequestValidationHelper |
206
|
|
|
*/ |
207
|
|
|
protected function getRequestValidationHelper(): RequestValidationHelper |
208
|
|
|
{ |
209
|
|
|
$banHelper = new BanHelper($this->getDatabase(), $this->getXffTrustProvider(), null); |
210
|
|
|
|
211
|
|
|
if($this->validationHelper === null) { |
212
|
|
|
$this->validationHelper = new RequestValidationHelper( |
213
|
|
|
$banHelper, |
214
|
|
|
$this->getDatabase(), |
215
|
|
|
$this->getAntiSpoofProvider(), |
216
|
|
|
$this->getXffTrustProvider(), |
217
|
|
|
$this->getHttpHelper(), |
218
|
|
|
$this->getTorExitProvider(), |
219
|
|
|
$this->getSiteConfiguration()); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
return $this->validationHelper; |
223
|
|
|
} |
224
|
|
|
} |