|
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\RequestAction; |
|
10
|
|
|
|
|
11
|
|
|
use Exception; |
|
12
|
|
|
use Waca\DataObjects\EmailTemplate; |
|
13
|
|
|
use Waca\DataObjects\Request; |
|
14
|
|
|
use Waca\DataObjects\User; |
|
15
|
|
|
use Waca\Exceptions\ApplicationLogicException; |
|
16
|
|
|
use Waca\Helpers\Logger; |
|
17
|
|
|
use Waca\Helpers\RequestEmailHelper; |
|
18
|
|
|
use Waca\PdoDatabase; |
|
19
|
|
|
use Waca\SessionAlert; |
|
20
|
|
|
use Waca\WebRequest; |
|
21
|
|
|
|
|
22
|
|
|
class PageCloseRequest extends RequestActionBase |
|
23
|
|
|
{ |
|
24
|
|
|
protected function main() |
|
25
|
|
|
{ |
|
26
|
|
|
$this->processClose(); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Main function for this page, when no specific actions are called. |
|
31
|
|
|
* @throws ApplicationLogicException |
|
32
|
|
|
*/ |
|
33
|
|
|
final protected function processClose() |
|
34
|
|
|
{ |
|
35
|
|
|
$this->checkPosted(); |
|
36
|
|
|
$database = $this->getDatabase(); |
|
37
|
|
|
|
|
38
|
|
|
$currentUser = User::getCurrent($database); |
|
39
|
|
|
$template = $this->getTemplate($database); |
|
40
|
|
|
$request = $this->getRequest($database); |
|
41
|
|
|
$request->setUpdateVersion(WebRequest::postInt('updateversion')); |
|
42
|
|
|
|
|
43
|
|
|
if ($request->getStatus() === 'Closed') { |
|
44
|
|
|
throw new ApplicationLogicException('Request is already closed'); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
if ($this->confirmEmailAlreadySent($request, $template)) { |
|
48
|
|
|
return; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
if ($this->checkReserveProtect($request, $currentUser)) { |
|
52
|
|
|
return; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
if ($this->confirmAccountCreated($request, $template)) { |
|
56
|
|
|
return; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
// I think we're good here... |
|
60
|
|
|
$request->setStatus('Closed'); |
|
61
|
|
|
$request->setReserved(null); |
|
62
|
|
|
|
|
63
|
|
|
Logger::closeRequest($database, $request, $template->getId(), null); |
|
64
|
|
|
|
|
65
|
|
|
$request->save(); |
|
66
|
|
|
|
|
67
|
|
|
$this->processWelcome($template->getDefaultAction()); |
|
68
|
|
|
|
|
69
|
|
|
// Perform the notifications and stuff *after* we've successfully saved, since the save can throw an OLE and |
|
70
|
|
|
// be rolled back. |
|
71
|
|
|
|
|
72
|
|
|
$this->getNotificationHelper()->requestClosed($request, $template->getName()); |
|
73
|
|
|
$sanitisedTemplateName = htmlentities($template->getName(), ENT_COMPAT, 'UTF-8'); |
|
74
|
|
|
SessionAlert::success("Request {$request->getId()} has been closed as {$sanitisedTemplateName}"); |
|
75
|
|
|
|
|
76
|
|
|
$this->sendMail($request, $template->getText(), $currentUser, false); |
|
77
|
|
|
|
|
78
|
|
|
$this->redirect(); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param PdoDatabase $database |
|
83
|
|
|
* |
|
84
|
|
|
* @return EmailTemplate |
|
85
|
|
|
* @throws ApplicationLogicException |
|
86
|
|
|
*/ |
|
87
|
|
|
protected function getTemplate(PdoDatabase $database) |
|
88
|
|
|
{ |
|
89
|
|
|
$templateId = WebRequest::postInt('template'); |
|
90
|
|
|
if ($templateId === null) { |
|
91
|
|
|
throw new ApplicationLogicException('No template specified'); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** @var EmailTemplate $template */ |
|
95
|
|
|
$template = EmailTemplate::getById($templateId, $database); |
|
96
|
|
|
if ($template === false || !$template->getActive()) { |
|
97
|
|
|
throw new ApplicationLogicException('Invalid or inactive template specified'); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
return $template; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @param Request $request |
|
105
|
|
|
* @param EmailTemplate $template |
|
106
|
|
|
* |
|
107
|
|
|
* @return bool |
|
108
|
|
|
*/ |
|
109
|
|
|
protected function confirmEmailAlreadySent(Request $request, EmailTemplate $template) |
|
110
|
|
|
{ |
|
111
|
|
|
if ($this->checkEmailAlreadySent($request)) { |
|
112
|
|
|
$this->showConfirmation($request, $template, 'close-confirmations/email-sent.tpl'); |
|
113
|
|
|
|
|
114
|
|
|
return true; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
return false; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
protected function checkEmailAlreadySent(Request $request) |
|
121
|
|
|
{ |
|
122
|
|
|
if ($request->getEmailSent() && !WebRequest::postBoolean('emailSentOverride')) { |
|
123
|
|
|
return true; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
return false; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
protected function checkReserveProtect(Request $request, User $currentUser) |
|
130
|
|
|
{ |
|
131
|
|
|
$reservationId = $request->getReserved(); |
|
132
|
|
|
|
|
133
|
|
|
if ($reservationId !== 0 && $reservationId !== null) { |
|
134
|
|
|
if ($currentUser->getId() !== $reservationId) { |
|
135
|
|
|
SessionAlert::error("Request is reserved by someone else."); |
|
136
|
|
|
$this->redirect('/viewRequest', null, ['id' => $request->getId()] ); |
|
137
|
|
|
return true; |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
return false; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* @param Request $request |
|
146
|
|
|
* @param EmailTemplate $template |
|
147
|
|
|
* |
|
148
|
|
|
* @return bool |
|
149
|
|
|
* @throws \Waca\Exceptions\CurlException |
|
150
|
|
|
*/ |
|
151
|
|
|
protected function confirmAccountCreated(Request $request, EmailTemplate $template) |
|
152
|
|
|
{ |
|
153
|
|
|
if ($this->checkAccountCreated($request, $template)) { |
|
154
|
|
|
$this->showConfirmation($request, $template, 'close-confirmations/account-created.tpl'); |
|
155
|
|
|
|
|
156
|
|
|
return true; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
return false; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
protected function checkAccountCreated(Request $request, EmailTemplate $template) |
|
163
|
|
|
{ |
|
164
|
|
|
if ($template->getDefaultAction() === EmailTemplate::CREATED && !WebRequest::postBoolean('createOverride')) { |
|
165
|
|
|
$parameters = array( |
|
166
|
|
|
'action' => 'query', |
|
167
|
|
|
'list' => 'users', |
|
168
|
|
|
'format' => 'php', |
|
169
|
|
|
'ususers' => $request->getName(), |
|
170
|
|
|
); |
|
171
|
|
|
|
|
172
|
|
|
$content = $this->getHttpHelper()->get($this->getSiteConfiguration()->getMediawikiWebServiceEndpoint(), |
|
173
|
|
|
$parameters); |
|
174
|
|
|
|
|
175
|
|
|
$apiResult = unserialize($content); |
|
176
|
|
|
$exists = !isset($apiResult['query']['users']['0']['missing']); |
|
177
|
|
|
|
|
178
|
|
|
if (!$exists) { |
|
179
|
|
|
return true; |
|
180
|
|
|
} |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
return false; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* @param Request $request |
|
188
|
|
|
* @param string $mailText |
|
189
|
|
|
* @param User $currentUser |
|
190
|
|
|
* @param boolean $ccMailingList |
|
191
|
|
|
*/ |
|
192
|
|
|
protected function sendMail(Request $request, $mailText, User $currentUser, $ccMailingList) |
|
193
|
|
|
{ |
|
194
|
|
|
$requestEmailHelper = new RequestEmailHelper($this->getEmailHelper()); |
|
195
|
|
|
$requestEmailHelper->sendMail($request, $mailText, $currentUser, $ccMailingList); |
|
196
|
|
|
|
|
197
|
|
|
$request->setEmailSent(true); |
|
198
|
|
|
$request->save(); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* @param Request $request |
|
203
|
|
|
* @param EmailTemplate $template |
|
204
|
|
|
* @param string $templateName |
|
205
|
|
|
* |
|
206
|
|
|
* @throws Exception |
|
207
|
|
|
* @return void |
|
208
|
|
|
*/ |
|
209
|
|
|
protected function showConfirmation(Request $request, EmailTemplate $template, $templateName) |
|
210
|
|
|
{ |
|
211
|
|
|
$this->assignCSRFToken(); |
|
212
|
|
|
|
|
213
|
|
|
$this->assign('request', $request->getId()); |
|
214
|
|
|
$this->assign('template', $template->getId()); |
|
215
|
|
|
|
|
216
|
|
|
$this->assign('updateversion', $request->getUpdateVersion()); |
|
217
|
|
|
|
|
218
|
|
|
$this->assign('emailSentOverride', WebRequest::postBoolean('emailSentOverride') ? 'true' : 'false'); |
|
219
|
|
|
$this->assign('reserveOverride', WebRequest::postBoolean('reserveOverride') ? 'true' : 'false'); |
|
220
|
|
|
$this->assign('createOverride', WebRequest::postBoolean('createOverride') ? 'true' : 'false'); |
|
221
|
|
|
|
|
222
|
|
|
$this->setTemplate($templateName); |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
/** |
|
226
|
|
|
* @param string $action |
|
227
|
|
|
* |
|
228
|
|
|
* @throws ApplicationLogicException |
|
229
|
|
|
*/ |
|
230
|
|
|
final protected function processWelcome(string $action): void |
|
231
|
|
|
{ |
|
232
|
|
|
$database = $this->getDatabase(); |
|
233
|
|
|
$currentUser = User::getCurrent($database); |
|
234
|
|
|
|
|
235
|
|
|
if ($action !== EmailTemplate::CREATED) { |
|
236
|
|
|
return; |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
if ($currentUser->getWelcomeTemplate() === null) { |
|
|
|
|
|
|
240
|
|
|
return; |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
if (WebRequest::postBoolean('skipAutoWelcome')) { |
|
244
|
|
|
return; |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
$this->enqueueWelcomeTask($this->getRequest($database), null, $currentUser, $database); |
|
248
|
|
|
} |
|
249
|
|
|
} |
|
250
|
|
|
|