custom/plugins/ImnxxCustomerHelper/src/Subscriber/CustomerSubscriber.php line 236

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace ImnxxCustomerHelper\Subscriber;
  4. use Exception;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Shopware\Storefront\Page\GenericPageLoadedEvent;
  7. use ImnxxCustomerHelper\Service\LoginService;
  8. use ImnxxCustomerHelper\Service\GuestLoginService;
  9. use ImnxxCustomerHelper\Service\HelperService;
  10. use Shopware\Core\System\SystemConfig\SystemConfigService;
  11. use Symfony\Component\HttpFoundation\Session\Session;
  12. use Shopware\Core\Checkout\Customer\SalesChannel\AccountService;
  13. use Shopware\Core\Framework\Event\BeforeSendResponseEvent;
  14. use Shopware\Core\Checkout\Customer\Event\CustomerLogoutEvent;
  15. use Shopware\Core\Checkout\Cart\SalesChannel\CartService;
  16. /**
  17.  * !IMPORTANT NOTE! The login Event gets captured in ImnxxCartRestorer, so the events don't collide
  18.  */
  19. class CustomerSubscriber implements EventSubscriberInterface {
  20.     private $loginService;
  21.     private $guestLoginService;
  22.     private $helperService;
  23.     private $session;
  24.     private $systemConfigService;
  25.     private $accountService;
  26.     private $cartService;
  27.     public function __construct(
  28.         LoginService $loginService,
  29.         GuestLoginService $guestLoginService,
  30.         HelperService $helperService,
  31.         Session $session,
  32.         SystemConfigService $systemConfigService,
  33.         AccountService $accountService,
  34.         CartService $cartService null,
  35.     ) {
  36.         $this->loginService $loginService;
  37.         $this->guestLoginService $guestLoginService;
  38.         $this->helperService $helperService;
  39.         $this->session $session;
  40.         $this->systemConfigService $systemConfigService;
  41.         $this->accountService $accountService;
  42.         $this->cartService $cartService;
  43.     }
  44.     public static function getSubscribedEvents(): array {
  45.         return [
  46.             GenericPageLoadedEvent::class => 'onPageLoaded',
  47.             BeforeSendResponseEvent::class => 'beforeSendResponse',
  48.             CustomerLogoutEvent::class => 'onLogout',
  49.         ];
  50.     }
  51.     public function onPageLoaded(GenericPageLoadedEvent $event) {
  52.         try {
  53.             if ($event->getSalesChannelContext()->getCustomer() !== null) {
  54.                 return;
  55.             }
  56.             $this->session->start();
  57.             $nwbService $event
  58.                 ->getSalesChannelContext()
  59.                 ->getSalesChannel()
  60.                 ->getCustomFields()['bis_url'];
  61.             $shopUrl $event
  62.                 ->getSalesChannelContext()
  63.                 ->getSalesChannel()
  64.                 ->getCustomFields()['shop_url'];
  65.             $cookieBisNameUser $event
  66.                 ->getSalesChannelContext()
  67.                 ->getSalesChannel()
  68.                 ->getCustomFields()['cookie_bis'];
  69.             $cookieAuthNameUser $event
  70.                 ->getSalesChannelContext()
  71.                 ->getSalesChannel()
  72.                 ->getCustomFields()['cookie_bis_auth'];
  73.             $cookieBisNameToBis $event
  74.                 ->getSalesChannelContext()
  75.                 ->getSalesChannel()
  76.                 ->getCustomFields()['cookie_bis_sending'];
  77.             $cookieAuthNameToBis $event
  78.                 ->getSalesChannelContext()
  79.                 ->getSalesChannel()
  80.                 ->getCustomFields()['cookie_bis_auth_sending'];
  81.             $cookieBisUser $_COOKIE[$cookieBisNameUser] ?? null;
  82.             $cookieAuthUser $_COOKIE[$cookieAuthNameUser] ?? null;
  83.             if ($cookieBisUser === null && !isset($_SESSION['addressGroupId'])) {
  84.                 return;
  85.             }
  86.             $context $event->getContext();
  87.             if ($cookieBisUser != null) {
  88.                 $url 'https://' $nwbService '/customercare/api/accounts/info';
  89.                 $curl curl_init($url);
  90.                 curl_setopt($curlCURLOPT_RETURNTRANSFERtrue);
  91.                 curl_setopt(
  92.                     $curl,
  93.                     CURLOPT_COOKIE,
  94.                     $cookieBisNameToBis .
  95.                         '=' .
  96.                         $cookieBisUser .
  97.                         ';' .
  98.                         $cookieAuthNameToBis .
  99.                         '=' .
  100.                         $cookieAuthUser,
  101.                 );
  102.                 curl_setopt($curlCURLOPT_SSL_VERIFYPEERfalse);
  103.                 $nwbUserData json_decode(curl_exec($curl));
  104.                 curl_close($curl);
  105.                 /* LOGOUT */
  106.                 if (
  107.                     isset($_SESSION['nwb_account_id']) &&
  108.                     $_SESSION['nwb_account_id'] != $nwbUserData->AccountID
  109.                 ) {
  110.                     $_SESSION['logout_redirect'] = $_SERVER['SCRIPT_URI'];
  111.                     unset($_SESSION['nwb_account_id']);
  112.                     header('Location: https://' $shopUrl '/account/logout');
  113.                     exit();
  114.                 }
  115.                 if (isset($_SESSION['nwb_account_id']) && $nwbUserData->State >= 1) {
  116.                     $_SESSION['logout_redirect'] = $_SERVER['SCRIPT_URI'];
  117.                     unset($_SESSION['nwb_account_id']);
  118.                     header('Location: https://' $shopUrl '/account/logout');
  119.                     exit();
  120.                 }
  121.             }
  122.             if (
  123.                 $cookieBisUser != null &&
  124.                 $nwbUserData->AccountID != '00000000-0000-0000-0000-000000000000' &&
  125.                 $event->getSalesChannelContext()->getCustomer() == null
  126.             ) {
  127.                 $dokumentenId null;
  128.                 $pk null;
  129.                 $guestAccountId null;
  130.                 if (isset($_SESSION['guestCreatedAccount']) && $_SESSION['guestCreatedAccount'] != '') {
  131.                     $guestAccountId $_SESSION['guestCreatedAccount'];
  132.                 }
  133.                 if (
  134.                     isset($_SESSION['nwb_add_product_next_url']) &&
  135.                     $_SESSION['nwb_add_product_next_url'] != ''
  136.                 ) {
  137.                     $_SESSION['next_url'] = $_SESSION['nwb_add_product_next_url'];
  138.                 }
  139.                 if (isset($_SESSION['DokumentenId']) && $_SESSION['DokumentenId'] != '') {
  140.                     $dokumentenId $_SESSION['DokumentenId'];
  141.                     unset($_SESSION['DokumentenId']);
  142.                     $_SESSION['dokument_kauf'] = 'true';
  143.                 }
  144.                 if (isset($_SESSION['pk']) && $_SESSION['pk'] != '') {
  145.                     $pk $_SESSION['pk'];
  146.                 }
  147.                 if (
  148.                     !is_null($event->getSalesChannelContext()->getCustomer()) &&
  149.                     $event
  150.                         ->getSalesChannelContext()
  151.                         ->getCustomer()
  152.                         ->getGuest()
  153.                 ) {
  154.                     $guestAccountId $event
  155.                         ->getSalesChannelContext()
  156.                         ->getCustomer()
  157.                         ->getId();
  158.                 }
  159.                 $_SESSION['login_timestamp'] = time();
  160.                 $loginBody $this->loginService->cookieLogin(
  161.                     $context,
  162.                     $this->helperService,
  163.                     $nwbService,
  164.                     $cookieBisUser,
  165.                     $cookieAuthUser,
  166.                     $cookieBisNameToBis,
  167.                     $cookieAuthNameToBis,
  168.                     $dokumentenId,
  169.                     $pk,
  170.                     $guestAccountId,
  171.                 );
  172.                 if (isset($loginBody->succes) && $loginBody->succes) {
  173.                     $event->getPage()->assign(['login_process' => true]);
  174.                     $this->helperService->updateOrCreateLastSignup(
  175.                         $loginBody->loginData->nwbAccountId,
  176.                         $context,
  177.                     );
  178.                     $this->accountService->loginById(
  179.                         $loginBody->loginData->id,
  180.                         $event->getSalesChannelContext(),
  181.                     );
  182.                 }
  183.             }
  184.             if (
  185.                 isset($_SESSION['addressGroupId']) &&
  186.                 !empty($_SESSION['addressGroupId']) &&
  187.                 empty($event->getSalesChannelContext()->getCustomer())
  188.             ) {
  189.                 if ($this->helperService->isAllowedToSignup($_SESSION['addressGroupId'], $context)) {
  190.                     $this->helperService->updateOrCreateLastSignup($_SESSION['addressGroupId'], $context);
  191.                     $event->getPage()->assign(['login_process' => true]);
  192.                     $newContext $this->guestLoginService->guestLogin(
  193.                         $_SESSION['addressGroupId'],
  194.                         $nwbService,
  195.                         $this->helperService,
  196.                         $this->accountService,
  197.                         $event->getSalesChannelContext(),
  198.                         $context,
  199.                     );
  200.                 }
  201.             }
  202.         } catch (Exception $ex) {
  203.         }
  204.     }
  205.     public function beforeSendResponse(BeforeSendResponseEvent $event) {
  206.         if (isset($_GET['addressGroupId']) && !empty($_GET['addressGroupId'])) {
  207.             session_start();
  208.             $_SESSION['addressGroupId'] = $_GET['addressGroupId'];
  209.         }
  210.     }
  211.     public function onLogout(CustomerLogoutEvent $event) {
  212.         $nwbService $event
  213.             ->getSalesChannelContext()
  214.             ->getSalesChannel()
  215.             ->getCustomFields()['bis_url'];
  216.         $shopUrl $event
  217.             ->getSalesChannelContext()
  218.             ->getSalesChannel()
  219.             ->getCustomFields()['shop_url'];
  220.         if (isset($_GET['guestCreateAccount'])) {
  221.             $_SESSION['guestCreatedAccount'] = $event->getCustomer()->getId();
  222.             $redirectUrl = isset($_SERVER['HTTP_REFERER'])
  223.                 ? $_SERVER['HTTP_REFERER']
  224.                 : 'https://' $shopUrl '/account';
  225.             header(
  226.                 'Location: https://' .
  227.                     $nwbService .
  228.                     '/customercare/public/regcreate/?addressesrequired=true&nextUrl=' .
  229.                     $redirectUrl,
  230.             );
  231.             die();
  232.         }
  233.         if (isset($_COOKIE['bis7'])) {
  234.             unset($_COOKIE['bis7']);
  235.         }
  236.         if (isset($_COOKIE['_ASPXAUTHBIS'])) {
  237.             unset($_COOKIE['_ASPXAUTHBIS']);
  238.         }
  239.     }
  240. }