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 3.9 3.9-beta 3.9.1 3.9.2 3.9.3 4.0 4.0.1 4.0.2 4.0.3 4.1 4.1-beta 4.1.1 4.1.2 4.2 4.2-beta 4.2.1 4.2.2 4.3 4.3.1 4.4 4.4.1 4.4.2 4.5 4.5.1 4.6 4.6.1 4.7 4.8 4.8.1 4.9 4.9.1 4.9.2 5.0 5.0.1
contact-form-7 / includes / classes.php
contact-form-7 / includes Last commit date
classes.php 16 years ago controller.php 16 years ago formatting.php 16 years ago functions.php 16 years ago pipe.php 16 years ago shortcodes.php 16 years ago taggenerator.php 16 years ago
classes.php
620 lines
1 <?php
2
3 class WPCF7_ContactForm {
4
5 var $initial = false;
6
7 var $id;
8 var $title;
9 var $form;
10 var $mail;
11 var $mail_2;
12 var $messages;
13 var $additional_settings;
14
15 var $unit_tag;
16
17 var $responses_count = 0;
18 var $scanned_form_tags;
19
20 var $posted_data;
21 var $uploaded_files;
22
23 var $skip_mail = false;
24
25 // Return true if this form is the same one as currently POSTed.
26 function is_posted() {
27 if ( ! isset( $_POST['_wpcf7_unit_tag'] ) || empty( $_POST['_wpcf7_unit_tag'] ) )
28 return false;
29
30 if ( $this->unit_tag == $_POST['_wpcf7_unit_tag'] )
31 return true;
32
33 return false;
34 }
35
36 /* Generating Form HTML */
37
38 function form_html() {
39 $form = '<div class="wpcf7" id="' . $this->unit_tag . '">';
40
41 $url = wpcf7_get_request_uri();
42
43 if ( $frag = strstr( $url, '#' ) )
44 $url = substr( $url, 0, -strlen( $frag ) );
45
46 $url .= '#' . $this->unit_tag;
47
48 $url = apply_filters( 'wpcf7_form_action_url', $url );
49 $url = esc_url_raw( $url );
50
51 $enctype = apply_filters( 'wpcf7_form_enctype', '' );
52
53 $form .= '<form action="' . $url
54 . '" method="post" class="wpcf7-form"' . $enctype . '>' . "\n";
55 $form .= '<div style="display: none;">' . "\n";
56 $form .= '<input type="hidden" name="_wpcf7" value="'
57 . esc_attr( $this->id ) . '" />' . "\n";
58 $form .= '<input type="hidden" name="_wpcf7_version" value="'
59 . esc_attr( WPCF7_VERSION ) . '" />' . "\n";
60 $form .= '<input type="hidden" name="_wpcf7_unit_tag" value="'
61 . esc_attr( $this->unit_tag ) . '" />' . "\n";
62 $form .= '</div>' . "\n";
63 $form .= $this->form_elements();
64
65 if ( ! $this->responses_count )
66 $form .= $this->form_response_output();
67
68 $form .= '</form>';
69
70 $form .= '</div>';
71
72 return $form;
73 }
74
75 function form_response_output() {
76 $class = 'wpcf7-response-output';
77 $content = '';
78
79 if ( $this->is_posted() ) { // Post response output for non-AJAX
80 if ( isset( $_POST['_wpcf7_mail_sent'] ) && $_POST['_wpcf7_mail_sent']['id'] == $this->id ) {
81 if ( $_POST['_wpcf7_mail_sent']['ok'] ) {
82 $class .= ' wpcf7-mail-sent-ok';
83 $content = $_POST['_wpcf7_mail_sent']['message'];
84 } else {
85 $class .= ' wpcf7-mail-sent-ng';
86 if ( $_POST['_wpcf7_mail_sent']['spam'] )
87 $class .= ' wpcf7-spam-blocked';
88 $content = $_POST['_wpcf7_mail_sent']['message'];
89 }
90 } elseif ( isset( $_POST['_wpcf7_validation_errors'] ) && $_POST['_wpcf7_validation_errors']['id'] == $this->id ) {
91 $class .= ' wpcf7-validation-errors';
92 $content = $this->message( 'validation_error' );
93 }
94 } else {
95 $class .= ' wpcf7-display-none';
96 }
97
98 $class = ' class="' . $class . '"';
99
100 return '<div' . $class . '>' . $content . '</div>';
101 }
102
103 function validation_error( $name ) {
104 if ( $this->is_posted() && $ve = $_POST['_wpcf7_validation_errors']['messages'][$name] )
105 return apply_filters( 'wpcf7_validation_error',
106 '<span class="wpcf7-not-valid-tip-no-ajax">' . esc_html( $ve ) . '</span>',
107 $name, $this );
108
109 return '';
110 }
111
112 /* Form Elements */
113
114 function form_do_shortcode() {
115 global $wpcf7_shortcode_manager;
116
117 $form = $this->form;
118
119 $form = $wpcf7_shortcode_manager->do_shortcode( $form );
120 $this->scanned_form_tags = $wpcf7_shortcode_manager->scanned_tags;
121
122 if ( WPCF7_AUTOP )
123 $form = wpcf7_autop( $form );
124
125 return $form;
126 }
127
128 function form_scan_shortcode( $cond = null ) {
129 global $wpcf7_shortcode_manager;
130
131 if ( ! empty( $this->scanned_form_tags ) ) {
132 $scanned = $this->scanned_form_tags;
133 } else {
134 $scanned = $wpcf7_shortcode_manager->scan_shortcode( $this->form );
135 $this->scanned_form_tags = $scanned;
136 }
137
138 if ( empty( $scanned ) )
139 return null;
140
141 if ( ! is_array( $cond ) || empty( $cond ) )
142 return $scanned;
143
144 for ( $i = 0, $size = count( $scanned ); $i < $size; $i++ ) {
145
146 if ( is_string( $cond['type'] ) && ! empty( $cond['type'] ) ) {
147 if ( $scanned[$i]['type'] != $cond['type'] ) {
148 unset( $scanned[$i] );
149 continue;
150 }
151 } elseif ( is_array( $cond['type'] ) ) {
152 if ( ! in_array( $scanned[$i]['type'], $cond['type'] ) ) {
153 unset( $scanned[$i] );
154 continue;
155 }
156 }
157
158 if ( is_string( $cond['name'] ) && ! empty( $cond['name'] ) ) {
159 if ( $scanned[$i]['name'] != $cond['name'] ) {
160 unset ( $scanned[$i] );
161 continue;
162 }
163 } elseif ( is_array( $cond['name'] ) ) {
164 if ( ! in_array( $scanned[$i]['name'], $cond['name'] ) ) {
165 unset( $scanned[$i] );
166 continue;
167 }
168 }
169 }
170
171 return array_values( $scanned );
172 }
173
174 function form_elements() {
175 $form = apply_filters( 'wpcf7_form_elements', $this->form_do_shortcode() );
176
177 // Response output
178 $response_regex = '%\[\s*response\s*\]%';
179 $form = preg_replace_callback( $response_regex,
180 array( &$this, 'response_replace_callback' ), $form );
181
182 return $form;
183 }
184
185 function response_replace_callback( $matches ) {
186 $this->responses_count += 1;
187 return $this->form_response_output();
188 }
189
190 /* Validate */
191
192 function validate() {
193 $fes = $this->form_scan_shortcode();
194
195 $result = array( 'valid' => true, 'reason' => array() );
196
197 foreach ( $fes as $fe ) {
198 $result = apply_filters( 'wpcf7_validate_' . $fe['type'], $result, $fe );
199 }
200
201 return $result;
202 }
203
204 /* Acceptance */
205
206 function accepted() {
207 $accepted = true;
208
209 return apply_filters( 'wpcf7_acceptance', $accepted );
210 }
211
212 /* Akismet */
213
214 function akismet() {
215 global $akismet_api_host, $akismet_api_port;
216
217 if ( ! function_exists( 'akismet_http_post' ) ||
218 ! ( get_option( 'wordpress_api_key' ) || $wpcom_api_key ) )
219 return false;
220
221 $akismet_ready = false;
222 $author = $author_email = $author_url = $content = '';
223 $fes = $this->form_scan_shortcode();
224
225 foreach ( $fes as $fe ) {
226 if ( ! is_array( $fe['options'] ) ) continue;
227
228 if ( preg_grep( '%^akismet:author$%', $fe['options'] ) && '' == $author ) {
229 $author = $_POST[$fe['name']];
230 $akismet_ready = true;
231 }
232
233 if ( preg_grep( '%^akismet:author_email$%', $fe['options'] ) && '' == $author_email ) {
234 $author_email = $_POST[$fe['name']];
235 $akismet_ready = true;
236 }
237
238 if ( preg_grep( '%^akismet:author_url$%', $fe['options'] ) && '' == $author_url ) {
239 $author_url = $_POST[$fe['name']];
240 $akismet_ready = true;
241 }
242
243 if ( '' != $content )
244 $content .= "\n\n";
245
246 $content .= $_POST[$fe['name']];
247 }
248
249 if ( ! $akismet_ready )
250 return false;
251
252 $c['blog'] = get_option( 'home' );
253 $c['user_ip'] = preg_replace( '/[^0-9., ]/', '', $_SERVER['REMOTE_ADDR'] );
254 $c['user_agent'] = $_SERVER['HTTP_USER_AGENT'];
255 $c['referrer'] = $_SERVER['HTTP_REFERER'];
256 $c['comment_type'] = 'contactform7';
257 if ( $permalink = get_permalink() )
258 $c['permalink'] = $permalink;
259 if ( '' != $author )
260 $c['comment_author'] = $author;
261 if ( '' != $author_email )
262 $c['comment_author_email'] = $author_email;
263 if ( '' != $author_url )
264 $c['comment_author_url'] = $author_url;
265 if ( '' != $content )
266 $c['comment_content'] = $content;
267
268 $ignore = array( 'HTTP_COOKIE' );
269
270 foreach ( $_SERVER as $key => $value )
271 if ( ! in_array( $key, (array) $ignore ) )
272 $c["$key"] = $value;
273
274 $query_string = '';
275 foreach ( $c as $key => $data )
276 $query_string .= $key . '=' . urlencode( stripslashes( $data ) ) . '&';
277
278 $response = akismet_http_post( $query_string, $akismet_api_host,
279 '/1.1/comment-check', $akismet_api_port );
280 if ( 'true' == $response[1] )
281 return true;
282 else
283 return false;
284 }
285
286 /* Mail */
287
288 function mail() {
289 $fes = $this->form_scan_shortcode();
290
291 foreach ( $fes as $fe ) {
292 $name = $fe['name'];
293 $pipes = $fe['pipes'];
294
295 if ( empty( $name ) )
296 continue;
297
298 $value = $_POST[$name];
299
300 if ( WPCF7_USE_PIPE && is_a( $pipes, 'WPCF7_Pipes' ) && ! $pipes->zero() ) {
301 if ( is_array( $value) ) {
302 $new_value = array();
303 foreach ( $value as $v ) {
304 $new_value[] = $pipes->do_pipe( $v );
305 }
306 $value = $new_value;
307 } else {
308 $value = $pipes->do_pipe( $value );
309 }
310 }
311
312 $this->posted_data[$name] = $value;
313 }
314
315 if ( $this->in_demo_mode() )
316 $this->skip_mail = true;
317
318 do_action_ref_array( 'wpcf7_before_send_mail', array( &$this ) );
319
320 if ( $this->skip_mail )
321 return true;
322
323 if ( $this->compose_and_send_mail( $this->mail ) ) {
324 if ( $this->mail_2['active'] )
325 $this->compose_and_send_mail( $this->mail_2 );
326
327 return true;
328 }
329
330 return false;
331 }
332
333 function compose_and_send_mail( $mail_template ) {
334 $regex = '/\[\s*([a-zA-Z][0-9a-zA-Z:._-]*)\s*\]/';
335 $callback = array( &$this, 'mail_callback' );
336
337 $subject = preg_replace_callback( $regex, $callback, $mail_template['subject'] );
338 $sender = preg_replace_callback( $regex, $callback, $mail_template['sender'] );
339 $body = preg_replace_callback( $regex, $callback, $mail_template['body'] );
340 $recipient = preg_replace_callback( $regex, $callback, $mail_template['recipient'] );
341 $additional_headers =
342 preg_replace_callback( $regex, $callback, $mail_template['additional_headers'] );
343
344 extract( apply_filters( 'wpcf7_mail_components',
345 compact( 'subject', 'sender', 'body', 'recipient', 'additional_headers' ) ) );
346
347 $headers = "From: $sender\n";
348
349 if ( $mail_template['use_html'] )
350 $headers .= "Content-Type: text/html\n";
351
352 $headers .= trim( $additional_headers ) . "\n";
353
354 if ( $this->uploaded_files ) {
355 $for_this_mail = array();
356 foreach ( $this->uploaded_files as $name => $path ) {
357 if ( false === strpos( $mail_template['attachments'], "[${name}]" ) )
358 continue;
359 $for_this_mail[] = $path;
360 }
361
362 return @wp_mail( $recipient, $subject, $body, $headers, $for_this_mail );
363 } else {
364 return @wp_mail( $recipient, $subject, $body, $headers );
365 }
366 }
367
368 function mail_callback( $matches ) {
369 if ( isset( $this->posted_data[$matches[1]] ) ) {
370 $submitted = $this->posted_data[$matches[1]];
371
372 if ( is_array( $submitted ) )
373 $submitted = join( ', ', $submitted );
374
375 return stripslashes( $submitted );
376
377 }
378
379 if ( $special = apply_filters( 'wpcf7_special_mail_tags', '', $matches[1] ) )
380 return $special;
381
382 return $matches[0];
383 }
384
385 /* Message */
386
387 function message( $status ) {
388 $messages = $this->messages;
389 $message = $messages[$status];
390
391 return apply_filters( 'wpcf7_display_message', $message );
392 }
393
394 /* Additional settings */
395
396 function additional_setting( $name, $max = 1 ) {
397 $tmp_settings = (array) explode( "\n", $this->additional_settings );
398
399 $count = 0;
400 $values = array();
401
402 foreach ( $tmp_settings as $setting ) {
403 if ( preg_match('/^([a-zA-Z0-9_]+)\s*:(.*)$/', $setting, $matches ) ) {
404 if ( $matches[1] != $name )
405 continue;
406
407 if ( ! $max || $count < (int) $max ) {
408 $values[] = trim( $matches[2] );
409 $count += 1;
410 }
411 }
412 }
413
414 return $values;
415 }
416
417 function in_demo_mode() {
418 $settings = $this->additional_setting( 'demo_mode', false );
419
420 foreach ( $settings as $setting ) {
421 if ( in_array( $setting, array( 'on', 'true', '1' ) ) )
422 return true;
423 }
424
425 return false;
426 }
427
428 /* Upgrade */
429
430 function upgrade() {
431 if ( ! isset( $this->mail['recipient'] ) )
432 $this->mail['recipient'] = get_option( 'admin_email' );
433
434
435 if ( ! is_array( $this->messages ) )
436 $this->messages = array();
437
438
439 foreach ( wpcf7_messages() as $key => $arr ) {
440 if ( ! isset( $this->messages[$key] ) )
441 $this->messages[$key] = $arr['default'];
442 }
443 }
444
445 /* Save */
446
447 function save() {
448 global $wpdb;
449
450 $table_name = wpcf7_table_name();
451
452 $fields = array(
453 'title' => maybe_serialize( stripslashes_deep( $this->title ) ),
454 'form' => maybe_serialize( stripslashes_deep( $this->form ) ),
455 'mail' => maybe_serialize( stripslashes_deep( $this->mail ) ),
456 'mail_2' => maybe_serialize ( stripslashes_deep( $this->mail_2 ) ),
457 'messages' => maybe_serialize( stripslashes_deep( $this->messages ) ),
458 'additional_settings' =>
459 maybe_serialize( stripslashes_deep( $this->additional_settings ) ) );
460
461 if ( $this->initial ) {
462 $result = $wpdb->insert( $table_name, $fields );
463
464 if ( $result ) {
465 $this->initial = false;
466 $this->id = $wpdb->insert_id;
467
468 do_action_ref_array( 'wpcf7_after_create', array( &$this ) );
469 } else {
470 return false; // Failed to save
471 }
472
473 } else { // Update
474 if ( ! (int) $this->id )
475 return false; // Missing ID
476
477 $result = $wpdb->update( $table_name, $fields,
478 array( 'cf7_unit_id' => absint( $this->id ) ) );
479
480 if ( false !== $result ) {
481 do_action_ref_array( 'wpcf7_after_update', array( &$this ) );
482 } else {
483 return false; // Failed to save
484 }
485 }
486
487 do_action_ref_array( 'wpcf7_after_save', array( &$this ) );
488 return true; // Succeeded to save
489 }
490
491 function copy() {
492 $new = new WPCF7_ContactForm();
493 $new->initial = true;
494
495 $new->title = $this->title . '_copy';
496 $new->form = $this->form;
497 $new->mail = $this->mail;
498 $new->mail_2 = $this->mail_2;
499 $new->messages = $this->messages;
500 $new->additional_settings = $this->additional_settings;
501
502 return $new;
503 }
504
505 function delete() {
506 global $wpdb;
507
508 if ( $this->initial )
509 return;
510
511 $table_name = wpcf7_table_name();
512
513 $query = $wpdb->prepare(
514 "DELETE FROM $table_name WHERE cf7_unit_id = %d LIMIT 1",
515 absint( $this->id ) );
516
517 $wpdb->query( $query );
518
519 $this->initial = true;
520 $this->id = null;
521 }
522 }
523
524 function wpcf7_contact_form( $id ) {
525 global $wpdb;
526
527 $table_name = wpcf7_table_name();
528
529 $id = (int) $id;
530
531 $query = $wpdb->prepare( "SELECT * FROM $table_name WHERE cf7_unit_id = %d", $id );
532
533 if ( ! $row = $wpdb->get_row( $query ) )
534 return false; // No data
535
536 $contact_form = new WPCF7_ContactForm();
537 $contact_form->id = $row->cf7_unit_id;
538 $contact_form->title = maybe_unserialize( $row->title );
539 $contact_form->form = maybe_unserialize( $row->form );
540 $contact_form->mail = maybe_unserialize( $row->mail );
541 $contact_form->mail_2 = maybe_unserialize( $row->mail_2 );
542 $contact_form->messages = maybe_unserialize( $row->messages );
543 $contact_form->additional_settings = maybe_unserialize( $row->additional_settings );
544
545 $contact_form->upgrade();
546
547 return $contact_form;
548 }
549
550 function wpcf7_contact_form_default_pack( $locale = null ) {
551 global $l10n;
552
553 if ( $locale && $locale != get_locale() ) {
554 $mo_orig = $l10n['wpcf7'];
555 unset( $l10n['wpcf7'] );
556
557 if ( 'en_US' != $locale ) {
558 $mofile = wpcf7_plugin_path( 'languages/wpcf7-' . $locale . '.mo' );
559 if ( ! load_textdomain( 'wpcf7', $mofile ) ) {
560 $l10n['wpcf7'] = $mo_orig;
561 unset( $mo_orig );
562 }
563 }
564 }
565
566 $contact_form = new WPCF7_ContactForm();
567 $contact_form->initial = true;
568
569 $contact_form->title = __( 'Untitled', 'wpcf7' );
570 $contact_form->form = wpcf7_default_form_template();
571 $contact_form->mail = wpcf7_default_mail_template();
572 $contact_form->mail_2 = wpcf7_default_mail_2_template();
573 $contact_form->messages = wpcf7_default_messages_template();
574
575 if ( isset( $mo_orig ) )
576 $l10n['wpcf7'] = $mo_orig;
577
578 return $contact_form;
579 }
580
581 /* Default Filters */
582
583 add_filter( 'wpcf7_special_mail_tags', 'wpcf7_special_mail_tag_for_remote_ip', 10, 2 );
584
585 function wpcf7_special_mail_tag_for_remote_ip( $output, $name ) {
586 // Special [wpcf7.remote_ip] tag
587 if ( 'wpcf7.remote_ip' == $name )
588 $output = preg_replace( '/[^0-9a-f.:, ]/', '', $_SERVER['REMOTE_ADDR'] );
589
590 return $output;
591 }
592
593 add_filter( 'wpcf7_special_mail_tags', 'wpcf7_special_mail_tag_for_post_data', 10, 2 );
594
595 function wpcf7_special_mail_tag_for_post_data( $output, $name ) {
596 if ( ! isset( $_POST['_wpcf7_unit_tag'] ) || empty( $_POST['_wpcf7_unit_tag'] ) )
597 return $output;
598
599 if ( ! preg_match( '/^wpcf7-f(\d+)-p(\d+)-o(\d+)$/', $_POST['_wpcf7_unit_tag'], $matches ) )
600 return $output;
601
602 $post_id = (int) $matches[2];
603
604 if ( ! $post = get_post( $post_id ) )
605 return $output;
606
607 if ( 'wpcf7.post_id' == $name ) { // Special [wpcf7.post_id] tag
608 $output = (string) $post->ID;
609 } elseif ( 'wpcf7.post_name' == $name ) { // Special [wpcf7.post_name] tag
610 $output = $post->post_name;
611 } elseif ( 'wpcf7.post_title' == $name ) { // Special [wpcf7.post_title] tag
612 $output = $post->post_title;
613 } elseif ( 'wpcf7.post_url' == $name ) { // Special [wpcf7.post_url] tag
614 $output = get_permalink( $post->ID );
615 }
616
617 return $output;
618 }
619
620 ?>