aboutsummaryrefslogtreecommitdiff
path: root/controllers
diff options
context:
space:
mode:
authorThedro Neely <thedroneely@gmail.com>2019-03-16 04:31:28 -0400
committerThedro Neely <thedroneely@gmail.com>2019-03-16 04:31:28 -0400
commita74923f43a7d1a0a70fba80fe6209fe43aac095a (patch)
tree50a186b2cac3b1235916d44641153a3c326dd9a2 /controllers
parent162883b2b18b15c54cfa530a2e13c3f9aa83f71f (diff)
downloadedwinmattiacci.com-a74923f43a7d1a0a70fba80fe6209fe43aac095a.tar.gz
edwinmattiacci.com-a74923f43a7d1a0a70fba80fe6209fe43aac095a.tar.bz2
edwinmattiacci.com-a74923f43a7d1a0a70fba80fe6209fe43aac095a.zip
controllers/contact: Merge mail and contact controllers
Check for post and get requests Optimize mailer logic
Diffstat (limited to 'controllers')
-rw-r--r--controllers/contact.controller.php55
-rw-r--r--controllers/contact.php3
-rw-r--r--controllers/mail.controller.php34
3 files changed, 55 insertions, 37 deletions
diff --git a/controllers/contact.controller.php b/controllers/contact.controller.php
new file mode 100644
index 0000000..36a0c73
--- /dev/null
+++ b/controllers/contact.controller.php
@@ -0,0 +1,55 @@
+<?php
+
+use PHPMailer\PHPMailer\PHPMailer;
+use PHPMailer\PHPMailer\Exception;
+
+if ($_SERVER['REQUEST_METHOD'] === 'POST') {
+
+ $submit = new Form();
+
+ $submit->name = $_POST['26471'] ?? null;
+ $submit->email = $_POST['26472'] ?? null;
+ $submit->message = $_POST['26473'] ?? null;
+
+ $spam = $_POST['agree'] ?? false;
+
+ $validEmail = PHPMailer::validateAddress($submit->email, 'auto');
+
+ if ((bool) empty($submit->name) == true
+ || (bool) empty($submit->email) == true
+ || (bool) empty($submit->message) == true
+ ) {
+ include '../views/contact.view.php';
+ return;
+ }
+
+ if ((bool) $spam == true) {
+ http_response_code(403);
+ error_log('Contact Form Spam: Error 403');
+ return;
+ }
+
+ if ($validEmail === false ) {
+ $emailError = 'Invalid email: ' . "\r" . '<b>'. $submit->email . '</b>';
+ include '../views/contact.view.php';
+ return;
+ }
+
+ $formSuccess = true;
+
+ include '../views/contact.view.php';
+ fastcgi_finish_request();
+
+ $contact['database']->insert(
+ 'contactform', [
+ 'name' => $submit->name,
+ 'email' => $submit->email,
+ 'message' => $submit->message,
+ ]
+ );
+
+ sendMail($submit->name, $submit->email, $submit->message);
+ return;
+}
+
+require '../views/contact.view.php';
diff --git a/controllers/contact.php b/controllers/contact.php
deleted file mode 100644
index 6ecc88e..0000000
--- a/controllers/contact.php
+++ /dev/null
@@ -1,3 +0,0 @@
-<?php
-
-require '../views/contact.view.php';
diff --git a/controllers/mail.controller.php b/controllers/mail.controller.php
deleted file mode 100644
index 262f999..0000000
--- a/controllers/mail.controller.php
+++ /dev/null
@@ -1,34 +0,0 @@
-<?php
-
-$submit = new Form();
-
-$submit->name = $_POST['26471'] ?? null;
-$submit->email = $_POST['26472'] ?? null;
-$submit->message = $_POST['26478'] ?? null;
-
-$spam = $_POST['contact'] ?? false;
-
-if ((bool) $spam == true) {
- http_response_code(403);
- error_log('Contact Form Spam: Error 403');
- return;
-}
-
-if ((bool) empty($submit->name) == true
- || (bool) empty($submit->email) == true
- || (bool) empty($submit->message) == true
-) {
- header('Location: /contact');
- return;
-} else {
- include '../views/sent.view.php';
- fastcgi_finish_request();
- $contact['database']->insert(
- 'contactform', [
- 'name' => $submit->name,
- 'email' => $submit->email,
- 'message' => $submit->message,
- ]
- );
- sendMail($submit->name, $submit->email, $submit->message);
-}