setBasePath("/api"); // Add middleware new middleware($app); $timelineData = new timelineData(); $projectData = new projectData(); $blogData = new blogData(); $user = new user(); $app->get("/timelineData/{timeline}", function (Request $request, Response $response, array $args) { global $timelineData; //check if route is available if it is get the data //otherwise return an error if($args["timeline"] == "edu") { $response->getBody()->write(json_encode($timelineData->getEduData())); return $response; } if($args["timeline"] == "work") { $response->getBody()->write(json_encode($timelineData->getWorkData())); return $response; } // something went wrong $response->getBody()->write(json_encode(array("errorMessage" => "Error, timeline data not found"))); return $response->withStatus(404); }); $app->patch("/timelineData/{timeline}/{id}", function (Request $request, Response $response, array $args) { global $timelineData; $data = $request->getParsedBody(); if ($args["timeline"] == "edu" && $args["id"] != "undefined") { if (empty($data["dateFrom"]) || empty($data["dateTo"]) || empty($data["grade"]) || empty($data["course"])) { // uh oh sent some empty data $response->getBody()->write(json_encode(array("error" => "Only some of the data was sent"))); return $response->withStatus(400); } if (!$timelineData->updateEduData($data["dateFrom"], $data["dateTo"], $data["grade"], $data["course"], $args["id"])) { // uh oh something went wrong $response->getBody()->write(json_encode(array("error" => "Something went wrong"))); return $response->withStatus(500); } $response->withStatus(201); return $response; } if ($args["timeline"] == "work" && $args["id"] != "undefined") { if (empty($data["dateFrom"]) || empty($data["dateTo"]) || empty($data["companyName"]) || empty($data["area"]) || empty($data["title"])) { // uh oh sent some empty data $response->getBody()->write(json_encode(array("error" => "Only some of the data was sent"))); return $response->withStatus(400); } if (!$timelineData->updateWorkData($data["dateFrom"], $data["dateTo"], $data["companyName"], $data["area"], $data["title"], $args["id"])) { // uh oh something went wrong $response->getBody()->write(json_encode(array("error" => "Something went wrong"))); return $response->withStatus(500); } $response->withStatus(201); return $response; } $response->getBody()->write(json_encode(array("error" => "The correct data was not sent"))); return $response->withStatus(400); }); $app->delete("/timelineData/{timeline}/{id}", function (Request $request, Response $response, array $args) { global $timelineData; if ($args["timeline"] == "edu" && $args["id"] != null) { if (!$timelineData->deleteEduData($args["id"])) { // uh oh something went wrong $response->getBody()->write(json_encode(array("error" => "Something went wrong"))); return $response->withStatus(500); } return $response; } if ($args["timeline"] == "work" && $args["id"] != null) { if (!$timelineData->deleteWorkData($args["id"])) { // uh oh something went wrong $response->getBody()->write(json_encode(array("error" => "Something went wrong"))); return $response->withStatus(500); } return $response; } $response->getBody()->write(json_encode(array("error" => "The correct data was not sent"))); return $response->withStatus(400); }); $app->post("/timelineData/{timeline}", function (Request $request, Response $response, array $args) { global $timelineData; $data = $request->getParsedBody(); if ($args["timeline"] == "edu") { if (empty($data["dateFrom"]) || empty($data["dateTo"]) || empty($data["grade"]) || empty($data["course"])) { // uh oh sent some empty data $response->getBody()->write(json_encode(array("error" => "Only some of the data was sent"))); return $response->withStatus(400); } $insertedID = $timelineData->addEduData($data["dateFrom"], $data["dateTo"], $data["grade"], $data["course"]); if (!is_int($insertedID)) { // uh oh something went wrong $response->getBody()->write(json_encode(array("error" => "Something went wrong"))); return $response->withStatus(500); } $response->getBody()->write(json_encode(array("ID" => $insertedID))); $response->withStatus(201); return $response; } if ($args["timeline"] == "work") { if (empty($data["dateFrom"]) || empty($data["companyName"]) || empty($data["area"]) || empty($data["title"])) { // uh oh sent some empty data $response->getBody()->write(json_encode(array("error" => "Only some of the data was sent"))); return $response->withStatus(400); } if (empty($data["dateTo"])) { $data["dateTo"] = ""; } $insertedID = $timelineData->addWorkData($data["dateFrom"], $data["dateTo"], $data["companyName"], $data["area"], $data["title"]); if (!is_int($insertedID)) { // uh oh something went wrong $response->getBody()->write(json_encode(array("error" => "Something went wrong"))); return $response->withStatus(500); } $response->getBody()->write(json_encode(array("ID" => $insertedID))); $response->withStatus(201); return $response; } $response->getBody()->write(json_encode(array("error" => "The correct data was not sent"))); return $response->withStatus(400); }); $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; }); $app->patch("/projectData/{id}", function (Request $request, Response $response, array $args) { global $projectData; $data = $request->getParsedBody(); if ($args["id"] != "undefined") { if (empty($data["title"]) || empty($data["isMainProject"]) || empty($data["information"]) || empty($data["gitLink"])) { // uh oh sent some empty data $response->getBody()->write(json_encode(array("error" => "Only some of the data was sent"))); return $response->withStatus(400); } $update = $projectData->updateProjectData($args["id"], $data["title"], $data["isMainProject"], $data["information"], $data["projectLink"], $data["gitLink"]); if ($update === "unset main project") { // uh oh something went wrong $response->getBody()->write(json_encode(array("error" => "Can't unset project as main project, try updating another project as the main project"))); return $response->withStatus(400); } if (!$update) { // uh oh something went wrong $response->getBody()->write(json_encode(array("error" => "Something went wrong"))); return $response->withStatus(500); } return $response; } $response->getBody()->write(json_encode(array("error" => "Please provide an ID"))); return $response->withStatus(400); }); $app->delete("/projectData/{id}", function (Request $request, Response $response, array $args) { global $projectData; if ($args["id"] != null) { $message = $projectData->deleteProjectData($args["id"]); if ($message === "error") { // uh oh something went wrong $response->getBody()->write(json_encode(array("error" => "Something went wrong or the project with ID ".$args["id"]."does not exist"))); return $response->withStatus(500); } if ($message === "cannot delete") { //uh oh cannot delete the main project $response->getBody()->write(json_encode(array("error" => "Cannot delete the main project"))); return $response->withStatus(409); } return $response; } $response->getBody()->write(json_encode(array("error" => "Please provide an ID"))); return $response->withStatus(400); }); $app->post("/projectData", function (Request $request, Response $response) { global $projectData; $data = $request->getParsedBody(); if (empty($data["title"]) || empty($data["isMainProject"]) || empty($data["information"]) || empty($data["gitLink"])) { // uh oh sent some empty data $response->getBody()->write(json_encode(array("error" => "Only some of the data was sent"))); return $response->withStatus(400); } $insertedID = $projectData->addProjectData($data["title"], $data["isMainProject"], $data["information"], $data["projectLink"], $data["gitLink"]); if (!is_int($insertedID)) { // uh oh something went wrong $response->getBody()->write(json_encode(array("error" => "Something went wrong", "message" => $insertedID))); return $response->withStatus(500); } $response->getBody()->write(json_encode(array("ID" => $insertedID))); return $response; }); $app->post("/projectImage/{id}", function (Request $request, Response $response, array $args) { global $projectData; $files = $request->getUploadedFiles(); if (empty($args["id"]) || empty($files)) { // uh oh only some of the data was sent $response->getBody()->write(json_encode(array("error" => "Only some of the data was sent"))); return $response->withStatus(400); } $message = $projectData->uploadImage($args["id"], $files["img"]); if (!is_array($message)) { // uh oh something went wrong $response->getBody()->write(json_encode(array("error" => $message))); return $response->withStatus(500); } $response->getBody()->write(json_encode($message)); return $response; }); $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"))); return $response->withStatus(400); } if (!filter_var($data["email"], FILTER_VALIDATE_EMAIL)) { $response->getBody()->write(json_encode(array("errorMessage" => "Email is not the correct format"))); return $response->withStatus(400); } // email form filler/conatcter $headers1 = "From: noreply@rohitpai.co.uk\r\n"; $headers1 .= "Reply-To: rohit@rohitpai.co.uk\r\n"; $headers1 .= "MIME-Version: 1.0\r\n"; $headers1 .= "Content-Type: text/html; charset=UTF-8\r\n"; $message1 = " {$data['subject']}

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 Email Subject message
{$data['fName']} {$data['lName']} {$data['email']} {$data['subject']} {$data['message']}


