PluginProbe
weForms – Easy Drag & Drop Contact Form Builder For WordPress / 1.6.27
weForms – Easy Drag & Drop Contact Form Builder For WordPress v1.6.27
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.27, at includes/class-form-entry.php

475 lines 18.7 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 $values = [];
112
113 $query = $wpdb->prepare(
114 "
115 SELECT * FROM {$wpdb->weforms_entries} as entry
116 LEFT JOIN {$wpdb->weforms_entrymeta} AS meta ON entry.id = meta.weforms_entry_id
117 WHERE entry.id = %d
118 ",
119 $this->id
120 );
121
122 $results = $wpdb->get_results( $query );
123
124 if ( $results ) {
125 $first_row = reset( $results );
126
127 $this->form_id = (int) $first_row->form_id;
128 $this->user_id = (int) $first_row->user_id;
129 $this->ip_address = long2ip( $first_row->user_ip );
130 $this->device = $first_row->user_device;
131 $this->referer = $first_row->referer;
132 $this->created = $first_row->created_at;
133
134 $this->fields = $this->form->get_field_values();
135 $this->raw_fields = $this->fields;
136
137 foreach ( $results as $result ) {
138 if ( array_key_exists( $result->meta_key, $this->fields ) ) {
139 $field = $this->fields[ $result->meta_key ];
140 $value = $result->meta_value;
141
142 $this->raw_fields[ $result->meta_key ]['value'] = $value;
143
144 if ( $field['type'] == 'textarea_field' ) {
145 $value = weforms_format_text( $value );
146 } elseif ( $field['type'] == 'name_field' ) {
147 $value = implode( ' ', explode( WeForms::$field_separator, $value ) );
148 } elseif ( in_array( $field['type'], [ 'dropdown_field', 'radio_field' ] ) ) {
149 if ( isset( $field['options'] ) && $field['options'] ) {
150 if ( isset( $field['options'][ $value ] ) ) {
151 $value = $field['options'][ $value ];
152 }
153 }
154 } elseif ( in_array( $field['type'], [ 'multiple_select', 'checkbox_field' ] ) ) {
155 $value = explode( WeForms::$field_separator, $value );
156 $temp_value = $value;
157
158 if ( is_array( $value ) && $value ) {
159 $new_array = [];
160
161 foreach ( $value as $option_key ) {
162 if ( is_array( $field['options'] ) && array_key_exists( $option_key, $field['options'] ) ) {
163 $new_array[] = $field['options'][ $option_key ];
164 } else {
165 $new_array[] = $option_key;
166 }
167 }
168
169 $value = $new_array;
170 }
171 } elseif ( in_array( $field['type'], [ 'image_upload', 'file_upload' ] ) ) {
172 $file_field = '';
173 // Security fix: Prevent PHP Object Injection by restricting allowed classes
174 $value = is_serialized( $value )
175 ? @unserialize( $value, [ 'allowed_classes' => false ] )
176 : $value;
177
178 if ( is_array( $value ) && $value ) {
179 foreach ( $value as $attachment_id ) {
180 if ( $field['type'] == 'image_upload' ) {
181 $thumb = wp_get_attachment_image( $attachment_id, 'thumbnail' );
182 } else {
183 $thumb = get_post_field( 'post_title', $attachment_id );
184 }
185
186 $full_size = wp_get_attachment_url( $attachment_id );
187
188 $file_field .= sprintf( '<a href="%s" target="_blank">%s</a> ', $full_size, $thumb );
189 }
190 }
191
192 $value = $file_field;
193 } elseif ( $field['type'] == 'google_map' ) {
194 list( $address, $lat, $long ) = explode( '||', $value );
195
196 $value = [
197 'address' => $address,
198 'lat' => trim( $lat ),
199 'long' => trim( $long ),
200 ];
201 } elseif ( $field['type'] == 'multiple_product' ) {
202 // Security fix: Prevent PHP Object Injection by restricting allowed classes
203 $field_value = is_serialized( $value )
204 ? @unserialize( $value, [ 'allowed_classes' => false ] )
205 : $value;
206
207 $serialized_value = [];
208
209 if ( is_array( $field_value ) ) {
210 foreach ( $field_value as $key => $sfv ) {
211 if ( is_array( $sfv ) ) {
212 $v = [];
213
214 foreach ( $sfv as $key => $sv ) {
215 $sv = str_replace( [ '_', '-' ], ' ', $key ) . ': ' . $sv;
216 $sv = ucwords( $sv );
217 $v[] = $sv;
218 }
219
220 $serialized_value[] = implode( '<br> ', $v );
221 }
222 }
223
224 $value = implode( '<br> <br> ', $serialized_value );
225 }
226 } elseif ( $field['type'] == 'checkbox_grid' ) {
227 // Security fix: Prevent PHP Object Injection by restricting allowed classes
228 $entry_value = is_serialized( $value )
229 ? @unserialize( $value, [ 'allowed_classes' => false ] )
230 : $value;
231
232 if ( $entry_value ) {
233 $return = '';
234 $check = '';
235
236 if ( !$grid_css_added ) {
237 $return = $grid_css;
238 $grid_css_added = true;
239 }
240
241 $new_val = [];
242
243 foreach ( $entry_value as $key => $option_value ) {
244 $new_val[ $key ] = $option_value;
245 }
246
247 if ( $field['grid_rows'] && count( $field['grid_rows'] ) > 0 && $field['grid_columns'] && count( $field['grid_columns'] ) > 0 ) {
248 $return .= '<div class="wpufTable">
249 <div class="wpufTableHeading">
250 <div class="wpufTableRow">
251 <div class="wpufTableHead">&nbsp;</div>';
252
253 foreach ( $field['grid_columns'] as $column ) {
254 $return .= '<div class="wpufTableHead">' . $column . '</div>';
255 }
256
257 $return .= '</div>
258 </div>
259 <div class="wpufTableBody">';
260
261 foreach ( $field['grid_rows'] as $row_key => $row_value ) {
262 $return .= '<div class="wpufTableRow">
263 <div class="wpufTableHead">' . $row_value . '</div>';
264
265 foreach ( $field['grid_columns'] as $column_key => $column_value ) {
266 if ( isset( $new_val[ $row_key ] ) ) {
267 $check = ( in_array( $column_value, $new_val[ $row_key ] ) ) ? 'checked ' : '';
268 }
269
270 $return .= '<div class="wpufTableCell">
271 <label class="wpuf-radio-inline">
272 <input
273 name="' . $field['name'] . '[' . $row_key . '][]"
274 class="wpuf_' . $field['name'] . '_' . $this->form_id . '"
275 type="checkbox"
276 value="' . esc_attr( $column_value ) . '"'
277 . $check . 'disabled
278 />
279 </label>
280 </div>';
281 }
282
283 $return .= '</div>';
284 }
285
286 $return .= '</div>
287 </div>';
288 }
289
290 $value = $return;
291 }
292 } elseif ( $field['type'] == 'multiple_choice_grid' ) {
293 // Security fix: Prevent PHP Object Injection by restricting allowed classes
294 $entry_value = is_serialized( $value )
295 ? @unserialize( $value, [ 'allowed_classes' => false ] )
296 : $value;
297
298 if ( $entry_value ) {
299 $return = '';
300 $check = '';
301
302 if ( !$grid_css_added ) {
303 $return = $grid_css;
304 $grid_css_added = true;
305 }
306
307 $new_val = [];
308
309 foreach ( $entry_value as $key => $option_value ) {
310 $new_val[ $key ] = $option_value;
311 }
312
313 if ( $field['grid_rows'] && count( $field['grid_rows'] ) > 0 && $field['grid_columns'] && count( $field['grid_columns'] ) > 0 ) {
314 $return .= '<div class="wpufTable">
315 <div class="wpufTableHeading">
316 <div class="wpufTableRow">
317 <div class="wpufTableHead">&nbsp;</div>';
318
319 foreach ( $field['grid_columns'] as $column ) {
320 $return .= '<div class="wpufTableHead">' . $column . '</div>';
321 }
322
323 $return .= '</div>
324 </div>
325 <div class="wpufTableBody">';
326
327 foreach ( $field['grid_rows'] as $row_key => $row_value ) {
328 $return .= '<div class="wpufTableRow">
329 <div class="wpufTableHead">' . $row_value . '</div>';
330
331 foreach ( $field['grid_columns'] as $column_key => $column_value ) {
332 if ( isset( $new_val[ $row_key ] ) ) {
333 $check = ( $new_val[ $row_key ] == $column_value ) ? 'checked ' : '';
334 }
335
336 $return .= '<div class="wpufTableCell">
337 <label class="wpuf-radio-inline">
338 <input
339 name="' . $field['name'] . '[' . $row_key . ']"
340 class="wpuf_' . $field['name'] . '_' . $this->form_id . '"
341 type="radio"
342 value="' . esc_attr( $column_value ) . '"'
343 . $check . 'disabled
344 />
345 </label>
346 </div>';
347 }
348
349 $return .= '</div>';
350 }
351
352 $return .= '</div>
353 </div>';
354 }
355
356 $value = $return;
357 }
358 } elseif ( $field['type'] == 'address_field' || is_serialized( $value ) ) {
359 // Security fix: Prevent PHP Object Injection by restricting allowed classes
360 $field_value = is_serialized( $value )
361 ? @unserialize( $value, [ 'allowed_classes' => false ] )
362 : $value;
363
364 $serialized_value = [];
365
366 if ( is_array( $field_value ) ) {
367 foreach ( $field_value as $key => $sfv ) {
368 $sfv = str_replace( [ '_', '-' ], ' ', $key ) . ': ' . $sfv;
369 $sfv = ucwords( $sfv );
370 $serialized_value[] = $sfv;
371 }
372
373 $value = implode( '<br> ', $serialized_value );
374 }
375 } elseif ( $field['type'] == 'signature_field' ) {
376 $url = $value;
377
378 if ( isset( $_REQUEST['action'] ) != 'weforms_pdf_download' ) {
379 $url = content_url() . '/' . $value;
380 $value = sprintf( '<img src="%s">', $url );
381 $value .= sprintf( '<a style="margin-left: -200px" href="%s">Download</a>', $url );
382 }
383 else{
384 $value = sprintf( '<img src="%s">', $url );
385 }
386 }
387
388 $this->fields[ $result->meta_key ]['value'] = apply_filters( 'weforms_entry_meta_field', $value, $field );
389 }
390 }
391 }
392 }
393
394 /**
395 * Get entry fields
396 *
397 * @return array
398 */
399 public function get_fields() {
400 return $this->fields;
401 }
402
403 /**
404 * Get entry fields
405 *
406 * @return array
407 */
408 public function get_raw_fields() {
409 return $this->raw_fields;
410 }
411
412 /**
413 * Get entry metadata
414 *
415 * @return array
416 */
417 public function get_metadata() {
418 return [
419 'id' => $this->id,
420 'form_id' => $this->form_id,
421 'form_title' => $this->form->get_name(),
422 'user' => $this->user_id ? get_user_by( 'id', $this->user_id )->display_name : false,
423 'ip_address' => $this->ip_address,
424 'device' => $this->device,
425 'referer' => $this->referer,
426 'created' => date_i18n( 'F j, Y g:i a', strtotime( $this->created ) ),
427 ];
428 }
429
430 /**
431 * Get entry metadata
432 *
433 * @return array
434 */
435 public function get_payment_data() {
436 global $wpdb;
437
438 if ( !class_exists( 'WeForms_Payment' ) ) {
439 return;
440 }
441
442 return $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}weforms_payments WHERE entry_id = {$this->id} " );
443 }
444
445 /**
446 * Get Form from entry id.
447 *
448 * @param int $entry_id The entry id.
449 * @global object $wpdb The Wordpress database object.
450 *
451 * @return object The form object.
452 */
453 public static function get_form( $entry_id ) {
454 $form_id = self::get_form_id( $entry_id );
455
456 return ! empty( $form_id ) ? weforms()->form->get( $form_id ) : null;
457 }
458
459 /**
460 * Get form id from entry id.
461 *
462 * @param int $entry_id The entry id.
463 * @global object $wpdb The Wordpress database object.
464 *
465 * @return int The form id.
466 */
467 public static function get_form_id( $entry_id ) {
468 global $wpdb;
469
470 $results = $wpdb->get_results( $wpdb->prepare( "SELECT form_id FROM {$wpdb->prefix}weforms_entries WHERE id = %d ", $entry_id ) );
471
472 return ! empty( $results[0]->form_id ) ? $results[0]->form_id : null;
473 }
474 }
475