diff --git a/controllers/front/ProductController.php b/controllers/front/ProductController.php index 2a983da43..1da5a589a 100644 --- a/controllers/front/ProductController.php +++ b/controllers/front/ProductController.php @@ -136,25 +136,31 @@ class ProductControllerCore extends FrontController else { // Load category - if (isset($_SERVER['HTTP_REFERER']) - && strstr($_SERVER['HTTP_REFERER'], Tools::getHttpHost()) // Assure us the previous page was one of the shop - && (stripos($_SERVER['HTTP_REFERER'], '.html') === false) - && preg_match('~^(.*)\/([0-9]+)\-((?![.]+html).*)|(.*)id_category=([0-9]+)(.*)$~', $_SERVER['HTTP_REFERER'], $regs)) + $id_category = false; + if (isset($_SERVER['HTTP_REFERER']) && $_SERVER['HTTP_REFERER'] == Tools::secureReferrer($_SERVER['HTTP_REFERER']) // Assure us the previous page was one of the shop + && preg_match('~^.*(?product->id, array('0' => array('id_category' => (int)$regs[2])))) - $this->category = new Category($regs[2], (int)$this->context->cookie->id_lang); - } - else if (isset($regs[5]) && is_numeric($regs[5])) - { - if (Product::idIsOnCategoryId((int)$this->product->id, array('0' => array('id_category' => (int)$regs[5])))) - $this->category = new Category($regs[5], (int)$this->context->cookie->id_lang); + $referers = array($_SERVER['HTTP_REFERER'],urldecode($_SERVER['HTTP_REFERER'])); + if (in_array($this->context->link->getCategoryLink($id_object), $referers)) + $id_category = (int)$id_object; + elseif (isset($this->context->cookie->last_visited_category) && (int)$this->context->cookie->last_visited_category && in_array($this->context->link->getProductLink($id_object), $referers)) + $id_category = (int)$this->context->cookie->last_visited_category; } + } - if (!isset($this->category)) - $this->category = new Category($this->product->id_category_default, (int)$this->context->cookie->id_lang); + if (!$id_category || !Category::inShopStatic($id_category, $this->context->shop) || !Product::idIsOnCategoryId((int)$this->product->id, array('0' => array('id_category' => $id_category)))) + $id_category = (int)$this->product->id_category_default; + $this->category = new Category((int)$id_category, (int)$this->context->cookie->id_lang); + if (isset($this->context->cookie) && isset($this->category->id_category) && !(Module::isInstalled('blockcategories') && Module::isEnabled('blockcategories'))) + $this->context->cookie->last_visited_category = (int)$this->category->id_category; } } }