Completed
Push — master ( fca83e...1f7c01 )
by Jamie
05:26
created

FrmEntryFormat::maybe_strip_html()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 8
rs 9.2
cc 4
eloc 5
nc 3
nop 2
1
<?php
2
3
class FrmEntryFormat {
4
	public static function show_entry( $atts ) {
5
		$atts = shortcode_atts( array(
6
			'id' => false, 'entry' => false, 'fields' => false, 'plain_text' => false,
7
			'user_info' => false, 'include_blank' => false, 'default_email' => false,
8
			'form_id' => false, 'format' => 'text', 'direction' => 'ltr',
9
			'font_size' => '', 'text_color' => '',
10
			'border_width' => '', 'border_color' => '',
11
			'bg_color' => '', 'alt_bg_color' => '',
12
			'clickable' => false,
13
		), $atts );
14
15
		if ( $atts['format'] != 'text' ) {
16
			//format options are text, array, or json
17
			$atts['plain_text'] = true;
18
		}
19
20
		if ( is_object( $atts['entry'] ) && ! isset( $atts['entry']->metas ) ) {
21
			// if the entry does not include metas, force it again
22
			$atts['entry'] = false;
23
		}
24
25
		if ( ! $atts['entry'] || ! is_object( $atts['entry'] ) ) {
26
			if ( ! $atts['id'] && ! $atts['default_email'] ) {
27
				return;
28
			}
29
30
			if ( $atts['id'] ) {
31
				$atts['entry'] = FrmEntry::getOne( $atts['id'], true );
32
			}
33
		}
34
35
		if ( $atts['entry'] ) {
36
			$atts['form_id'] = $atts['entry']->form_id;
37
			$atts['id'] = $atts['entry']->id;
38
		}
39
40
		if ( ! $atts['fields'] || ! is_array($atts['fields']) ) {
41
			$atts['fields'] = FrmField::get_all_for_form( $atts['form_id'], '', 'include' );
42
		}
43
44
		$values = array();
45
		foreach ( $atts['fields'] as $f ) {
46
			self::fill_entry_values( $atts, $f, $values );
47
			unset($f);
48
		}
49
50
		self::fill_entry_user_info( $atts, $values );
51
52
		if ( $atts['format'] == 'json' ) {
53
			return json_encode($values);
54
		} else if ( $atts['format'] == 'array' ) {
55
			return $values;
56
		}
57
58
		$content = array();
59
		self::convert_entry_to_content( $values, $atts, $content );
60
61
		if ( 'text' == $atts['format'] ) {
62
			$content = implode('', $content);
63
		}
64
65
		if ( $atts['clickable'] ) {
66
			$content = make_clickable( $content );
67
		}
68
69
		return $content;
70
	}
71
72
	public static function fill_entry_values( $atts, $f, array &$values ) {
73
		if ( FrmField::is_no_save_field( $f->type ) ) {
74
			return;
75
		}
76
77
		if ( $atts['default_email'] ) {
78
			$values[ $f->id ] = array( 'label' => '[' . $f->id . ' show=field_label]', 'val' => '[' . $f->id . ']' );
79
			return;
80
		}
81
82
		if ( $atts['entry'] && ! isset( $atts['entry']->metas[ $f->id ] ) ) {
83
			// In case include_blank is set
84
			$atts['entry']->metas[ $f->id ] = '';
85
86
			if ( FrmAppHelper::pro_is_installed() ) {
87
				FrmProEntryMeta::add_post_value_to_entry( $f, $atts['entry'] );
88
				FrmProEntryMeta::add_repeating_value_to_entry( $f, $atts['entry'] );
89
			}
90
		}
91
92
		$val = '';
93
		if ( $atts['entry'] ) {
94
			$prev_val = maybe_unserialize( $atts['entry']->metas[ $f->id ] );
95
			$meta = array( 'item_id' => $atts['id'], 'field_id' => $f->id, 'meta_value' => $prev_val, 'field_type' => $f->type );
0 ignored issues
show
introduced by
Detected usage of meta_value, possible slow query.
Loading history...
96
97
			//This filter applies to the default-message shortcode and frm-show-entry shortcode only
98
			if ( isset( $atts['filter'] ) && $atts['filter'] == false ) {
99
				$val = $prev_val;
100
			} else {
101
				$val = apply_filters( 'frm_email_value', $prev_val, (object) $meta, $atts['entry'] );
102
			}
103
		}
104
105
		// Don't include blank values
106
		if ( ! $atts['include_blank'] && FrmAppHelper::is_empty_value( $val ) ) {
107
			return;
108
		}
109
110
		self::textarea_display_value( $f->type, $atts['plain_text'], $val );
111
112
		if ( is_array( $val ) && $atts['format'] == 'text' ) {
113
			$val = implode( ', ', $val );
114
		}
115
116
		self::maybe_strip_html( $atts['plain_text'], $val );
117
118
		if ( $atts['format'] != 'text' ) {
119
			$values[ $f->field_key ] = $val;
120
		} else {
121
			$values[ $f->id ] = array( 'label' => $f->name, 'val' => $val );
122
		}
123
	}
124
125
	/**
126
	* Flatten multi-dimensional array for multi-file upload fields
127
	* @since 2.0.9
128
	*/
129
	public static function flatten_multi_file_upload( $field, &$val ) {
130
		if ( $field->type == 'file' && FrmField::is_option_true( $field, 'multiple' ) ) {
131
			$val = FrmAppHelper::array_flatten( $val );
132
		}
133
	}
134
135
    /**
136
     * Replace returns with HTML line breaks for display
137
     * @since 2.0.9
138
     */
139
	public static function textarea_display_value( $type, $plain_text, &$value ) {
140
		if ( $type == 'textarea' && ! $plain_text ) {
141
			$value = str_replace( array( "\r\n", "\r", "\n" ), ' <br/>', $value );
142
		}
143
	}
144
145
	/**
146
	 * Strip HTML if from email value if plain text is selected
147
	 *
148
	 * @since 2.0.21
149
	 * @param boolean $plain_text
150
	 * @param mixed $val
151
	 */
152
	private static function maybe_strip_html( $plain_text, &$val ) {
153
		if ( $plain_text && ! is_array( $val ) ) {
154
			if ( strpos( $val, '<img' ) !== false ) {
155
				$val = str_replace( array( '<img', 'src=', '/>', '"' ), '', $val );
156
			}
157
			$val = strip_tags( $val );
158
		}
159
	}
160
161
	public static function fill_entry_user_info( $atts, array &$values ) {
162
		if ( ! $atts['user_info'] || empty( $atts['entry'] ) ) {
163
			return;
164
		}
165
166
		$data  = self::get_entry_description_data( $atts );
167
168
		if ( $atts['default_email'] ) {
169
			$atts['entry']->ip = '[ip]';
170
		}
171
172
		if ( $atts['format'] != 'text' ) {
173
			$values['ip'] = $atts['entry']->ip;
174
			$values['browser'] = self::get_browser( $data['browser'] );
175
			$values['referrer'] = $data['referrer'];
176
		} else {
177
			$values['ip'] = array( 'label' => __( 'IP Address', 'formidable' ), 'val' => $atts['entry']->ip );
178
			$values['browser'] = array(
179
				'label' => __( 'User-Agent (Browser/OS)', 'formidable' ),
180
				'val'   => self::get_browser( $data['browser'] ),
181
			);
182
			$values['referrer'] = array( 'label' => __( 'Referrer', 'formidable' ), 'val' => $data['referrer'] );
183
		}
184
	}
185
186
	/**
187
	 * @param array $atts - include (object) entry, (boolean) default_email
188
	 * @since 2.0.9
189
	 */
190
	public static function get_entry_description_data( $atts ) {
191
		$default_data = array(
192
			'browser' => '',
193
			'referrer' => '',
194
		);
195
		$data = $default_data;
196
197
		if ( isset( $atts['entry']->description ) ) {
198
			$data = (array) maybe_unserialize( $atts['entry']->description );
199
		} else if ( $atts['default_email'] ) {
200
			$data = array(
201
				'browser'  => '[browser]',
202
				'referrer' => '[referrer]',
203
			);
204
		}
205
206
		return array_merge( $default_data, $data );
207
	}
208
209
	public static function get_browser( $u_agent ) {
210
		$bname = __( 'Unknown', 'formidable' );
211
		$platform = __( 'Unknown', 'formidable' );
212
		$ub = '';
213
214
		// Get the operating system
215
		if ( preg_match( '/windows|win32/i', $u_agent ) ) {
216
			$platform = 'Windows';
217
		} else if ( preg_match( '/android/i', $u_agent ) ) {
218
			$platform = 'Android';
219
		} else if ( preg_match( '/linux/i', $u_agent ) ) {
220
			$platform = 'Linux';
221
		} else if ( preg_match( '/macintosh|mac os x/i', $u_agent ) ) {
222
			$platform = 'OS X';
223
		}
224
225
		$agent_options = array(
226
			'Chrome'   => 'Google Chrome',
227
			'Safari'   => 'Apple Safari',
228
			'Opera'    => 'Opera',
229
			'Netscape' => 'Netscape',
230
			'Firefox'  => 'Mozilla Firefox',
231
		);
232
233
		// Next get the name of the useragent yes seperately and for good reason
234
		if ( strpos( $u_agent, 'MSIE' ) !== false && strpos( $u_agent, 'Opera' ) === false ) {
235
			$bname = 'Internet Explorer';
236
			$ub = 'MSIE';
237
		} else {
238
			foreach ( $agent_options as $agent_key => $agent_name ) {
239
				if ( strpos( $u_agent, $agent_key ) !== false ) {
240
					$bname = $agent_name;
241
					$ub = $agent_key;
242
					break;
243
				}
244
			}
245
		}
246
247
		// finally get the correct version number
248
		$known = array( 'Version', $ub, 'other' );
249
		$pattern = '#(?<browser>' . join( '|', $known ) . ')[/ ]+(?<version>[0-9.|a-zA-Z.]*)#';
250
		preg_match_all( $pattern, $u_agent, $matches ); // get the matching numbers
251
252
		// see how many we have
253
		$i = count($matches['browser']);
254
		if ( $i != 1 ) {
255
			//we will have two since we are not using 'other' argument yet
256
			//see if version is before or after the name
257
			if ( strripos( $u_agent, 'Version' ) < strripos( $u_agent, $ub ) ) {
258
				$version = $matches['version'][0];
259
			} else {
260
				$version = $matches['version'][1];
261
			}
262
		} else {
263
			$version = $matches['version'][0];
264
		}
265
266
		// check if we have a number
267
		if ( $version == '' ) {
268
			$version = '?';
269
		}
270
271
		return $bname .' '. $version .' / '. $platform;
272
	}
273
274
	public static function convert_entry_to_content( $values, $atts, array &$content ) {
275
276
		if ( $atts['plain_text'] ) {
277
			$bg_color_alt = $row_style = '';
278
		} else {
279
			$default_settings = apply_filters( 'frm_show_entry_styles', array(
280
				'border_color' => 'dddddd',
281
				'bg_color'     => 'f7f7f7',
282
				'text_color'   => '444444',
283
				'font_size'    => '12px',
284
				'border_width' => '1px',
285
				'alt_bg_color' => 'ffffff',
286
			) );
287
288
			// merge defaults, global settings, and shortcode options
289
			foreach ( $default_settings as $key => $setting ) {
290
				if ( $atts[ $key ] != '' ) {
291
					continue;
292
				}
293
294
				$atts[ $key ] = $setting;
295
				unset( $key, $setting );
296
			}
297
298
			unset($default_settings);
299
300
			$content[] = '<table cellspacing="0" style="font-size:'. $atts['font_size'] .';line-height:135%; border-bottom:'. $atts['border_width'] . ' solid #' . $atts['border_color'] . ';"><tbody>' . "\r\n";
301
			$atts['bg_color'] = ' style="background-color:#'. $atts['bg_color'] .';"';
302
			$bg_color_alt = ' style="background-color:#'. $atts['alt_bg_color'] .';"';
303
			$row_style = 'style="text-align:' . ( $atts['direction'] == 'rtl' ? 'right' : 'left' ) .';color:#'. $atts['text_color'] . ';padding:7px 9px;border-top:' . $atts['border_width'] .' solid #' . $atts['border_color'] . '"';
304
		}
305
306
		$odd = true;
307
		foreach ( $values as $id => $value ) {
308
			if ( $atts['plain_text'] ) {
309
				if ( 'rtl' == $atts['direction'] ) {
310
					$content[] = $value['val'] . ' :'. $value['label'] ."\r\n";
311
				} else {
312
					$content[] = $value['label'] . ': '. $value['val'] ."\r\n";
313
				}
314
				continue;
315
			}
316
317
			if ( $atts['default_email'] && is_numeric($id) ) {
318
				$content[] = '[if ' . $id . ']<tr style="[frm-alt-color]">';
319
			} else {
320
				$content[] = '<tr' . ( $odd ? $atts['bg_color'] : $bg_color_alt ) . '>';
321
			}
322
323
			$value['val'] = str_replace( "\r\n", '<br/>', $value['val'] );
324
			if ( 'rtl' == $atts['direction'] ) {
325
				$content[] = '<td ' . $row_style . '>' . $value['val'] . '</td><th ' . $row_style . '>' . $value['label'] . '</th>';
326
			} else {
327
				$content[] = '<th ' . $row_style . '>' . $value['label'] . '</th><td '. $row_style . '>' . $value['val'] . '</td>';
328
			}
329
			$content[] = '</tr>' . "\r\n";
330
331
			if ( $atts['default_email'] && is_numeric( $id ) ) {
332
				$content[] = '[/if ' . $id . ']';
333
			}
334
			$odd = $odd ? false : true;
335
		}
336
337
		if ( ! $atts['plain_text'] ) {
338
			$content[] = '</tbody></table>';
339
		}
340
	}
341
}
342