Regards,
Rohit Pai
rohit@rohitpai.co.uk "; mail($data["email"], $data["subject"], $message1, $headers1); // email to me $headers2 = "From: noreply@rohitpai.co.uk\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['subject']}

{$data['fName']} {$data['lName']} filled in the form on the website, here is what they sent.

Firstname Lastname Email Subject message
{$data['fName']} {$data['lName']} {$data['email']} {$data['subject']} {$data['message']}
"; mail("rohit@rohitpai.co.uk", "{$data['fName']} {$data['lName']} filled in the form", $message2, $headers2); return $response->withStatus(201); }); $app->post("/user/login", function (Request $request, Response $response) { global $user; // get request data $data = $request->getParsedBody(); if (empty($data["username"]) || empty($data["password"])) { // uh oh user sent empty data return $response->withStatus(400); } if ($user->checkUser($data["username"], $data["password"])) { // yay, user is logged in $_SESSION["token"] = $user->createToken($data["username"]); $_SESSION["username"] = $data["username"]; $response->getBody()->write(json_encode(array("token" => $_SESSION["token"]))); return $response; } $response->getBody()->write(json_encode(array("error" => "Unauthorised"))); return $response->withStatus(401); }); $app->get("/user/logout", function (Request $request, Response $response) { session_unset(); return $response; }); $app->get("/user/isLoggedIn", function (Request $request, Response $response) { global $user; if (empty($_SESSION["token"]) && empty($_SESSION["username"])) { // uh oh user not logged in return $response->withStatus(401); } if (empty($_SESSION["token"])) { // user is logged in but no token was created $_SESSION["token"] = $user->createToken($_SESSION["username"]); return $response; } $response->getBody()->write(json_encode(array("token" => $_SESSION["token"]))); return $response; }); $app->get("/user/checkResetEmail/{email}", function (Request $request, Response $response, array $args) { global $user; if (empty($args["email"])) { // uh oh sent empty data return $response->withStatus(400); } if ($user->checkEmail($args["email"])) { // yay email does exist $_SESSION["resetToken"] = $user->sendResetEmail($args["email"]); $_SESSION["resetEmail"] = $args["email"]; return $response; } return $response->withStatus(404); }); $app->get("/user/resendEmail", function (Request $request, Response $response) { if (empty($_SESSION["resetToken"])) { // uh oh not authorized to resend email return $response->withStatus(401); } global $user; $_SESSION["resetToken"] = $user->sendResetEmail($_SESSION["resetEmail"]); return $response; }); $app->get("/user/checkResetCode/{code}", function (Request $request, Response $response, array $args) { if (empty($args["code"])) { // uh oh sent empty data return $response->withStatus(400); } if ($_SESSION["resetToken"] === $args["code"]) { // yay, code code matches return $response; } return $response->withStatus(401); }); $app->post("/user/changePassword", function (Request $request, Response $response) { global $user; if (empty($_SESSION["resetToken"]) && empty($_SESSION["resetEmail"])) { // uh oh not authorized to change password return $response->withStatus(401); } $data = $request->getParsedBody(); if (empty($data["password"])) { // uh oh sent empty data return $response->withStatus(400); } if ($user->changePassword($_SESSION["resetEmail"], $data["password"])) { // yay, password changed unset($_SESSION["resetToken"]); unset($_SESSION["resetEmail"]); return $response; } return $response->withStatus(500); }); $app->run();