addRoutingMiddleware(); $ssConfig = new SameSiteCookieConfiguration(["same_site" => "strict"]); // add in same site cookie stuff $app->add(new SameSiteCookieMiddleware($ssConfig)); // for error checking $errorMiddleware = $app->addErrorMiddleware(true, true, true); $app->setBasePath("/api"); $timelineData = new timelineData(); $projectData = new projectData(); $app->get("/timelineData/{timeline}", function (Request $request, Response $response, array $args) { global $timelineData; $json = $result = ""; //check if route is available if it is get the data //otherwise return an error if($args["timeline"] == "edu") { $result = $timelineData->getEduData(); } else if($args["timeline"] == "work") { $result = $timelineData->getWorkData(); } else { $result = array(array("errorMessage" => "Error, timeline data not found")); } $json = json_encode($result); $response->getBody()->write($json); //if it is an error give a 404 code since it can't find the data if(array_key_exists("errorMessage", $result)) { $response = $response->withStatus(404); } //use content type json to indicate json data on frontend. return $response->withHeader("Content-Type", "application/json"); }); $app->get('/projectData', function (Request $request, Response $response) { global $projectData; $result = $projectData->getProjectData(); $json = json_encode($result); $response->getBody()->write($json); if(array_key_exists("errorMessage", $result)) { $response = $response->withStatus(404); } //use content type json to indicate json data on frontend. return $response->withHeader("Content-Type", "application/json"); }); $app->post('/contact', function (Request $request, Response $response) { $data = $request->getParsedBody(); if(empty($data["fName"]) || empty($data["lName"]) || empty($data["email"]) || empty($data["subject"]) || empty($data["message"])) { $response->getBody()->write(json_encode(array("errorMessage" => "Please fill out all the fields"))); $response = $response->withStatus(400); return $response->withHeader("Content-Type", "application/json"); } if (!filter_var($data["email"], FILTER_VALIDATE_EMAIL)) { $response->getBody()->write(json_encode(array("errorMessage" => "Email is not the correct format"))); $response = $response->withStatus(400); return $response->withHeader("Content-Type", "application/json"); } // email form filler/conatcter $headers1 = "From: noreply@rohitpai.tech\r\n"; $headers1 .= "Reply-To: rohit@rohitpai.tech\r\n"; $headers1 .= "MIME-Version: 1.0\r\n"; $headers1 .= "Content-Type: text/html; charset=UTF-8\r\n"; $message1 = "
Thank you for filling out the form on my website, I will try to respond to your query as soon as I can.
Below is what you filled in for your record
Firstname | Lastname | Subject | message | |
---|---|---|---|---|
{$data['fName']} | {$data['lName']} | {$data['email']} | {$data['subject']} | {$data['message']} |
Regards,
Rohit Pai
rohit@rohitpai.tech
";
mail($data["email"], $data["subject"], $message1, $headers1);
// email to me
$headers2 = "From: noreply@rohitpai.tech\r\n";
$headers2 .= "Reply-To: {$data['email']}\r\n";
$headers2 .= "MIME-Version: 1.0\r\n";
$headers2 .= "Content-Type: text/html; charset=UTF-8\r\n";
$message2 = "
{$data['fName']} {$data['lName']} filled in the form on the website, here is what they sent.
Firstname | Lastname | Subject | message | |
---|---|---|---|---|
{$data['fName']} | {$data['lName']} | {$data['email']} | {$data['subject']} | {$data['message']} |