Skip to content

Commit

Permalink
Merge branch 'development'
Browse files Browse the repository at this point in the history
  • Loading branch information
pinceladasdaweb committed Nov 20, 2016
2 parents 2755f49 + 85c16ea commit f0efeeb
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 49 deletions.
16 changes: 8 additions & 8 deletions contact-form/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "
Expand Down Expand Up @@ -68,8 +69,10 @@
</head>
<body>
<div class="jumbotron">
<h1>Simple PHP Contact Form</h1>
<p>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.</p>
<div class="container">
<h1>Simple PHP Contact Form</h1>
<p>A Simple Contact Form developed in PHP with HTML5 Form validation. Has a fallback in <strike>jQuery</strike> pure JavaScript for browsers that do not support HTML5 form validation.</p>
</div>
</div>
<?php if(!empty($emailSent)): ?>
<div class="col-md-6 col-md-offset-3">
Expand Down Expand Up @@ -123,12 +126,9 @@
</div>
<?php endif; ?>

<!--[if lt IE 9]>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<![endif]-->
<!--[if gte IE 9]><!-->
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<!--<![endif]-->
<script type="text/javascript" src="public/js/contact-form.js"></script>
<script type="text/javascript">
new ContactForm('#contact-form');
</script>
</body>
</html>
124 changes: 85 additions & 39 deletions contact-form/public/js/contact-form.js
Original file line number Diff line number Diff line change
@@ -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));
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;
}));
10 changes: 8 additions & 2 deletions contact-form/vendor/SimpleMail/SimpleMail.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class SimpleMail
protected $to;
protected $from;
protected $sender;
protected $sender_email;
protected $subject;
protected $html;

Expand All @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit f0efeeb

Please sign in to comment.