aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThedro Neely <thedroneely@gmail.com>2019-03-28 21:24:35 -0400
committerThedro Neely <thedroneely@gmail.com>2019-03-28 21:24:35 -0400
commitd79ee71389f9160f4cd82845917c4c41e7fde3f6 (patch)
tree086877f0b4fa90b313a516fd96fefa1bb8671b07
parenta8f94c559a44d743a1094cdf720207389a7684a3 (diff)
downloadthedroneely.com-d79ee71389f9160f4cd82845917c4c41e7fde3f6.tar.gz
thedroneely.com-d79ee71389f9160f4cd82845917c4c41e7fde3f6.tar.bz2
thedroneely.com-d79ee71389f9160f4cd82845917c4c41e7fde3f6.zip
app/controllers/contact: Refactor contact controller
Use single contact route
-rw-r--r--app/Routes.php2
-rw-r--r--app/controllers/contact.controller.php47
-rw-r--r--app/model/Form.php34
3 files changed, 58 insertions, 25 deletions
diff --git a/app/Routes.php b/app/Routes.php
index 6b81ee3..1784dc6 100644
--- a/app/Routes.php
+++ b/app/Routes.php
@@ -16,4 +16,4 @@ $router->head('', '../app/controllers/index.controller.php');
$router->head('contact', '../app/controllers/contact.controller.php');
-$router->post('?sent', '../app/controllers/mail.controller.php');
+$router->post('contact', '../app/controllers/contact.controller.php');
diff --git a/app/controllers/contact.controller.php b/app/controllers/contact.controller.php
index 60f5f08..803a67c 100644
--- a/app/controllers/contact.controller.php
+++ b/app/controllers/contact.controller.php
@@ -1,3 +1,50 @@
<?php
+if ($_SERVER['REQUEST_METHOD'] === 'POST') {
+
+ $form = new Form();
+
+ $form->name = $name = $_POST['26471'] ?? null;
+ $form->email = $email = $_POST['26472'] ?? null;
+ $form->message = $message = $_POST['26478'] ?? null;
+ $form->spam = $spam = $_POST['agree'] ?? false;
+
+ if ($form->isEmpty()) {
+ include '../app/views/contact.view.php';
+ return;
+ }
+
+ if ($form->isSpam()) {
+ error_log('Contact Form Spam: Error 403');
+ return http_response_code(403);
+ }
+
+ if ($form->isEmailValid() === false) {
+ $emailError = 'Invalid email: ' . "\r" . '<b>'. $email . '</b>';
+ include '../app/views/contact.view.php';
+ return;
+ }
+
+ $formSuccess = true;
+
+ $name = null;
+ $email = null;
+ $message = null;
+ $spam = false;
+
+ include '../app/views/contact.view.php';
+ fastcgi_finish_request();
+
+ $contact['database']->insert(
+ 'contactform', [
+ 'name' => $form->name,
+ 'email' => $form->email,
+ 'message' => $form->message,
+ ]
+ );
+
+ $form->isSubmit();
+ return;
+}
+
require '../app/views/contact.view.php';
diff --git a/app/model/Form.php b/app/model/Form.php
index 23e5613..7d972e7 100644
--- a/app/model/Form.php
+++ b/app/model/Form.php
@@ -8,43 +8,30 @@ class Form
public $name;
public $email;
public $message;
-
- public function __construct($name, $email, $message)
- {
- $this->name = $name;
- $this->email = $email;
- $this->message = $message;
-
- $this->isSpam();
- $this->isEmpty();
- }
+ public $spam;
public function isSpam()
{
- $spam = false;
-
- if (isset($_POST['contact'])) {
- $spam = $_POST['contact'];
- }
-
- if ((bool) $spam == true) {
- http_response_code(403);
+ if ((bool) $this->spam == true) {
error_log('Contact Form Spam: Error 403');
- exit;
+ return true;
}
}
public function isEmpty()
{
- if ((bool) empty($this->name) == true
- || (bool) empty($this->email) == true
+ if ((bool) empty($this->email) == true
|| (bool) empty($this->message) == true
) {
- header('Location: /contact/');
- exit;
+ return true;
}
}
+ public function isEmailValid()
+ {
+ return $email = PHPMailer::validateAddress($this->email, 'auto');
+ }
+
public function isSubmit()
{
// Include mail config
@@ -54,7 +41,6 @@ class Form
try {
//Server settings
- //$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = $config['mail']['host']; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication