| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Get set Form,fields |
| 5 |
*/ |
| 6 |
|
| 7 |
namespace BitCode\BitForm\Frontend\Form; |
| 8 |
|
| 9 |
/** |
| 10 |
* FrontendFormManager class |
| 11 |
*/ |
| 12 |
|
| 13 |
use WP_Error; |
| 14 |
use BitCode\BitForm\Core\Util\IpTool; |
| 15 |
use BitCode\BitForm\Core\Util\HttpHelper; |
| 16 |
use BitCode\BitForm\Core\Form\FormManager; |
| 17 |
use BitCode\BitForm\Core\Util\DateTimeHelper; |
| 18 |
use BitCode\BitForm\Core\Database\FormEntryModel; |
| 19 |
use BitCode\BitForm\Frontend\Form\View\FormViewer; |
| 20 |
use BitCode\BitForm\Core\WorkFlow\WorkFlowRunHelper; |
| 21 |
use BitCode\BitForm\Core\Integration\IntegrationHandler; |
| 22 |
use BitCode\BitForm\Core\Form\Validator\FormFieldValidator; |
| 23 |
use BitCode\BitForm\Core\Util\ApiResponse as UtilApiResponse; |
| 24 |
|
| 25 |
final class FrontendFormManager extends FormManager |
| 26 |
{ |
| 27 |
private $_form_identifier; |
| 28 |
private $_form_token; |
| 29 |
private $_form_id; |
| 30 |
// private $_has_upload = false; |
| 31 |
public function __construct($form_id, $shortCodeCounter = null) |
| 32 |
{ |
| 33 |
parent::__construct($form_id); |
| 34 |
$this->_form_identifier = 'bitforms_' . $form_id . '_submit_'; |
| 35 |
$this->_form_identifier .= !empty(get_post()->ID) ? get_post()->ID : ''; |
| 36 |
$this->_form_identifier .= !empty($shortCodeCounter) ? "_$shortCodeCounter" : ''; |
| 37 |
$this->_form_token = wp_create_nonce('bitforms_' . $form_id); |
| 38 |
$this->_form_id = $form_id; |
| 39 |
} |
| 40 |
|
| 41 |
public function getFormIdentifier() |
| 42 |
{ |
| 43 |
return $this->_form_identifier; |
| 44 |
} |
| 45 |
|
| 46 |
public function getFormID() |
| 47 |
{ |
| 48 |
return $this->_form_id; |
| 49 |
} |
| 50 |
|
| 51 |
public function getFormToken() |
| 52 |
{ |
| 53 |
return $this->_form_token; |
| 54 |
} |
| 55 |
|
| 56 |
public function isSubmitted() |
| 57 |
{ |
| 58 |
return isset($_POST[$this->_form_identifier]) ? true : false; |
| 59 |
} |
| 60 |
|
| 61 |
public function getSubmittedFields($submitted_data) |
| 62 |
{ |
| 63 |
unset($submitted_data[$this->_form_identifier]); |
| 64 |
return array_keys($submitted_data); |
| 65 |
} |
| 66 |
|
| 67 |
public function formView($fields = null, $hasFile = false, $errorMessages = null, $previousValue = null) |
| 68 |
{ |
| 69 |
$formContents = $this->getFormContent(); |
| 70 |
if (!empty($fields)) { |
| 71 |
$formContents->fields = is_string($fields) ? json_decode($fields) : $fields; |
| 72 |
} else { |
| 73 |
$workFlowRunHelper = new WorkFlowRunHelper($this->form_id); |
| 74 |
$workFlowreturnedOnLoad = $workFlowRunHelper->executeOnLoad( |
| 75 |
'create', |
| 76 |
$formContents->fields |
| 77 |
); |
| 78 |
$formContents->fields = empty($workFlowreturnedOnLoad['fields']) ? $formContents->fields : $workFlowreturnedOnLoad['fields']; |
| 79 |
} |
| 80 |
$formViewer = new FormViewer($this, $formContents, $errorMessages, $previousValue); |
| 81 |
return $formViewer->getView($hasFile); |
| 82 |
} |
| 83 |
|
| 84 |
private function checkEmptySubmission($data, $file) |
| 85 |
{ |
| 86 |
$form_fields = $this->getFields(); |
| 87 |
$emptySubmission = true; |
| 88 |
foreach ($form_fields as $key => $field) { |
| 89 |
if (!empty($data[$key]) || !empty($file[$key]['name'])) { |
| 90 |
$emptySubmission = false; |
| 91 |
break; |
| 92 |
} |
| 93 |
} |
| 94 |
return $emptySubmission; |
| 95 |
} |
| 96 |
|
| 97 |
private function getParams() |
| 98 |
{ |
| 99 |
$url = parse_url(wp_get_referer()); |
| 100 |
$parameter = []; |
| 101 |
if (isset($url['query'])) { |
| 102 |
$queries = explode('&', $url['query']); |
| 103 |
foreach ($queries as $query) { |
| 104 |
list($field, $value) = explode('=', $query); |
| 105 |
$parameter[$field] = $value; |
| 106 |
} |
| 107 |
} |
| 108 |
return $parameter; |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
public function handleSubmission() |
| 113 |
{ |
| 114 |
$validated = $this->beforeSubmittedValidate(); |
| 115 |
|
| 116 |
if ($validated === true) { |
| 117 |
unset($_POST['hidden_fields']); |
| 118 |
|
| 119 |
$redirectPage = ''; |
| 120 |
$regSuccMsg = ''; |
| 121 |
|
| 122 |
$existAuth = (new IntegrationHandler($this->_form_id))->getAllIntegration('wp_user_auth', 'wp_auth', 1); |
| 123 |
if (!is_wp_error($existAuth) && count($existAuth) > 0) { |
| 124 |
|
| 125 |
$parameter = $this->getParams(); |
| 126 |
$existAuthFilter = has_filter('bf_wp_user_auth'); |
| 127 |
|
| 128 |
if ($existAuthFilter === true) { |
| 129 |
$result = apply_filters('bf_wp_user_auth', $existAuth[0], $_POST, $parameter); |
| 130 |
|
| 131 |
if (isset($result['auth_type']) && $result['auth_type'] === 'register') { |
| 132 |
if (!$result['success']) { |
| 133 |
return new WP_Error('errors', __($result['message'], 'bit-form')); |
| 134 |
} elseif (isset($result['success'])) { |
| 135 |
$redirectPage = $result['redirect_url']; |
| 136 |
$regSuccMsg = $result['message']; |
| 137 |
$newNonce = wp_create_nonce('bitforms_' . $this->_form_id); |
| 138 |
} |
| 139 |
} else { |
| 140 |
if (!$result['success']) { |
| 141 |
return new WP_Error('errors', __($result['message'], 'bit-form')); |
| 142 |
} else { |
| 143 |
return $result; |
| 144 |
} |
| 145 |
} |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
$saveResponse = $this->saveFormEntry($_POST); |
| 150 |
if (is_wp_error($saveResponse)) { |
| 151 |
return $saveResponse; |
| 152 |
} |
| 153 |
$captchaV3Settings = $this->getCaptchaV3Settings(); |
| 154 |
if ($captchaV3Settings) { |
| 155 |
$token = $_POST['g-recaptcha-response']; |
| 156 |
$integrationHandler = new IntegrationHandler(0); |
| 157 |
$allFormIntegrations = $integrationHandler->getAllIntegration('app', 'gReCaptchaV3'); |
| 158 |
if (!is_wp_error($allFormIntegrations)) { |
| 159 |
foreach ($allFormIntegrations as $integration) { |
| 160 |
if (!is_null($integration->integration_type) && $integration->integration_type === 'gReCaptchaV3') { |
| 161 |
$integrationDetails = json_decode($integration->integration_details); |
| 162 |
$integrationDetails->id = $integration->id; |
| 163 |
$reCAPTCHA = $integrationDetails; |
| 164 |
} |
| 165 |
} |
| 166 |
} |
| 167 |
if (!empty($reCAPTCHA->secretKey)) { |
| 168 |
$gRecaptchaResponse = HttpHelper::post( |
| 169 |
'https://www.google.com/recaptcha/api/siteverify', |
| 170 |
['secret' => $reCAPTCHA->secretKey, 'response' => $token] |
| 171 |
); |
| 172 |
if ($captchaV3Settings && !empty($saveResponse['triggerData'])) { |
| 173 |
$logID = $saveResponse['triggerData']['logID']; |
| 174 |
$integId = $reCAPTCHA->id; |
| 175 |
$saveApiResponse = new UtilApiResponse(); |
| 176 |
$saveApiResponse->apiResponse($logID, $integId, ['type_name' => 'ReCaptcha', 'type' => 'v3'], 'success', $gRecaptchaResponse); |
| 177 |
} |
| 178 |
} |
| 179 |
unset($_POST['g-recaptcha-response']); |
| 180 |
} |
| 181 |
if (!empty($redirectPage) && empty($saveResponse['redirectPage']) || $saveResponse['redirectPage'] == null) { |
| 182 |
$saveResponse['redirectPage'] = $redirectPage; |
| 183 |
} |
| 184 |
if (!empty($regSuccMsg) && isset($saveResponse['dflt_message'])) { |
| 185 |
$saveResponse['message'] = $regSuccMsg; |
| 186 |
} |
| 187 |
$saveResponse = IntegrationHandler::maybeSetCronForIntegration($saveResponse, 'create'); |
| 188 |
|
| 189 |
$responseMsg = is_array($saveResponse) && !empty($saveResponse) ? $saveResponse : __('Form Submitted Successfully', 'bit-form'); |
| 190 |
if (isset($newNonce)) { |
| 191 |
$responseMsg['new_nonce'] = $newNonce; |
| 192 |
} |
| 193 |
$_POST = array(); |
| 194 |
|
| 195 |
return $responseMsg; |
| 196 |
} |
| 197 |
|
| 198 |
return $validated; |
| 199 |
} |
| 200 |
|
| 201 |
public function validateFormSubmission($submitted_data) |
| 202 |
{ |
| 203 |
$hidden_fields = isset($submitted_data['hidden_fields']) ? $submitted_data['hidden_fields'] : ''; |
| 204 |
$submitted_fields = $this->getSubmittedFields($submitted_data); |
| 205 |
$form_fields = $this->getFields(); |
| 206 |
$form_fields_names = array_keys($form_fields); |
| 207 |
if ($this->isGCLIDEnabled()) { |
| 208 |
array_push($form_fields_names, 'GCLID'); |
| 209 |
} |
| 210 |
foreach ($submitted_fields as $key => $field) { |
| 211 |
if ($field !== 'hidden_fields' && !in_array($field, $form_fields_names) || strpos($hidden_fields, $field) !== false) { |
| 212 |
unset($submitted_data[$field]); |
| 213 |
} |
| 214 |
} |
| 215 |
return $submitted_data; |
| 216 |
} |
| 217 |
|
| 218 |
public function beforeSubmittedValidate() |
| 219 |
{ |
| 220 |
if ($this->verifySubmissionNonce()) { |
| 221 |
if ($this->isExist()) { |
| 222 |
$isRestricted = $this->checkSubmissionRestriction(); |
| 223 |
if ($isRestricted && !empty($isRestricted)) { |
| 224 |
return new WP_Error('spam_detection', $isRestricted[0]); |
| 225 |
} |
| 226 |
if ($this->isTrappedInHoneypot()) { |
| 227 |
return new WP_Error('spam_detection', __('Token verification failed', 'bit-form')); |
| 228 |
} |
| 229 |
$captchaSettings = $this->getCaptchaSettings(); |
| 230 |
$captchaV3Settings = $this->getCaptchaV3Settings(); |
| 231 |
if ($captchaSettings || $captchaV3Settings) { |
| 232 |
$token = $_POST['g-recaptcha-response']; |
| 233 |
if (!isset($_POST['g-recaptcha-response'])) { |
| 234 |
return new WP_Error('spam_detection', __('Please verify reCAPTCHA', 'bit-form')); |
| 235 |
} |
| 236 |
$integrationHandler = new IntegrationHandler(0); |
| 237 |
$allFormIntegrations = $integrationHandler->getAllIntegration('app', $captchaSettings ? 'gReCaptcha' : 'gReCaptchaV3'); |
| 238 |
if (!is_wp_error($allFormIntegrations)) { |
| 239 |
foreach ($allFormIntegrations as $integration) { |
| 240 |
if (!is_null($integration->integration_type) && $integration->integration_type === ($captchaSettings ? 'gReCaptcha' : 'gReCaptchaV3')) { |
| 241 |
$integrationDetails = json_decode($integration->integration_details); |
| 242 |
$integrationDetails->id = $integration->id; |
| 243 |
$reCAPTCHA = $integrationDetails; |
| 244 |
} |
| 245 |
} |
| 246 |
} |
| 247 |
if (!empty($reCAPTCHA->secretKey)) { |
| 248 |
$gRecaptchaResponse = HttpHelper::post( |
| 249 |
'https://www.google.com/recaptcha/api/siteverify', |
| 250 |
['secret' => $reCAPTCHA->secretKey, 'response' => $token] |
| 251 |
); |
| 252 |
$isgReCaptchaVerified = false; |
| 253 |
if (!is_wp_error($gRecaptchaResponse)) { |
| 254 |
if ( |
| 255 |
$captchaV3Settings |
| 256 |
&& !empty($gRecaptchaResponse->score) |
| 257 |
&& ((float) $gRecaptchaResponse->score < (float) $captchaV3Settings->score) |
| 258 |
) { |
| 259 |
wp_send_json_error( |
| 260 |
__( |
| 261 |
$captchaV3Settings->message, |
| 262 |
'bit-form' |
| 263 |
) |
| 264 |
); |
| 265 |
} |
| 266 |
|
| 267 |
$isgReCaptchaVerified = $gRecaptchaResponse->success; |
| 268 |
} |
| 269 |
if (!$isgReCaptchaVerified) { |
| 270 |
return new WP_Error('spam_detection', __('Please verify reCAPTCHA', 'bit-form')); |
| 271 |
} |
| 272 |
} |
| 273 |
} |
| 274 |
$existAuth = (new IntegrationHandler($this->_form_id))->getAllIntegration('wp_user_auth', 'wp_auth', 1); |
| 275 |
if (!is_wp_error($existAuth) && count($existAuth) > 0 && is_user_logged_in()) { |
| 276 |
return new WP_Error('auth_error', __('You are already logged in', 'bit-form')); |
| 277 |
} |
| 278 |
$validateForm = $this->validateFormSubmission($_POST); |
| 279 |
$form_fields = $this->getFields(); |
| 280 |
$formFieldValidator = new FormFieldValidator($form_fields, $_POST, $_FILES); |
| 281 |
$validUniuqFields = []; |
| 282 |
|
| 283 |
$existFilter = has_filter('bf_check_duplicate_entry'); |
| 284 |
if ($existFilter === true) { |
| 285 |
$validUniuqFields = apply_filters('bf_check_duplicate_entry', $form_fields, $_POST); |
| 286 |
} |
| 287 |
|
| 288 |
$validateField = $formFieldValidator->validate('create', $this->_form_id); |
| 289 |
if ($validateForm && $validateField && count($validUniuqFields) == 0) { |
| 290 |
|
| 291 |
return true; |
| 292 |
} else { |
| 293 |
$error = __('Please submit form with valid fields', 'bit-form'); |
| 294 |
if (!$validateForm) { |
| 295 |
$errorMessages = $error; |
| 296 |
} else if (count($formFieldValidator->getMessage()) > 0) { |
| 297 |
$errorMessages = $formFieldValidator->getMessage(); |
| 298 |
} else { |
| 299 |
$errorMessages = count($validUniuqFields) == 0 ? $error : $validUniuqFields; |
| 300 |
} |
| 301 |
return new WP_Error('validation_error', $errorMessages); |
| 302 |
} |
| 303 |
} |
| 304 |
return new WP_Error('unknown_form', __('Form does not exist', 'bit-form')); |
| 305 |
} else { |
| 306 |
return new WP_Error('token_expired', __('Token expired', 'bit-form')); |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
public function verifySubmissionNonce() |
| 311 |
{ |
| 312 |
if (!isset($_POST['bitforms_token'])) { |
| 313 |
return false; |
| 314 |
} |
| 315 |
$token = sanitize_text_field($_POST['bitforms_token']); |
| 316 |
unset($_POST['_ajax_nonce'], $_POST['action'], $_POST['bitforms_id'], $_POST['bitforms_token']); |
| 317 |
if (!is_user_logged_in()) { |
| 318 |
return true; |
| 319 |
} |
| 320 |
return wp_verify_nonce($token, "bitforms_{$this->form_id}"); |
| 321 |
} |
| 322 |
|
| 323 |
public function setViewCount() |
| 324 |
{ |
| 325 |
if (!current_user_can('manage_options')) { |
| 326 |
$update_status = $this->formModel->update( |
| 327 |
array( |
| 328 |
'views' => intval(static::$form[0]->views) + 1 |
| 329 |
), |
| 330 |
array( |
| 331 |
'id' => $this->form_id |
| 332 |
) |
| 333 |
); |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
public function checkSubmissionRestriction() |
| 338 |
{ |
| 339 |
$formContents = $this->getFormContent(); |
| 340 |
$fromRestrictionSetitingsEnabled = empty($formContents->additional->enabled) ? [] : $formContents->additional->enabled; |
| 341 |
$fromRestrictionSetitings = empty($formContents->additional->settings) ? null : $formContents->additional->settings; |
| 342 |
if (is_null($formContents->additional->enabled) || is_null($formContents->additional->settings)) { |
| 343 |
return false; |
| 344 |
} |
| 345 |
$restrictionMessage = array(); |
| 346 |
$ipTool = new IpTool(); |
| 347 |
$ipAddress = $ipTool->getIP(); |
| 348 |
foreach ($fromRestrictionSetitingsEnabled as $restrictionKey => $isEnabled) { |
| 349 |
if ($isEnabled) { |
| 350 |
if ($restrictionKey === 'entry_limit' && isset($fromRestrictionSetitings->{$restrictionKey})) { |
| 351 |
$formEntry = new FormEntryModel(); |
| 352 |
$countResult = $formEntry->count( |
| 353 |
array( |
| 354 |
'form_id' => $this->form_id |
| 355 |
) |
| 356 |
); |
| 357 |
$count = !empty($countResult[0]) && !empty($countResult[0]->count) ? $countResult[0]->count : false; |
| 358 |
if ($count && $count >= intval($fromRestrictionSetitings->{$restrictionKey})) { |
| 359 |
$restrictionMessage[] = __('Sorry!! Entry limit exceeded', "bit-form"); |
| 360 |
} |
| 361 |
} |
| 362 |
if ($restrictionKey === 'onePerIp') { |
| 363 |
$formEntry = new FormEntryModel(); |
| 364 |
$countResult = $formEntry->count( |
| 365 |
array( |
| 366 |
'form_id' => $this->form_id, |
| 367 |
'user_ip' => ip2long($ipAddress) |
| 368 |
) |
| 369 |
); |
| 370 |
$count = !empty($countResult[0]) && !empty($countResult[0]->count) ? $countResult[0]->count : false; |
| 371 |
|
| 372 |
if ($count && $count > 0) { |
| 373 |
$restrictionMessage[] = __('Sorry!! You have already submitted', "bit-form"); |
| 374 |
} |
| 375 |
} |
| 376 |
if ($restrictionKey === 'is_login' && get_current_user_id() === 0) { |
| 377 |
$restrictionMessage[] = __($fromRestrictionSetitings->is_login->message, "bit-form"); |
| 378 |
} |
| 379 |
if ($restrictionKey === 'empty_submission') { |
| 380 |
$isEmpty = $this->checkEmptySubmission($_POST, $_FILES); |
| 381 |
if ($isEmpty) { |
| 382 |
$restrictionMessage[] = __($fromRestrictionSetitings->empty_submission->message, "bit-form"); |
| 383 |
} |
| 384 |
} |
| 385 |
if ($restrictionKey === 'restrict_form' && isset($fromRestrictionSetitings->{$restrictionKey})) { |
| 386 |
$day = empty($fromRestrictionSetitings->{$restrictionKey}->day) ? null : $fromRestrictionSetitings->{$restrictionKey}->day; |
| 387 |
$date = empty($fromRestrictionSetitings->{$restrictionKey}->date) ? null : $fromRestrictionSetitings->{$restrictionKey}->date; |
| 388 |
$time = empty($fromRestrictionSetitings->{$restrictionKey}->time) ? null : $fromRestrictionSetitings->{$restrictionKey}->time; |
| 389 |
|
| 390 |
$isdayOk = $isdateOk = $istimeOk = true; |
| 391 |
$dayNotOkMsg = $dateNotOkMsg = $timeNotOkMsg = ''; |
| 392 |
$dateTimeHelper = new DateTimeHelper(); |
| 393 |
if ( |
| 394 |
!empty($day) |
| 395 |
&& is_array($day) |
| 396 |
&& (in_array("Friday", $day) |
| 397 |
|| in_array("Saturday", $day) |
| 398 |
|| in_array("Sunday", $day) |
| 399 |
|| in_array("Monday", $day) |
| 400 |
|| in_array("Tuesday", $day) |
| 401 |
|| in_array("Wednesday", $day) |
| 402 |
|| in_array("Thursday", $day)) |
| 403 |
&& (!in_array($dateTimeHelper->getDay('full-name'), $day)) |
| 404 |
) { |
| 405 |
$isdayOk = false; |
| 406 |
$dayMsgVarsFormat = ''; |
| 407 |
foreach ($day as $dayIndex => $dayValue) { |
| 408 |
if ($dayIndex > 0) { |
| 409 |
$dayMsgVarsFormat .= ', '; |
| 410 |
} |
| 411 |
$dayMsgVarsFormat .= '%s'; |
| 412 |
} |
| 413 |
$dayNotOkMsg = vsprintf(__("in $dayMsgVarsFormat", 'bit-form'), $day); |
| 414 |
} |
| 415 |
if ( |
| 416 |
!empty($day) |
| 417 |
&& is_array($day) |
| 418 |
&& (in_array("Custom", $day)) |
| 419 |
) { |
| 420 |
$startDate = empty($date->from) ? '00-00-0000' : $date->from; |
| 421 |
$endDate = empty($date->to) ? '00-00-0000' : $date->to; |
| 422 |
if (!empty($date->from) && strpos($startDate, 'T') !== false) { |
| 423 |
$startDate = $dateTimeHelper->getDate($startDate, false, null, 'm-d-Y'); |
| 424 |
} |
| 425 |
if (!empty($date->to) && strpos($endDate, 'T') !== false) { |
| 426 |
$endDate = $dateTimeHelper->getDate($endDate, false, null, 'm-d-Y'); |
| 427 |
} |
| 428 |
$currentDate = $dateTimeHelper->getDate(null, null, null, 'm-d-Y'); |
| 429 |
if (!($currentDate >= $startDate && $currentDate <= $endDate)) { |
| 430 |
$isdateOk = false; |
| 431 |
$dateNotOkMsg = sprintf(__("within %s to %s", 'bit-form'), $startDate, $endDate); |
| 432 |
} |
| 433 |
} |
| 434 |
|
| 435 |
if (!empty($time)) { |
| 436 |
$startTime = empty($time->from) ? '00:00' : $time->from; |
| 437 |
$endTime = empty($time->to) ? '23:59.999' : $time->to; |
| 438 |
$currentTime = $dateTimeHelper->getTime(null, null, null, 'H:i'); |
| 439 |
if (!($currentTime >= $startTime && $currentTime <= $endTime)) { |
| 440 |
$istimeOk = false; |
| 441 |
$startTime = $dateTimeHelper->getTime($startTime, 'H:i', null); |
| 442 |
$endTime = $dateTimeHelper->getTime($endTime, 'H:i', null); |
| 443 |
$isTimeOk = false; |
| 444 |
$timeNotOkMsg = sprintf(__("%s to %s", 'bit-form'), $startTime, $endTime); |
| 445 |
} |
| 446 |
} |
| 447 |
|
| 448 |
if (!($isdateOk && $isdayOk && $istimeOk)) { |
| 449 |
if (!$isdayOk) { |
| 450 |
$restrictionMessage[] = !empty($timeNotOkMsg) ? sprintf(__("Form is available %s From %s", 'bit-form'), $dayNotOkMsg, $timeNotOkMsg) : |
| 451 |
sprintf(__("Form is available %s", 'bit-form'), $dayNotOkMsg, $timeNotOkMsg); |
| 452 |
} elseif (!$isdateOk) { |
| 453 |
$restrictionMessage[] = !empty($timeNotOkMsg) ? sprintf(__("Form is available %s From %s", 'bit-form'), $dateNotOkMsg, $timeNotOkMsg) : |
| 454 |
sprintf(__("Form is available %s", 'bit-form'), $dateNotOkMsg, $timeNotOkMsg); |
| 455 |
} elseif (!$istimeOk) { |
| 456 |
$restrictionMessage[] = sprintf(__("Form is available on %s", 'bit-form'), $timeNotOkMsg); |
| 457 |
} |
| 458 |
} |
| 459 |
} |
| 460 |
if ($restrictionKey === 'blocked_ip' && isset($fromRestrictionSetitings->{$restrictionKey})) { |
| 461 |
$isIpBlocked = false; |
| 462 |
foreach ($fromRestrictionSetitings->{$restrictionKey} as $ipIndex => $ipDetails) { |
| 463 |
if (!empty($ipDetails->status) && $ipDetails->status && !empty($ipDetails->ip) && $ipDetails->ip === $ipAddress) { |
| 464 |
$isIpBlocked = true; |
| 465 |
break; |
| 466 |
} |
| 467 |
} |
| 468 |
if ($isIpBlocked) { |
| 469 |
$restrictionMessage[] = sprintf(__("Sorry!! Your IP address is %s, Blocked from submitting the form", 'bit-form'), $ipAddress); |
| 470 |
} |
| 471 |
} |
| 472 |
if ($restrictionKey === 'private_ip' && isset($fromRestrictionSetitings->{$restrictionKey})) { |
| 473 |
$isIpWhiteListed = false; |
| 474 |
foreach ($fromRestrictionSetitings->{$restrictionKey} as $ipIndex => $ipDetails) { |
| 475 |
if (!empty($ipDetails->status) && $ipDetails->status && !empty($ipDetails->ip) && $ipDetails->ip === $ipAddress) { |
| 476 |
$isIpWhiteListed = true; |
| 477 |
break; |
| 478 |
} |
| 479 |
} |
| 480 |
if (!$isIpWhiteListed) { |
| 481 |
$restrictionMessage[] = sprintf(__("Sorry!! Your IP address is %s, Blocked from submitting the form", 'bit-form'), $ipAddress); |
| 482 |
} |
| 483 |
} |
| 484 |
} |
| 485 |
} |
| 486 |
return $restrictionMessage; |
| 487 |
} |
| 488 |
|
| 489 |
public function honeypotTrap() |
| 490 |
{ |
| 491 |
if ($this->isHoneypotActive()) { |
| 492 |
$time = \time(); |
| 493 |
$token = base64_encode(base64_encode($time . "." . wp_hash(wp_get_session_token() . $time))); |
| 494 |
$script = "document.addEventListener('DOMContentLoaded',(event)=>{ let frm=document.getElementById('form-{$this->_form_identifier}'),token=document.createElement('input');token.type='hidden',token.name='token',token.value='$token',frm.prepend(token);let nam=document.createElement('input');nam.type='text',nam.className='vis-n',nam.name='{$token}.name',frm.prepend(nam);let em=document.createElement('input');em.type='email',em.className='vis-n',em.name='{$token}.email',frm.prepend(em);let msg=document.createElement('textarea');msg.className='vis-n',msg.name='{$token}.message',frm.prepend(msg);})"; |
| 495 |
wp_add_inline_script('bitforms-frontend-script', $script, 'after'); |
| 496 |
} |
| 497 |
return; |
| 498 |
} |
| 499 |
|
| 500 |
/** |
| 501 |
* Will check if form is submitted by a bot |
| 502 |
* |
| 503 |
* @return Boolean true - if submitted by bot else false |
| 504 |
*/ |
| 505 |
public function isTrappedInHoneypot() |
| 506 |
{ |
| 507 |
if (!$this->isHoneypotActive()) { |
| 508 |
return false; |
| 509 |
} |
| 510 |
|
| 511 |
if (empty($_POST['token'])) { |
| 512 |
return true; |
| 513 |
} else { |
| 514 |
$token = $_POST['token']; |
| 515 |
} |
| 516 |
|
| 517 |
$dtoken = explode('.', base64_decode(base64_decode($token)))[1]; |
| 518 |
$time = explode('.', base64_decode(base64_decode($token)))[0]; |
| 519 |
|
| 520 |
if (time() - $time < 6 || hash_equals($dtoken, wp_hash(wp_get_session_token() . $time)) === false) { |
| 521 |
return true; |
| 522 |
} |
| 523 |
if ( |
| 524 |
!empty($_POST[$token . '.name']) |
| 525 |
|| !empty($_POST[$token . '_name']) |
| 526 |
|| !empty($_POST[$token . '.email']) |
| 527 |
|| !empty($_POST[$token . '_email']) |
| 528 |
|| !empty($_POST[$token . '.message']) |
| 529 |
|| !empty($_POST[$token . '_message']) |
| 530 |
) { |
| 531 |
return true; |
| 532 |
} |
| 533 |
unset($_POST['token'], $_POST[$token . 'name'], $_POST[$token . 'email'], $_POST[$token . 'message']); |
| 534 |
return false; |
| 535 |
} |
| 536 |
|
| 537 |
public function isHoneypotActive() |
| 538 |
{ |
| 539 |
$formContents = $this->getFormContent(); |
| 540 |
$enabled = empty($formContents->additional->enabled) ? null : $formContents->additional->enabled; |
| 541 |
if (!empty($enabled->honeypot) && $enabled->honeypot) { |
| 542 |
return true; |
| 543 |
} |
| 544 |
return false; |
| 545 |
} |
| 546 |
|
| 547 |
public function checkPaymentFields() |
| 548 |
{ |
| 549 |
$formContents = $this->getFormContent(); |
| 550 |
$fields = $formContents->fields; |
| 551 |
|
| 552 |
$payments = []; |
| 553 |
foreach ($fields as $fldData) { |
| 554 |
if ($fldData->typ === 'paypal' && property_exists($fldData, 'payIntegID')) { |
| 555 |
$payments['paypalKey'] = $this->getClientKey($fldData->payIntegID, 'clientID'); |
| 556 |
} else if ($fldData->typ === 'razorpay' && property_exists($fldData->options, 'payIntegID')) { |
| 557 |
$payments['razorpayKey'] = $this->getClientKey($fldData->options->payIntegID, 'apiKey'); |
| 558 |
} |
| 559 |
} |
| 560 |
|
| 561 |
return $payments; |
| 562 |
} |
| 563 |
|
| 564 |
private function getClientKey($integID, $keyName) |
| 565 |
{ |
| 566 |
$client = ''; |
| 567 |
if (!empty($integID)) { |
| 568 |
$integrationHandler = new IntegrationHandler(0); |
| 569 |
$integration = $integrationHandler->getAIntegration($integID, 'app', 'payments'); |
| 570 |
if (!is_wp_error($integration)) { |
| 571 |
$integration_details = json_decode($integration[0]->integration_details); |
| 572 |
$client = base64_encode($integration_details->{$keyName}); |
| 573 |
} |
| 574 |
} |
| 575 |
return $client; |
| 576 |
} |
| 577 |
} |
| 578 |
|