PluginProbe
weForms – Easy Drag & Drop Contact Form Builder For WordPress / 1.6.28
weForms – Easy Drag & Drop Contact Form Builder For WordPress v1.6.28
1.6.7 1.6.8 1.6.9 1.6.12 1.6.13 1.6.14 1.6.15 1.6.16 1.6.17 1.6.18 1.6.19 1.6.2 1.6.20 1.6.21 1.6.22 1.6.23 1.6.24 1.6.25 1.6.26 1.6.27 1.6.28 1.6.3 1.6.4 1.6.5 1.6.6 All 74 releases
weforms / includes / class-form-entry.php

class-form-entry.php in weForms – Easy Drag & Drop Contact Form Builder For WordPress 1.6.28, at includes/class-form-entry.php

490 lines 19.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Single Form Entry Class
5 *
6 * @since 1.1.0
7 */
8 class WeForms_Form_Entry {
9
10 /**
11 * The form object
12 *
13 * @var \WeForms_Form
14 */
15 private $form;
16
17 /**
18 * Entry id
19 *
20 * @var int
21 */
22 public $id = 0;
23
24 /**
25 * The form id
26 *
27 * @var int
28 */
29 public $form_id = 0;
30
31 /**
32 * The user id
33 *
34 * @var int
35 */
36 public $user_id = 0;
37
38 /**
39 * IP Address
40 *
41 * @var string
42 */
43 public $ip_address = '127.0.0.1';
44
45 /**
46 * The device of the user
47 *
48 * @var string
49 */
50 public $device = '';
51
52 /**
53 * Referer URL
54 *
55 * @var string
56 */
57 public $referer = '';
58
59 /**
60 * Entry creation date
61 *
62 * @var string
63 */
64 public $created = '0000-00-00 00:00:00';
65
66 /**
67 * Form fields
68 *
69 * @var array
70 */
71 public $fields = [];
72
73 /**
74 * Form fields raw data
75 *
76 * @var array
77 */
78 public $raw_fields = [];
79
80 /**
81 * The constructor
82 *
83 * @param int $entry_id
84 * @param \WeForms_Form $form
85 */
86 public function __construct( $entry_id, $form ) {
87 $this->id = $entry_id;
88 $this->form = $form;
89
90 $this->populate_entry_data();
91 }
92
93 /**
94 * Populate the class with data
95 *
96 * @TODO: Abstract this
97 *
98 * @return void
99 */
100 public function populate_entry_data() {
101 global $wpdb;
102
103 // return if we populated the already, ensures single db call
104 if ( $this->form_id ) {
105 return;
106 }
107
108 $grid_css_added = false;
109 $grid_css = '<style>.wpufTable {display: table; width: 100%; } .wpufTableRow {display: table-row; } .wpufTableRow:nth-child(even) {background-color: #f5f5f5; } .wpufTableHeading {background-color: #eee; display: table-header-group; font-weight: bold; } .wpufTableCell, .wpufTableHead {border: none; display: table-cell; padding: 3px 10px; } .wpufTableFoot {background-color: #eee; display: table-footer-group; font-weight: bold; } .wpufTableBody {display: table-row-group; }</style>';
110
111 // Custom allowlist for grid field HTML: wp_kses_post() strips <style> and <input>,
112 // but all dynamic values are already escaped (esc_html/esc_attr) at construction time.
113 $grid_kses_allowed = array(
114 'style' => array(),
115 'div' => array( 'class' => true ),
116 'label' => array( 'class' => true ),
117 'input' => array(
118 'name' => true,
119 'class' => true,
120 'type' => true,
121 'value' => true,
122 'checked' => true,
123 'disabled' => true,
124 ),
125 );
126
127 $values = [];
128
129 $query = $wpdb->prepare(
130 "
131 SELECT * FROM {$wpdb->weforms_entries} as entry
132 LEFT JOIN {$wpdb->weforms_entrymeta} AS meta ON entry.id = meta.weforms_entry_id
133 WHERE entry.id = %d
134 ",
135 $this->id
136 );
137
138 $results = $wpdb->get_results( $query );
139
140 if ( $results ) {
141 $first_row = reset( $results );
142
143 $this->form_id = (int) $first_row->form_id;
144 $this->user_id = (int) $first_row->user_id;
145 $this->ip_address = long2ip( $first_row->user_ip );
146 $this->device = $first_row->user_device;
147 $this->referer = $first_row->referer;
148 $this->created = $first_row->created_at;
149
150 $this->fields = $this->form->get_field_values();
151 $this->raw_fields = $this->fields;
152
153 foreach ( $results as $result ) {
154 if ( array_key_exists( $result->meta_key, $this->fields ) ) {
155 $field = $this->fields[ $result->meta_key ];
156 $value = $result->meta_value;
157
158 $this->raw_fields[ $result->meta_key ]['value'] = $value;
159
160 if ( $field['type'] == 'textarea_field' ) {
161 $value = wp_kses_post( weforms_format_text( $value ) );
162 } elseif ( $field['type'] == 'name_field' ) {
163 $value = implode( ' ', explode( WeForms::$field_separator, $value ) );
164 } elseif ( in_array( $field['type'], [ 'dropdown_field', 'radio_field' ] ) ) {
165 if ( isset( $field['options'] ) && $field['options'] ) {
166 if ( isset( $field['options'][ $value ] ) ) {
167 $value = $field['options'][ $value ];
168 }
169 }
170 } elseif ( in_array( $field['type'], [ 'multiple_select', 'checkbox_field' ] ) ) {
171 $value = explode( WeForms::$field_separator, $value );
172 $temp_value = $value;
173
174 if ( is_array( $value ) && $value ) {
175 $new_array = [];
176
177 foreach ( $value as $option_key ) {
178 if ( is_array( $field['options'] ) && array_key_exists( $option_key, $field['options'] ) ) {
179 $new_array[] = $field['options'][ $option_key ];
180 } else {
181 $new_array[] = $option_key;
182 }
183 }
184
185 $value = $new_array;
186 }
187 } elseif ( in_array( $field['type'], [ 'image_upload', 'file_upload' ] ) ) {
188 $file_field = '';
189 // Security fix: Prevent PHP Object Injection by restricting allowed classes
190 $value = is_serialized( $value )
191 ? @unserialize( $value, [ 'allowed_classes' => false ] )
192 : $value;
193
194 if ( is_array( $value ) && $value ) {
195 foreach ( $value as $attachment_id ) {
196 if ( $field['type'] == 'image_upload' ) {
197 $thumb = wp_get_attachment_image( $attachment_id, 'thumbnail' );
198 } else {
199 $thumb = esc_html( get_post_field( 'post_title', $attachment_id ) );
200 }
201
202 $full_size = esc_url( wp_get_attachment_url( $attachment_id ) );
203
204 $file_field .= sprintf( '<a href="%s" target="_blank" rel="noopener noreferrer">%s</a> ', $full_size, $thumb );
205 }
206 }
207
208 $value = wp_kses_post( $file_field );
209 } elseif ( $field['type'] == 'google_map' ) {
210 list( $address, $lat, $long ) = explode( '||', $value );
211
212 $value = [
213 'address' => $address,
214 'lat' => trim( $lat ),
215 'long' => trim( $long ),
216 ];
217 } elseif ( $field['type'] == 'multiple_product' ) {
218 // Security fix: Prevent PHP Object Injection by restricting allowed classes
219 $field_value = is_serialized( $value )
220 ? @unserialize( $value, [ 'allowed_classes' => false ] )
221 : $value;
222
223 $serialized_value = [];
224
225 if ( is_array( $field_value ) ) {
226 foreach ( $field_value as $key => $sfv ) {
227 if ( is_array( $sfv ) ) {
228 $v = [];
229
230 foreach ( $sfv as $key => $sv ) {
231 $sv = str_replace( [ '_', '-' ], ' ', $key ) . ': ' . $sv;
232 $sv = ucwords( $sv );
233 $v[] = $sv;
234 }
235
236 $serialized_value[] = implode( '<br> ', $v );
237 }
238 }
239
240 $value = wp_kses_post( implode( '<br> <br> ', $serialized_value ) );
241 }
242 } elseif ( $field['type'] == 'checkbox_grid' ) {
243 // Security fix: Prevent PHP Object Injection by restricting allowed classes
244 $entry_value = is_serialized( $value )
245 ? @unserialize( $value, [ 'allowed_classes' => false ] )
246 : $value;
247
248 if ( $entry_value ) {
249 $return = '';
250 $check = '';
251
252 if ( !$grid_css_added ) {
253 $return = $grid_css;
254 $grid_css_added = true;
255 }
256
257 $new_val = [];
258
259 foreach ( $entry_value as $key => $option_value ) {
260 $new_val[ $key ] = $option_value;
261 }
262
263 if ( $field['grid_rows'] && count( $field['grid_rows'] ) > 0 && $field['grid_columns'] && count( $field['grid_columns'] ) > 0 ) {
264 $return .= '<div class="wpufTable">
265 <div class="wpufTableHeading">
266 <div class="wpufTableRow">
267 <div class="wpufTableHead">&nbsp;</div>';
268
269 foreach ( $field['grid_columns'] as $column ) {
270 $return .= '<div class="wpufTableHead">' . esc_html( $column ) . '</div>';
271 }
272
273 $return .= '</div>
274 </div>
275 <div class="wpufTableBody">';
276
277 foreach ( $field['grid_rows'] as $row_key => $row_value ) {
278 $return .= '<div class="wpufTableRow">
279 <div class="wpufTableHead">' . esc_html( $row_value ) . '</div>';
280
281 foreach ( $field['grid_columns'] as $column_key => $column_value ) {
282 if ( isset( $new_val[ $row_key ] ) ) {
283 $check = ( in_array( $column_value, $new_val[ $row_key ] ) ) ? 'checked ' : '';
284 }
285
286 $return .= '<div class="wpufTableCell">
287 <label class="wpuf-radio-inline">
288 <input
289 name="' . $field['name'] . '[' . $row_key . '][]"
290 class="wpuf_' . $field['name'] . '_' . $this->form_id . '"
291 type="checkbox"
292 value="' . esc_attr( $column_value ) . '"'
293 . $check . 'disabled
294 />
295 </label>
296 </div>';
297 }
298
299 $return .= '</div>';
300 }
301
302 $return .= '</div>
303 </div>';
304 }
305
306 $value = wp_kses( $return, $grid_kses_allowed );
307 }
308 } elseif ( $field['type'] == 'multiple_choice_grid' ) {
309 // Security fix: Prevent PHP Object Injection by restricting allowed classes
310 $entry_value = is_serialized( $value )
311 ? @unserialize( $value, [ 'allowed_classes' => false ] )
312 : $value;
313
314 if ( $entry_value ) {
315 $return = '';
316 $check = '';
317
318 if ( !$grid_css_added ) {
319 $return = $grid_css;
320 $grid_css_added = true;
321 }
322
323 $new_val = [];
324
325 foreach ( $entry_value as $key => $option_value ) {
326 $new_val[ $key ] = $option_value;
327 }
328
329 if ( $field['grid_rows'] && count( $field['grid_rows'] ) > 0 && $field['grid_columns'] && count( $field['grid_columns'] ) > 0 ) {
330 $return .= '<div class="wpufTable">
331 <div class="wpufTableHeading">
332 <div class="wpufTableRow">
333 <div class="wpufTableHead">&nbsp;</div>';
334
335 foreach ( $field['grid_columns'] as $column ) {
336 $return .= '<div class="wpufTableHead">' . esc_html( $column ) . '</div>';
337 }
338
339 $return .= '</div>
340 </div>
341 <div class="wpufTableBody">';
342
343 foreach ( $field['grid_rows'] as $row_key => $row_value ) {
344 $return .= '<div class="wpufTableRow">
345 <div class="wpufTableHead">' . esc_html( $row_value ) . '</div>';
346
347 foreach ( $field['grid_columns'] as $column_key => $column_value ) {
348 if ( isset( $new_val[ $row_key ] ) ) {
349 $check = ( $new_val[ $row_key ] == $column_value ) ? 'checked ' : '';
350 }
351
352 $return .= '<div class="wpufTableCell">
353 <label class="wpuf-radio-inline">
354 <input
355 name="' . $field['name'] . '[' . $row_key . ']"
356 class="wpuf_' . $field['name'] . '_' . $this->form_id . '"
357 type="radio"
358 value="' . esc_attr( $column_value ) . '"'
359 . $check . 'disabled
360 />
361 </label>
362 </div>';
363 }
364
365 $return .= '</div>';
366 }
367
368 $return .= '</div>
369 </div>';
370 }
371
372 $value = wp_kses( $return, $grid_kses_allowed );
373 }
374 } elseif ( $field['type'] == 'address_field' || is_serialized( $value ) ) {
375 // Security fix: Prevent PHP Object Injection by restricting allowed classes
376 $field_value = is_serialized( $value )
377 ? @unserialize( $value, [ 'allowed_classes' => false ] )
378 : $value;
379
380 $serialized_value = [];
381
382 if ( is_array( $field_value ) ) {
383 foreach ( $field_value as $key => $sfv ) {
384 $sfv = str_replace( [ '_', '-' ], ' ', $key ) . ': ' . $sfv;
385 $sfv = ucwords( $sfv );
386 $serialized_value[] = $sfv;
387 }
388
389 $value = implode( '<br> ', $serialized_value );
390 }
391 } elseif ( $field['type'] == 'signature_field' ) {
392 if ( ! isset( $_REQUEST['action'] ) || $_REQUEST['action'] !== 'weforms_pdf_download' ) {
393 $url = esc_url( content_url() . '/' . $value );
394 $value = sprintf( '<img src="%s">', $url );
395 $value .= sprintf( '<a style="margin-left: -200px" href="%s">Download</a>', $url );
396 } else {
397 $url = esc_url( $value );
398 $value = sprintf( '<img src="%s">', $url );
399 }
400 $value = wp_kses_post( $value );
401 }
402
403 $this->fields[ $result->meta_key ]['value'] = apply_filters( 'weforms_entry_meta_field', $value, $field );
404 }
405 }
406 }
407 }
408
409 /**
410 * Get entry fields
411 *
412 * @return array
413 */
414 public function get_fields() {
415 return $this->fields;
416 }
417
418 /**
419 * Get entry fields
420 *
421 * @return array
422 */
423 public function get_raw_fields() {
424 return $this->raw_fields;
425 }
426
427 /**
428 * Get entry metadata
429 *
430 * @return array
431 */
432 public function get_metadata() {
433 return [
434 'id' => $this->id,
435 'form_id' => $this->form_id,
436 'form_title' => $this->form->get_name(),
437 'user' => $this->user_id ? get_user_by( 'id', $this->user_id )->display_name : false,
438 'ip_address' => $this->ip_address,
439 'device' => $this->device,
440 'referer' => $this->referer,
441 'created' => date_i18n( 'F j, Y g:i a', strtotime( $this->created ) ),
442 ];
443 }
444
445 /**
446 * Get entry metadata
447 *
448 * @return array
449 */
450 public function get_payment_data() {
451 global $wpdb;
452
453 if ( !class_exists( 'WeForms_Payment' ) ) {
454 return;
455 }
456
457 return $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}weforms_payments WHERE entry_id = {$this->id} " );
458 }
459
460 /**
461 * Get Form from entry id.
462 *
463 * @param int $entry_id The entry id.
464 * @global object $wpdb The Wordpress database object.
465 *
466 * @return object The form object.
467 */
468 public static function get_form( $entry_id ) {
469 $form_id = self::get_form_id( $entry_id );
470
471 return ! empty( $form_id ) ? weforms()->form->get( $form_id ) : null;
472 }
473
474 /**
475 * Get form id from entry id.
476 *
477 * @param int $entry_id The entry id.
478 * @global object $wpdb The Wordpress database object.
479 *
480 * @return int The form id.
481 */
482 public static function get_form_id( $entry_id ) {
483 global $wpdb;
484
485 $results = $wpdb->get_results( $wpdb->prepare( "SELECT form_id FROM {$wpdb->prefix}weforms_entries WHERE id = %d ", $entry_id ) );
486
487 return ! empty( $results[0]->form_id ) ? $results[0]->form_id : null;
488 }
489 }
490