/**
     * Initialize product controller
     * @see FrontController::init()
     */
    public function init()
    {
        if (Tools::getValue('product_rewrite')) {
            $rewrite_url = Tools::getValue('product_rewrite');
            $id_product = Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue('
				SELECT `id_product`
				FROM `' . _DB_PREFIX_ . 'product_lang`
				WHERE `link_rewrite` = \'' . $rewrite_url . '\'');
            if ($id_product > 0) {
                $_GET['id_product'] = $id_product;
                $_GET['noredirect'] = 1;
            } else {
                $redirect_url = str_replace('.html', '', $_SERVER['REQUEST_URI']);
                $redirect_type = Configuration::get('PS_CANONICAL_REDIRECT') == 2 ? '301' : '302';
                header('HTTP/1.0 ' . $redirect_type . ' Moved');
                header('Cache-Control: no-cache');
                Tools::redirectLink('http://' . $_SERVER['HTTP_HOST'] . $redirect_url);
                Tools::display404Error();
                die;
            }
        }
        parent::init();
        if ($id_product = (int) Tools::getValue('id_product')) {
            $this->product = new Product($id_product, true, $this->context->language->id, $this->context->shop->id);
        }
        if (!Validate::isLoadedObject($this->product)) {
            header('HTTP/1.1 404 Not Found');
            header('Status: 404 Not Found');
            $this->errors[] = Tools::displayError('Product not found');
        } else {
            $this->canonicalRedirection();
            /*
             * If the product is associated to the shop
             * and is active or not active but preview mode (need token + file_exists)
             * allow showing the product
             * In all the others cases => 404 "Product is no longer available"
             */
            if (!$this->product->isAssociatedToShop() || !$this->product->active) {
                if (Tools::getValue('adtoken') == Tools::getAdminToken('AdminProducts' . (int) Tab::getIdFromClassName('AdminProducts') . (int) Tools::getValue('id_employee')) && $this->product->isAssociatedToShop()) {
                    // If the product is not active, it's the admin preview mode
                    $this->context->smarty->assign('adminActionDisplay', true);
                } else {
                    $this->context->smarty->assign('adminActionDisplay', false);
                    if ($this->product->id_product_redirected == $this->product->id) {
                        $this->product->redirect_type = '404';
                    }
                    switch ($this->product->redirect_type) {
                        case '301':
                            header('HTTP/1.1 301 Moved Permanently');
                            header('Location: ' . $this->context->link->getProductLink($this->product->id_product_redirected));
                            break;
                        case '302':
                            header('HTTP/1.1 302 Moved Temporarily');
                            header('Cache-Control: no-cache');
                            header('Location: ' . $this->context->link->getProductLink($this->product->id_product_redirected));
                            break;
                        case '404':
                        default:
                            header('HTTP/1.1 404 Not Found');
                            header('Status: 404 Not Found');
                            $this->errors[] = Tools::displayError('This product is no longer available.');
                            break;
                    }
                }
            } elseif (!$this->product->checkAccess(isset($this->context->customer) ? $this->context->customer->id : 0)) {
                header('HTTP/1.1 403 Forbidden');
                header('Status: 403 Forbidden');
                $this->errors[] = Tools::displayError('You do not have access to this product.');
            } else {
                // Load category
                $id_category = false;
                if (isset($_SERVER['HTTP_REFERER']) && $_SERVER['HTTP_REFERER'] == Tools::secureReferrer($_SERVER['HTTP_REFERER']) && preg_match('~^.*(?<!\\/content)\\/([0-9]+)\\-(.*[^\\.])|(.*)id_(category|product)=([0-9]+)(.*)$~', $_SERVER['HTTP_REFERER'], $regs)) {
                    // If the previous page was a category and is a parent category of the product use this category as parent category
                    $id_object = false;
                    if (isset($regs[1]) && is_numeric($regs[1])) {
                        $id_object = (int) $regs[1];
                    } elseif (isset($regs[5]) && is_numeric($regs[5])) {
                        $id_object = (int) $regs[5];
                    }
                    if ($id_object) {
                        $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 (!$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;
                }
                if (Category::inDecor($id_category) && !$this->ajax && strpos($_SERVER['SERVER_NAME'], 'decor') <= -1) {
                    $final_url = $this->context->link->getProductLink($this->product);
                    $redirect_type = Configuration::get('PS_CANONICAL_REDIRECT') == 2 ? '301' : '302';
                    header('HTTP/1.0 ' . $redirect_type . ' Moved');
                    header('Cache-Control: no-cache');
                    Tools::redirectLink($final_url);
                }
                $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;
                }
            }
        }
    }
 /**
  * Initialize category controller
  * @see FrontController::init()
  */
 public function init()
 {
     if (Tools::getValue('category_rewrite')) {
         $category_rewrite = Tools::getValue('category_rewrite');
         $count = (int) Db::getInstance()->getValue('
                             SELECT COUNT(*)
                             FROM `' . _DB_PREFIX_ . 'category_lang` cl
                             LEFT JOIN `' . _DB_PREFIX_ . 'category` c ON c.`id_category` = cl.`id_category`
                             WHERE cl.`link_rewrite` = \'' . $category_rewrite . '\'
                             AND cl.`id_shop`=1
                             AND c.`refers_to_decor` = ' . (int) preg_match('/decor/i', $_SERVER['SERVER_NAME']));
         if ($count == 1) {
             $this->id_category = Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue('
                             SELECT c.`id_category`
                             FROM `' . _DB_PREFIX_ . 'category_lang` cl
                             LEFT JOIN `' . _DB_PREFIX_ . 'category` c ON c.`id_category` = cl.`id_category`
                             WHERE cl.`link_rewrite` = \'' . $category_rewrite . '\'
                             AND cl.`id_shop`=1
                             AND c.`refers_to_decor` = ' . (int) preg_match('/decor/i', $_SERVER['SERVER_NAME']));
         } else {
             $parents = Tools::getValue('categories_rewrite') . '/' . Tools::getValue('category_rewrite');
             $this->getIdCategory(explode('/', $parents), null);
         }
         if ($this->id_category > 0) {
             $_GET['id_category'] = $this->id_category;
             $_GET['noredirect'] = 1;
         } else {
             header('HTTP/1.1 404 Not Found');
             header('Status: 404 Not Found');
         }
     } else {
         if ($_SERVER['SERVER_NAME'] == 'decor.' . Configuration::get('PS_SHOP_DOMAIN') && !isset($_GET['id_category'])) {
             $_GET['id_category'] = Configuration::get('PS_DECOR_CATEGORY');
             $_GET['noredirect'] = 1;
         }
     }
     if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') {
         $this->ajax = true;
     }
     // Get category ID
     $id_category = (int) Tools::getValue('id_category');
     //print_r($id_category);
     if (!$id_category || !Validate::isUnsignedId($id_category)) {
         $this->errors[] = Tools::displayError('Missing category ID');
     }
     // Instantiate category
     $this->category = new Category($id_category, $this->context->language->id);
     if ($this->category->is_all) {
         $this->is_all = true;
     }
     if (Category::inDecor($id_category) && !$this->ajax && strpos($_SERVER['SERVER_NAME'], 'decor') <= -1) {
         $final_url = $this->context->link->getCategoryLink($this->category);
         $redirect_type = Configuration::get('PS_CANONICAL_REDIRECT') == 2 ? '301' : '302';
         header('HTTP/1.0 ' . $redirect_type . ' Moved');
         header('Cache-Control: no-cache');
         Tools::redirectLink($final_url);
     }
     parent::init();
     //check if the category is active and return 404 error if is disable.
     if (!$this->category->active) {
         header('HTTP/1.1 404 Not Found');
         header('Status: 404 Not Found');
     }
     //check if category can be accessible by current customer and return 403 if not
     if (!$this->category->checkAccess($this->context->customer->id)) {
         header('HTTP/1.1 403 Forbidden');
         header('Status: 403 Forbidden');
         $this->errors[] = Tools::displayError('You do not have access to this category.');
         $this->customer_access = false;
     }
     if ($this->category->is_special) {
         $this->is_special = true;
     }
 }
Example #3
0
 public function getCategoryLink1($category, $alias = null, $id_lang = null, $selected_filters = null, $id_shop = null)
 {
     if (!$id_lang) {
         $id_lang = Context::getContext()->language->id;
     }
     $url = $this->getBaseLink($id_shop) . $this->getLangLink($id_lang, null, $id_shop);
     if (!is_object($category)) {
         $category = new Category($category, $id_lang);
         if (Category::inDecor($category->id)) {
             $url = $this->getBaseLink($id_shop, null, true) . $this->getLangLink($id_lang, null, $id_shop);
             if ($category->id == Configuration::get('PS_DECOR_CATEGORY')) {
                 return $url;
             }
         }
     }
     // Set available keywords
     $params = array();
     //		$params['id'] = $category->id;
     $params['rewrite'] = !$alias ? $category->link_rewrite : $alias;
     $params['meta_keywords'] = Tools::str2url($category->getFieldByLang('meta_keywords'));
     $params['meta_title'] = Tools::str2url($category->getFieldByLang('meta_title'));
     // Selected filters is used by the module blocklayered
     $selected_filters = is_null($selected_filters) ? '' : $selected_filters;
     if (empty($selected_filters)) {
         $rule = 'category_rule';
     } else {
         $rule = 'layered_rule';
         $params['selected_filters'] = $selected_filters;
     }
     return $url . Dispatcher::getInstance()->createUrl($rule, $id_lang, $params, $this->allow, '', $id_shop);
 }