/** * 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); } }
/** * Set Cookie for msg box when it displays first * * @param FrontController $subject * @param ResponseInterface $response * * @return ResponseInterface * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function afterDispatch(FrontController $subject, ResponseInterface $response) { if ($this->request->isPost() && $this->messageManager->hasMessages()) { $publicCookieMetadata = $this->cookieMetadataFactory->createPublicCookieMetadata()->setDuration(self::COOKIE_PERIOD)->setPath('/'); $this->cookieManager->setPublicCookie(self::COOKIE_NAME, 1, $publicCookieMetadata); } return $response; }
/** * 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; }
/** * 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); } }
/** * @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; }
/** * 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; }
/** * Set guest-view cookie * * @param string $cookieValue * @return void */ private function setGuestViewCookie($cookieValue) { $metadata = $this->cookieMetadataFactory->createPublicCookieMetadata()->setPath(self::COOKIE_PATH)->setDuration(self::COOKIE_LIFETIME)->setHttpOnly(true); $this->cookieManager->setPublicCookie(self::COOKIE_NAME, $cookieValue, $metadata); }
/** * 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; }
/** * 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); }
/** * Return count of sent in last period by cookie * * @param bool $increment - flag, increase count before return value * @return int */ protected function _sentCountByCookies($increment = false) { $cookieName = $this->_sendfriendData->getCookieName(); $time = time(); $newTimes = array(); if (isset($this->_lastCookieValue[$cookieName])) { $oldTimes = $this->_lastCookieValue[$cookieName]; } else { $oldTimes = $this->cookieManager->getCookie($cookieName); } if ($oldTimes) { $oldTimes = explode(',', $oldTimes); foreach ($oldTimes as $oldTime) { $periodTime = $time - $this->_sendfriendData->getPeriod(); if (is_numeric($oldTime) and $oldTime >= $periodTime) { $newTimes[] = $oldTime; } } } if ($increment) { $newTimes[] = $time; $newValue = implode(',', $newTimes); $this->cookieManager->setPublicCookie($cookieName, $newValue); $this->_lastCookieValue[$cookieName] = $newValue; } return count($newTimes); }
/** * 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; }