Issues (186)

includes/Pages/PageRequestFormManagement.php (6 issues)

Labels
Severity
1
<?php
2
/******************************************************************************
3
 * Wikipedia Account Creation Assistance tool                                 *
4
 * ACC Development Team. Please see team.json for a list of contributors.     *
5
 *                                                                            *
6
 * This is free and unencumbered software released into the public domain.    *
7
 * Please see LICENSE.md for the full licencing statement.                    *
8
 ******************************************************************************/
9
10
namespace Waca\Pages;
11
12
use Waca\DataObjects\Domain;
13
use Waca\DataObjects\RequestForm;
14
use Waca\DataObjects\RequestQueue;
15
use Waca\DataObjects\User;
16
use Waca\Exceptions\AccessDeniedException;
17
use Waca\Exceptions\ApplicationLogicException;
18
use Waca\Helpers\Logger;
19
use Waca\Helpers\MarkdownRenderingHelper;
20
use Waca\SessionAlert;
21
use Waca\Tasks\InternalPageBase;
22
use Waca\WebRequest;
23
24
class PageRequestFormManagement extends InternalPageBase
25
{
26
    protected function main()
27
    {
28
        $this->setHtmlTitle('Request Form Management');
29
30
        $database = $this->getDatabase();
31
        $domainId = Domain::getCurrent($database)->getId();
32
        $forms = RequestForm::getAllForms($database, $domainId);
33
        $this->assign('forms', $forms);
34
35
        $queues = [];
36
        foreach ($forms as $f) {
37
            $queueId = $f->getOverrideQueue();
38
            if ($queueId !== null) {
39
                if (!isset($queues[$queueId])) {
40
                    /** @var RequestQueue $queue */
41
                    $queue = RequestQueue::getById($queueId, $this->getDatabase());
42
43
                    if ($queue->getDomain() == $domainId) {
44
                        $queues[$queueId] = $queue;
45
                    }
46
                }
47
            }
48
        }
49
50
        $this->assign('queues', $queues);
51
52
        $user = User::getCurrent($database);
53
        $this->assign('canCreate', $this->barrierTest('create', $user));
54
        $this->assign('canEdit', $this->barrierTest('edit', $user));
55
        $this->assign('canView', $this->barrierTest('view', $user));
56
57
        $this->setTemplate('form-management/main.tpl');
58
    }
59
60
    protected function preview() {
61
        $previewContent = WebRequest::getSessionContext('preview');
62
63
        $renderer = new MarkdownRenderingHelper();
64
        $this->assign('renderedContent', $renderer->doRender($previewContent['main']));
65
        $this->assign('username', $renderer->doRenderInline($previewContent['username']));
66
        $this->assign('email', $renderer->doRenderInline($previewContent['email']));
67
        $this->assign('comment', $renderer->doRenderInline($previewContent['comment']));
68
69
        $this->setTemplate('form-management/preview.tpl');
70
    }
71
72
    protected function create()
73
    {
74
        if (WebRequest::wasPosted()) {
75
            $this->validateCSRFToken();
76
            $database = $this->getDatabase();
77
            $domainId = Domain::getCurrent($database)->getId();
78
79
            $form = new RequestForm();
80
81
            $form->setDatabase($database);
82
            $form->setDomain($domainId);
83
84
            $this->setupObjectFromPost($form);
85
            $form->setPublicEndpoint(WebRequest::postString('endpoint'));
0 ignored issues
show
It seems like Waca\WebRequest::postString('endpoint') can also be of type null; however, parameter $publicEndpoint of Waca\DataObjects\RequestForm::setPublicEndpoint() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

85
            $form->setPublicEndpoint(/** @scrutinizer ignore-type */ WebRequest::postString('endpoint'));
Loading history...
86
87
            if (WebRequest::postString("preview") === "preview") {
88
                $this->populateFromObject($form);
89
90
                WebRequest::setSessionContext('preview', [
91
                    'main' => $form->getFormContent(),
92
                    'username' => $form->getUsernameHelp(),
93
                    'email' => $form->getEmailHelp(),
94
                    'comment' => $form->getCommentHelp(),
95
                ]);
96
97
                $this->assign('createMode', true);
98
                $this->setTemplate('form-management/edit.tpl');
99
100
                return;
101
            }
102
103
            $proceed = true;
104
105
            if (RequestForm::getByPublicEndpoint($database, $form->getPublicEndpoint(), $domainId) !== false) {
106
                SessionAlert::error("The chosen public endpoint is already in use. Please choose another.");
107
                $proceed = false;
108
            }
109
110
            if (preg_match('/^[A-Za-z][a-zA-Z0-9-]*$/', $form->getPublicEndpoint()) !== 1) {
111
                SessionAlert::error("The chosen public endpoint contains invalid characters");
112
                $proceed = false;
113
            }
114
115
            if (RequestForm::getByName($database, $form->getName(), $domainId) !== false) {
116
                SessionAlert::error("The chosen name is already in use. Please choose another.");
117
                $proceed = false;
118
            }
119
120
            if ($form->getOverrideQueue() !== null) {
121
                /** @var RequestQueue|bool $queue */
122
                $queue = RequestQueue::getById($form->getOverrideQueue(), $database);
123
                if ($queue === false || $queue->getDomain() !== $domainId || !$queue->isEnabled()) {
124
                    SessionAlert::error("The chosen queue does not exist or is disabled.");
125
                    $proceed = false;
126
                }
127
            }
128
129
            if ($proceed) {
130
                $form->save();
131
                Logger::requestFormCreated($database, $form);
132
                $this->redirect('requestFormManagement');
133
            }
134
            else {
135
                $this->populateFromObject($form);
136
                WebRequest::setSessionContext('preview', [
137
                    'main' => $form->getFormContent(),
138
                    'username' => $form->getUsernameHelp(),
139
                    'email' => $form->getEmailHelp(),
140
                    'comment' => $form->getCommentHelp(),
141
                ]);
142
143
                $this->assign('createMode', true);
144
                $this->setTemplate('form-management/edit.tpl');
145
            }
146
        }
147
        else {
148
            $this->populateFromObject(new RequestForm());
149
            WebRequest::setSessionContext('preview', null);
150
            $this->assign('hidePreview', true);
151
152
            $this->assignCSRFToken();
153
            $this->assign('createMode', true);
154
            $this->setTemplate('form-management/edit.tpl');
155
        }
156
    }
157
158
    protected function view()
159
    {
160
        $database = $this->getDatabase();
161
162
        /** @var RequestForm $form */
163
        $form = RequestForm::getById(WebRequest::getInt('form'), $database);
164
165
        if ($form->getDomain() !== Domain::getCurrent($database)->getId()) {
166
            throw new AccessDeniedException($this->getSecurityManager(), $this->getDomainAccessManager());
167
        }
168
169
        $this->populateFromObject($form);
170
171
        if ($form->getOverrideQueue() !== null) {
172
            $this->assign('queueObject', RequestQueue::getById($form->getOverrideQueue(), $database));
173
        }
174
175
        WebRequest::setSessionContext('preview', [
176
            'main' => $form->getFormContent(),
177
            'username' => $form->getUsernameHelp(),
178
            'email' => $form->getEmailHelp(),
179
            'comment' => $form->getCommentHelp(),
180
        ]);
181
182
        $renderer = new MarkdownRenderingHelper();
183
        $this->assign('renderedContent', $renderer->doRender($form->getFormContent()));
184
185
        $this->setTemplate('form-management/view.tpl');
186
    }
187
188
    protected function edit()
189
    {
190
        $database = $this->getDatabase();
191
192
        /** @var RequestForm $form */
193
        $form = RequestForm::getById(WebRequest::getInt('form'), $database);
194
195
        if ($form->getDomain() !== Domain::getCurrent($database)->getId()) {
196
            throw new AccessDeniedException($this->getSecurityManager(), $this->getDomainAccessManager());
197
        }
198
199
        if (WebRequest::wasPosted()) {
200
            $this->validateCSRFToken();
201
202
            $this->setupObjectFromPost($form);
203
204
            if (WebRequest::postString("preview") === "preview") {
205
                $this->populateFromObject($form);
206
207
                WebRequest::setSessionContext('preview', [
208
                    'main' => $form->getFormContent(),
209
                    'username' => $form->getUsernameHelp(),
210
                    'email' => $form->getEmailHelp(),
211
                    'comment' => $form->getCommentHelp(),
212
                ]);
213
214
                $this->assign('createMode', false);
215
                $this->setTemplate('form-management/edit.tpl');
216
217
                return;
218
            }
219
220
            $proceed = true;
221
222
            $foundForm = RequestForm::getByName($database, $form->getName(), $form->getDomain());
223
            if ($foundForm !== false && $foundForm->getId() !== $form->getId()) {
224
                SessionAlert::error("The chosen name is already in use. Please choose another.");
225
                $proceed = false;
226
            }
227
228
            if ($form->getOverrideQueue() !== null) {
229
                /** @var RequestQueue $queue */
230
                $queue = RequestQueue::getById($form->getOverrideQueue(), $database);
231
                if ($queue === false || $queue->getDomain() !== $form->getDomain() || !$queue->isEnabled()) {
232
                    SessionAlert::error("The chosen queue does not exist or is disabled.");
233
                    $proceed = false;
234
                }
235
            }
236
237
            if ($proceed) {
238
                Logger::requestFormEdited($database, $form);
239
                $form->save();
240
                $this->redirect('requestFormManagement');
241
            }
242
            else {
243
                $this->populateFromObject($form);
244
                WebRequest::setSessionContext('preview', [
245
                    'main' => $form->getFormContent(),
246
                    'username' => $form->getUsernameHelp(),
247
                    'email' => $form->getEmailHelp(),
248
                    'comment' => $form->getCommentHelp(),
249
                ]);
250
251
                $this->assign('createMode', false);
252
                $this->setTemplate('form-management/edit.tpl');
253
            }
254
        }
255
        else {
256
            $this->populateFromObject($form);
257
            WebRequest::setSessionContext('preview', [
258
                'main' => $form->getFormContent(),
259
                'username' => $form->getUsernameHelp(),
260
                'email' => $form->getEmailHelp(),
261
                'comment' => $form->getCommentHelp(),
262
            ]);
263
264
            $this->assign('createMode', false);
265
            $this->setTemplate('form-management/edit.tpl');
266
        }
267
    }
268
269
    /**
270
     * @param RequestForm $form
271
     */
272
    protected function populateFromObject(RequestForm $form): void
273
    {
274
        $this->assignCSRFToken();
275
276
        $this->assign('name', $form->getName());
277
        $this->assign('enabled', $form->isEnabled());
278
        $this->assign('endpoint', $form->getPublicEndpoint());
279
        $this->assign('queue', $form->getOverrideQueue());
280
        $this->assign('content', $form->getFormContent());
281
        $this->assign('username', $form->getUsernameHelp());
282
        $this->assign('email', $form->getEmailHelp());
283
        $this->assign('comment', $form->getCommentHelp());
284
285
        $this->assign('domain', $form->getDomainObject());
286
287
        $this->assign('availableQueues', RequestQueue::getEnabledQueues($this->getDatabase()));
288
    }
289
290
    /**
291
     * @param RequestForm $form
292
     *
293
     * @return void
294
     * @throws ApplicationLogicException
295
     */
296
    protected function setupObjectFromPost(RequestForm $form): void
297
    {
298
        if (WebRequest::postString('content') === null
299
            || WebRequest::postString('username') === null
300
            || WebRequest::postString('email') === null
301
            || WebRequest::postString('comment') === null
302
        ) {
303
            throw new ApplicationLogicException("Form content, username help, email help, and comment help are all required fields.");
304
        }
305
306
        $form->setName(WebRequest::postString('name'));
0 ignored issues
show
It seems like Waca\WebRequest::postString('name') can also be of type null; however, parameter $name of Waca\DataObjects\RequestForm::setName() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

306
        $form->setName(/** @scrutinizer ignore-type */ WebRequest::postString('name'));
Loading history...
307
        $form->setEnabled(WebRequest::postBoolean('enabled'));
308
        $form->setFormContent(WebRequest::postString('content'));
0 ignored issues
show
It seems like Waca\WebRequest::postString('content') can also be of type null; however, parameter $formContent of Waca\DataObjects\RequestForm::setFormContent() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

308
        $form->setFormContent(/** @scrutinizer ignore-type */ WebRequest::postString('content'));
Loading history...
309
        $form->setOverrideQueue(WebRequest::postInt('queue'));
310
        $form->setUsernameHelp(WebRequest::postString('username'));
0 ignored issues
show
It seems like Waca\WebRequest::postString('username') can also be of type null; however, parameter $usernamehelp of Waca\DataObjects\RequestForm::setUsernameHelp() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

310
        $form->setUsernameHelp(/** @scrutinizer ignore-type */ WebRequest::postString('username'));
Loading history...
311
        $form->setEmailHelp(WebRequest::postString('email'));
0 ignored issues
show
It seems like Waca\WebRequest::postString('email') can also be of type null; however, parameter $emailhelp of Waca\DataObjects\RequestForm::setEmailHelp() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

311
        $form->setEmailHelp(/** @scrutinizer ignore-type */ WebRequest::postString('email'));
Loading history...
312
        $form->setCommentHelp(WebRequest::postString('comment'));
0 ignored issues
show
It seems like Waca\WebRequest::postString('comment') can also be of type null; however, parameter $commenthelp of Waca\DataObjects\RequestForm::setCommentHelp() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

312
        $form->setCommentHelp(/** @scrutinizer ignore-type */ WebRequest::postString('comment'));
Loading history...
313
    }
314
}
315