Path : /home/vishqocm/pcib.in/
File Upload :
Current File : /home/vishqocm//pcib.in/google_auth.php

<?php
// Enable error reporting for debugging
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Start session
session_start();

// Include necessary files
require_once 'admin/database/db_config.php';
require_once 'includes/oauth_config.php';

// Initialize variables
$error = '';
$redirect_to = isset($_GET['state']) && !empty($_GET['state']) ? urldecode($_GET['state']) : 'index.php';

// Check if there's an error in the callback
if (isset($_GET['error'])) {
    $error = 'Google authentication failed: ' . htmlspecialchars($_GET['error']);
    $_SESSION['error'] = $error;
    header('Location: login.php');
    exit;
}

// Check if we have an authorization code
if (!isset($_GET['code'])) {
    $error = 'No authorization code received from Google.';
    $_SESSION['error'] = $error;
    header('Location: login.php');
    exit;
}

// Get the authorization code
$code = $_GET['code'];

// Get user info from Google
$google_user = getGoogleUserInfo($code);

if (!$google_user) {
    // Check error logs for details
    $error_log_path = ini_get('error_log');
    $error = 'Failed to get user information from Google. Check error logs for details.';
    
    // For debugging purposes, display last few error log lines
    if (file_exists($error_log_path)) {
        $error_lines = array_slice(file($error_log_path), -15);
        echo '<h2>Google Authentication Error</h2>';
        echo '<p>Failed to get user information from Google. Detailed error information:</p>';
        echo '<pre style="background:#f8f9fa;padding:15px;border-radius:5px;overflow:auto;font-size:12px;line-height:1.5">';
        foreach ($error_lines as $line) {
            if (strpos($line, 'Google OAuth') !== false) {
                echo htmlspecialchars($line);
            }
        }
        echo '</pre>';
        echo '<p><a href="login.php" class="btn btn-primary">Return to login page</a></p>';
        exit;
    }
    
    $_SESSION['error'] = $error;
    header('Location: login.php');
    exit;
}

// Process OAuth login/registration
$user = processOAuthLogin($google_user, 'google');

if (!$user) {
    $error = 'Failed to process Google authentication. Please try again later.';
    $_SESSION['error'] = $error;
    header('Location: login.php');
    exit;
}

if (isset($user['error'])) {
    $_SESSION['error'] = $user['error'];
    header('Location: login.php');
    exit;
}

// Login successful, set session variables
$_SESSION['user_id'] = $user['id'];
$_SESSION['username'] = $user['username'];
$_SESSION['role'] = $user['role'] ?? 'student';
$_SESSION['email'] = $user['email'] ?? '';

// Check if activities table exists before logging
$check_activities = mysqli_query($conn, "SHOW TABLES LIKE 'activities'");
if (mysqli_num_rows($check_activities) > 0) {
    // Log successful login
    $login_activity_query = "INSERT INTO activities (user_id, user_type, activity_type, activity_description, ip_address, created_at) 
                          VALUES (?, ?, 'login', 'User logged in via Google', ?, NOW())";
    $stmt = $conn->prepare($login_activity_query);
    $ip = $_SERVER['REMOTE_ADDR'];
    $role = $user['role'] ?? 'student';
    $stmt->bind_param("iss", $user['id'], $role, $ip);
    $stmt->execute();
}

// Redirect based on role
$role = $user['role'] ?? 'student';
$destination = 'index.php'; // default

if ($role == 'student') {
    $destination = 'student/index.php';
} elseif ($role == 'faculty' || $role == 'instructor') {
    $destination = 'faculty/index.php';
} elseif ($role == 'admin' || $role == 'director') {
    $destination = 'admin/index.php';
}

// Redirect to the appropriate page
header("Location: $destination");
exit;