PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 6.2.14
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v6.2.14
6.2.14 6.2.13 6.2.12 6.2.10 6.2.11 6.2.9 6.2.8 6.2.7 6.2.6 6.2.5 6.2.4 6.2.3 6.2.2 3.6.22 3.6.31 3.6.40 3.6.41 3.6.42 3.6.50 3.6.51 3.6.60 3.6.61 3.6.62 3.6.64 3.6.65 All 196 releases
fluentform / app / Modules / Form / FormDataParser.php

FormDataParser.php in Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder 6.2.14, at app/Modules/Form/FormDataParser.php

378 lines 12.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentForm\App\Modules\Form;
4
5 use FluentForm\Framework\Helpers\ArrayHelper;
6
7 class FormDataParser
8 {
9 protected static $data = null;
10 protected static $submissionId = null;
11
12 public static function parseFormEntries($entries, $form, $fields = null)
13 {
14 $fields = $fields ? $fields : FormFieldsParser::getEntryInputs($form);
15
16 foreach ($entries as $entry) {
17 static::parseFormEntry($entry, $form, $fields);
18 }
19
20 return $entries;
21 }
22
23 public static function parseFormEntry($entry, $form, $fields = null, $isHtml = false)
24 {
25 $fields = $fields ? $fields : FormFieldsParser::getEntryInputs($form);
26
27 $entry->user_inputs = static::parseData(
28 json_decode($entry->response),
29 $fields,
30 $form->id,
31 $isHtml
32 );
33
34 return $entry;
35 }
36
37 public static function parseFormSubmission($submission, $form, $fields, $isHtml = false)
38 {
39 // Sometimes submission will change inside loop. So we need to parse submission data for new one
40 $newSubmission = $submission->id != static::$submissionId;
41
42 if (is_null(static::$data) || $newSubmission) {
43 static::$data = static::parseData(
44 json_decode($submission->response),
45 $fields,
46 $form->id,
47 $isHtml
48 );
49 static::$submissionId = $submission->id;
50 }
51
52 $submission->user_inputs = static::$data;
53
54 return $submission;
55 }
56
57 public static function parseData($response, $fields, $formId, $isHtml = false)
58 {
59 $trans = [];
60 foreach ($fields as $field_key => $field) {
61 if (isset($response->{$field_key})) {
62 $value = $response->{$field_key};
63
64 $value = apply_filters_deprecated(
65 'fluentform_response_render_' . $field['element'],
66 [
67 $value,
68 $field,
69 $formId,
70 $isHtml
71 ],
72 FLUENTFORM_FRAMEWORK_UPGRADE,
73 'fluentform/response_render_' . $field['element'],
74 'Use fluentform/response_render_' . $field['element'] . ' instead of fluentform_response_render_' . $field['element']
75 );
76
77 $value = apply_filters(
78 'fluentform/response_render_' . $field['element'],
79 $value,
80 $field,
81 $formId,
82 $isHtml
83 );
84 $trans[$field_key] = $value;
85 } else {
86 $trans[$field_key] = '';
87 }
88 }
89
90 return $trans;
91 }
92
93 public static function formatValue($value)
94 {
95 if (is_array($value) || is_object($value)) {
96 return fluentImplodeRecursive(', ', array_filter(array_values((array) $value)));
97 }
98
99 return $value;
100 }
101
102 public static function formatFileValues($values, $isHtml, $form_id = null)
103 {
104 if (!$values) {
105 return $values;
106 }
107
108 if (is_string($values)) {
109 return $values;
110 }
111
112 if (!$isHtml) {
113 return fluentImplodeRecursive(', ', array_filter(array_values((array) $values)));
114 }
115 if ($form_id && \FluentForm\App\Helpers\Helper::isEntryAutoDeleteEnabled($form_id)) {
116 return '';
117 }
118
119 $html = '<ul class="ff_entry_list">';
120 foreach ($values as $value) {
121 if (!$value) {
122 continue;
123 }
124 // SECURITY (FINDING-23): escape the submitted upload value. It reaches this HTML sink
125 // via an unauthenticated submission and is only sanitize_text_field'd (keeps " and :),
126 // so a javascript: URL or an " onmouseover=" attribute breakout would otherwise render
127 // in the admin entry view, notification email and PDF. esc_url enforces a safe scheme.
128 $html .= '<li><a href="' . esc_url($value) . '" target="_blank">' . esc_html(basename($value)) . '</a></li>';
129 }
130
131 $html .= '</ul>';
132 return $html;
133 }
134
135 public static function formatImageValues($values, $isHtml, $form_id = null)
136 {
137 if (!$values) {
138 return $values;
139 }
140
141 if (is_string($values)) {
142 return $values;
143 }
144
145 $isHtml = apply_filters('fluentform/render_field_as_html', $isHtml, $values, $form_id);
146
147
148 if (!$isHtml) {
149 return fluentImplodeRecursive(', ', array_filter(array_values((array) $values)));
150 }
151 if ($form_id && \FluentForm\App\Helpers\Helper::isEntryAutoDeleteEnabled($form_id)) {
152 return '';
153 }
154 if (1 == count($values)) {
155 $value = $values[0];
156 if (!$value) {
157 return '';
158 }
159 // SECURITY (FINDING-23): escape the submitted upload value (see formatFileValues).
160 return '<a href="' . esc_url($value) . '" target="_blank"><img style="max-width:180px" src="' . esc_url($value) . '" /></a>';
161 }
162
163 $html = '<ul class="ff_entry_list ff_entry_images">';
164 foreach ($values as $value) {
165 if (!$value) {
166 continue;
167 }
168 // SECURITY (FINDING-23): escape the submitted upload value (see formatFileValues).
169 $html .= '<li style="margin: 20px 20px 20px 0px; display: inline-block; margin-right: 20px;"><a href="' . esc_url($value) . '" target="_blank"><img style="max-width:180px" src="' . esc_url($value) . '" /></a></li>';
170 }
171
172 $html .= '</ul>';
173 return $html;
174 }
175
176 public static function formatRepeatFieldValue($value, $field, $form_id)
177 {
178
179 if (defined('FLUENTFORM_RENDERING_ENTRIES')) {
180 return __('....', 'fluentform');
181 }
182
183 if (is_string($value)) {
184 return $value;
185 }
186
187 try {
188 $repeatColumns = ArrayHelper::get($field, 'raw.fields');
189 $rows = count($value[0]);
190 $columns = count($value);
191
192 ob_start();
193 if ($repeatColumns) {
194 ?>
195 <div class="ff_entry_table_wrapper">
196 <table class="ff_entry_table_field ff-table">
197 <thead>
198 <tr>
199 <?php foreach ($repeatColumns as $repeatColumn) : ?>
200 <th><?php echo fluentform_sanitize_html(ArrayHelper::get($repeatColumn, 'settings.label')); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- fluentform_sanitize_html() removes XSS vectors and uses wp_kses() with allowed tags ?>
201 </th>
202 <?php endforeach; ?>
203 </tr>
204 </thead>
205
206 <tbody>
207 <?php for ($i = 0; $i < $rows; $i++) : ?>
208 <tr>
209 <?php for ($j = 0; $j < $columns; $j++) : ?>
210 <td>
211 <?php echo fluentform_sanitize_html($value[$j][$i]); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- fluentform_sanitize_html() removes XSS vectors and uses wp_kses() with allowed tags ?>
212 </td>
213 <?php endfor; ?>
214 </tr>
215 <?php endfor; ?>
216 </tbody>
217 </table>
218 </div>
219 <?php
220 }
221 return ob_get_clean();
222 } catch (\Exception $e) {
223 }
224
225 return $value;
226 }
227
228 public static function formatTabularGridFieldValue($value, $field, $form_id, $isHtml = false)
229 {
230 if (defined('FLUENTFORM_RENDERING_ENTRIES')) {
231 return __('....', 'fluentform');
232 }
233
234 if (is_string($value)) {
235 return $value;
236 }
237
238 if (is_array($value)) {
239 $value = (object) $value;
240 }
241 try {
242 if (empty($field['raw'])) {
243 return $value;
244 }
245 $columnLabels = $field['raw']['settings']['grid_columns'];
246 $fieldType = $field['raw']['settings']['tabular_field_type'];
247 $columnHeaders = implode('</th><th style="text-align: center;">', array_values($columnLabels));
248
249 $elMarkup = "<table class='ff-table'><thead><tr><th></th><th style='text-align: center;'>{$columnHeaders}</th></tr></thead><tbody>";
250
251 foreach (static::makeTabularData($field['raw']) as $row) {
252 $elMarkup .= '<tr>';
253 $elMarkup .= "<td>{$row['label']}</td>";
254 foreach ($row['columns'] as $column) {
255 $isChecked = '';
256 if ('radio' == $fieldType) {
257 if (isset($value->{$row['name']})) {
258 $isChecked = $value->{$row['name']} == $column['name'] ? 'checked' : '';
259 }
260 } else {
261 if (isset($value->{$row['name']})) {
262 $isChecked = in_array($column['name'], $value->{$row['name']}) ? 'checked' : '';
263 }
264 }
265 $icon = "<input disabled type='{$fieldType}' {$isChecked}>";
266 if ($isChecked) {
267 $icon = '';
268 }
269 $elMarkup .= "<td style='text-align: center;'>" . $icon . '</td>';
270 }
271 $elMarkup .= '</tr>';
272 }
273
274 $elMarkup .= '</tbody></table>';
275
276 return $elMarkup;
277 } catch (\Exception $e) {
278 }
279 return '';
280 }
281
282 public static function makeTabularData($data)
283 {
284 $table = [];
285 $rows = $data['settings']['grid_rows'];
286 $columns = $data['settings']['grid_columns'];
287
288 foreach ($rows as $rowKey => $rowValue) {
289 $rowKey = trim(sanitize_text_field($rowKey));
290 $table[$rowKey] = [
291 'name' => $rowKey,
292 'label' => $rowValue,
293 'columns' => [],
294 ];
295
296 foreach ($columns as $columnKey => $columnValue) {
297 $table[$rowKey]['columns'][] = [
298 'name' => trim(sanitize_text_field($columnKey)),
299 'label' => $columnValue,
300 ];
301 }
302 }
303 return $table;
304 }
305
306 /**
307 * Format input_name field value by concatenating all name fields.
308 *
309 * @param array|object $value
310 *
311 * @return string $value
312 */
313 public static function formatName($value)
314 {
315 if (is_array($value) || is_object($value)) {
316 $value = (array) $value;
317 $order = ['first_name', 'middle_name', 'last_name'];
318 uksort($value, function($a, $b) use ($order) {
319 $posA = array_search($a, $order);
320 $posB = array_search($b, $order);
321 return $posA - $posB;
322 });
323 return fluentImplodeRecursive(' ', array_filter(array_values($value)));
324 }
325
326 return $value;
327 }
328
329 public static function formatCheckBoxValues($values, $field, $isHtml = false)
330 {
331 if (!$isHtml) {
332 if (
333 defined('FLUENTFORM_RENDERING_ENTRIES') &&
334 $values && is_array($values) &&
335 $options = ArrayHelper::get($field, 'raw.settings.advanced_options', [])
336 ) {
337 $options = \FluentForm\App\Helpers\Helper::advancedOptionsValueLabelMap($options);
338 foreach ($values as &$value) {
339 if ($label = ArrayHelper::get($options, $value)) {
340 $value = $label;
341 }
342 }
343 }
344 return self::formatValue($values);
345 }
346
347 if (!is_array($values)) {
348 return $values;
349 }
350
351 if (empty($values)) {
352 return '';
353 }
354
355 if (!isset($field['options'])) {
356 $field['options'] = \FluentForm\App\Helpers\Helper::advancedOptionsValueLabelMap(
357 ArrayHelper::get($field, 'raw.settings.advanced_options', [])
358 );
359 }
360
361 $html = '<ul style="white-space: normal;">';
362 foreach ($values as $value) {
363 $item = $value;
364 if ($itemLabel = ArrayHelper::get($field, 'options.' . $item)) {
365 $item = $itemLabel;
366 }
367 $html .= '<li>' . $item . '</li>';
368 }
369
370 return $html . '</ul>';
371 }
372
373 public static function resetData()
374 {
375 static::$data = null;
376 }
377 }
378