Code restructure push
This commit is contained in:
36
src/api/fetch_bar_tabs.php
Normal file
36
src/api/fetch_bar_tabs.php
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
$rootPath = dirname(dirname(__DIR__));
|
||||
require_once($rootPath . "/src/config/session.php");
|
||||
require_once($rootPath . "/src/config/connection.php");
|
||||
require_once($rootPath . "/src/config/functions.php");
|
||||
|
||||
// Prepare the SQL query to fetch bar tabs along with user details, including user_id
|
||||
$sql = "
|
||||
SELECT bt.tab_id, u.user_id, u.first_name, u.last_name, u.profile_pic
|
||||
FROM bar_tabs bt
|
||||
JOIN users u ON bt.user_id = u.user_id
|
||||
";
|
||||
|
||||
// Execute the query
|
||||
$result = mysqli_query($conn, $sql);
|
||||
|
||||
// Check if there are results
|
||||
if (mysqli_num_rows($result) > 0) {
|
||||
// Create an array to hold the data
|
||||
$barTabs = [];
|
||||
|
||||
// Fetch each row
|
||||
while ($row = mysqli_fetch_assoc($result)) {
|
||||
$barTabs[] = $row;
|
||||
}
|
||||
|
||||
// Return the data as JSON
|
||||
echo json_encode($barTabs);
|
||||
} else {
|
||||
echo json_encode([]);
|
||||
}
|
||||
|
||||
// Close the database connection
|
||||
mysqli_close($conn);
|
||||
?>
|
||||
|
||||
30
src/api/fetch_drinks.php
Normal file
30
src/api/fetch_drinks.php
Normal file
@@ -0,0 +1,30 @@
|
||||
|
||||
|
||||
<?php
|
||||
$rootPath = dirname(dirname(__DIR__));
|
||||
require_once($rootPath . "/src/config/connection.php");
|
||||
|
||||
if (isset($_GET['tab_id'])) {
|
||||
$tab_id = (int) $_GET['tab_id']; // Convert to integer
|
||||
|
||||
if ($tab_id <= 0) {
|
||||
echo json_encode(['status' => 'error', 'message' => 'Invalid tab ID.']);
|
||||
exit();
|
||||
}
|
||||
|
||||
// Fetch drinks available for this tab
|
||||
$stmt = $conn->prepare("SELECT * FROM bar_items");
|
||||
$stmt->execute();
|
||||
$result = $stmt->get_result();
|
||||
|
||||
$drinks = [];
|
||||
while ($row = $result->fetch_assoc()) {
|
||||
$drinks[] = $row;
|
||||
}
|
||||
|
||||
echo json_encode($drinks);
|
||||
} else {
|
||||
echo json_encode(['status' => 'error', 'message' => 'Tab ID is required.']);
|
||||
}
|
||||
?>
|
||||
|
||||
24
src/api/fetch_users.php
Normal file
24
src/api/fetch_users.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
$rootPath = dirname(dirname(__DIR__));
|
||||
require_once($rootPath . "/src/config/env.php");
|
||||
require_once($rootPath . "/src/config/session.php");
|
||||
require_once($rootPath . "/src/config/connection.php");
|
||||
require_once($rootPath . "/src/config/functions.php");
|
||||
|
||||
if ($conn->connect_error) {
|
||||
die(json_encode([])); // Return empty JSON on failure
|
||||
}
|
||||
|
||||
$stmt = $conn->prepare("SELECT user_id, first_name, last_name FROM users ORDER BY first_name ASC");
|
||||
$stmt->execute();
|
||||
$result = $stmt->get_result();
|
||||
|
||||
$users = [];
|
||||
while ($row = $result->fetch_assoc()) {
|
||||
$users[] = $row;
|
||||
}
|
||||
|
||||
echo json_encode($users);
|
||||
$conn->close();
|
||||
?>
|
||||
|
||||
39
src/api/get_campsites.php
Normal file
39
src/api/get_campsites.php
Normal file
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
$rootPath = dirname(dirname(__DIR__));
|
||||
require_once($rootPath . "/src/config/env.php");
|
||||
include_once('../config/connection.php');
|
||||
include_once('../config/functions.php');
|
||||
$conn = openDatabaseConnection();
|
||||
|
||||
$stmt = $conn->prepare("SELECT
|
||||
c.*,
|
||||
u.first_name,
|
||||
u.last_name,
|
||||
u.profile_pic
|
||||
FROM campsites c
|
||||
LEFT JOIN users u ON c.user_id = u.user_id");
|
||||
$stmt->execute();
|
||||
$result = $stmt->get_result();
|
||||
|
||||
$campsites = [];
|
||||
while ($row = $result->fetch_assoc()) {
|
||||
$campsites[] = [
|
||||
'id' => $row['id'],
|
||||
'name' => $row['name'],
|
||||
'description' => $row['description'],
|
||||
'website' => $row['website'],
|
||||
'telephone' => $row['telephone'],
|
||||
'latitude' => $row['latitude'],
|
||||
'longitude' => $row['longitude'],
|
||||
'thumbnail' => $row['thumbnail'],
|
||||
'user' => [
|
||||
'first_name' => $row['first_name'],
|
||||
'last_name' => $row['last_name'],
|
||||
'profile_pic' => $row['profile_pic']
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
header('Content-Type: application/json');
|
||||
echo json_encode($campsites);
|
||||
|
||||
24
src/api/get_tab_total.php
Normal file
24
src/api/get_tab_total.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
$rootPath = dirname(dirname(__DIR__));
|
||||
require_once($rootPath . "/src/config/env.php");
|
||||
require_once($rootPath . "/src/config/connection.php");
|
||||
|
||||
if (isset($_POST['tab_id'])) {
|
||||
$tab_id = (int) $_POST['tab_id']; // Ensure it's an integer
|
||||
|
||||
// Get the total from the bar_transactions table
|
||||
$query = "SELECT SUM(item_price) AS total FROM bar_transactions WHERE tab_id = '$tab_id'";
|
||||
$result = mysqli_query($conn, $query);
|
||||
|
||||
if ($result) {
|
||||
$row = mysqli_fetch_assoc($result);
|
||||
$total = $row['total'] ? $row['total'] : 0; // If no transactions, total is 0
|
||||
echo json_encode(['status' => 'success', 'total' => $total]);
|
||||
} else {
|
||||
echo json_encode(['status' => 'error', 'message' => 'Failed to fetch total.']);
|
||||
}
|
||||
} else {
|
||||
echo json_encode(['status' => 'error', 'message' => 'Missing tab ID.']);
|
||||
}
|
||||
?>
|
||||
|
||||
146
src/api/google_validate_login.php
Normal file
146
src/api/google_validate_login.php
Normal file
@@ -0,0 +1,146 @@
|
||||
<?php
|
||||
$rootPath = dirname(dirname(__DIR__));
|
||||
require_once($rootPath . "/src/config/env.php");
|
||||
require_once($rootPath . "/src/config/session.php");
|
||||
require_once($rootPath . "/src/config/connection.php");
|
||||
require_once($rootPath . "/src/config/functions.php");
|
||||
require_once($rootPath . '/google-client/vendor/autoload.php'); // Add this line for Google Client
|
||||
|
||||
// Check if connection is established
|
||||
if (!$conn) {
|
||||
json_encode(['status' => 'error', 'message' => 'Database connection failed.']);
|
||||
exit();
|
||||
}
|
||||
|
||||
// Google Client Setup
|
||||
$client = new Google_Client();
|
||||
$client->setClientId('948441222188-8qhboq2urr8o9n35mc70s5h2nhd52v0m.apps.googleusercontent.com');
|
||||
$client->setClientSecret('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');
|
||||
$client->setRedirectUri($_ENV['HOST'] . '/google_validate_login.php');
|
||||
$client->addScope("email");
|
||||
$client->addScope("profile");
|
||||
|
||||
// Check if Google login code is set
|
||||
if (isset($_GET['code'])) {
|
||||
$token = $client->fetchAccessTokenWithAuthCode($_GET['code']);
|
||||
if (!isset($token["error"])) {
|
||||
$client->setAccessToken($token['access_token']);
|
||||
$google_oauth = new Google_Service_Oauth2($client);
|
||||
$google_account_info = $google_oauth->userinfo->get();
|
||||
|
||||
// Get user info from Google
|
||||
$email = $google_account_info->email;
|
||||
$name = $google_account_info->name;
|
||||
$first_name = $google_account_info->given_name;
|
||||
$last_name = $google_account_info->family_name;
|
||||
$picture = $google_account_info->picture;
|
||||
|
||||
// Check if the user exists in the database
|
||||
$query = "SELECT * FROM users WHERE email = ?";
|
||||
$stmt = $conn->prepare($query);
|
||||
$stmt->bind_param("s", $email);
|
||||
$stmt->execute();
|
||||
$result = $stmt->get_result();
|
||||
|
||||
if ($result->num_rows == 0) {
|
||||
// User does not exist, so register them
|
||||
$password = null; // No password for Google login
|
||||
$query = "INSERT INTO users (email, first_name, last_name, profile_pic, password, is_verified) VALUES (?, ?, ?, ?, ?, ?)";
|
||||
$stmt = $conn->prepare($query);
|
||||
$is_verified = 1; // Assuming Google users are considered verified
|
||||
$stmt->bind_param("sssssi", $email, $first_name, $last_name, $picture, $password, $is_verified);
|
||||
if ($stmt->execute()) {
|
||||
// User successfully registered, set session and redirect
|
||||
$_SESSION['user_id'] = $conn->insert_id;
|
||||
$_SESSION['first_name'] = $first_name;
|
||||
$_SESSION['profile_pic'] = $picture;
|
||||
// echo json_encode(['status' => 'success', 'message' => 'Google login successful']);
|
||||
header("Location: index.php");
|
||||
exit();
|
||||
} else {
|
||||
// echo json_encode(['status' => 'error', 'message' => 'Failed to register user.']);
|
||||
header("Location: index.php");
|
||||
exit();
|
||||
}
|
||||
} else {
|
||||
// User exists, set session and redirect
|
||||
$row = $result->fetch_assoc();
|
||||
$_SESSION['user_id'] = $row['user_id'];
|
||||
$_SESSION['first_name'] = $row['first_name'];
|
||||
$_SESSION['profile_pic'] = $row['profile_pic'];
|
||||
// echo json_encode(['status' => 'success', 'message' => 'Google login successful']);
|
||||
header("Location: index.php");
|
||||
exit();
|
||||
}
|
||||
|
||||
$stmt->close();
|
||||
} else {
|
||||
echo "Login failed.";
|
||||
exit();
|
||||
}
|
||||
}
|
||||
|
||||
// Check if email and password login is requested
|
||||
if (isset($_POST['email']) && isset($_POST['password'])) {
|
||||
// Retrieve and sanitize form data
|
||||
$email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
|
||||
$password = trim($_POST['password']); // Remove extra spaces
|
||||
|
||||
// Validate input
|
||||
if (empty($email) || empty($password)) {
|
||||
echo json_encode(['status' => 'error', 'message' => 'Please enter both email and password.']);
|
||||
exit();
|
||||
}
|
||||
|
||||
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
||||
echo json_encode(['status' => 'error', 'message' => 'Invalid email format.']);
|
||||
exit();
|
||||
}
|
||||
|
||||
// Prepare SQL statement to fetch user details
|
||||
$query = "SELECT * FROM users WHERE email = ?";
|
||||
$stmt = $conn->prepare($query);
|
||||
|
||||
if (!$stmt) {
|
||||
echo json_encode(['status' => 'error', 'message' => 'Database query preparation failed.']);
|
||||
exit();
|
||||
}
|
||||
|
||||
$stmt->bind_param("s", $email);
|
||||
$stmt->execute();
|
||||
$result = $stmt->get_result();
|
||||
|
||||
// Check if user exists and verify password
|
||||
if ($result->num_rows == 1) {
|
||||
$row = $result->fetch_assoc();
|
||||
|
||||
// Check if the user is verified
|
||||
if ($row['is_verified'] == 0) {
|
||||
echo json_encode(['status' => 'error', 'message' => 'Your account is not verified. Please check your email for the verification link.']);
|
||||
exit();
|
||||
}
|
||||
|
||||
if (password_verify($password, $row['password'])) {
|
||||
// Password is correct, set up session
|
||||
$_SESSION['user_id'] = $row['user_id']; // Adjust as per your table structure
|
||||
$_SESSION['first_name'] = $row['first_name']; // Adjust as per your table structure
|
||||
$_SESSION['profile_pic'] = $row['profile_pic'];
|
||||
echo json_encode(['status' => 'success', 'message' => 'Successful Login']);
|
||||
} else {
|
||||
// Password is incorrect
|
||||
echo json_encode(['status' => 'error', 'message' => 'Invalid password.']);
|
||||
}
|
||||
} else {
|
||||
// User does not exist
|
||||
echo json_encode(['status' => 'error', 'message' => 'User with that email does not exist.']);
|
||||
}
|
||||
|
||||
// Close the statement and connection
|
||||
$stmt->close();
|
||||
}
|
||||
|
||||
// Close connection
|
||||
$conn->close();
|
||||
exit();
|
||||
?>
|
||||
|
||||
Reference in New Issue
Block a user