/
ModuleRecommendationForm.php
514 lines (433 loc) · 18.6 KB
/
ModuleRecommendationForm.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
<?php
/*
* This file is part of Oveleon Recommendation Bundle.
*
* (c) https://www.oveleon.de/
*/
namespace Oveleon\ContaoRecommendationBundle;
use Contao\BackendTemplate;
use Contao\CoreBundle\Exception\InternalServerErrorException;
use Contao\CoreBundle\OptIn\OptIn;
use Contao\Email;
use Contao\Environment;
use Contao\FrontendTemplate;
use Contao\Idna;
use Contao\Input;
use Contao\PageModel;
use Contao\StringUtil;
use Contao\System;
use Contao\Widget;
use Oveleon\ContaoRecommendationBundle\Model\RecommendationArchiveModel;
use Oveleon\ContaoRecommendationBundle\Model\RecommendationModel;
/**
* Front end module "recommendation form".
*
* @property integer $id
* @property string $headline
* @property string $name
* @property integer $recommendation_archive
* @property array $recommendation_optionalFormFields
* @property string $recommendation_customFieldLabel
* @property boolean $recommendation_notify
* @property boolean $recommendation_moderate
* @property boolean $recommendation_disableCaptcha
* @property string $recommendation_privacyText
* @property integer $jumpTo
* @property boolean $recommendation_activate
* @property string $recommendation_activateText
*/
class ModuleRecommendationForm extends ModuleRecommendation
{
/**
* Template
* @var string
*/
protected $strTemplate = 'mod_recommendationform';
/**
* Display a wildcard in the back end
*
* @return string
*/
public function generate(): string
{
$request = System::getContainer()->get('request_stack')->getCurrentRequest();
if ($request && System::getContainer()->get('contao.routing.scope_matcher')->isBackendRequest($request))
{
$objTemplate = new BackendTemplate('be_wildcard');
$objTemplate->wildcard = '### ' . $GLOBALS['TL_LANG']['FMD']['recommendationform'][0] . ' ###';
$objTemplate->title = $this->headline;
$objTemplate->id = $this->id;
$objTemplate->link = $this->name;
$objTemplate->href = StringUtil::specialcharsUrl(System::getContainer()->get('router')->generate('contao_backend', array('do'=>'themes', 'table'=>'tl_module', 'act'=>'edit', 'id'=>$this->id)));
return $objTemplate->parse();
}
$this->recommendation_archives = $this->sortOutProtected([$this->recommendation_archive]);
if (empty($this->recommendation_archives) || !\is_array($this->recommendation_archives))
{
throw new InternalServerErrorException('The recommendation form ID ' . $this->id . ' has no archive specified.');
}
return parent::generate();
}
/**
* Generate the module
*/
protected function compile(): void
{
System::loadLanguageFile('tl_recommendation');
System::loadLanguageFile('tl_recommendation_notification');
// Verify recommendation
if (strncmp(Input::get('token'), 'rec-', 4) === 0)
{
$this->verifyRecommendation();
return;
}
// Get archive record
$archive = RecommendationArchiveModel::findMultipleByIds($this->recommendation_archives);
$archive = $archive[0] ?? null;
// Form fields
$arrFields = [
'author' => [
'name' => 'author',
'label' => $GLOBALS['TL_LANG']['tl_recommendation']['author'],
'inputType' => 'text',
'eval' => ['mandatory'=>true, 'maxlength'=>128]
],
'rating' => [
'name' => 'rating',
'label' => $GLOBALS['TL_LANG']['tl_recommendation']['rating'],
'inputType' => 'select',
'options' => [5,4,3,2,1],
'eval' => ['mandatory'=>true]
],
'title' => [
'name' => 'title',
'label' => $GLOBALS['TL_LANG']['tl_recommendation']['title'],
'inputType' => 'text',
'eval' => ['optional'=>true, 'maxlength'=>255],
],
'customField' => [
'name' => 'customField',
'label' => $this->recommendation_customFieldLabel ?: $GLOBALS['TL_LANG']['tl_recommendation']['customFieldLabel'],
'inputType' => 'text',
'eval' => ['optional'=>true, 'maxlength'=>255],
],
'location' => [
'name' => 'location',
'label' => $GLOBALS['TL_LANG']['tl_recommendation']['location'],
'inputType' => 'text',
'eval' => ['optional'=>true, 'maxlength'=>128],
],
'text' => [
'name' => 'text',
'label' => $GLOBALS['TL_LANG']['tl_recommendation']['text'],
'inputType' => 'textarea',
'eval' => ['mandatory'=>true, 'rows'=>4, 'cols'=>40]
],
'email' => [
'name' => 'email',
'label' => $GLOBALS['TL_LANG']['tl_recommendation']['email'],
'inputType' => 'text',
'eval' => ['optional'=>true, 'maxlength'=>255, 'rgxp'=>'email', 'decodeEntities'=>true],
],
];
// Add scope for auto alias archives
if($archive && $archive->useAutoItem)
{
$arrFields['scope'] = [
'name' => 'scope',
'inputType' => 'hidden',
'value' => Input::get('auto_item', false, true)
];
}
// Captcha
if (!$this->recommendation_disableCaptcha)
{
$arrFields['captcha'] = [
'name' => 'captcha',
'label' => $GLOBALS['TL_LANG']['MSC']['securityQuestion'],
'inputType' => 'captcha',
'eval' => ['mandatory'=>true]
];
}
// Set e-mail as mandatory and non-optional if comments should be validated via activation mail
if ($this->recommendation_activate)
{
$arrFields['email']['eval']['optional'] = false;
$arrFields['email']['eval']['mandatory'] = true;
}
// Set an opt-in checkbox when privacy text is given
if ($this->recommendation_privacyText)
{
$arrFields['privacy'] = [
'name' => 'privacy',
'inputType' => 'checkbox',
'options' => [1=>$this->recommendation_privacyText],
'eval' => ['mandatory'=>true]
];
}
$doNotSubmit = false;
$arrWidgets = [];
$strFormId = 'recommendation_' . $this->id;
// Optional recommendation form fields
$arrOptionalFormFields = StringUtil::deserialize($this->recommendation_optionalFormFields, true);
// Initialize the widgets
foreach ($arrFields as $fieldName => $arrField)
{
// Check for optional form fields
if (($arrField['eval']['optional'] ?? null) && !\in_array($fieldName, $arrOptionalFormFields))
{
continue;
}
/** @var Widget $strClass */
$strClass = $GLOBALS['TL_FFL'][$arrField['inputType']];
// Continue if the class is not defined
if (!class_exists($strClass))
{
continue;
}
$arrField['eval']['required'] = $arrField['eval']['mandatory'] ?? null;
/** @var Widget $objWidget */
$objWidget = new $strClass($strClass::getAttributesFromDca($arrField, $arrField['name'], $arrField['value'] ?? null));
// Append the parent ID to prevent duplicate IDs (see #1493)
$objWidget->id .= '_' . $this->id;
// Validate the widget
if (Input::post('FORM_SUBMIT') == $strFormId)
{
$objWidget->validate();
if ($objWidget->hasErrors())
{
$doNotSubmit = true;
}
}
$arrWidgets[$arrField['name']] = $objWidget;
}
$this->Template->fields = $arrWidgets;
$this->Template->submit = $GLOBALS['TL_LANG']['tl_recommendation']['recommendation_submit'];
$this->Template->formId = $strFormId;
$this->Template->hasError = $doNotSubmit;
$this->Template->requestToken = System::getContainer()->get('contao.csrf.token_manager')->getDefaultTokenValue();
$objSession = System::getContainer()->get('request_stack')->getSession();
// Do not index or cache the page with the confirmation message
if ($objSession->isStarted())
{
$flashBag = $objSession->getFlashBag();
if ($flashBag->has('recommendation_added'))
{
$this->Template->confirm = $flashBag->get('recommendation_added')[0];
}
}
// Store the recommendation
if (!$doNotSubmit && Input::post('FORM_SUBMIT') == $strFormId)
{
$time = time();
// Do not parse any tags in the recommendation
$strText = StringUtil::specialchars(trim($arrWidgets['text']->value));
$strText = str_replace(['&', '<', '>'], ['[&]', '[lt]', '[gt]'], $strText);
// Remove multiple line feeds
$strText = preg_replace('@\n\n+@', "\n\n", $strText);
// Prevent cross-site request forgeries
$strText = preg_replace('/(href|src|on[a-z]+)="[^"]*(contao\/main\.php|typolight\/main\.php|javascript|vbscri?pt|script|alert|document|cookie|window)[^"]*"+/i', '$1="#"', $strText);
// Prepare the record
$arrData = [
'tstamp' => $time,
'pid' => $this->recommendation_archive,
'title' => $arrWidgets['title']->value ?? '',
'alias' => isset($arrWidgets['title']) ? StringUtil::generateAlias($arrWidgets['title']->value) : '',
'author' => $arrWidgets['author']->value,
'email' => $arrWidgets['email']->value ?? '',
'location' => $arrWidgets['location']->value ?? '',
'customField' => $arrWidgets['customField']->value ?? '',
'date' => $time,
'time' => $time,
'text' => $this->convertLineFeeds($strText),
'rating' => $arrWidgets['rating']->value,
'scope' => $arrWidgets['scope']->value ?? '',
'published' => $this->recommendation_moderate ? '' : 1
];
// Store the recommendation
$objRecommendation = new RecommendationModel();
$objRecommendation->setRow($arrData)->save();
// Notify system administrator via e-mail
if ($this->recommendation_notify && !$this->recommendation_activate)
{
$this->sendNotificationMail($objRecommendation);
}
// Send verification e-mail
if ($this->recommendation_activate)
{
// Unverify recommendation
$objRecommendation->verified = 0;
$objRecommendation->save();
$this->sendVerificationMail($arrData, $objRecommendation->id);
}
// Check whether there is a jumpTo page
if (($objJumpTo = $this->objModel->getRelated('jumpTo')) instanceof PageModel)
{
$this->jumpToOrReload($objJumpTo->row());
}
else
{
$objSession->getFlashBag()->set('recommendation_added', $this->getFlashBagMessage());
}
$this->reload();
}
}
/**
* Convert line feeds to <br /> tags
*/
public function convertLineFeeds(string $strText): string
{
$strText = preg_replace('/\r?\n/', '<br>', $strText);
// Use paragraphs to generate new lines
if (strncmp('<p>', $strText, 3) !== 0)
{
$strText = '<p>' . $strText . '</p>';
}
$arrReplace = [
'@<br>\s?<br>\s?@' => "</p>\n<p>", // Convert two linebreaks into a new paragraph
'@\s?<br></p>@' => '</p>', // Remove BR tags before closing P tags
'@<p><div@' => '<div', // Do not nest DIVs inside paragraphs
'@</div></p>@' => '</div>' // Do not nest DIVs inside paragraphs
];
return preg_replace(array_keys($arrReplace), array_values($arrReplace), $strText);
}
/**
* Get flashbag message
*/
protected function getFlashBagMessage(): string
{
// Confirmation e-mail
if ($this->recommendation_activate)
{
return $GLOBALS['TL_LANG']['tl_recommendation_notification']['confirm'];
}
// Needs approval
else if ($this->recommendation_moderate)
{
return $GLOBALS['TL_LANG']['tl_recommendation_notification']['approval'];
}
else
{
return $GLOBALS['TL_LANG']['tl_recommendation_notification']['added'];
}
}
/**
* Sends a notification to the administrator
*/
protected function sendNotificationMail(RecommendationModel $objRecommendation): void
{
$strText = $objRecommendation->text;
$objEmail = new Email();
$objEmail->from = $GLOBALS['TL_ADMIN_EMAIL'] ?? null;
$objEmail->fromName = $GLOBALS['TL_ADMIN_NAME'] ?? null;
$objEmail->subject = sprintf($GLOBALS['TL_LANG']['tl_recommendation_notification']['email_subject'], Idna::decode(Environment::get('host')));
// Convert the recommendation to plain text
$strText = strip_tags($strText);
$strText = StringUtil::decodeEntities($strText);
$strText = str_replace(['[&]', '[lt]', '[gt]'], ['&', '<', '>'], $strText);
// Add the recommendation details
$objEmail->text = sprintf(
$GLOBALS['TL_LANG']['tl_recommendation_notification']['email_message'],
$objRecommendation->author,
RecommendationArchiveModel::findById($this->recommendation_archive)->title,
$objRecommendation->rating,
$strText,
Idna::decode(Environment::get('base')) . 'contao?do=recommendation&table=tl_recommendation&id=' . $objRecommendation->id . '&act=edit'
);
// Add a moderation hint to the e-mail
if ($this->recommendation_moderate)
{
$objEmail->text .= "\n" . $GLOBALS['TL_LANG']['tl_recommendation_notification']['email_moderated'] . "\n";
}
// Send E-mail
$objEmail->sendTo($GLOBALS['TL_ADMIN_EMAIL']);
}
/**
* Send the verification mail
*/
protected function sendVerificationMail(array $arrData, int $id): void
{
$container = System::getContainer();
/** @var OptIn $optIn */
$optIn = $container->get('contao.opt_in');
$optInToken = $optIn->create('rec', $arrData['email'], ['tl_recommendation'=> [$id]]);
// Prepare the simple token data
$arrTokenData = $arrData;
$arrTokenData['token'] = $optInToken->getIdentifier();
$arrTokenData['domain'] = Idna::decode(Environment::get('host'));
$arrTokenData['link'] = Idna::decode(Environment::get('base')) . Environment::get('request') . ((str_contains(Environment::get('request'), '?')) ? '&' : '?') . 'token=' . $optInToken->getIdentifier();
$arrTokenData['channel'] = '';
// Send the token
$optInToken->send(sprintf($GLOBALS['TL_LANG']['tl_recommendation_notification']['email_activation'][0], Idna::decode(Environment::get('host'))), $container->get('contao.string.simple_token_parser')->parse($this->recommendation_activateText, $arrTokenData));
}
/**
* Verifies the recommendation
*/
protected function verifyRecommendation(): void
{
$this->Template = new FrontendTemplate('mod_message');
/** @var OptIn $optin */
$optIn = System::getContainer()->get('contao.opt_in');
// Find an unconfirmed token
if (
(!$optInToken = $optIn->find(Input::get('token'))) ||
!$optInToken->isValid() ||
\count($arrRelated = $optInToken->getRelatedRecords()) < 1 ||
key($arrRelated) != 'tl_recommendation' ||
\count($arrIds = current($arrRelated)) < 1
)
{
$this->Template->type = 'error';
$this->Template->message = $GLOBALS['TL_LANG']['MSC']['invalidToken'];
return;
}
if ($optInToken->isConfirmed())
{
$this->Template->type = 'error';
$this->Template->message = $GLOBALS['TL_LANG']['MSC']['tokenConfirmed'];
return;
}
$arrRecommendations = [];
foreach ($arrIds as $intId)
{
if (!$objRecommendation = RecommendationModel::findByPk($intId))
{
$this->Template->type = 'error';
$this->Template->message = $GLOBALS['TL_LANG']['MSC']['invalidToken'];
return;
}
if ($optInToken->getEmail() != $objRecommendation->email)
{
$this->Template->type = 'error';
$this->Template->message = $GLOBALS['TL_LANG']['MSC']['tokenEmailMismatch'];
return;
}
$arrRecommendations[] = $objRecommendation;
}
$objRecommendation->verified = 1;
$objRecommendation->save();
// Notify system administrator via e-mail
if ($this->recommendation_notify)
{
$this->sendNotificationMail($objRecommendation);
}
$optInToken->confirm();
// Log activity
$logger = System::getContainer()->get('monolog.logger.contao');
$logger?->info('Recommendation ID ' . $objRecommendation->id . ' (' . Idna::decodeEmail($objRecommendation->email) . ') has been verified');
// Redirect to the jumpTo page
if (($objTarget = $this->objModel->getRelated('recommendation_activateJumpTo')) instanceof PageModel)
{
/** @var PageModel $objTarget */
$this->redirect($objTarget->getFrontendUrl());
}
// Confirm activation
$this->Template->type = 'confirm';
$this->Template->message = $GLOBALS['TL_LANG']['tl_recommendation_notification']['verified'];
if ($this->recommendation_moderate)
{
$this->Template->message = $GLOBALS['TL_LANG']['tl_recommendation_notification']['approval'];
}
}
}
class_alias(ModuleRecommendationForm::class, 'ModuleRecommendationForm');