/data/wpcom-themes/sweet-blossoms/contact.php

https://gitlab.com/Blueprint-Marketing/wordpress-unit-tests · PHP · 110 lines · 86 code · 18 blank · 6 comment · 12 complexity · 421d7a1d76047d0c1906a905f3d36336 MD5 · raw file

  1. <?php
  2. /*
  3. Template Name: contact
  4. */
  5. ?>
  6. <?php get_header(); ?>
  7. <!-- content -->
  8. <div id="content">
  9. <?php if (have_posts()) : ?>
  10. <?php while (have_posts()) : the_post(); ?>
  11. <?php
  12. //validate email adress
  13. function is_valid_email($email)
  14. {
  15. return (eregi ("^([a-z0-9_]|\\-|\\.)+@(([a-z0-9_]|\\-)+\\.)+[a-z]{2,4}$", $email));
  16. }
  17. //clean up text
  18. function clean($text)
  19. {
  20. return stripslashes($text);
  21. }
  22. //encode special chars (in name and subject)
  23. function encodeMailHeader ($string, $charset = 'UTF-8')
  24. {
  25. return sprintf ('=?%s?B?%s?=', strtoupper ($charset),base64_encode ($string));
  26. }
  27. $bx_name = (!empty($_POST['bx_name'])) ? $_POST['bx_name'] : "";
  28. $bx_email = (!empty($_POST['bx_email'])) ? $_POST['bx_email'] : "";
  29. $bx_url = (!empty($_POST['bx_url'])) ? $_POST['bx_url'] : "";
  30. $bx_subject = (!empty($_POST['bx_subject'])) ? $_POST['bx_subject'] : "";
  31. $bx_message = (!empty($_POST['bx_message'])) ? $_POST['bx_message'] : "";
  32. $bx_subject = clean($bx_subject);
  33. $bx_message = clean($bx_message);
  34. $error_msg = "";
  35. $send = 0;
  36. if (!empty($_POST['submit'])) {
  37. $send = 1;
  38. if (empty($bx_name) || empty($bx_email) || empty($bx_subject) || empty($bx_message)) {
  39. $error_msg.= "<p><strong>Please fill in all required fields.</strong></p>\n";
  40. $send = 0;
  41. }
  42. if (!is_valid_email($bx_email)) {
  43. $error_msg.= "<p><strong>Your email adress failed to validate.</strong></p>\n";
  44. $send = 0;
  45. }
  46. }
  47. if (!$send) { ?>
  48. <h2><?php the_title(); ?></h2>
  49. <?php
  50. the_content();
  51. echo $error_msg;
  52. ?>
  53. <form method="post" action="<?php echo attribute_escape("http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']); ?>" id="contactform">
  54. <fieldset>
  55. <p><label for="bx_name">Name</label> <input type="text" name="bx_name" id="bx_name" value="<?php echo $bx_name; ?>" tabindex="1" /></p>
  56. <p><label for="bx_email">Email</label> <input type="text" name="bx_email" id="bx_email" value="<?php echo $bx_email; ?>" tabindex="2" /></p>
  57. <p><label for="bx_url">Url</label> <input type="text" name="bx_url" id="bx_url" value="<?php echo $bx_url; ?>" tabindex="3" /> <em>Optional</em></p>
  58. <p><label for="bx_subject">Subject</label> <input type="text" name="bx_subject" id="bx_subject" value='<?php echo $bx_subject; ?>' tabindex="4" /></p>
  59. <p><label for="bx_message">Message</label> <textarea name="bx_message" id="bx_message" cols="45" rows="10" tabindex="5"><?php echo $bx_message; ?></textarea></p>
  60. <p><input type="submit" name="submit" value="Submit" class="button" tabindex="6" /></p>
  61. </fieldset>
  62. </form>
  63. <?php
  64. } else {
  65. $displayName_array = explode(" ",$bx_name);
  66. $displayName = htmlentities(utf8_decode($displayName_array[0]));
  67. $header = "MIME-Version: 1.0\n";
  68. $header .= "Content-Type: text/plain; charset=\"utf-8\"\n";
  69. $header .= "From:" . encodeMailHeader($bx_name) . "<" . $bx_email . ">\n";
  70. $email_subject = "[" . get_settings('blogname') . "] " . encodeMailHeader($bx_subject);
  71. $email_text = "From......: " . $bx_name . "\n" .
  72. "Email.....: " . $bx_email . "\n" .
  73. "Url.......: " . $bx_url . "\n\n" .
  74. "..........................................................\n" .
  75. "Subject...: " . $bx_subject . "\n" .
  76. "..........................................................\n\n" .
  77. $bx_message;
  78. if (@mail(get_settings('admin_email'), $email_subject, $email_text, $header)) {
  79. echo "<h2>Hey " . $displayName . ",</h2><p>thanks for your message! I'll get back to you as soon as possible.</p>";
  80. }
  81. }
  82. ?>
  83. <?php endwhile; ?>
  84. <?php endif; ?>
  85. </div>
  86. <!-- /content -->
  87. <?php get_footer(); ?>