qr_id route php getDog
This commit is contained in:
24
php/php-dog/classes/Database.php
Normal file
24
php/php-dog/classes/Database.php
Normal file
@ -0,0 +1,24 @@
|
||||
<?php
|
||||
class Database{
|
||||
|
||||
// CHANGE THE DB INFO ACCORDING TO YOUR DATABASE
|
||||
private $db_host = 'localhost';
|
||||
//private $db_host = '10.35.232.188:3306';
|
||||
private $db_name = 'k200835_dog';
|
||||
private $db_username = 'k200835_user_dog';
|
||||
private $db_password = 'X0pEiBsXN3RLazGhQVvP';
|
||||
|
||||
public function dbConnection(){
|
||||
|
||||
try{
|
||||
$conn = new PDO('mysql:host='.$this->db_host.';dbname='.$this->db_name,$this->db_username,$this->db_password);
|
||||
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||
return $conn;
|
||||
}
|
||||
catch(PDOException $e){
|
||||
echo "Connection error ".$e->getMessage();
|
||||
exit;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
69
php/php-dog/getDog.php
Normal file
69
php/php-dog/getDog.php
Normal file
@ -0,0 +1,69 @@
|
||||
<?php
|
||||
header("Access-Control-Allow-Origin: *");
|
||||
header("Access-Control-Allow-Headers: access");
|
||||
header("Access-Control-Allow-Methods: POST");
|
||||
header("Content-Type: application/json; charset=UTF-8");
|
||||
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
|
||||
|
||||
session_start();
|
||||
|
||||
require __DIR__.'/classes/Database.php';
|
||||
//require __DIR__.'/classes/JwtHandler.php';
|
||||
|
||||
function msg($success,$status,$message,$fields,$data = []){
|
||||
return ([
|
||||
'success' => $success,
|
||||
'status' => $status,
|
||||
'message' => $message,
|
||||
'fields' => $message,
|
||||
'data' => $data
|
||||
]);
|
||||
}
|
||||
|
||||
$db_connection = new Database();
|
||||
$conn = $db_connection->dbConnection();
|
||||
|
||||
$data = json_decode(file_get_contents("php://input"));
|
||||
$returnData = [];
|
||||
$fields = ['fields' => ['qr_id']];
|
||||
|
||||
// IF REQUEST METHOD IS NOT EQUAL TO POST
|
||||
if($_SERVER["REQUEST_METHOD"] != "POST")
|
||||
{
|
||||
$returnData = msg(0,404,'Page Not Found!',$fields,null);
|
||||
}
|
||||
// CHECKING EMPTY FIELDS
|
||||
elseif(!isset($data->qr_id)
|
||||
|| empty(trim($data->qr_id))
|
||||
)
|
||||
{
|
||||
$returnData = msg(0,422,'Please Fill in all Required Fields!',$fields, null);
|
||||
|
||||
}
|
||||
// IF THERE ARE NO EMPTY FIELDS THEN-
|
||||
else
|
||||
{
|
||||
$qr_id = trim($data->qr_id);
|
||||
|
||||
try
|
||||
{
|
||||
|
||||
$fetch_user_by_email = "SELECT * FROM `dogs` WHERE `qr_id`=:qr_id";
|
||||
$query_stmt = $conn->prepare($fetch_user_by_email);
|
||||
$query_stmt->bindValue(':qr_id', $email,PDO::PARAM_STR);
|
||||
$query_stmt->execute();
|
||||
|
||||
// IF THE dog IS FOUNDED BY qr_id
|
||||
if($query_stmt->rowCount())
|
||||
{
|
||||
$row = $query_stmt->fetch(PDO::FETCH_ASSOC);
|
||||
$returnData = msg(1,200,'get dog',$fields,$row);
|
||||
}
|
||||
}
|
||||
catch(PDOException $e)
|
||||
{
|
||||
$returnData = msg(0,500,$e->getMessage(),$fields,null);
|
||||
}
|
||||
|
||||
}
|
||||
echo json_encode($returnData);
|
||||
18
php/php-dog/getUser.php
Normal file
18
php/php-dog/getUser.php
Normal file
@ -0,0 +1,18 @@
|
||||
<?php
|
||||
header("Access-Control-Allow-Origin: *");
|
||||
header("Access-Control-Allow-Headers: access");
|
||||
header("Access-Control-Allow-Methods: GET");
|
||||
header("Content-Type: application/json; charset=UTF-8");
|
||||
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
|
||||
|
||||
session_start();
|
||||
|
||||
require __DIR__.'/classes/Database.php';
|
||||
require __DIR__.'/AuthMiddleware.php';
|
||||
|
||||
$allHeaders = getallheaders();
|
||||
$db_connection = new Database();
|
||||
$conn = $db_connection->dbConnection();
|
||||
$auth = new Auth($conn, $allHeaders);
|
||||
|
||||
echo json_encode($auth->isValid());
|
||||
106
php/php-dog/login.php
Normal file
106
php/php-dog/login.php
Normal file
@ -0,0 +1,106 @@
|
||||
<?php
|
||||
header("Access-Control-Allow-Origin: *");
|
||||
header("Access-Control-Allow-Headers: access");
|
||||
header("Access-Control-Allow-Methods: POST");
|
||||
header("Content-Type: application/json; charset=UTF-8");
|
||||
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
|
||||
|
||||
session_start();
|
||||
|
||||
require __DIR__.'/classes/Database.php';
|
||||
//require __DIR__.'/classes/JwtHandler.php';
|
||||
|
||||
function msg($success,$status,$message,$extra = []){
|
||||
return array_merge([
|
||||
'success' => $success,
|
||||
'status' => $status,
|
||||
'message' => $message
|
||||
],$extra);
|
||||
}
|
||||
|
||||
$db_connection = new Database();
|
||||
$conn = $db_connection->dbConnection();
|
||||
|
||||
$data = json_decode(file_get_contents("php://input"));
|
||||
$returnData = [];
|
||||
|
||||
// IF REQUEST METHOD IS NOT EQUAL TO POST
|
||||
if($_SERVER["REQUEST_METHOD"] != "POST"):
|
||||
$returnData = msg(0,404,'Page Not Found!');
|
||||
|
||||
// CHECKING EMPTY FIELDS
|
||||
elseif(!isset($data->email)
|
||||
|| !isset($data->password)
|
||||
|| empty(trim($data->email))
|
||||
|| empty(trim($data->password))
|
||||
):
|
||||
|
||||
$fields = ['fields' => ['email','password']];
|
||||
$returnData = msg(0,422,'Please Fill in all Required Fields!',$fields);
|
||||
|
||||
// IF THERE ARE NO EMPTY FIELDS THEN-
|
||||
else:
|
||||
$email = trim($data->email);
|
||||
$password = trim($data->password);
|
||||
|
||||
// CHECKING THE EMAIL FORMAT (IF INVALID FORMAT)
|
||||
if(!filter_var($email, FILTER_VALIDATE_EMAIL)):
|
||||
$returnData = msg(0,422,'Invalid Email Address!');
|
||||
|
||||
// IF PASSWORD IS LESS THAN 8 THE SHOW THE ERROR
|
||||
elseif(strlen($password) < 8):
|
||||
$returnData = msg(0,422,'Your password must be at least 8 characters long!');
|
||||
|
||||
// THE USER IS ABLE TO PERFORM THE LOGIN ACTION
|
||||
else:
|
||||
try{
|
||||
|
||||
$fetch_user_by_email = "SELECT * FROM `users` WHERE `email`=:email";
|
||||
$query_stmt = $conn->prepare($fetch_user_by_email);
|
||||
$query_stmt->bindValue(':email', $email,PDO::PARAM_STR);
|
||||
$query_stmt->execute();
|
||||
|
||||
// IF THE USER IS FOUNDED BY EMAIL
|
||||
if($query_stmt->rowCount()):
|
||||
$row = $query_stmt->fetch(PDO::FETCH_ASSOC);
|
||||
$check_password = password_verify($password, $row['password']);
|
||||
|
||||
// VERIFYING THE PASSWORD (IS CORRECT OR NOT?)
|
||||
// IF PASSWORD IS CORRECT THEN SEND THE LOGIN TOKEN
|
||||
if ($check_password):
|
||||
|
||||
// $jwt = new JwtHandler();
|
||||
$user = array(
|
||||
'id' => $row['id'],
|
||||
'vorname' => $row['vorname'],
|
||||
'nachname' => $row['nachname'],
|
||||
'email' => $row['email']
|
||||
);
|
||||
$_SESSION['user'] = $user;
|
||||
|
||||
$returnData = [
|
||||
'success' => 1,
|
||||
'message' => 'You have successfully logged in.',
|
||||
'user' => $user,
|
||||
'session' => $_SESSION
|
||||
];
|
||||
|
||||
// IF INVALID PASSWORD
|
||||
else:
|
||||
$returnData = msg(0,422,'Invalid Password!');
|
||||
endif;
|
||||
|
||||
// IF THE USER IS NOT FOUNDED BY EMAIL THEN SHOW THE FOLLOWING ERROR
|
||||
else:
|
||||
$returnData = msg(0,422,'Invalid Email Address!');
|
||||
endif;
|
||||
}
|
||||
catch(PDOException $e){
|
||||
$returnData = msg(0,500,$e->getMessage());
|
||||
}
|
||||
|
||||
endif;
|
||||
|
||||
endif;
|
||||
|
||||
echo json_encode($returnData);
|
||||
11
php/php-dog/logout.php
Normal file
11
php/php-dog/logout.php
Normal file
@ -0,0 +1,11 @@
|
||||
<?php
|
||||
header("Access-Control-Allow-Origin: *");
|
||||
header("Access-Control-Allow-Headers: access");
|
||||
header("Access-Control-Allow-Methods: POST");
|
||||
header("Content-Type: application/json; charset=UTF-8");
|
||||
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
|
||||
|
||||
session_start();
|
||||
session_destroy();
|
||||
session_abort();
|
||||
?>
|
||||
94
php/php-dog/register.php
Normal file
94
php/php-dog/register.php
Normal file
@ -0,0 +1,94 @@
|
||||
<?php
|
||||
header("Access-Control-Allow-Origin: *");
|
||||
header("Access-Control-Allow-Headers: access");
|
||||
header("Access-Control-Allow-Methods: POST");
|
||||
header("Content-Type: application/json; charset=UTF-8");
|
||||
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
|
||||
|
||||
session_start();
|
||||
|
||||
require __DIR__ . '/classes/Database.php';
|
||||
$db_connection = new Database();
|
||||
$conn = $db_connection->dbConnection();
|
||||
|
||||
function msg($success, $status, $message, $extra = [])
|
||||
{
|
||||
return array_merge([
|
||||
'success' => $success,
|
||||
'status' => $status,
|
||||
'message' => $message
|
||||
], $extra);
|
||||
}
|
||||
|
||||
// DATA FORM REQUEST
|
||||
$data = json_decode(file_get_contents("php://input"));
|
||||
$returnData = [];
|
||||
|
||||
if ($_SERVER["REQUEST_METHOD"] != "POST") :
|
||||
|
||||
$returnData = msg(0, 404, 'Page Not Found!');
|
||||
|
||||
elseif (
|
||||
!isset($data->vorname)
|
||||
|| !isset($data->nachname)
|
||||
|| !isset($data->email)
|
||||
|| !isset($data->password)
|
||||
|| empty(trim($data->vorname))
|
||||
|| empty(trim($data->nachname))
|
||||
|| empty(trim($data->email))
|
||||
|| empty(trim($data->password))
|
||||
) :
|
||||
|
||||
$fields = ['fields' => ['vorname', 'nachname', 'email', 'password']];
|
||||
$returnData = msg(0, 422, 'Please Fill in all Required Fields!', $fields);
|
||||
|
||||
// IF THERE ARE NO EMPTY FIELDS THEN-
|
||||
else :
|
||||
|
||||
$vorname = trim($data->vorname);
|
||||
$nachname = trim($data->nachname);
|
||||
$email = trim($data->email);
|
||||
$password = trim($data->password);
|
||||
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) :
|
||||
$returnData = msg(0, 422, 'Invalid Email Address!');
|
||||
|
||||
elseif (strlen($password) < 8) :
|
||||
$returnData = msg(0, 422, 'Your password must be at least 8 characters long!');
|
||||
|
||||
elseif (strlen($nachname) < 3) :
|
||||
$returnData = msg(0, 422, 'Your name must be at least 3 characters long!');
|
||||
|
||||
else :
|
||||
try {
|
||||
|
||||
$check_email = "SELECT `email` FROM `users` WHERE `email`=:email";
|
||||
$check_email_stmt = $conn->prepare($check_email);
|
||||
$check_email_stmt->bindValue(':email', $email, PDO::PARAM_STR);
|
||||
$check_email_stmt->execute();
|
||||
|
||||
if ($check_email_stmt->rowCount()) :
|
||||
$returnData = msg(0, 422, 'This E-mail already in use!');
|
||||
|
||||
else :
|
||||
$insert_query = "INSERT INTO `users`(`vorname`,`nachname`,`email`,`password`) VALUES(:vorname,:nachname,:email,:password)";
|
||||
|
||||
$insert_stmt = $conn->prepare($insert_query);
|
||||
|
||||
// DATA BINDING
|
||||
$insert_stmt->bindValue(':vorname', htmlspecialchars(strip_tags($vorname)), PDO::PARAM_STR);
|
||||
$insert_stmt->bindValue(':nachname', htmlspecialchars(strip_tags($nachname)), PDO::PARAM_STR);
|
||||
$insert_stmt->bindValue(':email', $email, PDO::PARAM_STR);
|
||||
$insert_stmt->bindValue(':password', password_hash($password, PASSWORD_DEFAULT), PDO::PARAM_STR);
|
||||
|
||||
$insert_stmt->execute();
|
||||
|
||||
$returnData = msg(1, 201, 'You have successfully registered.');
|
||||
|
||||
endif;
|
||||
} catch (PDOException $e) {
|
||||
$returnData = msg(0, 500, $e->getMessage());
|
||||
}
|
||||
endif;
|
||||
endif;
|
||||
|
||||
echo json_encode($returnData);
|
||||
23
php/php-dog/session.php
Normal file
23
php/php-dog/session.php
Normal file
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
header("Access-Control-Allow-Origin: *");
|
||||
header("Access-Control-Allow-Headers: access");
|
||||
header("Access-Control-Allow-Methods: GET");
|
||||
header("Content-Type: application/json; charset=UTF-8");
|
||||
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
|
||||
|
||||
session_start();
|
||||
$return =
|
||||
[
|
||||
"success" => 0,
|
||||
"user" => null
|
||||
];
|
||||
|
||||
if (isset($_SESSION['user'])) {
|
||||
$return =
|
||||
[
|
||||
"success" => 1,
|
||||
"user" => $_SESSION['user']
|
||||
];
|
||||
}
|
||||
echo json_encode($return);
|
||||
?>
|
||||
Reference in New Issue
Block a user