Esempio n. 1
0
 /**
  * Handle private content version cookie
  * Set cookie if it is not set.
  * Increment version on post requests.
  * In all other cases do nothing.
  *
  * @return void
  */
 public function process()
 {
     if ($this->request->isPost()) {
         $publicCookieMetadata = $this->cookieMetadataFactory->createPublicCookieMetadata()->setDuration(self::COOKIE_PERIOD)->setPath('/')->setHttpOnly(false);
         $this->cookieManager->setPublicCookie(self::COOKIE_NAME, $this->generateValue(), $publicCookieMetadata);
     }
 }
 /**
  * @param string $formKey
  * @return void
  */
 private function updateCookieFormKey($formKey)
 {
     $cookieMetadata = $this->cookieMetadataFactory->createPublicCookieMetadata();
     $cookieMetadata->setDomain($this->sessionConfig->getCookieDomain());
     $cookieMetadata->setPath($this->sessionConfig->getCookiePath());
     $cookieMetadata->setDuration($this->sessionConfig->getCookieLifetime());
     $this->cookieFormKey->set($formKey, $cookieMetadata);
 }
Esempio n. 3
0
 /**
  * Set Cookie for msg box when it displays first
  *
  * @param FrontController $subject
  * @param \Magento\Framework\App\ResponseInterface|\Magento\Framework\Controller\ResultInterface $result
  *
  * @return \Magento\Framework\App\ResponseInterface|\Magento\Framework\Controller\ResultInterface
  * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  */
 public function afterDispatch(FrontController $subject, $result)
 {
     if ($this->request->isPost() && $this->messageManager->hasMessages()) {
         $publicCookieMetadata = $this->cookieMetadataFactory->createPublicCookieMetadata()->setDuration(self::COOKIE_PERIOD)->setPath('/')->setHttpOnly(false);
         $this->cookieManager->setPublicCookie(self::COOKIE_NAME, 1, $publicCookieMetadata);
     }
     return $result;
 }
Esempio n. 4
0
 /**
  * @param ResultInterface $subject
  * @param ResultInterface $result
  * @return ResultInterface
  */
 public function afterRenderResult(ResultInterface $subject, ResultInterface $result)
 {
     if (!$subject instanceof Json) {
         $publicCookieMetadata = $this->cookieMetadataFactory->createPublicCookieMetadata();
         $publicCookieMetadata->setDurationOneYear();
         $publicCookieMetadata->setPath('/');
         $publicCookieMetadata->setHttpOnly(false);
         $this->cookieManager->setPublicCookie(self::MESSAGES_COOKIES_NAME, $this->jsonHelper->jsonEncode($this->getMessages()), $publicCookieMetadata);
     }
     return $result;
 }
 /**
  * Expire the session cookie
  *
  * Sends a session cookie with no value, and with an expiry in the past.
  *
  * @return void
  */
 public function expireSessionCookie()
 {
     if (!$this->sessionConfig->getUseCookies()) {
         return;
     }
     $metadata = $this->cookieMetadataFactory->createPublicCookieMetadata();
     $metadata->setPath($this->sessionConfig->getCookiePath());
     $metadata->setDomain($this->sessionConfig->getCookieDomain());
     $metadata->setSecure($this->sessionConfig->getCookieSecure());
     $metadata->setHttpOnly($this->sessionConfig->getCookieHttpOnly());
     $this->cookieManager->deleteCookie($this->getName(), $metadata);
     $this->clearSubDomainSessionCookie();
 }
Esempio n. 6
0
 /**
  * Send Vary coookie
  *
  * @return void
  */
 public function sendVary()
 {
     $data = $this->context->getData();
     if (!empty($data)) {
         ksort($data);
         $cookieValue = sha1(serialize($data));
         $publicCookMetadata = $this->cookieMetadataFactory->createPublicCookieMetadata()->setPath('/');
         $this->cookieManager->setPublicCookie(self::COOKIE_VARY_STRING, $cookieValue, $publicCookMetadata);
     } else {
         $cookieMetadata = $this->cookieMetadataFactory->createCookieMetadata()->setPath('/');
         $this->cookieManager->deleteCookie(self::COOKIE_VARY_STRING, $cookieMetadata);
     }
 }
