-
Notifications
You must be signed in to change notification settings - Fork 45
/
contactform.php
679 lines (606 loc) · 24.8 KB
/
contactform.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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <[email protected]>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
*/
if (!defined('_PS_VERSION_')) {
exit;
}
use PrestaShop\PrestaShop\Core\Module\WidgetInterface;
class Contactform extends Module implements WidgetInterface
{
/** @var string */
const SEND_CONFIRMATION_EMAIL = 'CONTACTFORM_SEND_CONFIRMATION_EMAIL';
/** @var string */
const SEND_NOTIFICATION_EMAIL = 'CONTACTFORM_SEND_NOTIFICATION_EMAIL';
/** @var string */
const MESSAGE_PLACEHOLDER_FOR_OLDER_VERSION = '(hidden)';
/** @var string */
const SUBMIT_NAME = 'update-configuration';
/** @var Contact */
protected $contact;
/** @var array */
protected $customer_thread;
public function __construct()
{
$this->name = 'contactform';
$this->author = 'PrestaShop';
$this->tab = 'front_office_features';
$this->version = '4.4.3';
$this->bootstrap = true;
parent::__construct();
$this->displayName = $this->trans('Contact form', [], 'Modules.Contactform.Admin');
$this->description = $this->trans(
'Help your customers get in touch when they need, add a contact form on your store.',
[],
'Modules.Contactform.Admin'
);
$this->ps_versions_compliancy = [
'min' => '1.7.2.0',
'max' => _PS_VERSION_,
];
}
/**
* @return bool
*/
public function install()
{
return parent::install() && $this->registerHook(['registerGDPRConsent', 'displayContactContent']);
}
/**
* @return string
*/
public function getContent()
{
$html = $this->renderForm();
if (Tools::getValue(self::SUBMIT_NAME)) {
Configuration::updateValue(
self::SEND_CONFIRMATION_EMAIL,
Tools::getValue(self::SEND_CONFIRMATION_EMAIL)
);
Configuration::updateValue(
self::SEND_NOTIFICATION_EMAIL,
Tools::getValue(self::SEND_NOTIFICATION_EMAIL)
);
Tools::redirectAdmin($this->context->link->getAdminLink('AdminModules') . '&configure=' . $this->name . '&conf=6');
}
return $html;
}
/**
* @return string
*/
protected function renderForm()
{
$fieldsValue = [
self::SEND_CONFIRMATION_EMAIL => Tools::getValue(
self::SEND_CONFIRMATION_EMAIL,
Configuration::get(self::SEND_CONFIRMATION_EMAIL)
),
self::SEND_NOTIFICATION_EMAIL => Tools::getValue(
self::SEND_NOTIFICATION_EMAIL,
Configuration::get(self::SEND_NOTIFICATION_EMAIL)
),
];
$form = [
'form' => [
'legend' => [
'title' => $this->trans('Parameters', [], 'Modules.Contactform.Admin'),
'icon' => 'icon-envelope',
],
'input' => [
[
'type' => 'switch',
'label' => $this->trans(
'Send confirmation email to your customers',
[],
'Modules.Contactform.Admin'
),
'desc' => $this->trans(
"Choose Yes and your customers will receive a generic confirmation email including a tracking number after their message is sent. Note: to discourage spam, the content of their message won't be included in the email.",
[],
'Modules.Contactform.Admin'
),
'name' => self::SEND_CONFIRMATION_EMAIL,
'is_bool' => true,
'required' => true,
'values' => [
[
'id' => self::SEND_CONFIRMATION_EMAIL . '_on',
'value' => 1,
'label' => $this->trans('Yes', [], 'Admin.Global'),
],
[
'id' => self::SEND_CONFIRMATION_EMAIL . '_off',
'value' => 0,
'label' => $this->trans('No', [], 'Admin.Global'),
],
],
],
[
'type' => 'switch',
'label' => $this->trans(
"Receive customers' messages by email",
[],
'Modules.Contactform.Admin'
),
'desc' => $this->trans(
'By default, you will only receive contact messages through your Customer service tab.',
[],
'Modules.Contactform.Admin'
),
'name' => self::SEND_NOTIFICATION_EMAIL,
'is_bool' => true,
'required' => true,
'values' => [
[
'id' => self::SEND_NOTIFICATION_EMAIL . '_on',
'value' => 1,
'label' => $this->trans('Yes', [], 'Admin.Global'),
],
[
'id' => self::SEND_NOTIFICATION_EMAIL . '_off',
'value' => 0,
'label' => $this->trans('No', [], 'Admin.Global'),
],
],
],
],
'submit' => [
'name' => self::SUBMIT_NAME,
'title' => $this->trans('Save', [], 'Admin.Actions'),
],
],
];
$helper = new HelperForm();
$helper->table = $this->table;
$lang = new Language((int) Configuration::get('PS_LANG_DEFAULT'));
$helper->default_form_language = $lang->id;
$helper->submit_action = 'update-configuration';
$helper->currentIndex = $this->getModuleConfigurationPageLink();
$helper->token = Tools::getAdminTokenLite('AdminModules');
$helper->tpl_vars = [
'fields_value' => $fieldsValue,
'languages' => $this->context->controller->getLanguages(),
'id_language' => $this->context->language->id,
];
return $helper->generateForm([$form]);
}
/**
* @return string
*/
protected function getModuleConfigurationPageLink()
{
$parsedUrl = parse_url($this->context->link->getAdminLink('AdminModules', false));
$urlParams = http_build_query(
[
'configure' => $this->name,
'tab_module' => $this->tab,
'module_name' => $this->name,
]
);
if (!empty($parsedUrl['query'])) {
$parsedUrl['query'] .= "&$urlParams";
} else {
$parsedUrl['query'] = $urlParams;
}
/*
* http_build_query function is available through composer package jakeasmith/http_build_url
*/
return http_build_url($parsedUrl);
}
/**
* {@inheritdoc}
*/
public function renderWidget($hookName = null, array $configuration = [])
{
if (!$this->active) {
return;
}
$this->smarty->assign($this->getWidgetVariables($hookName, $configuration));
return $this->display(__FILE__, 'views/templates/widget/contactform.tpl');
}
/**
* {@inheritdoc}
*/
public function getWidgetVariables($hookName = null, array $configuration = [])
{
$notifications = [];
if (Tools::isSubmit('submitMessage')) {
$this->sendMessage();
$notifications = [];
if (!empty($this->context->controller->errors)) {
$notifications['messages'] = $this->context->controller->errors;
$notifications['nw_error'] = true;
} elseif (!empty($this->context->controller->success)) {
$notifications['messages'] = $this->context->controller->success;
$notifications['nw_error'] = false;
}
} elseif (empty($this->context->cookie->contactFormToken)
|| empty($this->context->cookie->contactFormTokenTTL)
|| $this->context->cookie->contactFormTokenTTL < time()
) {
$this->createNewToken();
}
if (($id_customer_thread = (int) Tools::getValue('id_customer_thread'))
&& $token = Tools::getValue('token')
) {
$cm = new CustomerThread($id_customer_thread);
if ($cm->token == $token) {
$this->customer_thread = $this->context->controller->objectPresenter->present($cm);
}
}
$this->contact['contacts'] = $this->getTemplateVarContact();
$this->contact['message'] = Tools::getValue('message');
$this->contact['allow_file_upload'] = (bool) Configuration::get('PS_CUSTOMER_SERVICE_FILE_UPLOAD');
if (!(bool) Configuration::isCatalogMode()) {
$this->contact['orders'] = $this->getTemplateVarOrders();
} else {
$this->contact['orders'] = [];
}
if (isset($this->customer_thread['email'])) {
$this->contact['email'] = $this->customer_thread['email'];
} else {
$this->contact['email'] = Tools::safeOutput(
Tools::getValue(
'from',
!empty($this->context->cookie->email) && Validate::isEmail($this->context->cookie->email) ?
$this->context->cookie->email :
''
)
);
}
return [
'contact' => $this->contact,
'notifications' => $notifications,
'token' => $this->context->cookie->contactFormToken,
'id_module' => $this->id,
];
}
/**
* @return $this
*/
protected function createNewToken()
{
$this->context->cookie->contactFormToken = md5(uniqid());
$this->context->cookie->contactFormTokenTTL = time() + 600;
return $this;
}
/**
* @return array
*/
public function getTemplateVarContact()
{
$contacts = [];
$all_contacts = Contact::getContacts($this->context->language->id);
foreach ($all_contacts as $one_contact) {
$contacts[$one_contact['id_contact']] = $one_contact;
}
if (!empty($this->customer_thread['id_contact'])) {
return [
$contacts[$this->customer_thread['id_contact']],
];
}
return $contacts;
}
/**
* @return array
*
* @throws Exception
*/
public function getTemplateVarOrders()
{
$orders = [];
if (empty($this->customer_thread['id_order'])
&& isset($this->context->customer)
&& $this->context->customer->isLogged()
) {
$customer_orders = Order::getCustomerOrders($this->context->customer->id);
foreach ($customer_orders as $customer_order) {
$myOrder = new Order((int) $customer_order['id_order']);
if (Validate::isLoadedObject($myOrder)) {
$orders[$customer_order['id_order']] = $customer_order;
$orders[$customer_order['id_order']]['products'] = $myOrder->getProducts();
}
}
} elseif (isset($this->customer_thread['id_order']) && (int) $this->customer_thread['id_order'] > 0) {
$myOrder = new Order($this->customer_thread['id_order']);
if (Validate::isLoadedObject($myOrder)) {
$orders[$myOrder->id] = $this->context->controller->objectPresenter->present($myOrder);
$orders[$myOrder->id]['id_order'] = $myOrder->id;
$orders[$myOrder->id]['products'] = $myOrder->getProducts();
}
}
if (!empty($this->customer_thread['id_product'])) {
$id_order = isset($this->customer_thread['id_order']) ?
(int) $this->customer_thread['id_order'] :
0;
$orders[$id_order]['products'][(int) $this->customer_thread['id_product']] = $this->context->controller->objectPresenter->present(
new Product((int) $this->customer_thread['id_product'])
);
}
return $orders;
}
/**
* @throws PrestaShopDatabaseException
* @throws PrestaShopException
*/
public function sendMessage()
{
$extension = ['.txt', '.rtf', '.doc', '.docx', '.pdf', '.zip', '.png', '.jpeg', '.gif', '.jpg', '.webp'];
$file_attachment = Tools::fileAttachment('fileUpload');
$message = trim(Tools::getValue('message'));
$url = Tools::getValue('url');
$clientToken = Tools::getValue('token');
$serverToken = $this->context->cookie->contactFormToken;
$clientTokenTTL = $this->context->cookie->contactFormTokenTTL;
if (!($from = trim(Tools::getValue('from'))) || !Validate::isEmail($from)) {
$this->context->controller->errors[] = $this->trans(
'Invalid email address.',
[],
'Shop.Notifications.Error'
);
}
if (empty($message)) {
$this->context->controller->errors[] = $this->trans(
'The message cannot be blank.',
[],
'Shop.Notifications.Error'
);
}
if (!Validate::isCleanHtml($message)) {
$this->context->controller->errors[] = $this->trans(
'Invalid message',
[],
'Shop.Notifications.Error'
);
}
$id_contact = (int) Tools::getValue('id_contact');
$contact = new Contact($id_contact, $this->context->language->id);
if (!$id_contact || !(Validate::isLoadedObject($contact))) {
$this->context->controller->errors[] = $this->trans(
'Please select a subject from the list provided. ',
[],
'Modules.Contactform.Shop'
);
}
if (!empty($file_attachment['name']) && $file_attachment['error'] != 0) {
$this->context->controller->errors[] = $this->trans(
'An error occurred during the file-upload process.',
[],
'Modules.Contactform.Shop'
);
}
if (!empty($file_attachment['name']) &&
!in_array(Tools::strtolower(Tools::substr($file_attachment['name'], -4)), $extension) &&
!in_array(Tools::strtolower(Tools::substr($file_attachment['name'], -5)), $extension)
) {
$this->context->controller->errors[] = $this->trans(
'Bad file extension',
[],
'Modules.Contactform.Shop'
);
}
if ($url !== ''
|| empty($serverToken)
|| $clientToken !== $serverToken
|| $clientTokenTTL < time()
) {
$this->context->controller->errors[] = $this->trans(
'An error occurred while sending the message, please try again.',
[],
'Modules.Contactform.Shop'
);
$this->createNewToken();
}
if (!empty($this->context->controller->errors)) {
return;
}
$customer = $this->context->customer;
if (!$customer->id) {
$customer->getByEmail($from);
}
/**
* Check that the order belongs to the customer.
*/
$id_order = (int) Tools::getValue('id_order');
if (!empty($id_order)) {
$order = new Order($id_order);
$id_order = (int) $order->id_customer === (int) $customer->id ? $id_order : 0;
}
$id_customer_thread = CustomerThread::getIdCustomerThreadByEmailAndIdOrder($from, $id_order);
if ($contact->customer_service) {
if ((int) $id_customer_thread) {
$ct = new CustomerThread($id_customer_thread);
$ct->status = 'open';
$ct->id_lang = (int) $this->context->language->id;
$ct->id_contact = (int) $id_contact;
$ct->id_order = $id_order;
if ($id_product = (int) Tools::getValue('id_product')) {
$ct->id_product = $id_product;
}
$ct->update();
} else {
$ct = new CustomerThread();
if (isset($customer->id)) {
$ct->id_customer = (int) $customer->id;
}
$ct->id_shop = (int) $this->context->shop->id;
$ct->id_order = $id_order;
if ($id_product = (int) Tools::getValue('id_product')) {
$ct->id_product = $id_product;
}
$ct->id_contact = (int) $id_contact;
$ct->id_lang = (int) $this->context->language->id;
$ct->email = $from;
$ct->status = 'open';
$ct->token = Tools::passwdGen(12);
$ct->add();
}
if ($ct->id) {
$lastMessage = CustomerMessage::getLastMessageForCustomerThread($ct->id);
$testFileUpload = (isset($file_attachment['rename']) && !empty($file_attachment['rename']));
// if last message is the same as new message (and no file upload), do not consider this contact
if ($lastMessage != $message || $testFileUpload) {
$cm = new CustomerMessage();
$cm->id_customer_thread = $ct->id;
$cm->message = $message;
if ($testFileUpload && rename($file_attachment['tmp_name'], _PS_UPLOAD_DIR_ . basename($file_attachment['rename']))) {
$cm->file_name = $file_attachment['rename'];
@chmod(_PS_UPLOAD_DIR_ . basename($file_attachment['rename']), 0664);
}
$cm->ip_address = (string) ip2long(Tools::getRemoteAddr());
$cm->user_agent = $_SERVER['HTTP_USER_AGENT'];
if (!$cm->add()) {
$this->context->controller->errors[] = $this->trans(
'An error occurred while sending the message.',
[],
'Modules.Contactform.Shop'
);
}
} else {
$mailAlreadySend = true;
}
} else {
$this->context->controller->errors[] = $this->trans(
'An error occurred while sending the message.',
[],
'Modules.Contactform.Shop'
);
}
}
$sendConfirmationEmail = Configuration::get(self::SEND_CONFIRMATION_EMAIL);
$sendNotificationEmail = Configuration::get(self::SEND_NOTIFICATION_EMAIL);
if (!count($this->context->controller->errors)
&& empty($mailAlreadySend)
&& ($sendConfirmationEmail || $sendNotificationEmail)
) {
$message = version_compare(_PS_VERSION_, '8.0.0', '>=') ? stripslashes($message) : Tools::stripslashes($message);
$var_list = [
'{firstname}' => '',
'{lastname}' => '',
'{order_name}' => '-',
'{attached_file}' => '-',
'{message}' => Tools::nl2br(Tools::htmlentitiesUTF8($message)),
'{email}' => $from,
'{product_name}' => '',
];
if (isset($customer->id)) {
$var_list['{firstname}'] = $customer->firstname;
$var_list['{lastname}'] = $customer->lastname;
}
if (isset($file_attachment['name'])) {
$var_list['{attached_file}'] = $file_attachment['name'];
}
$id_product = (int) Tools::getValue('id_product');
if ($id_order) {
$order = new Order((int) $id_order);
$var_list['{order_name}'] = $order->getUniqReference();
$var_list['{id_order}'] = (int) $order->id;
}
if ($id_product) {
$product = new Product((int) $id_product);
if (Validate::isLoadedObject($product) &&
isset($product->name[Context::getContext()->language->id])
) {
$var_list['{product_name}'] = $product->name[Context::getContext()->language->id];
}
}
if ($sendNotificationEmail) {
if (empty($contact->email) || !Mail::Send(
$this->context->language->id,
'contact',
$this->trans('Message from contact form', [], 'Emails.Subject') . ' [no_sync]',
$var_list,
$contact->email,
$contact->name,
null,
null,
$file_attachment,
null,
_PS_MAIL_DIR_,
false,
null,
null,
$from
)) {
$this->context->controller->errors[] = $this->trans(
'An error occurred while sending the message.',
[],
'Modules.Contactform.Shop'
);
}
}
if ($sendConfirmationEmail) {
$var_list['{message}'] = self::MESSAGE_PLACEHOLDER_FOR_OLDER_VERSION;
if (!Mail::Send(
$this->context->language->id,
'contact_form',
((isset($ct) && Validate::isLoadedObject($ct)) ? $this->trans(
'Your message has been correctly sent #ct%thread_id% #tc%thread_token%',
[
'%thread_id%' => $ct->id,
'%thread_token%' => $ct->token,
],
'Emails.Subject'
) : $this->trans('Your message has been correctly sent', [], 'Emails.Subject')),
$var_list,
$from,
null,
null,
null,
$file_attachment,
null,
_PS_MAIL_DIR_,
false,
null,
null,
$contact->email
)) {
$this->context->controller->errors[] = $this->trans(
'An error occurred while sending the message.',
[],
'Modules.Contactform.Shop'
);
}
}
}
if (!count($this->context->controller->errors)) {
$this->context->controller->success[] = $this->trans(
'Your message has been successfully sent to our team.',
[],
'Modules.Contactform.Shop'
);
}
}
/**
* empty listener for registerGDPRConsent hook
*/
public function hookRegisterGDPRConsent()
{
/* registerGDPRConsent is a special kind of hook that doesn't need a listener, see :
https://build.prestashop.com/howtos/module/how-to-make-your-module-compliant-with-prestashop-official-gdpr-compliance-module/
However since Prestashop 1.7.8, modules must implement a listener for all the hooks they register: a check is made
at module installation.
*/
}
}