| 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 BitCode\BitForm\Core\Util\IpTool; |
| 14 |
use BitCode\BitForm\Core\Form\FormManager; |
| 15 |
use BitCode\BitForm\Core\Util\DateTimeHelper; |
| 16 |
use BitCode\BitForm\Core\Database\FormEntryModel; |
| 17 |
use BitCode\BitForm\Frontend\Form\View\FormViewer; |
| 18 |
use BitCode\BitForm\Core\WorkFlow\WorkFlowRunHelper; |
| 19 |
|
| 20 |
final class FrontendFormManager extends FormManager |
| 21 |
{ |
| 22 |
private $_form_identifier; |
| 23 |
private $_form_token; |
| 24 |
// private $_has_upload = false; |
| 25 |
public function __construct($form_id, $shortCodeCounter = null) |
| 26 |
{ |
| 27 |
parent::__construct($form_id); |
| 28 |
$this->_form_identifier = 'bitforms_' . $form_id . '_submit_'; |
| 29 |
$this->_form_identifier .= !empty(get_post()->ID) ? get_post()->ID : ''; |
| 30 |
$this->_form_identifier .= !empty($shortCodeCounter) ? "_$shortCodeCounter" : ''; |
| 31 |
$this->_form_token = wp_create_nonce('bitforms_' . $form_id); |
| 32 |
} |
| 33 |
|
| 34 |
public function getFormIdentifier() |
| 35 |
{ |
| 36 |
return $this->_form_identifier; |
| 37 |
} |
| 38 |
|
| 39 |
public function getFormID() |
| 40 |
{ |
| 41 |
return $this->form_id; |
| 42 |
} |
| 43 |
|
| 44 |
public function getFormToken() |
| 45 |
{ |
| 46 |
return $this->_form_token; |
| 47 |
} |
| 48 |
|
| 49 |
public function isSubmitted() |
| 50 |
{ |
| 51 |
return isset($_POST[$this->_form_identifier]) ? true : false; |
| 52 |
} |
| 53 |
|
| 54 |
public function getSubmittedFields($submitted_data) |
| 55 |
{ |
| 56 |
unset($submitted_data[$this->_form_identifier]); |
| 57 |
return array_keys($submitted_data); |
| 58 |
} |
| 59 |
|
| 60 |
public function formView($fields = null, $hasFile = false, $errorMessages = null, $previousValue = null) |
| 61 |
{ |
| 62 |
$formContents = $this->getFormContent(); |
| 63 |
if (!empty($fields)) { |
| 64 |
$formContents->fields = is_string($fields) ? json_decode($fields) : $fields; |
| 65 |
} else { |
| 66 |
$workFlowRunHelper = new WorkFlowRunHelper($this->form_id); |
| 67 |
$workFlowreturnedOnLoad = $workFlowRunHelper->executeOnLoad( |
| 68 |
'create', |
| 69 |
$formContents->fields |
| 70 |
); |
| 71 |
$formContents->fields = empty($workFlowreturnedOnLoad['fields']) ? $formContents->fields : $workFlowreturnedOnLoad['fields']; |
| 72 |
} |
| 73 |
$formViewer = new FormViewer($this, $formContents, $errorMessages, $previousValue); |
| 74 |
return $formViewer->getView($hasFile); |
| 75 |
} |
| 76 |
|
| 77 |
public function validateFormSubmission($submitted_data) |
| 78 |
{ |
| 79 |
$submitted_fields = $this->getSubmittedFields($submitted_data); |
| 80 |
$form_fields = $this->getFields(); |
| 81 |
$form_fields_names = array_keys($form_fields); |
| 82 |
if ($this->isGCLIDEnabled()) { |
| 83 |
array_push($form_fields_names, 'GCLID'); |
| 84 |
} |
| 85 |
foreach ($submitted_fields as $key => $field) { |
| 86 |
if (!in_array($field, $form_fields_names)) { |
| 87 |
unset($submitted_data[$field]); |
| 88 |
} |
| 89 |
} |
| 90 |
return $submitted_data; |
| 91 |
} |
| 92 |
|
| 93 |
public function verifySubmissionNonce($submitted_data) |
| 94 |
{ |
| 95 |
if (!isset($submitted_data['bitforms_token'])) { |
| 96 |
return false; |
| 97 |
} |
| 98 |
return wp_verify_nonce(sanitize_text_field($submitted_data['bitforms_token']), "bitforms_{$this->form_id}"); |
| 99 |
} |
| 100 |
|
| 101 |
public function setViewCount() |
| 102 |
{ |
| 103 |
if (!current_user_can('manage_options')) { |
| 104 |
$update_status = $this->formModel->update( |
| 105 |
array( |
| 106 |
'views' => intval(static::$form[0]->views) + 1 |
| 107 |
), |
| 108 |
array( |
| 109 |
'id' => $this->form_id |
| 110 |
) |
| 111 |
); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
public function checkSubmissionRestriction() |
| 116 |
{ |
| 117 |
$formContents = $this->getFormContent(); |
| 118 |
$fromRestrictionSetitingsEnabled = empty($formContents->additional->enabled) ? null : $formContents->additional->enabled; |
| 119 |
$fromRestrictionSetitings = empty($formContents->additional->settings) ? null : $formContents->additional->settings; |
| 120 |
if (is_null($formContents->additional->enabled) || is_null($formContents->additional->settings)) { |
| 121 |
return false; |
| 122 |
} |
| 123 |
$restrictionMessage = array(); |
| 124 |
$ipTool = new IpTool(); |
| 125 |
$ipAddress = $ipTool->getIP(); |
| 126 |
foreach ($fromRestrictionSetitingsEnabled as $restrictionKey => $isEnabled) { |
| 127 |
if ($isEnabled) { |
| 128 |
if ($restrictionKey === 'entry_limit' && isset($fromRestrictionSetitings->{$restrictionKey})) { |
| 129 |
$formEntry = new FormEntryModel(); |
| 130 |
$countResult = $formEntry->count( |
| 131 |
array( |
| 132 |
'form_id' => $this->form_id |
| 133 |
) |
| 134 |
); |
| 135 |
$count = !empty($countResult[0]) && !empty($countResult[0]->count) ? $countResult[0]->count : false; |
| 136 |
if ($count && $count >= intval($fromRestrictionSetitings->{$restrictionKey})) { |
| 137 |
$restrictionMessage[] = __('Sorry!! Entry limit exceeded', "bitform"); |
| 138 |
} |
| 139 |
} |
| 140 |
if ($restrictionKey === 'onePerIp') { |
| 141 |
$formEntry = new FormEntryModel(); |
| 142 |
$countResult = $formEntry->count( |
| 143 |
array( |
| 144 |
'form_id' => $this->form_id, |
| 145 |
'user_ip' => ip2long($ipAddress) |
| 146 |
) |
| 147 |
); |
| 148 |
$count = !empty($countResult[0]) && !empty($countResult[0]->count) ? $countResult[0]->count : false; |
| 149 |
|
| 150 |
if ($count && $count > 0) { |
| 151 |
$restrictionMessage[] = __('Sorry!! You have already submitted', "bitform"); |
| 152 |
} |
| 153 |
} |
| 154 |
if ($restrictionKey === 'restrict_form' && isset($fromRestrictionSetitings->{$restrictionKey})) { |
| 155 |
$day = empty($fromRestrictionSetitings->{$restrictionKey}->day) ? null : $fromRestrictionSetitings->{$restrictionKey}->day; |
| 156 |
$date = empty($fromRestrictionSetitings->{$restrictionKey}->date) ? null : $fromRestrictionSetitings->{$restrictionKey}->date; |
| 157 |
$time = empty($fromRestrictionSetitings->{$restrictionKey}->time) ? null : $fromRestrictionSetitings->{$restrictionKey}->time; |
| 158 |
|
| 159 |
$isdayOk = $isdateOk = $istimeOk = true; |
| 160 |
$dayNotOkMsg = $dateNotOkMsg = $timeNotOkMsg = ''; |
| 161 |
$dateTimeHelper = new DateTimeHelper(); |
| 162 |
if ( |
| 163 |
!empty($day) |
| 164 |
&& is_array($day) |
| 165 |
&& (in_array("Friday", $day) |
| 166 |
|| in_array("Saturday", $day) |
| 167 |
|| in_array("Sunday", $day) |
| 168 |
|| in_array("Monday", $day) |
| 169 |
|| in_array("Tuesday", $day) |
| 170 |
|| in_array("Wednesday", $day) |
| 171 |
|| in_array("Thursday", $day)) |
| 172 |
&& (!in_array($dateTimeHelper->getDay('full-name'), $day)) |
| 173 |
) { |
| 174 |
$isdayOk = false; |
| 175 |
$dayMsgVarsFormat = ''; |
| 176 |
foreach ($day as $dayIndex => $dayValue) { |
| 177 |
if ($dayIndex > 0) { |
| 178 |
$dayMsgVarsFormat .= ', '; |
| 179 |
} |
| 180 |
$dayMsgVarsFormat .= '%s'; |
| 181 |
} |
| 182 |
$dayNotOkMsg = vsprintf(__("in $dayMsgVarsFormat", 'bitform'), $day); |
| 183 |
} |
| 184 |
if ( |
| 185 |
!empty($day) |
| 186 |
&& is_array($day) |
| 187 |
&& (in_array("Custom", $day)) |
| 188 |
) { |
| 189 |
$startDate = empty($date->from) ? '00-00-0000' : $date->from; |
| 190 |
$endDate = empty($date->to) ? '00-00-0000' : $date->to; |
| 191 |
if (!empty($date->from) && strpos($startDate, 'T') !== false) { |
| 192 |
$startDate = $dateTimeHelper->getDate($startDate, false, null, 'm-d-Y'); |
| 193 |
} |
| 194 |
if (!empty($date->to) && strpos($endDate, 'T') !== false) { |
| 195 |
$endDate = $dateTimeHelper->getDate($endDate, false, null, 'm-d-Y'); |
| 196 |
} |
| 197 |
$currentDate = $dateTimeHelper->getDate(null, null, null, 'm-d-Y'); |
| 198 |
if (!($currentDate >= $startDate && $currentDate <= $endDate)) { |
| 199 |
$isdateOk = false; |
| 200 |
$dateNotOkMsg = sprintf(__("within %s to %s", 'bitform'), $startDate, $endDate); |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
if (!empty($time)) { |
| 205 |
$startTime = empty($time->from) ? '00:00' : $time->from; |
| 206 |
$endTime = empty($time->to) ? '23:59.999' : $time->to; |
| 207 |
$currentTime = $dateTimeHelper->getTime(null, null, null, 'H:i'); |
| 208 |
if (!($currentTime >= $startTime && $currentTime <= $endTime)) { |
| 209 |
$istimeOk = false; |
| 210 |
$startTime = $dateTimeHelper->getTime($startTime, 'H:i', null); |
| 211 |
$endTime = $dateTimeHelper->getTime($endTime, 'H:i', null); |
| 212 |
$isTimeOk = false; |
| 213 |
$timeNotOkMsg = sprintf(__("%s to %s", 'bitform'), $startTime, $endTime); |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
if (!($isdateOk && $isdayOk && $istimeOk)) { |
| 218 |
if (!$isdayOk) { |
| 219 |
$restrictionMessage[] = !empty($timeNotOkMsg) ? sprintf(__("Form is available %s From %s", 'bitform'), $dayNotOkMsg, $timeNotOkMsg) : |
| 220 |
sprintf(__("Form is available %s", 'bitform'), $dayNotOkMsg, $timeNotOkMsg); |
| 221 |
} elseif (!$isdateOk) { |
| 222 |
$restrictionMessage[] = !empty($timeNotOkMsg) ? sprintf(__("Form is available %s From %s", 'bitform'), $dateNotOkMsg, $timeNotOkMsg) : |
| 223 |
sprintf(__("Form is available %s", 'bitform'), $dateNotOkMsg, $timeNotOkMsg); |
| 224 |
} elseif (!$istimeOk) { |
| 225 |
$restrictionMessage[] = sprintf(__("Form is available on %s", 'bitform'), $timeNotOkMsg); |
| 226 |
} |
| 227 |
} |
| 228 |
} |
| 229 |
if ($restrictionKey === 'blocked_ip' && isset($fromRestrictionSetitings->{$restrictionKey})) { |
| 230 |
$isIpBlocked = false; |
| 231 |
foreach ($fromRestrictionSetitings->{$restrictionKey} as $ipIndex => $ipDetails) { |
| 232 |
if (!empty($ipDetails->status) && $ipDetails->status && !empty($ipDetails->ip) && $ipDetails->ip === $ipAddress) { |
| 233 |
$isIpBlocked = true; |
| 234 |
break; |
| 235 |
} |
| 236 |
} |
| 237 |
if ($isIpBlocked) { |
| 238 |
$restrictionMessage[] = sprintf(__("Sorry!! Your IP address is %s, Blocked from submitting the form", 'bitform'), $ipAddress); |
| 239 |
} |
| 240 |
} |
| 241 |
if ($restrictionKey === 'private_ip' && isset($fromRestrictionSetitings->{$restrictionKey})) { |
| 242 |
$isIpWhiteListed = false; |
| 243 |
foreach ($fromRestrictionSetitings->{$restrictionKey} as $ipIndex => $ipDetails) { |
| 244 |
if (!empty($ipDetails->status) && $ipDetails->status && !empty($ipDetails->ip) && $ipDetails->ip === $ipAddress) { |
| 245 |
$isIpWhiteListed = true; |
| 246 |
break; |
| 247 |
} |
| 248 |
} |
| 249 |
if (!$isIpWhiteListed) { |
| 250 |
$restrictionMessage[] = sprintf(__("Sorry!! Your IP address is %s, Blocked from submitting the form", 'bitform'), $ipAddress); |
| 251 |
} |
| 252 |
} |
| 253 |
} |
| 254 |
} |
| 255 |
return $restrictionMessage; |
| 256 |
} |
| 257 |
} |
| 258 |
|