Esempio n. 7
0
 /**
  * Set persistent shopping cart cookie.
  *
  * @param string $value
  * @param int $duration
  * @param string $path
  * @return void
  */
 private function setCookie($value, $duration, $path)
 {
     $publicCookieMetadata = $this->_cookieMetadataFactory->createPublicCookieMetadata()->setDuration($duration)->setPath($path)->setHttpOnly(true);
     $this->_cookieManager->setPublicCookie(self::COOKIE_NAME, $value, $publicCookieMetadata);
 }
Esempio n. 8
0
 /**
  * Delete store cookie.
  *
  * @return $this
  */
 public function deleteCookie()
 {
     $cookieMetadata = $this->_cookieMetadataFactory->createPublicCookieMetadata()->setPath($this->getStorePath());
     $this->_cookieManager->deleteCookie(self::COOKIE_NAME, $cookieMetadata);
     return $this;
 }
 /**
  * {@inheritdoc}
  */
 public function deleteStoreCookie(StoreInterface $store)
 {
     $cookieMetadata = $this->cookieMetadataFactory->createPublicCookieMetadata()->setPath($store->getStorePath());
     $this->cookieManager->deleteCookie(self::COOKIE_NAME, $cookieMetadata);
 }
Esempio n. 10
0
 /**
  * Set store cookie with this store's code for a year.
  *
  * @return $this
  */
 public function setCookie()
 {
     $cookieMetadata = $this->_cookieMetadataFactory->createPublicCookieMetadata()->setHttpOnly(true)->setDurationOneYear();
     $this->_cookieManager->setPublicCookie(self::COOKIE_NAME, $this->getCode(), $cookieMetadata);
     return $this;
 }
Esempio n. 11
0
 /**
  * Set guest-view cookie
  *
  * @param string $cookieValue
  * @return void
  */
 private function setGuestViewCookie($cookieValue)
 {
     $metadata = $this->cookieMetadataFactory->createPublicCookieMetadata()->setPath(self::COOKIE_PATH)->setHttpOnly(true);
     $this->cookieManager->setPublicCookie(self::COOKIE_NAME, $cookieValue, $metadata);
 }
 /**
  * Delete debug cookie.
  *
  * @return $this
  */
 public function deleteDebugCookie()
 {
     $cookieMetadata = $this->_cookieMetadataFactory->createPublicCookieMetadata();
     $this->_cookieManager->deleteCookie(self::COOKIE_NAME, $cookieMetadata);
     return $this;
 }
