1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Craft Contact Form Extensions plugin for Craft CMS 3.x. |
4
|
|
|
* |
5
|
|
|
* Adds extensions to the Craft CMS contact form plugin. |
6
|
|
|
* |
7
|
|
|
* @link https://rias.be |
8
|
|
|
* |
9
|
|
|
* @copyright Copyright (c) 2018 Rias |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace rias\contactformextensions\services; |
13
|
|
|
|
14
|
|
|
use Craft; |
15
|
|
|
use craft\base\Component; |
16
|
|
|
use craft\contactform\models\Submission; |
17
|
|
|
use craft\helpers\StringHelper; |
18
|
|
|
use rias\contactformextensions\ContactFormExtensions; |
19
|
|
|
use rias\contactformextensions\elements\ContactFormSubmission; |
20
|
|
|
use rias\contactformextensions\models\RecaptchaV3; |
21
|
|
|
use yii\base\Exception; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* CraftContactFormExtensionsService Service. |
25
|
|
|
* |
26
|
|
|
* All of your plugin’s business logic should go in services, including saving data, |
27
|
|
|
* retrieving data, etc. They provide APIs that your controllers, template variables, |
28
|
|
|
* and other plugins can interact with. |
29
|
|
|
* |
30
|
|
|
* https://craftcms.com/docs/plugins/services |
31
|
|
|
* |
32
|
|
|
* @author Rias |
33
|
|
|
* |
34
|
|
|
* @since 1.0.0 |
35
|
|
|
*/ |
36
|
|
|
class ContactFormExtensionsService extends Component |
37
|
|
|
{ |
38
|
|
|
// Public Methods |
39
|
|
|
// ========================================================================= |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* This function can literally be anything you want, and you can have as many service |
43
|
|
|
* functions as you want. |
44
|
|
|
* |
45
|
|
|
* From any other plugin file, call it like this: |
46
|
|
|
* |
47
|
|
|
* CraftContactFormExtensions::$plugin->craftContactFormExtensionsService->exampleService() |
48
|
|
|
* |
49
|
|
|
* @param Submission $submission |
50
|
|
|
* |
51
|
|
|
* @throws Exception |
52
|
|
|
* @throws \Throwable |
53
|
|
|
* @throws \craft\errors\ElementNotFoundException |
54
|
|
|
* |
55
|
|
|
* @return mixed |
56
|
|
|
*/ |
57
|
|
|
public function saveSubmission(Submission $submission) |
58
|
|
|
{ |
59
|
|
|
$contactFormSubmission = new ContactFormSubmission(); |
60
|
|
|
$contactFormSubmission->form = $submission->message['formName'] ?? 'contact'; |
61
|
|
|
$contactFormSubmission->fromName = $submission->fromName; |
62
|
|
|
$contactFormSubmission->fromEmail = $submission->fromEmail; |
63
|
|
|
$contactFormSubmission->subject = $submission->subject; |
64
|
|
|
|
65
|
|
|
if (!is_array($submission->message)) { |
66
|
|
|
$submission->message = ['message' => $this->utf8Value($submission->message)]; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$message = $this->utf8AllTheThings($submission->message); |
70
|
|
|
$contactFormSubmission->message = json_encode($message); |
71
|
|
|
|
72
|
|
|
if (Craft::$app->elements->saveElement($contactFormSubmission)) { |
73
|
|
|
return $contactFormSubmission; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
throw new Exception(json_encode($contactFormSubmission->errors)); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function getRecaptcha() |
80
|
|
|
{ |
81
|
|
|
$siteKey = Craft::parseEnv(ContactFormExtensions::$plugin->settings->recaptchaSiteKey); |
82
|
|
|
$secretKey = Craft::parseEnv(ContactFormExtensions::$plugin->settings->recaptchaSecretKey); |
83
|
|
|
|
84
|
|
|
if (ContactFormExtensions::$plugin->settings->recaptchaVersion === '3') { |
85
|
|
|
$recaptcha = new RecaptchaV3( |
86
|
|
|
$siteKey, |
|
|
|
|
87
|
|
|
$secretKey, |
|
|
|
|
88
|
|
|
ContactFormExtensions::$plugin->settings->recaptchaThreshold, |
89
|
|
|
ContactFormExtensions::$plugin->settings->recaptchaTimeout, |
90
|
|
|
ContactFormExtensions::$plugin->settings->recaptchaHideBadge |
91
|
|
|
); |
92
|
|
|
|
93
|
|
|
return $recaptcha; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$options = [ |
97
|
|
|
'hideBadge' => ContactFormExtensions::$plugin->settings->recaptchaHideBadge, |
98
|
|
|
'dataBadge' => ContactFormExtensions::$plugin->settings->recaptchaDataBadge, |
99
|
|
|
'timeout' => ContactFormExtensions::$plugin->settings->recaptchaTimeout, |
100
|
|
|
'debug' => ContactFormExtensions::$plugin->settings->recaptchaDebug, |
101
|
|
|
]; |
102
|
|
|
|
103
|
|
|
return new \AlbertCht\InvisibleReCaptcha\InvisibleReCaptcha($siteKey, $secretKey, $options); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param array $things |
108
|
|
|
* |
109
|
|
|
* @return array |
110
|
|
|
*/ |
111
|
|
|
public function utf8AllTheThings(array $things): array |
112
|
|
|
{ |
113
|
|
|
foreach ($things as $key => $value) { |
114
|
|
|
$things[$key] = $this->utf8Value($value); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $things; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param array|string $value |
122
|
|
|
* |
123
|
|
|
* @return array|string |
124
|
|
|
*/ |
125
|
|
|
public function utf8Value($value) |
126
|
|
|
{ |
127
|
|
|
if (is_array($value)) { |
128
|
|
|
return $this->utf8AllTheThings($value); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return StringHelper::convertToUtf8($value); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|