<?php
require_once('common_cadline_libraries/crm_users.class.php');
require_once($_SERVER['DOCUMENT_ROOT'] . '/libraries/vendor/google-api/src/Google/autoload.php');

if (!isset($_GET['state']) || !isset($_GET['code'])) exit();

$datas = json_decode(base64_decode($_GET['state']));

$client = new Google_Client();
$client->setClientId('427394287601-f5fvh3809726dah7lq7d4pkl4af8p7av.apps.googleusercontent.com');
$client->setClientSecret('_qMqFv2zuk2b0z7fazwgNuBk');
$client->setRedirectUri('https://www.archlinexp.com/maintenance/google_redirect.php');
$client->addScope('email');
$client->addScope('profile');

$client->authenticate($_GET['code']);

$objOAuthService = new Google_Service_Oauth2($client);

if ($client->getAccessToken())
    $userData = $objOAuthService->userinfo->get();

$datas->userName    = $userData['name'];
$datas->userEmail   = $userData['email'];

switch ($datas->action) {
    case 'event_registration':
        $event          = new Event($datas);
        $success        = $event->registration() ? 'success' : 'failed';

        if ($datas->evt_id == '107')
            $returnedURL    = 'https://www.archlinexp.com/education/events/2025-update-day-march';
        else
            $returnedURL    = 'https://www.archlinexp.com/events';

        header('Location: ' . $returnedURL . '?success=' . $success);
        exit;
        break;

    case 'win_al_live':
        $event          = new Event($datas);
        $success        = $event->registerToWinLIVE() ? 'success' : 'failed';
        $returnedURL    = 'https://www.archlinexp.com/win-al-live';

        header('Location: ' . $returnedURL . '?success=' . $success);
        exit;
        break;

    case 'webinar_registration':
        $event          = new Event($datas);
        $success        = $event->registrationToWebinar() ? 'success' : 'failed';
        $returnedURL    = $datas->crm_db == 'clusers' ? 'https://www.archline.hu/oktatas/webinariumok/regisztracio' : 'https://www.archlinexp.com/webinars/registration';

        header('Location: ' . $returnedURL . '?webinar_id=' . $datas->course_id . '&success=' . $success);
        exit;
        break;

    default:
        $returnedURL = 'https://www.archlinexp.com/';

        header('Location: ' . $returnedURL);
        exit;
        break;
}
