<?php
declare(strict_types=1);
namespace ImnxxCustomerHelper\Subscriber;
use Exception;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Storefront\Page\GenericPageLoadedEvent;
use ImnxxCustomerHelper\Service\LoginService;
use ImnxxCustomerHelper\Service\GuestLoginService;
use ImnxxCustomerHelper\Service\HelperService;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Symfony\Component\HttpFoundation\Session\Session;
use Shopware\Core\Checkout\Customer\SalesChannel\AccountService;
use Shopware\Core\Framework\Event\BeforeSendResponseEvent;
use Shopware\Core\Checkout\Customer\Event\CustomerLogoutEvent;
use Shopware\Core\Checkout\Cart\SalesChannel\CartService;
/**
* !IMPORTANT NOTE! The login Event gets captured in ImnxxCartRestorer, so the events don't collide
*/
class CustomerSubscriber implements EventSubscriberInterface {
private $loginService;
private $guestLoginService;
private $helperService;
private $session;
private $systemConfigService;
private $accountService;
private $cartService;
public function __construct(
LoginService $loginService,
GuestLoginService $guestLoginService,
HelperService $helperService,
Session $session,
SystemConfigService $systemConfigService,
AccountService $accountService,
CartService $cartService = null,
) {
$this->loginService = $loginService;
$this->guestLoginService = $guestLoginService;
$this->helperService = $helperService;
$this->session = $session;
$this->systemConfigService = $systemConfigService;
$this->accountService = $accountService;
$this->cartService = $cartService;
}
public static function getSubscribedEvents(): array {
return [
GenericPageLoadedEvent::class => 'onPageLoaded',
BeforeSendResponseEvent::class => 'beforeSendResponse',
CustomerLogoutEvent::class => 'onLogout',
];
}
public function onPageLoaded(GenericPageLoadedEvent $event) {
try {
if ($event->getSalesChannelContext()->getCustomer() !== null) {
return;
}
$this->session->start();
$nwbService = $event
->getSalesChannelContext()
->getSalesChannel()
->getCustomFields()['bis_url'];
$shopUrl = $event
->getSalesChannelContext()
->getSalesChannel()
->getCustomFields()['shop_url'];
$cookieBisNameUser = $event
->getSalesChannelContext()
->getSalesChannel()
->getCustomFields()['cookie_bis'];
$cookieAuthNameUser = $event
->getSalesChannelContext()
->getSalesChannel()
->getCustomFields()['cookie_bis_auth'];
$cookieBisNameToBis = $event
->getSalesChannelContext()
->getSalesChannel()
->getCustomFields()['cookie_bis_sending'];
$cookieAuthNameToBis = $event
->getSalesChannelContext()
->getSalesChannel()
->getCustomFields()['cookie_bis_auth_sending'];
$cookieBisUser = $_COOKIE[$cookieBisNameUser] ?? null;
$cookieAuthUser = $_COOKIE[$cookieAuthNameUser] ?? null;
if ($cookieBisUser === null && !isset($_SESSION['addressGroupId'])) {
return;
}
$context = $event->getContext();
if ($cookieBisUser != null) {
$url = 'https://' . $nwbService . '/customercare/api/accounts/info';
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt(
$curl,
CURLOPT_COOKIE,
$cookieBisNameToBis .
'=' .
$cookieBisUser .
';' .
$cookieAuthNameToBis .
'=' .
$cookieAuthUser,
);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$nwbUserData = json_decode(curl_exec($curl));
curl_close($curl);
/* LOGOUT */
if (
isset($_SESSION['nwb_account_id']) &&
$_SESSION['nwb_account_id'] != $nwbUserData->AccountID
) {
$_SESSION['logout_redirect'] = $_SERVER['SCRIPT_URI'];
unset($_SESSION['nwb_account_id']);
header('Location: https://' . $shopUrl . '/account/logout');
exit();
}
if (isset($_SESSION['nwb_account_id']) && $nwbUserData->State >= 1) {
$_SESSION['logout_redirect'] = $_SERVER['SCRIPT_URI'];
unset($_SESSION['nwb_account_id']);
header('Location: https://' . $shopUrl . '/account/logout');
exit();
}
}
if (
$cookieBisUser != null &&
$nwbUserData->AccountID != '00000000-0000-0000-0000-000000000000' &&
$event->getSalesChannelContext()->getCustomer() == null
) {
$dokumentenId = null;
$pk = null;
$guestAccountId = null;
if (isset($_SESSION['guestCreatedAccount']) && $_SESSION['guestCreatedAccount'] != '') {
$guestAccountId = $_SESSION['guestCreatedAccount'];
}
if (
isset($_SESSION['nwb_add_product_next_url']) &&
$_SESSION['nwb_add_product_next_url'] != ''
) {
$_SESSION['next_url'] = $_SESSION['nwb_add_product_next_url'];
}
if (isset($_SESSION['DokumentenId']) && $_SESSION['DokumentenId'] != '') {
$dokumentenId = $_SESSION['DokumentenId'];
unset($_SESSION['DokumentenId']);
$_SESSION['dokument_kauf'] = 'true';
}
if (isset($_SESSION['pk']) && $_SESSION['pk'] != '') {
$pk = $_SESSION['pk'];
}
if (
!is_null($event->getSalesChannelContext()->getCustomer()) &&
$event
->getSalesChannelContext()
->getCustomer()
->getGuest()
) {
$guestAccountId = $event
->getSalesChannelContext()
->getCustomer()
->getId();
}
$_SESSION['login_timestamp'] = time();
$loginBody = $this->loginService->cookieLogin(
$context,
$this->helperService,
$nwbService,
$cookieBisUser,
$cookieAuthUser,
$cookieBisNameToBis,
$cookieAuthNameToBis,
$dokumentenId,
$pk,
$guestAccountId,
);
if (isset($loginBody->succes) && $loginBody->succes) {
$event->getPage()->assign(['login_process' => true]);
$this->helperService->updateOrCreateLastSignup(
$loginBody->loginData->nwbAccountId,
$context,
);
$this->accountService->loginById(
$loginBody->loginData->id,
$event->getSalesChannelContext(),
);
}
}
if (
isset($_SESSION['addressGroupId']) &&
!empty($_SESSION['addressGroupId']) &&
empty($event->getSalesChannelContext()->getCustomer())
) {
if ($this->helperService->isAllowedToSignup($_SESSION['addressGroupId'], $context)) {
$this->helperService->updateOrCreateLastSignup($_SESSION['addressGroupId'], $context);
$event->getPage()->assign(['login_process' => true]);
$newContext = $this->guestLoginService->guestLogin(
$_SESSION['addressGroupId'],
$nwbService,
$this->helperService,
$this->accountService,
$event->getSalesChannelContext(),
$context,
);
}
}
} catch (Exception $ex) {
}
}
public function beforeSendResponse(BeforeSendResponseEvent $event) {
if (isset($_GET['addressGroupId']) && !empty($_GET['addressGroupId'])) {
session_start();
$_SESSION['addressGroupId'] = $_GET['addressGroupId'];
}
}
public function onLogout(CustomerLogoutEvent $event) {
$nwbService = $event
->getSalesChannelContext()
->getSalesChannel()
->getCustomFields()['bis_url'];
$shopUrl = $event
->getSalesChannelContext()
->getSalesChannel()
->getCustomFields()['shop_url'];
if (isset($_GET['guestCreateAccount'])) {
$_SESSION['guestCreatedAccount'] = $event->getCustomer()->getId();
$redirectUrl = isset($_SERVER['HTTP_REFERER'])
? $_SERVER['HTTP_REFERER']
: 'https://' . $shopUrl . '/account';
header(
'Location: https://' .
$nwbService .
'/customercare/public/regcreate/?addressesrequired=true&nextUrl=' .
$redirectUrl,
);
die();
}
if (isset($_COOKIE['bis7'])) {
unset($_COOKIE['bis7']);
}
if (isset($_COOKIE['_ASPXAUTHBIS'])) {
unset($_COOKIE['_ASPXAUTHBIS']);
}
}
}