From db7bbc2aa214aad2c8d8adb79d5454a998657a74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20Rog=C3=A9rio?= Date: Sun, 20 Nov 2016 00:35:17 -0200 Subject: [PATCH 1/3] Migrated to VanillaJS :satisfied: --- contact-form/public/js/contact-form.js | 124 +++++++++++++++++-------- 1 file changed, 85 insertions(+), 39 deletions(-) diff --git a/contact-form/public/js/contact-form.js b/contact-form/public/js/contact-form.js index 15c33b7..f07252f 100644 --- a/contact-form/public/js/contact-form.js +++ b/contact-form/public/js/contact-form.js @@ -1,53 +1,99 @@ -(function ($, window, document, undefined) { +(function (root, factory) { 'use strict'; - function isSafari() { - return /^((?!chrome).)*safari/i.test(navigator.userAgent); + if (typeof define === 'function' && define.amd) { + define([], factory); + } else if (typeof exports === 'object') { + module.exports = factory(); + } else { + root.ContactForm = factory(); } +}(this, function () { + 'use strict'; - function addError (el) { - return el.parent().addClass('has-error'); - }; + var ContactForm = function (target) { + if (!this || !(this instanceof ContactForm)) { + return new ContactForm(target); + } - var inputElem = document.createElement('input'); + this.form = target instanceof Node ? target : document.querySelector(target); - if (!('required' in inputElem) || isSafari()) { - $('#contact-form').submit(function () { - var hasError = false, - name = $('#form-name'), - mail = $('#form-email'), - subject = $('#form-subject'), - message = $('#form-message'), - testmail = /^[^0-9][A-z0-9._%+-]+([.][A-z0-9_]+)*[@][A-z0-9_-]+([.][A-z0-9_]+)*[.][A-z]{2,4}$/, - $this = $(this); + if (this.form === null) { + return; + } - $this.find('div').removeClass('has-error'); + this.init(); + }; - if (name.val() === '') { - hasError = true; - addError(name); + ContactForm.prototype = { + hasClass: function (el, name) { + return new RegExp('(\\s|^)' + name + '(\\s|$)').test(el.className); + }, + addClass: function (el, name) { + if (!this.hasClass(el, name)) { + el.className += (el.className ? ' ' : '') + name; } - - if (!testmail.test(mail.val())) { - hasError = true; - addError(mail); + }, + addError: function (el) { + return this.addClass(el.parentNode, 'has-error'); + }, + removeClass: function (el, name) { + if (this.hasClass(el, name)) { + el.className = el.className.replace(new RegExp('(\\s|^)' + name + '(\\s|$)'), ' ').replace(/^\s+|\s+$/g, ''); } + }, + validate: function () { + var elements = Array.prototype.slice.call(document.querySelectorAll('.form-control')); - if (subject.val() === '') { - hasError = true; - addError(subject); - } + this.form.addEventListener('submit', function(e) { + if (!event.target.checkValidity()) { + e.preventDefault(); - if (message.val() === '') { - hasError = true; - addError(message); - } + elements.map(function(element) { + if (this.hasClass(element.parentNode, 'has-error')) { + this.removeClass(element.parentNode, 'has-error'); + } + }.bind(this)); - if (hasError === false) { - return true; - } + var hasError = false, + name = document.querySelector('#form-name'), + email = document.querySelector('#form-email'), + subject = document.querySelector('#form-subject'), + message = document.querySelector('#form-message'), + // @from: https://html.spec.whatwg.org/multipage/forms.html#e-mail-state-(type=email) + testmail = /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/; - return false; - }); - } -}(jQuery, window, document)); \ No newline at end of file + if (name.value === '') { + hasError = true; + this.addError(name); + } + + if (!testmail.test(email.value)) { + hasError = true; + this.addError(email); + } + + if (subject.value === '') { + hasError = true; + this.addError(subject); + } + + if (message.value === '') { + hasError = true; + this.addError(message); + } + + if (hasError === false) { + this.form.submit(); + } + } + }.bind(this), false); + }, + init: function () { + document.addEventListener('DOMContentLoaded', this.validate.bind(this), false); + } + }; + + + return ContactForm; +})); \ No newline at end of file From 6587aa47d312496940442126d9fb24d3f5286873 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20Rog=C3=A9rio?= Date: Sun, 20 Nov 2016 00:36:23 -0200 Subject: [PATCH 2/3] Added method to set Sender Email --- contact-form/vendor/SimpleMail/SimpleMail.class.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/contact-form/vendor/SimpleMail/SimpleMail.class.php b/contact-form/vendor/SimpleMail/SimpleMail.class.php index c163bd0..86e41ce 100644 --- a/contact-form/vendor/SimpleMail/SimpleMail.class.php +++ b/contact-form/vendor/SimpleMail/SimpleMail.class.php @@ -15,6 +15,7 @@ class SimpleMail protected $to; protected $from; protected $sender; + protected $sender_email; protected $subject; protected $html; @@ -33,6 +34,11 @@ public function setSender($sender) $this->sender = $sender; } + public function setSenderEmail($sender_email) + { + $this->sender_email = $sender_email; + } + public function setSubject($subject) { $this->subject = $subject; @@ -50,8 +56,8 @@ public function send() $header = 'MIME-Version: 1.0' . PHP_EOL; $header .= 'To: <' . $this->to . '>' . PHP_EOL; $header .= 'Date: ' . date('D, d M Y H:i:s O') . PHP_EOL; - $header .= 'From: =?UTF-8?B?' . base64_encode($this->sender) . '?= <' . $this->from . '>' . PHP_EOL; - $header .= 'Reply-To: =?UTF-8?B?' . base64_encode($this->sender) . '?= <' . $this->from . '>' . PHP_EOL; + $header .= 'From: =?UTF-8?B?' . base64_encode($this->sender) . '?= <' . $this->sender_email . '>' . PHP_EOL; + $header .= 'Reply-To: =?UTF-8?B?' . base64_encode($this->sender) . '?= <' . $this->sender_email . '>' . PHP_EOL; $header .= 'Return-Path: ' . $this->from . PHP_EOL; $header .= 'X-Mailer: PHP/' . phpversion() . PHP_EOL; $header .= 'X-Priority: 3' . PHP_EOL; From 85c16ea49d557f7ab1818d245bf77977512f5c2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20Rog=C3=A9rio?= Date: Sun, 20 Nov 2016 00:37:00 -0200 Subject: [PATCH 3/3] Minor code improvements --- contact-form/index.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/contact-form/index.php b/contact-form/index.php index dff14ad..1dc3360 100644 --- a/contact-form/index.php +++ b/contact-form/index.php @@ -33,6 +33,7 @@ $mail->setTo($config->get('emails.to')); $mail->setFrom($config->get('emails.from')); $mail->setSender($name); + $mail->setSenderEmail($email); $mail->setSubject($config->get('subject.prefix') . ' ' . $subject); $body = " @@ -68,8 +69,10 @@
-

Simple PHP Contact Form

-

A Simple Contact Form developed in PHP with HTML5 Form validation. Has a fallback in jQuery for browsers that do not support HTML5 form validation.

+
+

Simple PHP Contact Form

+

A Simple Contact Form developed in PHP with HTML5 Form validation. Has a fallback in jQuery pure JavaScript for browsers that do not support HTML5 form validation.

+
@@ -123,12 +126,9 @@
- - - - + \ No newline at end of file