PluginProbe
Double Opt-In for Contact Form 7 – Secure, GDPR-Compliant Email Verification / 2.15
Double Opt-In for Contact Form 7 – Secure, GDPR-Compliant Email Verification v2.15
5.6.0 5.5.0 5.4.0 5.3.2 5.3.1 5.1.6 5.1.5 trunk 2.1.5 2.11 2.12 2.13 2.15 3.0.0 3.0.1 3.0.2 3.0.3 3.0.5 3.0.51 3.0.60 3.0.61 3.0.62 3.0.70 3.0.71 3.0.72 All 35 releases
double-opt-in / compatibility / cf7 / Frontend.class.php

Frontend.class.php in Double Opt-In for Contact Form 7 – Secure, GDPR-Compliant Email Verification 2.15, at compatibility/cf7/Frontend.class.php

338 lines 12.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace forge12\contactform7\CF7DoubleOptIn {
4 if (!defined('ABSPATH')) {
5 exit;
6 }
7
8 use forge12\plugins\ContactForm7;
9
10 /**
11 * Class Frontend
12 * Responsible to handle the frontend of the Double OptIn
13 *
14 * @package forge12\contactform7\CF7DoubleOptIn
15 */
16 class Frontend
17 {
18 /**
19 * Admin constructor.
20 */
21 public function __construct()
22 {
23 add_action('init', array($this, 'validateOptIn'));
24 add_action('wpcf7_before_send_mail', array($this, 'onSubmit'), 5, 3);
25 add_action('shutdown', array($this, 'cleanup'));
26 }
27
28 /**
29 * Store the optin value for the given object by the hash value
30 *
31 * @param $hash
32 * @param $value
33 *
34 * @return int The number of rows changed.
35 */
36 private function updateOptInByHash($hash, $value)
37 {
38 $OptIn = OptIn::get_by_hash($hash);
39 if ($OptIn->is_confirmed()) {
40 do_action('f12_cf7_doubleoptin_already_confirmed', $hash, $OptIn);
41 return 0;
42 }
43
44 do_action('f12_cf7_doubleoptin_before_confirm', $hash, $OptIn);
45
46 $OptIn->set_doubleoptin($value);
47 $OptIn->set_updatetime(time());
48 $OptIn->set_ipaddr_confirmation(CF7DoubleOptIn::getInstance()->getIPAdress());
49
50 $result = $OptIn->save();
51
52 if ($result) {
53 do_action('f12_cf7_doubleoptin_after_confirm', $hash, $OptIn);
54 }
55
56 return (int)$result;
57 }
58
59
60 /**
61 * Add Stylesheets
62 */
63 public function validateOptIn()
64 {
65
66 if (!isset($_GET['optin'])) {
67 return;
68 }
69
70 $hash = sanitize_text_field($_GET['optin']);
71
72 $OptIn = OptIn::get_by_hash($hash);
73
74 if (null != $OptIn) {
75 if ($OptIn->isType('cf7') && $this->updateOptInByHash($hash, 1) > 0) {
76 if (!apply_filters('f12_cf7_doubleoptin_send_default_mail', true, $OptIn->get_cf_form_id())) {
77 return;
78 }
79
80 // Remove the filter for the forge12 spam captcha
81 if (class_exists('\forge12\contactform7\CF7Captcha\TimerValidatorCF7')) {
82 remove_filter('wpcf7_spam', '\forge12\contactform7\CF7Captcha::isSpam');
83 remove_filter('wpcf7_spam', '\forge12\contactform7\CF7Captcha\CF7IPLog::isSpam');
84 remove_action('wpcf7_mail_sent', '\forge12\contactform7\CF7Captcha\CF7IPLog::doLogIP');
85 }
86
87 // Remove the filter for the google repatcha validation
88 remove_filter('wpcf7_spam', 'wpcf7_recaptcha_verify_response', 9);
89
90 // Setup the contact form 7 which will fire the form send after loading the page)
91 $data = maybe_unserialize($OptIn->get_content());
92 $_POST = CF7DoubleOptIn::getInstance()->sanitize_array($data);
93
94 $ContactForm = \WPCF7_ContactForm::get_instance($OptIn->get_cf_form_id());
95
96 $submission = \WPCF7_Submission::get_instance($ContactForm);
97
98 // re add the filter to ensure for all other forms the recaptcha is used
99 if (function_exists('wpcf7_recaptcha_verifiy_response')) {
100 add_filter('wpcf7_spam', 'wpcf7_recaptcha_verify_response', 9, 2);
101 }
102
103 // re add the filter for the forge12 spam captcha
104 if (class_exists('\forge12\contactform7\CF7Captcha\TimerValidatorCF7')) {
105 add_filter('wpcf7_spam', '\forge12\contactform7\CF7Captcha\TimerValidatorCF7::isSpam', 100, 2);
106 add_filter('wpcf7_spam', '\forge12\contactform7\CF7Captcha\CF7IPLog::isSpam', 100, 2);
107 add_action('wpcf7_mail_sent', '\forge12\contactform7\CF7Captcha\CF7IPLog::doLogIP', 100, 1);
108 }
109 }
110 }
111 }
112
113 /**
114 * Add the double opt in link to the body
115 *
116 * @param string $body
117 * @param OptIn $OptIn
118 *
119 * @return string
120 */
121 private function addDoubleOptInLink($body, $OptIn)
122 {
123 $link = $OptIn->get_link_optin();
124
125 return str_replace('[doubleoptinlink]', $link, $body);
126 }
127
128 /**
129 * Add additional placeholder like time, date, subject
130 *
131 * @param $form \WPCF7_ContactForm
132 * @param $submission \WPCF7_Submission
133 * @param $body
134 * @param $OptIn
135 * @param $parameter
136 */
137 private function addAdditionalPlaceholder($form, $submission, $body, $OptIn, $parameter)
138 {
139 # set the default timezone
140 date_default_timezone_set(get_option('timezone_string'));
141 $placeholder = array(
142 'doubleoptin_form_url' => $submission->get_meta('url'),
143 'doubleoptin_form_subject' => $parameter['subject'],
144 'doubleoptin_form_date' => date(get_option('date_format')),
145 'doubleoptin_form_time' => date(get_option('time_format')),
146 'doubleoptin_form_email' => get_option('admin_email')
147 );
148
149 foreach ($placeholder as $key => $value) {
150 $body = str_replace('[' . $key . ']', $value, $body);
151 }
152 return $body;
153 }
154
155 /**
156 * Add formular informaition to the database
157 *
158 * @param $form \WPCF7_ContactForm
159 * @param array $parameter
160 * @param array $files
161 *
162 * @return OptIn|null
163 */
164 private function addRequest($form, $parameter, $files = array())
165 {
166 global $wpdb;
167
168 if (null == $wpdb) {
169 return 0;
170 }
171
172 $copiedFields = array();
173
174 if (!empty($files)) {
175 foreach ($files as $key => $subfiles) {
176 foreach ($subfiles as $file) {
177 $newFile = explode('/', $file);
178 $name = $newFile[count($newFile) - 1];
179 $name = time() . '_' . $name;
180 $newFile[count($newFile) - 1] = $name;
181 $newFile = implode("/", $newFile);
182
183 if (copy($file, $newFile)) {
184 $copiedFields[] = $newFile;
185 }
186 }
187 }
188 }
189
190 $parameter = \apply_filters('f12_cf7_doubleoptin_add_request_parameter', $parameter);
191
192 $formParameter = CF7DoubleOptIn::getInstance()->getParameter($form->id());
193
194 // Get Recipient
195 $recipient = $formParameter['recipient'];
196 $recipient = str_replace('[', '',$recipient);
197 $recipient = str_replace(']', '', $recipient);
198
199 if(isset($_POST[$recipient])){
200 $recipient = sanitize_email($_POST[$recipient]);
201 }else{
202 $recipient = '';
203 }
204
205 $data = array(
206 'cf_form_id' => $form->id(),
207 'doubleoptin' => 0,
208 'createtime' => time(),
209 'content' => maybe_serialize($parameter),
210 'files' => maybe_serialize($copiedFields),
211 'ipaddr_register' => CF7DoubleOptIn::getInstance()->getIPAdress(),
212 'category' => (int)$formParameter['category'],
213 'form' => $form->form_html(),
214 'email' => $recipient
215 );
216
217 $OptIn = new OptIn($data);
218 if ($OptIn->save()) {
219 return $OptIn;
220 }
221 return null;
222 }
223
224 /**
225 * Validate if the optin is enabled.
226 */
227 private function isOptinEnabled($form)
228 {
229 // Disable optin sending if the optin flag is set.
230 if (isset($_GET['optin'])) {
231 return false;
232 }
233
234 $parameter = CF7DoubleOptIn::getInstance()->getParameter($form->id());
235
236 if ((int)$parameter['enable'] != 1) {
237 return false;
238 }
239
240 // Check the custom condition
241 if (isset($parameter['conditions'])) {
242 $condition = sanitize_text_field($parameter['conditions']);
243
244 if ($condition != 'disable' && (!isset($_POST[$condition]) || empty($_POST[$condition]))) {
245 return false;
246 }
247 }
248
249 return true;
250 }
251
252 /**
253 * On Form Submit add the double optin if enabled
254 *
255 * @param $form \WPCF7_ContactForm
256 * @param bool
257 * @param $submission \WPCF7_Submission
258 */
259 public function onSubmit($form, &$abort, $submission)
260 {
261 $parameter = CF7DoubleOptIn::getInstance()->getParameter($form->id());
262
263 if ($this->isOptinEnabled($form)) {
264 // Remove Contact Form 7 DB hook to ensure the optin mail is not saved
265 remove_action('wpcf7_before_send_mail', 'cfdb7_before_send_mail');
266
267 $OptIn = $this->addRequest($form, $submission->get_posted_data(), $submission->uploaded_files());
268 $body = $this->addDoubleOptInLink($parameter['body'], $OptIn);
269 $body = $this->addAdditionalPlaceholder($form, $submission, $body, $OptIn, $parameter);
270
271 $body = apply_filters('f12_cf7_doubleoptin_body', $body);
272
273 // Update the opt in after setting the opt-in content.
274 $OptIn->set_mail_optin($body);
275 $OptIn->save();
276
277 // Prepare the Opt-In Mail
278 \WPCF7_Mail::send(array(
279 'subject' => $parameter['subject'],
280 'body' => $body,
281 'sender' => $parameter['sender'],
282 'recipient' => $parameter['recipient'],
283 'use_html' => true,
284 ), 'mail');
285
286 // Skip all mails following
287 add_filter('wpcf7_skip_mail', '__return_true');
288
289 do_action('f12_cf7_doubleoptin_sent', $form, $form->id());
290
291 //$abort = true;
292 } else if (isset($_GET['optin'])) {
293
294 $OptIn = OptIn::get_by_hash(sanitize_text_field($_GET['optin']));
295
296 if (null != $OptIn) {
297 $files = maybe_unserialize($OptIn->get_files());
298
299 foreach ($files as $file) {
300 if (!empty($file)) {
301 if (apply_filters('f12_cf7_doubleoptin_files_mail_1', true, $OptIn)) {
302 $submission->add_extra_attachments($file);
303 }
304
305 if (apply_filters('f12_cf7_doubleoptin_files_mail_2', true, $OptIn)) {
306 $submission->add_extra_attachments($file, 'mail_2');
307 }
308 }
309 }
310 }
311 }
312 }
313
314 /**
315 * On Cleanup we delete all files that are not required anymore.
316 */
317 public function cleanup()
318 {
319 if (isset($_GET['optin'])) {
320 $OptIn = OptIn::get_by_hash(esc_sql($_GET['optin']));
321
322 if (null != $OptIn) {
323 $files = maybe_unserialize($OptIn->get_files());
324
325 foreach ($files as $file) {
326 if (!empty($file)) {
327 if (is_file($file)) {
328 if (!unlink($file)) {
329 error_log("Could not delete file " . $file . "!");
330 }
331 }
332 }
333 }
334 }
335 }
336 }
337 }
338 }