Esempio n. 13
0
 /**
  * Try to load valid order by $_POST or $_COOKIE
  *
  * @param App\RequestInterface $request
  * @param App\ResponseInterface $response
  * @return bool
  * 
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  */
 public function loadValidOrder(App\RequestInterface $request, App\ResponseInterface $response)
 {
     if ($this->customerSession->isLoggedIn()) {
         $response->setRedirect($this->_urlBuilder->getUrl('sales/order/history'));
         return false;
     }
     $post = $request->getPost();
     $errors = false;
     /** @var $order \Magento\Sales\Model\Order */
     $order = $this->orderFactory->create();
     $fromCookie = $this->cookieManager->getCookie(self::COOKIE_NAME);
     if (empty($post) && !$fromCookie) {
         $response->setRedirect($this->_urlBuilder->getUrl('sales/guest/form'));
         return false;
     } elseif (!empty($post) && isset($post['oar_order_id']) && isset($post['oar_type'])) {
         $type = $post['oar_type'];
         $incrementId = $post['oar_order_id'];
         $lastName = $post['oar_billing_lastname'];
         $email = $post['oar_email'];
         $zip = $post['oar_zip'];
         if (empty($incrementId) || empty($lastName) || empty($type) || !in_array($type, array('email', 'zip')) || $type == 'email' && empty($email) || $type == 'zip' && empty($zip)) {
             $errors = true;
         }
         if (!$errors) {
             $order->loadByIncrementId($incrementId);
         }
         $errors = true;
         if ($order->getId()) {
             $billingAddress = $order->getBillingAddress();
             if (strtolower($lastName) == strtolower($billingAddress->getLastname()) && ($type == 'email' && strtolower($email) == strtolower($billingAddress->getEmail()) || $type == 'zip' && strtolower($zip) == strtolower($billingAddress->getPostcode()))) {
                 $errors = false;
             }
         }
         if (!$errors) {
             $toCookie = base64_encode($order->getProtectCode() . ':' . $incrementId);
             $metadata = $this->cookieMetadataFactory->createPublicCookieMetadata();
             $metadata->setPath(self::COOKIE_PATH);
             $metadata->setDuration(self::COOKIE_LIFETIME);
             $this->cookieManager->setPublicCookie(self::COOKIE_NAME, $toCookie, $metadata);
         }
     } elseif ($fromCookie) {
         $cookieData = explode(':', base64_decode($fromCookie));
         $protectCode = isset($cookieData[0]) ? $cookieData[0] : null;
         $incrementId = isset($cookieData[1]) ? $cookieData[1] : null;
         $errors = true;
         if (!empty($protectCode) && !empty($incrementId)) {
             $order->loadByIncrementId($incrementId);
             if ($order->getProtectCode() == $protectCode) {
                 // renew cookie
                 $metadata = $this->cookieMetadataFactory->createPublicCookieMetadata();
                 $metadata->setPath(self::COOKIE_PATH);
                 $metadata->setDuration(self::COOKIE_LIFETIME);
                 $this->cookieManager->setPublicCookie(self::COOKIE_NAME, $fromCookie, $metadata);
                 $errors = false;
             }
         }
     }
     if (!$errors && $order->getId()) {
         $this->coreRegistry->register('current_order', $order);
         return true;
     }
     $this->messageManager->addError(__('You entered incorrect data. Please try again.'));
     $response->setRedirect($this->_urlBuilder->getUrl('sales/guest/form'));
     return false;
 }
