source: trunk/www.guidonia.net/wp/wp-content/plugins/contact-form-7/includes/classes.php@ 44

Last change on this file since 44 was 44, checked in by luciano, 14 years ago
File size: 14.1 KB
Line 
1<?php
2
3class 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 // Return true if this form is the same one as currently POSTed.
24 function is_posted() {
25 if ( ! isset( $_POST['_wpcf7_unit_tag'] ) || empty( $_POST['_wpcf7_unit_tag'] ) )
26 return false;
27
28 if ( $this->unit_tag == $_POST['_wpcf7_unit_tag'] )
29 return true;
30
31 return false;
32 }
33
34 /* Generating Form HTML */
35
36 function form_html() {
37 $form = '<div class="wpcf7" id="' . $this->unit_tag . '">';
38
39 $url = parse_url( $_SERVER['REQUEST_URI'] );
40 $url = $url['path'] . ( empty( $url['query'] ) ? '' : '?' . $url['query'] ) . '#' . $this->unit_tag;
41
42 $enctype = apply_filters( 'wpcf7_form_enctype', '' );
43
44 $form .= '<form action="' . $url . '" method="post" class="wpcf7-form"' . $enctype . '>';
45 $form .= '<div style="display: none;">';
46 $form .= '<input type="hidden" name="_wpcf7" value="' . $this->id . '" />';
47 $form .= '<input type="hidden" name="_wpcf7_version" value="' . WPCF7_VERSION . '" />';
48 $form .= '<input type="hidden" name="_wpcf7_unit_tag" value="' . $this->unit_tag . '" />';
49 $form .= '</div>';
50 $form .= $this->form_elements();
51
52 if ( ! $this->responses_count )
53 $form .= $this->form_response_output();
54
55 $form .= '</form>';
56
57 $form .= '</div>';
58
59 return $form;
60 }
61
62 function form_response_output() {
63 $class = 'wpcf7-response-output';
64
65 if ( $this->is_posted() ) { // Post response output for non-AJAX
66 if ( isset( $_POST['_wpcf7_mail_sent'] ) && $_POST['_wpcf7_mail_sent']['id'] == $this->id ) {
67 if ( $_POST['_wpcf7_mail_sent']['ok'] ) {
68 $class .= ' wpcf7-mail-sent-ok';
69 $content = $_POST['_wpcf7_mail_sent']['message'];
70 } else {
71 $class .= ' wpcf7-mail-sent-ng';
72 if ( $_POST['_wpcf7_mail_sent']['spam'] )
73 $class .= ' wpcf7-spam-blocked';
74 $content = $_POST['_wpcf7_mail_sent']['message'];
75 }
76 } elseif ( isset( $_POST['_wpcf7_validation_errors'] ) && $_POST['_wpcf7_validation_errors']['id'] == $this->id ) {
77 $class .= ' wpcf7-validation-errors';
78 $content = $this->message( 'validation_error' );
79 }
80 } else {
81 $class .= ' wpcf7-display-none';
82 }
83
84 $class = ' class="' . $class . '"';
85
86 return '<div' . $class . '>' . $content . '</div>';
87 }
88
89 function validation_error( $name ) {
90 if ( $this->is_posted() && $ve = $_POST['_wpcf7_validation_errors']['messages'][$name] )
91 return '<span class="wpcf7-not-valid-tip-no-ajax">' . esc_html( $ve ) . '</span>';
92
93 return '';
94 }
95
96 /* Form Elements */
97
98 function form_do_shortcode() {
99 global $wpcf7_shortcode_manager;
100
101 $form = $this->form;
102
103 $form = $wpcf7_shortcode_manager->do_shortcode( $form );
104 $this->scanned_form_tags = $wpcf7_shortcode_manager->scanned_tags;
105
106 if ( WPCF7_AUTOP )
107 $form = wpcf7_autop( $form );
108
109 return $form;
110 }
111
112 function form_scan_shortcode( $cond = null ) {
113 global $wpcf7_shortcode_manager;
114
115 if ( ! empty( $this->scanned_form_tags ) ) {
116 $scanned = $this->scanned_form_tags;
117 } else {
118 $scanned = $wpcf7_shortcode_manager->scan_shortcode( $this->form );
119 $this->scanned_form_tags = $scanned;
120 }
121
122 if ( empty( $scanned ) )
123 return null;
124
125 if ( ! is_array( $cond ) || empty( $cond ) )
126 return $scanned;
127
128 for ( $i = 0, $size = count( $scanned ); $i < $size; $i++ ) {
129
130 if ( is_string( $cond['type'] ) && ! empty( $cond['type'] ) ) {
131 if ( $scanned[$i]['type'] != $cond['type'] ) {
132 unset( $scanned[$i] );
133 continue;
134 }
135 } elseif ( is_array( $cond['type'] ) ) {
136 if ( ! in_array( $scanned[$i]['type'], $cond['type'] ) ) {
137 unset( $scanned[$i] );
138 continue;
139 }
140 }
141
142 if ( is_string( $cond['name'] ) && ! empty( $cond['name'] ) ) {
143 if ( $scanned[$i]['name'] != $cond['name'] ) {
144 unset ( $scanned[$i] );
145 continue;
146 }
147 } elseif ( is_array( $cond['name'] ) ) {
148 if ( ! in_array( $scanned[$i]['name'], $cond['name'] ) ) {
149 unset( $scanned[$i] );
150 continue;
151 }
152 }
153 }
154
155 return array_values( $scanned );
156 }
157
158 function form_elements() {
159 $form = $this->form_do_shortcode();
160
161 // Response output
162 $response_regex = '%\[\s*response\s*\]%';
163 $form = preg_replace_callback( $response_regex,
164 array( &$this, 'response_replace_callback' ), $form );
165
166 return $form;
167 }
168
169 function response_replace_callback( $matches ) {
170 $this->responses_count += 1;
171 return $this->form_response_output();
172 }
173
174 /* Validate */
175
176 function validate() {
177 $fes = $this->form_scan_shortcode();
178
179 $result = array( 'valid' => true, 'reason' => array() );
180
181 foreach ( $fes as $fe ) {
182 $type = $fe['type'];
183 $name = $fe['name'];
184
185 if ( empty( $name ) )
186 continue;
187
188 $result = apply_filters( 'wpcf7_validate_' . $type, $result, $fe );
189 }
190
191 return $result;
192 }
193
194 /* Acceptance */
195
196 function accepted() {
197 $accepted = true;
198
199 return apply_filters( 'wpcf7_acceptance', $accepted );
200 }
201
202 /* Akismet */
203
204 function akismet() {
205 global $akismet_api_host, $akismet_api_port;
206
207 if ( ! function_exists( 'akismet_http_post' ) ||
208 ! ( get_option( 'wordpress_api_key' ) || $wpcom_api_key ) )
209 return false;
210
211 $akismet_ready = false;
212 $author = $author_email = $author_url = $content = '';
213 $fes = $this->form_scan_shortcode();
214
215 foreach ( $fes as $fe ) {
216 if ( ! is_array( $fe['options'] ) ) continue;
217
218 if ( preg_grep( '%^akismet:author$%', $fe['options'] ) && '' == $author ) {
219 $author = $_POST[$fe['name']];
220 $akismet_ready = true;
221 }
222
223 if ( preg_grep( '%^akismet:author_email$%', $fe['options'] ) && '' == $author_email ) {
224 $author_email = $_POST[$fe['name']];
225 $akismet_ready = true;
226 }
227
228 if ( preg_grep( '%^akismet:author_url$%', $fe['options'] ) && '' == $author_url ) {
229 $author_url = $_POST[$fe['name']];
230 $akismet_ready = true;
231 }
232
233 if ( '' != $content )
234 $content .= "\n\n";
235
236 $content .= $_POST[$fe['name']];
237 }
238
239 if ( ! $akismet_ready )
240 return false;
241
242 $c['blog'] = get_option( 'home' );
243 $c['user_ip'] = preg_replace( '/[^0-9., ]/', '', $_SERVER['REMOTE_ADDR'] );
244 $c['user_agent'] = $_SERVER['HTTP_USER_AGENT'];
245 $c['referrer'] = $_SERVER['HTTP_REFERER'];
246 $c['comment_type'] = 'contactform7';
247 if ( $permalink = get_permalink() )
248 $c['permalink'] = $permalink;
249 if ( '' != $author )
250 $c['comment_author'] = $author;
251 if ( '' != $author_email )
252 $c['comment_author_email'] = $author_email;
253 if ( '' != $author_url )
254 $c['comment_author_url'] = $author_url;
255 if ( '' != $content )
256 $c['comment_content'] = $content;
257
258 $ignore = array( 'HTTP_COOKIE' );
259
260 foreach ( $_SERVER as $key => $value )
261 if ( ! in_array( $key, (array) $ignore ) )
262 $c["$key"] = $value;
263
264 $query_string = '';
265 foreach ( $c as $key => $data )
266 $query_string .= $key . '=' . urlencode( stripslashes( $data ) ) . '&';
267
268 $response = akismet_http_post( $query_string, $akismet_api_host,
269 '/1.1/comment-check', $akismet_api_port );
270 if ( 'true' == $response[1] )
271 return true;
272 else
273 return false;
274 }
275
276 /* Mail */
277
278 function mail() {
279 $fes = $this->form_scan_shortcode();
280
281 foreach ( $fes as $fe ) {
282 $name = $fe['name'];
283 $pipes = $fe['pipes'];
284
285 if ( empty( $name ) )
286 continue;
287
288 $value = $_POST[$name];
289
290 if ( WPCF7_USE_PIPE && is_a( $pipes, 'WPCF7_Pipes' ) && ! $pipes->zero() ) {
291 if ( is_array( $value) ) {
292 $new_value = array();
293 foreach ( $value as $v ) {
294 $new_value[] = $pipes->do_pipe( $v );
295 }
296 $value = $new_value;
297 } else {
298 $value = $pipes->do_pipe( $value );
299 }
300 }
301
302 $this->posted_data[$name] = $value;
303 }
304
305 if ( $this->compose_and_send_mail( $this->mail ) ) {
306 if ( $this->mail_2['active'] )
307 $this->compose_and_send_mail( $this->mail_2 );
308
309 return true;
310 }
311
312 return false;
313 }
314
315 function compose_and_send_mail( $mail_template ) {
316 $regex = '/\[\s*([a-zA-Z][0-9a-zA-Z:._-]*)\s*\]/';
317 $callback = array( &$this, 'mail_callback' );
318
319 $mail_subject = preg_replace_callback( $regex, $callback, $mail_template['subject'] );
320 $mail_sender = preg_replace_callback( $regex, $callback, $mail_template['sender'] );
321 $mail_body = preg_replace_callback( $regex, $callback, $mail_template['body'] );
322 $mail_recipient = preg_replace_callback( $regex, $callback, $mail_template['recipient'] );
323
324 $mail_headers = "From: $mail_sender\n";
325
326 if ( $mail_template['use_html'] )
327 $mail_headers .= "Content-Type: text/html\n";
328
329 $mail_additional_headers = preg_replace_callback( $regex, $callback,
330 $mail_template['additional_headers'] );
331 $mail_headers .= trim( $mail_additional_headers ) . "\n";
332
333 if ( $this->uploaded_files ) {
334 $for_this_mail = array();
335 foreach ( $this->uploaded_files as $name => $path ) {
336 if ( false === strpos( $mail_template['attachments'], "[${name}]" ) )
337 continue;
338 $for_this_mail[] = $path;
339 }
340
341 return @wp_mail( $mail_recipient, $mail_subject, $mail_body, $mail_headers,
342 $for_this_mail );
343 } else {
344 return @wp_mail( $mail_recipient, $mail_subject, $mail_body, $mail_headers );
345 }
346 }
347
348 function mail_callback( $matches ) {
349 if ( isset( $this->posted_data[$matches[1]] ) ) {
350 $submitted = $this->posted_data[$matches[1]];
351
352 if ( is_array( $submitted ) )
353 $submitted = join( ', ', $submitted );
354
355 return stripslashes( $submitted );
356
357 } else {
358 // Special [wpcf7.remote_ip] tag
359 if ( 'wpcf7.remote_ip' == $matches[1] )
360 return preg_replace( '/[^0-9a-f.:, ]/', '', $_SERVER['REMOTE_ADDR'] );
361
362 return $matches[0];
363 }
364 }
365
366 /* Message */
367
368 function message( $status ) {
369 $messages = $this->messages;
370 $message = '';
371
372 if ( ! is_array( $messages ) || ! isset( $messages[$status] ) )
373 $message = wpcf7_default_message( $status );
374 else
375 $message = $messages[$status];
376
377 return apply_filters( 'wpcf7_display_message', $message );
378 }
379
380 /* Additional settings */
381
382 function additional_setting( $name, $max = 1 ) {
383 $tmp_settings = (array) explode( "\n", $this->additional_settings );
384
385 $count = 0;
386 $values = array();
387
388 foreach ( $tmp_settings as $setting ) {
389 if ( preg_match('/^([a-zA-Z0-9_]+)\s*:(.*)$/', $setting, $matches ) ) {
390 if ( $matches[1] != $name )
391 continue;
392
393 if ( ! $max || $count < (int) $max ) {
394 $values[] = trim( $matches[2] );
395 $count += 1;
396 }
397 }
398 }
399
400 return $values;
401 }
402
403 /* Upgrade */
404
405 function upgrade() {
406 if ( ! isset( $this->mail['recipient'] ) )
407 $this->mail['recipient'] = get_option( 'admin_email' );
408
409
410 if ( ! is_array( $this->messages ) )
411 $this->messages = array();
412
413 $messages = array(
414 'mail_sent_ok', 'mail_sent_ng', 'akismet_says_spam', 'validation_error', 'accept_terms',
415 'invalid_email', 'invalid_required', 'captcha_not_match', 'upload_failed',
416 'upload_file_type_invalid', 'upload_file_too_large', 'upload_failed_php_error',
417 'quiz_answer_not_correct' );
418
419 foreach ($messages as $message) {
420 if ( ! isset( $this->messages[$message] ) )
421 $this->messages[$message] = wpcf7_default_message( $message );
422 }
423 }
424
425 /* Save */
426
427 function save() {
428 global $wpdb;
429
430 $table_name = wpcf7_table_name();
431
432 if ( $this->initial ) {
433 $result = $wpdb->insert( $table_name, array(
434 'title' => $this->title,
435 'form' => maybe_serialize( $this->form ),
436 'mail' => maybe_serialize( $this->mail ),
437 'mail_2' => maybe_serialize ( $this->mail_2 ),
438 'messages' => maybe_serialize( $this->messages ),
439 'additional_settings' => maybe_serialize( $this->additional_settings ) ) );
440
441 if ( $result ) {
442 $this->initial = false;
443 $this->id = $wpdb->insert_id;
444
445 do_action_ref_array( 'wpcf7_after_create', array( &$this ) );
446 } else {
447 return false; // Failed to save
448 }
449
450 } else { // Update
451 if ( ! (int) $this->id )
452 return false; // Missing ID
453
454 $result = $wpdb->update( $table_name, array(
455 'title' => $this->title,
456 'form' => maybe_serialize( $this->form ),
457 'mail' => maybe_serialize( $this->mail ),
458 'mail_2' => maybe_serialize ( $this->mail_2 ),
459 'messages' => maybe_serialize( $this->messages ),
460 'additional_settings' => maybe_serialize( $this->additional_settings )
461 ), array( 'cf7_unit_id' => absint( $this->id) ) );
462
463 if ( false !== $result ) {
464 do_action_ref_array( 'wpcf7_after_update', array( &$this ) );
465 } else {
466 return false; // Failed to save
467 }
468 }
469
470 do_action_ref_array( 'wpcf7_after_save', array( &$this ) );
471 return true; // Succeeded to save
472 }
473
474 function copy() {
475 $new = new WPCF7_ContactForm();
476 $new->initial = true;
477
478 $new->title = $this->title . '_copy';
479 $new->form = $this->form;
480 $new->mail = $this->mail;
481 $new->mail_2 = $this->mail_2;
482 $new->messages = $this->messages;
483 $new->additional_settings = $this->additional_settings;
484
485 return $new;
486 }
487
488 function delete() {
489 global $wpdb;
490
491 if ( $this->initial )
492 return;
493
494 $table_name = wpcf7_table_name();
495
496 $query = $wpdb->prepare(
497 "DELETE FROM $table_name WHERE cf7_unit_id = %d LIMIT 1",
498 absint( $this->id ) );
499
500 $wpdb->query( $query );
501
502 $this->initial = true;
503 $this->id = null;
504 }
505}
506
507function wpcf7_contact_form( $id ) {
508 global $wpdb;
509
510 $table_name = wpcf7_table_name();
511
512 $id = (int) $id;
513
514 $query = $wpdb->prepare( "SELECT * FROM $table_name WHERE cf7_unit_id = %d", $id );
515
516 if ( ! $row = $wpdb->get_row( $query ) )
517 return false; // No data
518
519 $contact_form = new WPCF7_ContactForm();
520 $contact_form->id = $row->cf7_unit_id;
521 $contact_form->title = stripslashes_deep( $row->title );
522 $contact_form->form = stripslashes_deep( maybe_unserialize( $row->form ) );
523 $contact_form->mail = stripslashes_deep( maybe_unserialize( $row->mail ) );
524 $contact_form->mail_2 = stripslashes_deep( maybe_unserialize( $row->mail_2 ) );
525 $contact_form->messages = stripslashes_deep( maybe_unserialize( $row->messages ) );
526 $contact_form->additional_settings = stripslashes_deep( maybe_unserialize( $row->additional_settings ) );
527
528 $contact_form->upgrade();
529
530 return $contact_form;
531}
532
533function wpcf7_contact_form_default_pack() {
534 $contact_form = new WPCF7_ContactForm();
535 $contact_form->initial = true;
536
537 $contact_form->title = __( 'Untitled', 'wpcf7' );
538 $contact_form->form = wpcf7_default_form_template();
539 $contact_form->mail = wpcf7_default_mail_template();
540 $contact_form->mail_2 = wpcf7_default_mail_2_template();
541 $contact_form->messages = wpcf7_default_messages_template();
542
543 return $contact_form;
544}
545
546?>
Note: See TracBrowser for help on using the repository browser.