%PDF- %GIF98; %PNG;
Server : ApacheSystem : Linux sh00666.bluehost.com 5.14.0-687.26.1.el9_8.x86_64 #1 SMP PREEMPT_DYNAMIC Tue Jul 14 16:32:02 EDT 2026 x86_64 User : ndkhermy ( 1200) PHP Version : 8.5.12 Disable Function : NONE Directory : /home1/ndkhermy/www/website_6ca89795/api/ |
Upload File : |
<?php
header("Content-Type: application/json");
include("../dbcon.php");
include("firebase_admin.php");
function response($status, $message, $data = [])
{
echo json_encode([
"status" => $status,
"message" => $message,
"data" => $data
], JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);
exit;
}
/*
|--------------------------------------------------------------------------
| Firebase Settings Check
|--------------------------------------------------------------------------
*/
$firebaseQry = mysqli_query(
$conn,
"SELECT *
FROM firebase_settings
WHERE status='1'
LIMIT 1"
);
if (mysqli_num_rows($firebaseQry) == 0) {
response(false, "Social Login Disabled By Admin");
}
/*
|--------------------------------------------------------------------------
| Support Form Data + Raw JSON
|--------------------------------------------------------------------------
*/
$json = json_decode(file_get_contents("php://input"), true);
$username = trim(
$_REQUEST['username']
?? $json['username']
?? ''
);
$email = trim(
$_REQUEST['email']
?? $json['email']
?? ''
);
$phone = trim(
$_REQUEST['phone']
?? $json['phone']
?? ''
);
$photo = trim(
$_REQUEST['photo']
?? $json['photo']
?? ''
);
$social_type = trim(
$_REQUEST['social_type']
?? $json['social_type']
?? ''
);
$social_id = trim(
$_REQUEST['social_id']
?? $json['social_id']
?? ''
);
$id_token = trim(
$_REQUEST['id_token']
?? $json['id_token']
?? ''
);
if (empty($id_token)) {
response(false, "Firebase ID token is required. Please sign in with Firebase first.");
}
$firebase_user = verifyFirebaseIdTokenWithAdmin($id_token);
if (!$firebase_user['success']) {
response(false, $firebase_user['message']);
}
if (empty($username) && !empty($firebase_user['name'])) {
$username = trim($firebase_user['name']);
}
if (empty($email) && !empty($firebase_user['email'])) {
$email = trim($firebase_user['email']);
}
if (empty($social_id) && !empty($firebase_user['uid'])) {
$social_id = trim($firebase_user['uid']);
}
if (empty($social_type)) {
$social_type = 'google';
}
/*
|--------------------------------------------------------------------------
| Validation
|--------------------------------------------------------------------------
*/
if (
empty($username) ||
empty($email) ||
empty($social_type) ||
empty($social_id)
) {
response(false, "Required Fields Missing", [
"required_fields" => [
"username",
"email",
"social_type",
"social_id"
],
"example_url" => "https://dataduck.in/client-project/meatandgo/api/social_login.php?username=Siddharth%20Jha&email=siddharth@gmail.com&phone=8210302952&photo=https://example.com/profile.jpg&social_type=google&social_id=103866960456122946171",
"sample_request" => [
"username" => "Siddharth Jha",
"email" => "siddharth@gmail.com",
"phone" => "8210302952",
"photo" => "https://example.com/profile.jpg",
"social_type" => "google",
"social_id" => "103866960456122946171"
]
]);
}
if (!in_array($social_type, ['google', 'facebook'])) {
response(false, "Invalid Social Type");
}
$email = mysqli_real_escape_string($conn, $email);
$username = mysqli_real_escape_string($conn, $username);
$phone = mysqli_real_escape_string($conn, $phone);
$photo = mysqli_real_escape_string($conn, $photo);
$social_id = mysqli_real_escape_string($conn, $social_id);
/*
|--------------------------------------------------------------------------
| Check Existing User
|--------------------------------------------------------------------------
*/
$userQry = mysqli_query(
$conn,
"SELECT *
FROM user
WHERE email='$email'
LIMIT 1"
);
/*
|--------------------------------------------------------------------------
| LOGIN EXISTING USER
|--------------------------------------------------------------------------
*/
if (mysqli_num_rows($userQry) > 0) {
$user = mysqli_fetch_assoc($userQry);
if (empty($user['api_token'])) {
$token = bin2hex(random_bytes(32));
mysqli_query(
$conn,
"UPDATE user
SET api_token='$token'
WHERE id='" . $user['id'] . "'"
);
} else {
$token = $user['api_token'];
}
mysqli_query(
$conn,
"UPDATE user
SET
username='$username',
phone='$phone',
profile_image='$photo',
social_type='$social_type',
social_id='$social_id'
WHERE id='" . $user['id'] . "'"
);
response(true, "Login Successful", [
"is_new_user" => false,
"user" => [
"id" => (int) $user['id'],
"username" => $username,
"email" => $email,
"phone" => $phone,
"profile_image" => $photo,
"social_type" => $social_type,
"social_id" => $social_id,
"token" => $token,
"firebase_uid" => $social_id
]
]);
}
/*
|--------------------------------------------------------------------------
| REGISTER NEW USER (Same Flow As register.php)
|--------------------------------------------------------------------------
*/
$created_date = date('Y-m-d');
$usertype = '';
$stmt = $conn->prepare("
INSERT INTO user
(
username,
email,
password,
phone,
usertype,
created_date,
profile_image,
social_type,
social_id
)
VALUES
(
?, ?, ?, ?, ?, ?, ?, ?, ?
)
");
$password = "";
$stmt->bind_param(
"sssssssss",
$username,
$email,
$password,
$phone,
$usertype,
$created_date,
$photo,
$social_type,
$social_id
);
if ($stmt->execute()) {
$user_id = $conn->insert_id;
/*
|--------------------------------------------------------------------------
| Generate API Token
|--------------------------------------------------------------------------
*/
$token = bin2hex(random_bytes(32));
$updateToken = $conn->prepare("
UPDATE user
SET api_token=?
WHERE id=?
");
$updateToken->bind_param(
"si",
$token,
$user_id
);
$updateToken->execute();
response(true, "Registration Successful", [
"is_new_user" => true,
"user" => [
"id" => $user_id,
"username" => $username,
"email" => $email,
"phone" => $phone,
"profile_image" => $photo,
"social_type" => $social_type,
"social_id" => $social_id,
"token" => $token,
"firebase_uid" => $social_id
],
"next_step" => [
"Login API" => "https://dataduck.in/client-project/meatandgo/api/login.php"
]
]);
} else {
response(false, "Registration Failed", [
"mysql_error" => $stmt->error
]);
}
$stmt->close();
if (isset($updateToken)) {
$updateToken->close();
}
$conn->close();
?>