Esempio n. 14
0
 /**
  * Perform custom url rewrites
  *
  * @param \Magento\Framework\App\RequestInterface $request
  * @return bool
  */
 public function rewrite(\Magento\Framework\App\RequestInterface $request = null)
 {
     if (!$this->_appState->isInstalled()) {
         return false;
     }
     if (is_null($this->getStoreId()) || false === $this->getStoreId()) {
         $this->setStoreId($this->_storeManager->getStore()->getId());
     }
     /**
      * We have two cases of incoming paths - with and without slashes at the end ("/somepath/" and "/somepath").
      * Each of them matches two url rewrite request paths - with and without slashes at the end
      * ("/somepath/" and "/somepath").
      * Choose any matched rewrite, but in priority order that depends on same presence of slash and query params.
      */
     $requestCases = array();
     $pathInfo = $request->getPathInfo();
     $origSlash = substr($pathInfo, -1) == '/' ? '/' : '';
     $requestPath = trim($pathInfo, '/');
     // If there were final slash - add nothing to less priority paths. And vice versa.
     $altSlash = $origSlash ? '' : '/';
     $queryString = $this->_getQueryString();
     // Query params in request, matching "path + query" has more priority
     if ($queryString) {
         $requestCases[] = $requestPath . $origSlash . '?' . $queryString;
         $requestCases[] = $requestPath . $altSlash . '?' . $queryString;
     }
     $requestCases[] = $requestPath . $origSlash;
     $requestCases[] = $requestPath . $altSlash;
     $this->loadByRequestPath($requestCases);
     $targetUrl = $request->getBaseUrl();
     /**
      * Try to find rewrite by request path at first, if no luck - try to find by id_path
      */
     if (!$this->getId() && isset($_GET['___from_store'])) {
         try {
             $fromStoreId = $this->_storeManager->getStore($_GET['___from_store'])->getId();
         } catch (\Exception $e) {
             return false;
         }
         $this->setStoreId($fromStoreId)->loadByRequestPath($requestCases);
         if (!$this->getId()) {
             return false;
         }
         $currentStore = $this->_storeManager->getStore();
         $this->setStoreId($currentStore->getId())->loadByIdPath($this->getIdPath());
         $cookieMetadata = $this->_cookieMetadataFactory->createPublicCookieMetadata()->setDurationOneYear();
         $this->_cookieManager->setPublicCookie(\Magento\Store\Model\Store::COOKIE_NAME, $currentStore->getCode(), $cookieMetadata);
         $targetUrl .= '/' . $this->getRequestPath();
         $this->_sendRedirectHeaders($targetUrl, true);
     }
     if (!$this->getId()) {
         return false;
     }
     $request->setAlias(\Magento\Framework\Url::REWRITE_REQUEST_PATH_ALIAS, $this->getRequestPath());
     $external = substr($this->getTargetPath(), 0, 6);
     $isPermanentRedirectOption = $this->hasOption('RP');
     if ($external === 'http:/' || $external === 'https:') {
         $destinationStoreCode = $this->_storeManager->getStore($this->getStoreId())->getCode();
         $cookieMetadata = $this->_cookieMetadataFactory->createPublicCookieMetadata()->setDurationOneYear();
         $this->_cookieManager->setPublicCookie(\Magento\Store\Model\Store::COOKIE_NAME, $destinationStoreCode, $cookieMetadata);
         $this->_sendRedirectHeaders($this->getTargetPath(), $isPermanentRedirectOption);
     } else {
         $targetUrl .= '/' . $this->getTargetPath();
     }
     $isRedirectOption = $this->hasOption('R');
     $isStoreInUrl = $this->_scopeConfig->getValue(\Magento\Store\Model\Store::XML_PATH_STORE_IN_URL, \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
     if ($isRedirectOption || $isPermanentRedirectOption) {
         if ($isStoreInUrl && ($storeCode = $this->_storeManager->getStore()->getCode())) {
             $targetUrl .= '/' . $storeCode . '/' . $this->getTargetPath();
         }
         $this->_sendRedirectHeaders($targetUrl, $isPermanentRedirectOption);
     }
     if ($isStoreInUrl && ($storeCode = $this->_storeManager->getStore()->getCode())) {
         $targetUrl .= '/' . $storeCode . '/' . $this->getTargetPath();
     }
     $queryString = $this->_getQueryString();
     if ($queryString) {
         $targetUrl .= '?' . $queryString;
     }
     $request->setRequestUri($targetUrl);
     $request->setPathInfo($this->getTargetPath());
     return true;
 }
Esempio n. 15
0
 /**
  * @param \Magento\Store\Model\StoreManagerInterface $storage
  * @param string $scopeType
  * @return void
  */
 protected function _checkRequestStore(\Magento\Store\Model\StoreManagerInterface $storage, $scopeType)
 {
     $storeCode = $this->request->getParam('___store');
     if (empty($storeCode)) {
         return;
     }
     if (!$this->setCurrentStore($storage, $storeCode, $scopeType)) {
         return;
     }
     if ($storage->getStore()->getCode() == $storeCode) {
         $store = $storage->getStore($storeCode);
         if ($store->getWebsite()->getDefaultStore()->getId() == $store->getId()) {
             $this->_cookieManager->deleteCookie(Store::COOKIE_NAME);
         } else {
             $publicCookieMetadata = $this->cookieMetadataFactory->createPublicCookieMetadata()->setDurationOneYear();
             $this->_cookieManager->setPublicCookie(Store::COOKIE_NAME, $storage->getStore()->getCode(), $publicCookieMetadata);
             $this->_httpContext->setValue(Store::ENTITY, $storage->getStore()->getCode(), \Magento\Store\Model\Store::DEFAULT_CODE);
         }
     }
     return;
 }
Esempio n. 16
0
 /**
  * Postpone cookie expiration time if cookie value defined
  *
  * @param int $duration Time in seconds.
  * @param string $path
  * @return $this
  */
 public function renewPersistentCookie($duration, $path)
 {
     if ($duration === null) {
         return $this;
     }
     $value = $this->_cookieManager->getCookie(self::COOKIE_NAME);
     if (null !== $value) {
         $publicCookieMetadata = $this->_cookieMetadataFactory->createPublicCookieMetadata()->setDuration($duration)->setPath($path);
         $this->_cookieManager->setPublicCookie(self::COOKIE_NAME, $value, $publicCookieMetadata);
     }
     return $this;
 }