PageRenderTime 46ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/demo/data/contact-dist.php

http://simplemodal.googlecode.com/
PHP | 203 lines | 133 code | 32 blank | 38 comment | 30 complexity | 540be0b90e3bb8424faa62bdc17e7149 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0
  1. <?php
  2. /*
  3. * SimpleModal Contact Form
  4. * http://simplemodal.com
  5. *
  6. * Copyright (c) 2013 Eric Martin - http://ericmmartin.com
  7. *
  8. * Licensed under the MIT license:
  9. * http://www.opensource.org/licenses/mit-license.php
  10. */
  11. date_default_timezone_set('America/Los_Angeles');
  12. // User settings
  13. $to = "user@yourdomain.com";
  14. $subject = "SimpleModal Contact Form";
  15. // Include extra form fields and/or submitter data?
  16. // false = do not include
  17. $extra = array(
  18. "form_subject" => true,
  19. "form_cc" => true,
  20. "ip" => true,
  21. "user_agent" => true
  22. );
  23. // Process
  24. $action = isset($_POST["action"]) ? $_POST["action"] : "";
  25. if (empty($action)) {
  26. // Send back the contact form HTML
  27. $output = "<div style='display:none'>
  28. <div class='contact-top'></div>
  29. <div class='contact-content'>
  30. <h1 class='contact-title'>Send us a message:</h1>
  31. <div class='contact-loading' style='display:none'></div>
  32. <div class='contact-message' style='display:none'></div>
  33. <form action='#' style='display:none'>
  34. <label for='contact-name'>*Name:</label>
  35. <input type='text' id='contact-name' class='contact-input' name='name' tabindex='1001' />
  36. <label for='contact-email'>*Email:</label>
  37. <input type='text' id='contact-email' class='contact-input' name='email' tabindex='1002' />";
  38. if ($extra["form_subject"]) {
  39. $output .= "
  40. <label for='contact-subject'>Subject:</label>
  41. <input type='text' id='contact-subject' class='contact-input' name='subject' value='' tabindex='1003' />";
  42. }
  43. $output .= "
  44. <label for='contact-message'>*Message:</label>
  45. <textarea id='contact-message' class='contact-input' name='message' cols='40' rows='4' tabindex='1004'></textarea>
  46. <br/>";
  47. if ($extra["form_cc"]) {
  48. $output .= "
  49. <label>&nbsp;</label>
  50. <input type='checkbox' id='contact-cc' name='cc' value='1' tabindex='1005' /> <span class='contact-cc'>Send me a copy</span>
  51. <br/>";
  52. }
  53. $output .= "
  54. <label>&nbsp;</label>
  55. <button type='submit' class='contact-send contact-button' tabindex='1006'>Send</button>
  56. <button type='submit' class='contact-cancel contact-button simplemodal-close' tabindex='1007'>Cancel</button>
  57. <br/>
  58. <input type='hidden' name='token' value='" . smcf_token($to) . "'/>
  59. </form>
  60. </div>
  61. <div class='contact-bottom'><a href='http://www.ericmmartin.com/projects/simplemodal/'>Powered by SimpleModal</a></div>
  62. </div>";
  63. echo $output;
  64. }
  65. else if ($action == "send") {
  66. // Send the email
  67. $name = isset($_POST["name"]) ? $_POST["name"] : "";
  68. $email = isset($_POST["email"]) ? $_POST["email"] : "";
  69. $subject = isset($_POST["subject"]) ? $_POST["subject"] : $subject;
  70. $message = isset($_POST["message"]) ? $_POST["message"] : "";
  71. $cc = isset($_POST["cc"]) ? $_POST["cc"] : "";
  72. $token = isset($_POST["token"]) ? $_POST["token"] : "";
  73. // make sure the token matches
  74. if ($token === smcf_token($to)) {
  75. smcf_send($name, $email, $subject, $message, $cc);
  76. echo "Your message was successfully sent.";
  77. }
  78. else {
  79. echo "Unfortunately, your message could not be verified.";
  80. }
  81. }
  82. function smcf_token($s) {
  83. return md5("smcf-" . $s . date("WY"));
  84. }
  85. // Validate and send email
  86. function smcf_send($name, $email, $subject, $message, $cc) {
  87. global $to, $extra;
  88. // Filter and validate fields
  89. $name = smcf_filter($name);
  90. $subject = smcf_filter($subject);
  91. $email = smcf_filter($email);
  92. if (!smcf_validate_email($email)) {
  93. $subject .= " - invalid email";
  94. $message .= "\n\nBad email: $email";
  95. $email = $to;
  96. $cc = 0; // do not CC "sender"
  97. }
  98. // Add additional info to the message
  99. if ($extra["ip"]) {
  100. $message .= "\n\nIP: " . $_SERVER["REMOTE_ADDR"];
  101. }
  102. if ($extra["user_agent"]) {
  103. $message .= "\n\nUSER AGENT: " . $_SERVER["HTTP_USER_AGENT"];
  104. }
  105. // Set and wordwrap message body
  106. $body = "From: $name\n\n";
  107. $body .= "Message: $message";
  108. $body = wordwrap($body, 70);
  109. // Build header
  110. $headers = "From: $email\n";
  111. if ($cc == 1) {
  112. $headers .= "Cc: $email\n";
  113. }
  114. $headers .= "X-Mailer: PHP/SimpleModalContactForm";
  115. // UTF-8
  116. if (function_exists('mb_encode_mimeheader')) {
  117. $subject = mb_encode_mimeheader($subject, "UTF-8", "B", "\n");
  118. }
  119. else {
  120. // you need to enable mb_encode_mimeheader or risk
  121. // getting emails that are not UTF-8 encoded
  122. }
  123. $headers .= "MIME-Version: 1.0\n";
  124. $headers .= "Content-type: text/plain; charset=utf-8\n";
  125. $headers .= "Content-Transfer-Encoding: quoted-printable\n";
  126. // Send email
  127. @mail($to, $subject, $body, $headers) or
  128. die("Unfortunately, a server issue prevented delivery of your message.");
  129. }
  130. // Remove any un-safe values to prevent email injection
  131. function smcf_filter($value) {
  132. $pattern = array("/\n/","/\r/","/content-type:/i","/to:/i", "/from:/i", "/cc:/i");
  133. $value = preg_replace($pattern, "", $value);
  134. return $value;
  135. }
  136. // Validate email address format in case client-side validation "fails"
  137. function smcf_validate_email($email) {
  138. $at = strrpos($email, "@");
  139. // Make sure the at (@) sybmol exists and
  140. // it is not the first or last character
  141. if ($at && ($at < 1 || ($at + 1) == strlen($email)))
  142. return false;
  143. // Make sure there aren't multiple periods together
  144. if (preg_match("/(\.{2,})/", $email))
  145. return false;
  146. // Break up the local and domain portions
  147. $local = substr($email, 0, $at);
  148. $domain = substr($email, $at + 1);
  149. // Check lengths
  150. $locLen = strlen($local);
  151. $domLen = strlen($domain);
  152. if ($locLen < 1 || $locLen > 64 || $domLen < 4 || $domLen > 255)
  153. return false;
  154. // Make sure local and domain don't start with or end with a period
  155. if (preg_match("/(^\.|\.$)/", $local) || preg_match("/(^\.|\.$)/", $domain))
  156. return false;
  157. // Check for quoted-string addresses
  158. // Since almost anything is allowed in a quoted-string address,
  159. // we're just going to let them go through
  160. if (!preg_match('/^"(.+)"$/', $local)) {
  161. // It's a dot-string address...check for valid characters
  162. if (!preg_match('/^[-a-zA-Z0-9!#$%*\/?|^{}`~&\'+=_\.]*$/', $local))
  163. return false;
  164. }
  165. // Make sure domain contains only valid characters and at least one period
  166. if (!preg_match("/^[-a-zA-Z0-9\.]*$/", $domain) || !strpos($domain, "."))
  167. return false;
  168. return true;
  169. }
  170. exit;
  171. ?>