/** * Set Cookie for msg box when it displays first * * @param \Magento\Framework\App\FrontController $subject * @param \Magento\Framework\App\ResponseInterface $response * * @return \Magento\Framework\App\ResponseInterface * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function afterDispatch(\Magento\Framework\App\FrontController $subject, \Magento\Framework\App\ResponseInterface $response) { if ($this->request->isPost() && $this->messageManager->hasMessages()) { $this->cookie->set(self::COOKIE_NAME, 1, self::COOKIE_PERIOD, '/'); } return $response; }
/** * Send Vary coookie * @return void */ public function sendVary() { $data = $this->context->getData(); if (!empty($data)) { ksort($data); $vary = sha1(serialize($data)); $this->cookie->set(self::COOKIE_VARY_STRING, $vary, null, '/'); } else { $this->cookie->set(self::COOKIE_VARY_STRING, null, -1, '/'); } }
/** * @param Observer $observer * @return void */ public function synchronizePersistentOnLogin(Observer $observer) { /** @var $customer \Magento\Customer\Service\V1\Data\Customer */ $customer = $observer->getEvent()->getCustomer(); // Check if customer is valid (remove persistent cookie for invalid customer) if (!$customer || !$customer->getId() || !$this->_persistentSession->isRememberMeChecked()) { $this->_sessionFactory->create()->removePersistentCookie(); return; } $persistentLifeTime = $this->_persistentData->getLifeTime(); // Delete persistent session, if persistent could not be applied if ($this->_persistentData->isEnabled() && $persistentLifeTime <= 0) { // Remove current customer persistent session $this->_sessionFactory->create()->deleteByCustomerId($customer->getId()); return; } /** @var $sessionModel \Magento\Persistent\Model\Session */ $sessionModel = $this->_persistentSession->getSession(); // Check if session is wrong or not exists, so create new session if (!$sessionModel->getId() || $sessionModel->getCustomerId() != $customer->getId()) { /** @var \Magento\Persistent\Model\Session $sessionModel */ $sessionModel = $this->_sessionFactory->create(); $sessionModel->setLoadExpired()->loadByCustomerId($customer->getId()); if (!$sessionModel->getId()) { /** @var \Magento\Persistent\Model\Session $sessionModel */ $sessionModel = $this->_sessionFactory->create(); $sessionModel->setCustomerId($customer->getId())->save(); } $this->_persistentSession->setSession($sessionModel); } // Set new cookie if ($sessionModel->getId()) { $this->_cookie->set(\Magento\Persistent\Model\Session::COOKIE_NAME, $sessionModel->getKey(), $persistentLifeTime, $this->_customerSession->getCookiePath()); } }
/** * Try to load valid order by $_POST or $_COOKIE * * @param App\RequestInterface $request * @param App\ResponseInterface $response * @return bool */ 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(); if (empty($post) && !$this->_coreCookie->get(self::COOKIE_NAME)) { $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); $this->_coreCookie->set(self::COOKIE_NAME, $toCookie, self::COOKIE_LIFETIME, self::COOKIE_PATH); } } elseif ($this->_coreCookie->get(self::COOKIE_NAME)) { $fromCookie = $this->_coreCookie->get(self::COOKIE_NAME); $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) { $this->_coreCookie->renew(self::COOKIE_NAME, self::COOKIE_LIFETIME, self::COOKIE_PATH); $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; }
/** * Set session UpdatedAt to current time and update cookie expiration time * * @return void */ public function prolong() { $lifetime = $this->_config->getValue(self::XML_PATH_SESSION_LIFETIME); $currentTime = time(); $this->setUpdatedAt($currentTime); $cookieValue = $this->_cookie->get($this->getName()); if ($cookieValue) { $this->_cookie->set($this->getName(), $cookieValue, $lifetime, $this->sessionConfig->getCookiePath(), $this->sessionConfig->getCookieDomain(), $this->sessionConfig->getCookieSecure(), $this->sessionConfig->getCookieHttpOnly()); } }
/** * @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->_cookie->set(Store::COOKIE_NAME, null); } else { $this->_cookie->set(Store::COOKIE_NAME, $storage->getStore()->getCode(), true); $this->_httpContext->setValue(Store::ENTITY, $storage->getStore()->getCode(), \Magento\Store\Model\Store::DEFAULT_CODE); } } return; }
/** * 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()) { $this->cookie->set(self::COOKIE_NAME, $this->generateValue(), self::COOKIE_PERIOD, '/'); } }
/** * Set current store currency code * * @param string $code * @return string */ public function setCurrentCurrencyCode($code) { $code = strtoupper($code); if (in_array($code, $this->getAvailableCurrencyCodes())) { $this->_getSession()->setCurrencyCode($code); $path = $this->_getSession()->getCookiePath(); if ($code == $this->getDefaultCurrency()->getCurrencyCode()) { $this->_cookie->set(self::COOKIE_CURRENCY, null, null, $path); } else { $this->_cookie->set(self::COOKIE_CURRENCY, $code, null, $path); } $this->_httpContext->setValue(\Magento\Core\Helper\Data::CONTEXT_CURRENCY, $code, $this->_storeManager->getWebsite()->getDefaultStore()->getDefaultCurrency()->getCode()); } return $this; }
/** * Remove persistent cookie * * @return $this */ public function removePersistentCookie() { $this->_cookie->set(self::COOKIE_NAME, null, null, $this->sessionConfig->getCookiePath()); return $this; }
/** * 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); /** * 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()); $this->_cookie->set(\Magento\Store\Model\Store::COOKIE_NAME, $currentStore->getCode(), true); $targetUrl = $request->getBaseUrl() . '/' . $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(); $this->_cookie->set(\Magento\Store\Model\Store::COOKIE_NAME, $destinationStoreCode, true); $this->_sendRedirectHeaders($this->getTargetPath(), $isPermanentRedirectOption); } else { $targetUrl = $request->getBaseUrl() . '/' . $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 = $request->getBaseUrl() . '/' . $storeCode . '/' . $this->getTargetPath(); } $this->_sendRedirectHeaders($targetUrl, $isPermanentRedirectOption); } if ($isStoreInUrl && ($storeCode = $this->_storeManager->getStore()->getCode())) { $targetUrl = $request->getBaseUrl() . '/' . $storeCode . '/' . $this->getTargetPath(); } $queryString = $this->_getQueryString(); if ($queryString) { $targetUrl .= '?' . $queryString; } $request->setRequestUri($targetUrl); $request->setPathInfo($this->getTargetPath()); return true; }