<?php
/**
* File to handle all API requests
* Accepts GET and POST
*
* Each request will be identified by TAG
* Response will be JSON data
/**
* check for POST request
*/
if (isset($_POST['tag']) && $_POST['tag'] != '') {
// get tag
$tag = $_POST['tag'];
// include db handler
require_once 'include/DB_Functions.php';
$db = new DB_Functions();
// response Array
$response = array("tag" => $tag, "error" => FALSE);
// check for tag type
if ($tag == 'login') {
// Request type is check Login
$email = $_POST['email'];
$password = $_POST['password'];
// check for user
$user = $db->getUserByEmailAndPassword($email, $password);
if ($user != false) {
// user found
$response["error"] = FALSE;
$response["user"]["unique_id"] = $user["unique_id"];
$response["user"]["name"] = $user["name"];
$response["user"]["email"] = $user["email"];
$response["user"]["created_at"] = $user["created_at"];
$response["user"]["updated_at"] = $user["updated_at"];
$response["user"]["StudentID"] = $user["StudentID"];
echo json_encode($response);
} else {
// user not found
// echo json with error = 1
$response["error"] = TRUE;
$response["error_msg"] = "Incorrect email or password!";
echo json_encode($response);
}
} else if ($tag == 'register') {
// Request type is Register new user
$name = $_POST['name'];
$email = $_POST['email'];
$password = $_POST['password'];
// check if user is already existed
if ($db->isUserExisted($email)) {
// user is already existed - error response
$response["error"] = TRUE;
$response["error_msg"] = "User already existed";
echo json_encode($response);
} else {
// store user
$user = $db->storeUser($name, $email, $password);
if ($user) {
// user stored successfully
$response["error"] = FALSE;
$response["uid"] = $user["unique_id"];
$response["user"]["name"] = $user["name"];
$response["user"]["email"] = $user["email"];
$response["user"]["created_at"] = $user["created_at"];
$response["user"]["updated_at"] = $user["updated_at"];
echo json_encode($response);
} else {
// user failed to store
$response["error"] = TRUE;
$response["error_msg"] = "Error occurred in registration";
echo json_encode($response);
}
}
}
else if ($tag == 'getCourses') {
$user = FALSE;
if(isset ($_POST['StudentID']))
$user = $_POST['StudentID'];
$selectionKey = $_POST['searchKey'];
$selectionValue = $_POST['searchValue'];
$response = $db->getCourses($user, $selectionKey, $selectionValue);
if($response){
$response["error"] = FALSE;
} else {
$response["error"]=TRUE;
$response["error_mesg"] = "Error occurred getting the courses";
}
echo json_encode($response);
}
else if ($tag = 'addCourse'){
// Request type is add a course to a specific student
$StudentID = $_POST['StudentID'];
$CourseID = $_POST['CourseID'];
$email = $_POST['email'];
// check if user is already existed
if ($db->isUserExisted($email)) {
// store course
$course = $db->storeCourse($StudentID, $CourseID);
if ($course !=FALSE) {
// course stored successfully
$response["error"] = FALSE;
$response["course"]["StudentID"] = $course["StudentID"];
$response["course"]["CourseID"] = $course["CourseID"];
echo json_encode($response);
} else {
// course failed to store
$response["error"] = TRUE;
$response["error_msg"] = "Error occured adding courses";
echo json_encode($response);
}
}
else{
$response["error"] = TRUE;
$response["error_msg"] = "User does not exist";
echo json_encode($response);
}
}
else if($tag == 'getFriends'){
$courseID = $_POST['courseID'];
$studentID = $_POST['studentID'];
$result = $db->getFriendsByCourseIdAndStudentId($courseID, $studentID);
if($result !=FALSE){
$result['error']=FALSE;
} else{
$result['error']=TRUE;
$result["error_mesg"] = "Error occurred retrieving friends";
}
echo json_encode($result);
}
else {
// user failed to store
$response["error"] = TRUE;
$response["error_msg"] = "Unknow 'tag' value. It should be either 'login' or 'register'";
echo json_encode($response);
}
} else {
$response["error"] = TRUE;
$response["error_msg"] = "Required parameter 'tag' is missing!";
echo json_encode($response);
}
?>
- Output for 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.28, 7.0.0 - 7.0.20, 7.1.0 - 7.1.33, 7.2.6 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.30, 8.2.0 - 8.2.24, 8.3.0 - 8.3.12
- {"error":true,"error_msg":"Required parameter 'tag' is missing!"}
preferences:
85.68 ms | 408 KiB | 5 Q