123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 |
- <?php
-
- require_once 'config.php';
- require_once 'dbh.inc.php';
- require_once 'checkLogin.php';
-
-
-
-
-
-
-
- if(isset($_POST['newExperience'])) {
-
-
-
- $title = mysqli_real_escape_string($connection, trim($_POST['title']));
- $description = mysqli_real_escape_string($connection, trim($_POST['description']));
- $type = mysqli_real_escape_string($connection, trim($_POST['type']));
-
- $start_date = mysqli_real_escape_string($connection, trim($_POST['start']));
- $end_date = mysqli_real_escape_string($connection, trim($_POST['end']));
- $institution = mysqli_real_escape_string($connection, trim($_POST['institution']));
- $expiry_time = mysqli_real_escape_string($connection, trim($_POST['expiry_time']));
-
-
- $typeOfExperience = mysqli_real_escape_string($connection, trim($_POST['typeOfExperience']));
- $typeOfProject = mysqli_real_escape_string($connection, trim($_POST['typeOfProject']));
- $projectID = mysqli_real_escape_string($connection, trim($_POST['projectID']));
- $projectName = mysqli_real_escape_string($connection, trim($_POST['projectName']));
- $projectDescription = mysqli_real_escape_string($connection, trim($_POST['projectDescription']));
-
-
-
-
-
- if($title === "") {
- http_response_code(400);
- echo json_encode(array("error" => "Please specify experience title."));
- exit();
- } else if(mb_strlen($title) > 60) {
- http_response_code(400);
- echo json_encode(array("error" => "Experience title too long (max. is 60 characters)."));
- exit();
- }
-
-
-
-
- if($description === "") {
- http_response_code(400);
- echo json_encode(array("error" => "Please specify experience description."));
- exit();
- } else if(mb_strlen($description) > 100) {
- http_response_code(400);
- echo json_encode(array("error" => "Experience description too long (max. is 100 characters)."));
- exit();
- }
-
-
-
-
-
-
-
-
-
-
- if(mb_strlen($type) > 60) {
- http_response_code(400);
- echo json_encode(array("error" => "Experience type too long (max. is 60 characters)."));
- exit();
- }
-
-
-
-
-
-
-
- function validDate($date) {
- $d = date_create_from_format("Y-m-d", $date);
- return $d && date_format($d, "Y-m-d") === $date;
- }
-
- if($start_date === "") {
- http_response_code(400);
- echo json_encode(array("error" => "Please specify experience's start date."));
- exit();
- } else if(!validDate($start_date)) {
- http_response_code(400);
- echo json_encode(array("error" => "Experience's start date ($start_date) given in wrong format (use YYYY-MM-DD instead)."));
- exit();
- }
-
-
-
-
-
-
- if($end_date === "") {
- http_response_code(400);
- echo json_encode(array("error" => "Please specify experience's end date."));
- exit();
- } else if(!validDate($end_date)) {
- http_response_code(400);
- echo json_encode(array("error" => "Experience's end date ($end_date) given in wrong format (use YYYY-MM-DD instead)."));
- exit();
- }
-
-
-
- $duration_seconds = strtotime($end_date) - strtotime($start_date);
-
-
-
- if($duration_seconds <= 0) {
- http_response_code(400);
- echo json_encode(array("error" => "Experience's end date ($end_date) must occur at least a day after the start date ($start_date)."));
- exit();
- }
-
-
-
- $duration_weeks = round($duration_seconds / 604800);
-
-
-
-
- if($institution === "") {
- http_response_code(400);
- echo json_encode(array("error" => "Please specify institution ID."));
- exit();
- } else if(mysqli_query($connection, "SELECT * FROM institution WHERE id = '$institution';")->num_rows !== 1) {
- http_response_code(400);
- echo json_encode(array("error" => "Given institution ID not in database."));
- exit();
- }
-
-
-
- $permittedExpiryTimes = array("30", "60", "120", "180", "360", "720", "1080", "1440");
- if(!in_array($expiry_time, $permittedExpiryTimes, TRUE)) {
- http_response_code(400);
- echo json_encode(array("error" => "Expiry time given is not permitted (use only 30, 60, 120, 180, 360, 720, 1080 or 1440)."));
- exit();
- }
-
-
-
-
-
-
- if($typeOfExperience !== "Standalone" && $typeOfExperience !== "Part of a Project") {
- http_response_code(400);
- echo json_encode(array("error" => "Invalid type of experience."));
- exit();
- }
-
-
- if($typeOfExperience === "Part of a Project") {
-
-
-
- if($typeOfProject === "Existing") {
-
- if(mysqli_query($connection, "SELECT * FROM project WHERE id = '$projectID';")->num_rows !== 1) {
- http_response_code(400);
- echo json_encode(array("error" => "Given project ID ($projectID) not in database."));
- exit();
- }
-
- } else if($typeOfProject === "New") {
-
- if($projectName === "") {
- http_response_code(400);
- echo json_encode(array("error" => "Please specify project name."));
- exit();
- } else if(mb_strlen($projectName) > 256) {
- http_response_code(400);
- echo json_encode(array("error" => "Project name too long (max. is 256 characters)."));
- exit();
- }
-
- if($projectDescription === "") {
- http_response_code(400);
- echo json_encode(array("error" => "Please specify project description."));
- exit();
- } else if(mb_strlen($projectDescription) > 256) {
- http_response_code(400);
- echo json_encode(array("error" => "Project description too long (max. is 256 characters)."));
- exit();
- }
-
- }
-
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- $queryInsert = "INSERT INTO `experience` (`title`, `description`, `type`, `duration_weeks`, `start_date`, `end_date`, `expiry_time`) VALUES ('$title', '$description', '$type', '$duration_weeks', '$start_date', '$end_date', '$expiry_time');";
- if(!mysqli_query($connection, $queryInsert)) die("Error: ".mysqli_error($connection));
-
-
-
- $id_experience = mysqli_insert_id($connection) or die('Error: '.mysqli_error($connection));
-
-
-
- $hash = substr(sha1($id_experience), 0, 40);
- $queryHash = "UPDATE experience SET hash_id = '$hash' WHERE id = '$id_experience';";
- if(!mysqli_query($connection, $queryHash)) die("Error: ".mysqli_error($connection));
-
-
-
- $queryHookExperienceToInstitution = "INSERT INTO `institution_experience` (`id_institution`, `id_experience`) VALUES ('$institution', '$id_experience');";
- if(!mysqli_query($connection, $queryHookExperienceToInstitution)) die("Error: ".mysqli_error($connection));
-
-
-
- $queryHookExperienceToUser = "INSERT INTO `researcher_experience` (`id_researcher`, `id_experience`) VALUES ('" . $_SESSION['dbUserData']['id_researcher'] . "', '$id_experience')";
- if(!mysqli_query($connection, $queryHookExperienceToUser)) die("Error: ".mysqli_error($connection));
-
-
-
-
-
-
-
-
-
- if($typeOfExperience === "Part of a Project") {
-
-
-
- if($typeOfProject === "New") {
-
- $queryProject = "INSERT INTO project (`name`, `description`) VALUES ('$projectName', '$projectDescription');";
- if(!mysqli_query($connection, $queryProject)) die("Error: ".mysqli_error($connection));
-
-
- $projectID = mysqli_insert_id($connection) or die('Error: '.mysqli_error($connection));
-
- }
-
- $queryHookExperienceToProject = "INSERT INTO project_experience (`id_experience`, `id_project`) VALUES ('$id_experience', '$projectID');";
- if(!mysqli_query($connection, $queryHookExperienceToProject)) die("Error: ".mysqli_error($connection));
-
- }
-
-
-
-
-
-
-
- }
-
-
-
|