43 lines
1.4 KiB
PHP
43 lines
1.4 KiB
PHP
<?php
|
|
session_start();
|
|
require_once '../config/db.php';
|
|
require_once '../includes/functions.php';
|
|
|
|
// Include Composer autoloader
|
|
require_once '../vendor/autoload.php';
|
|
|
|
if (!isset($_SESSION['loggedin']) || $_SESSION['loggedin'] !== true) {
|
|
header("location: ../index.php");
|
|
exit;
|
|
}
|
|
|
|
$client_id = get_setting($pdo_sys, 'google_client_id');
|
|
$client_secret = get_setting($pdo_sys, 'google_client_secret');
|
|
|
|
if (empty($client_id) || empty($client_secret)) {
|
|
$_SESSION['settings_error'] = "กรุณาตั้งค่า Client ID และ Client Secret ก่อนทำการเชื่อมต่อ";
|
|
header("location: ../settings.php");
|
|
exit;
|
|
}
|
|
|
|
$client = new Google\Client();
|
|
$client->setClientId($client_id);
|
|
$client->setClientSecret($client_secret);
|
|
|
|
// Construct redirect URI dynamically
|
|
$protocol = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on') ? "https" : "http";
|
|
$host = $_SERVER['HTTP_HOST'];
|
|
$base_dir = dirname(dirname($_SERVER['PHP_SELF']));
|
|
$redirect_uri = $protocol . "://" . $host . $base_dir . "/auth/google_callback.php";
|
|
$redirect_uri = str_replace('//auth', '/auth', $redirect_uri);
|
|
|
|
$client->setRedirectUri($redirect_uri);
|
|
$client->addScope(Google\Service\Calendar::CALENDAR_EVENTS);
|
|
$client->setAccessType('offline');
|
|
$client->setPrompt('consent');
|
|
|
|
$auth_url = $client->createAuthUrl();
|
|
header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
|
|
exit;
|
|
?>
|