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

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. {
  21.     private $loginService;
  22.     private $guestLoginService;
  23.     private $helperService;
  24.     private $session;
  25.     private $systemConfigService;
  26.     private $accountService;
  27.     private $cartService;
  28.     public function __construct(
  29.         LoginService        $loginService,
  30.         GuestLoginService   $guestLoginService,
  31.         HelperService       $helperService,
  32.         Session             $session,
  33.         SystemConfigService $systemConfigService,
  34.         AccountService      $accountService,
  35.         CartService         $cartService null,
  36.     ) {
  37.         $this->loginService $loginService;
  38.         $this->guestLoginService $guestLoginService;
  39.         $this->helperService $helperService;
  40.         $this->session $session;
  41.         $this->systemConfigService $systemConfigService;
  42.         $this->accountService $accountService;
  43.         $this->cartService $cartService;
  44.     }
  45.     public static function getSubscribedEvents(): array
  46.     {
  47.         return [
  48.             GenericPageLoadedEvent::class => 'onPageLoaded',
  49.             BeforeSendResponseEvent::class => 'beforeSendResponse',
  50.             CustomerLogoutEvent::class => 'onLogout',
  51.         ];
  52.     }
  53.     public function onPageLoaded(GenericPageLoadedEvent $event)
  54.     {
  55.         if($event->getRequest()->server->get('REDIRECT_URL') != null &&
  56.         (strpos($event->getRequest()->server->get('REDIRECT_URL'), '/NWB-NEO/')) !== false
  57.         && !in_array($event->getRequest()->server->get('HTTP_X_REAL_IP'), $event->getSalesChannelContext()->getSalesChannel()->getMaintenanceIpWhitelist())) {
  58.             header(
  59.                 'Location: https://shop.nwb.de',
  60.             );
  61.         }
  62.         header(
  63.                 'Location: https://shop.nwb.de',
  64.             );
  65.             die();
  66.         try {
  67.             if ($event->getSalesChannelContext()->getCustomer() !== null) {
  68.                 if ((isset($_COOKIE['bisLogon']) && $_COOKIE['bisLogon'] == 'Logged_Out')) {
  69.                     header('Location: https://' $_SERVER['SERVER_NAME'] . '/account/logout');
  70.                     die();
  71.                 }
  72.                 return;
  73.             }
  74.             $this->session->start();
  75.             $nwbService $event
  76.                 ->getSalesChannelContext()
  77.                 ->getSalesChannel()
  78.                 ->getCustomFields()['bis_url'];
  79.             $shopUrl $event
  80.                 ->getSalesChannelContext()
  81.                 ->getSalesChannel()
  82.                 ->getCustomFields()['shop_url'];
  83.             $cookieBisNameUser $event
  84.                 ->getSalesChannelContext()
  85.                 ->getSalesChannel()
  86.                 ->getCustomFields()['cookie_bis'];
  87.             $cookieAuthNameUser $event
  88.                 ->getSalesChannelContext()
  89.                 ->getSalesChannel()
  90.                 ->getCustomFields()['cookie_bis_auth'];
  91.             $cookieBisNameToBis $event
  92.                 ->getSalesChannelContext()
  93.                 ->getSalesChannel()
  94.                 ->getCustomFields()['cookie_bis_sending'];
  95.             $cookieAuthNameToBis $event
  96.                 ->getSalesChannelContext()
  97.                 ->getSalesChannel()
  98.                 ->getCustomFields()['cookie_bis_auth_sending'];
  99.             $cookieBisUser $_COOKIE[$cookieBisNameUser] ?? null;
  100.             $cookieAuthUser $_COOKIE[$cookieAuthNameUser] ?? null;
  101.             if ($cookieBisUser === null && !isset($_SESSION['addressGroupId'])) {
  102.                 return;
  103.             }
  104.             $context $event->getContext();
  105.             if ($cookieBisUser != null) {
  106.                 $url 'https://' $nwbService '/customercare/api/accounts/info';
  107.                 $curl curl_init($url);
  108.                 curl_setopt($curlCURLOPT_RETURNTRANSFERtrue);
  109.                 curl_setopt(
  110.                     $curl,
  111.                     CURLOPT_COOKIE,
  112.                     $cookieBisNameToBis .
  113.                     '=' .
  114.                     $cookieBisUser .
  115.                     ';' .
  116.                     $cookieAuthNameToBis .
  117.                     '=' .
  118.                     $cookieAuthUser,
  119.                 );
  120.                 curl_setopt($curlCURLOPT_SSL_VERIFYPEERfalse);
  121.                 $response curl_exec($curl);
  122.                 $nwbUserData $response json_decode($response) : null;
  123.                 curl_close($curl);
  124.                 /* LOGOUT */
  125.                 if (
  126.                     isset($_SESSION['nwb_account_id']) &&
  127.                     $_SESSION['nwb_account_id'] != $nwbUserData->AccountID
  128.                 ) {
  129.                     $_SESSION['logout_redirect'] = $_SERVER['SCRIPT_URI'];
  130.                     unset($_SESSION['nwb_account_id']);
  131.                     header('Location: https://' $shopUrl '/account/logout');
  132.                     exit();
  133.                 }
  134.                 if (isset($_SESSION['nwb_account_id']) && $nwbUserData->State >= 1) {
  135.                     $_SESSION['logout_redirect'] = $_SERVER['SCRIPT_URI'];
  136.                     unset($_SESSION['nwb_account_id']);
  137.                     header('Location: https://' $shopUrl '/account/logout');
  138.                     exit();
  139.                 }
  140.             }
  141.             if (
  142.                 $cookieBisUser != null &&
  143.                 $nwbUserData->AccountID != '00000000-0000-0000-0000-000000000000' &&
  144.                 $event->getSalesChannelContext()->getCustomer() == null
  145.             ) {
  146.                 $dokumentenId null;
  147.                 $pk null;
  148.                 $guestAccountId null;
  149.                 if (isset($_SESSION['guestCreatedAccount']) && $_SESSION['guestCreatedAccount'] != '') {
  150.                     $guestAccountId $_SESSION['guestCreatedAccount'];
  151.                 }
  152.                 if (
  153.                     isset($_SESSION['nwb_add_product_next_url']) &&
  154.                     $_SESSION['nwb_add_product_next_url'] != ''
  155.                 ) {
  156.                     $_SESSION['next_url'] = $_SESSION['nwb_add_product_next_url'];
  157.                 }
  158.                 if (isset($_SESSION['DokumentenId']) && $_SESSION['DokumentenId'] != '') {
  159.                     $dokumentenId $_SESSION['DokumentenId'];
  160.                     unset($_SESSION['DokumentenId']);
  161.                     $_SESSION['dokument_kauf'] = 'true';
  162.                 }
  163.                 if (isset($_SESSION['pk']) && $_SESSION['pk'] != '') {
  164.                     $pk $_SESSION['pk'];
  165.                 }
  166.                 if (
  167.                     !is_null($event->getSalesChannelContext()->getCustomer()) &&
  168.                     $event
  169.                         ->getSalesChannelContext()
  170.                         ->getCustomer()
  171.                         ->getGuest()
  172.                 ) {
  173.                     $guestAccountId $event
  174.                         ->getSalesChannelContext()
  175.                         ->getCustomer()
  176.                         ->getId();
  177.                 }
  178.                 $_SESSION['login_timestamp'] = time();
  179.                 $loginBody $this->loginService->cookieLogin(
  180.                     $context,
  181.                     $this->helperService,
  182.                     $nwbService,
  183.                     $cookieBisUser,
  184.                     $cookieAuthUser,
  185.                     $cookieBisNameToBis,
  186.                     $cookieAuthNameToBis,
  187.                     $dokumentenId,
  188.                     $pk,
  189.                     $guestAccountId,
  190.                 );
  191.                 if (isset($loginBody->succes) && $loginBody->succes) {
  192.                     $event->getPage()->assign(['login_process' => true]);
  193.                     $this->helperService->updateOrCreateLastSignup(
  194.                         $loginBody->loginData->nwbAccountId,
  195.                         $context,
  196.                     );
  197.                     $this->accountService->loginById(
  198.                         $loginBody->loginData->id,
  199.                         $event->getSalesChannelContext(),
  200.                     );
  201.                 }
  202.             }
  203.             if (
  204.                 isset($_SESSION['addressGroupId']) &&
  205.                 !empty($_SESSION['addressGroupId']) &&
  206.                 empty($event->getSalesChannelContext()->getCustomer())
  207.             ) {
  208.                 if ($this->helperService->isAllowedToSignup($_SESSION['addressGroupId'], $context)) {
  209.                     $this->helperService->updateOrCreateLastSignup($_SESSION['addressGroupId'], $context);
  210.                     $event->getPage()->assign(['login_process' => true]);
  211.                     $newContext $this->guestLoginService->guestLogin(
  212.                         $_SESSION['addressGroupId'],
  213.                         $nwbService,
  214.                         $this->helperService,
  215.                         $this->accountService,
  216.                         $event->getSalesChannelContext(),
  217.                         $context,
  218.                     );
  219.                 }
  220.             }
  221.         } catch (Exception $ex) {
  222.         }
  223.     }
  224.     public function beforeSendResponse(BeforeSendResponseEvent $event)
  225.     {
  226.         if (isset($_GET['addressGroupId']) && !empty($_GET['addressGroupId'])) {
  227.             session_start();
  228.             $_SESSION['addressGroupId'] = $_GET['addressGroupId'];
  229.         }
  230.     }
  231.     public function onLogout(CustomerLogoutEvent $event)
  232.     {
  233.         $nwbService $event
  234.             ->getSalesChannelContext()
  235.             ->getSalesChannel()
  236.             ->getCustomFields()['bis_url'];
  237.         $shopUrl $event
  238.             ->getSalesChannelContext()
  239.             ->getSalesChannel()
  240.             ->getCustomFields()['shop_url'];
  241.         if (isset($_GET['guestCreateAccount'])) {
  242.             $_SESSION['guestCreatedAccount'] = $event->getCustomer()->getId();
  243.             $redirectUrl = isset($_SERVER['HTTP_REFERER'])
  244.                 ? $_SERVER['HTTP_REFERER']
  245.                 : 'https://' $shopUrl '/account';
  246.             header(
  247.                 'Location: https://' .
  248.                 $nwbService .
  249.                 '/customercare/public/regcreate/?addressesrequired=true&nextUrl=' .
  250.                 $redirectUrl,
  251.             );
  252.             die();
  253.         }
  254.         if (isset($_COOKIE['bis7'])) {
  255.             unset($_COOKIE['bis7']);
  256.         }
  257.         if (isset($_COOKIE['_ASPXAUTHBIS'])) {
  258.             unset($_COOKIE['_ASPXAUTHBIS']);
  259.         }
  260.     }
  261. }