コード例 #1
0
ファイル: magicredirect.php プロジェクト: acreno/pm-ps
 private function autoCatch($page_name, $id_lang, $id_shop)
 {
     $uri_var = $this->formatLink($_SERVER['REQUEST_URI']);
     $cache_enabled = (bool) Configuration::get('MGRT_URLCACHE');
     $cache_name = 'MR2_' . $id_shop . '_' . Tools::str2url($uri_var);
     $cache_file = $this->cache_folder . md5($cache_name);
     // Better checking this before
     if ($cache_enabled === true && is_writable(_PS_CACHE_DIR_) && file_exists($this->cache_folder)) {
         $time_cache = (int) Configuration::get('MGRT_CACHETIME');
         if (file_exists($cache_file) && filemtime($cache_file) > time() - $time_cache * 60) {
             // Checked, can go ahead
             return;
         } else {
             try {
                 file_put_contents($cache_file, 'checked', LOCK_EX);
             } catch (Exception $e) {
                 PrestaShopLogger::addLog('Redirect Cache folder not writable, please check your modules folder permissions.');
             }
         }
     }
     $link = new Link();
     $db = Db::getInstance();
     $force_ssl = Configuration::get('PS_SSL_ENABLED') && Configuration::get('PS_SSL_ENABLED_EVERYWHERE');
     $full_uri = 'http' . ($force_ssl === true ? 's' : '') . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
     if ($full_uri == $link->getPageLink('404')) {
         // nothing to stare
         return;
     }
     if ($page_name == 'pagenotfound') {
         // Check if similar redirect exist, tryig to avoid duplicates
         $exist = $db->getRow('SELECT * FROM ' . _DB_PREFIX_ . 'redirect r WHERE r.old = "' . pSQL($uri_var) . '"');
         if (!empty($uri_var) && empty($exist)) {
             // Let's log it
             $redirect = new Redirect();
             $redirect->old = trim($uri_var);
             $redirect->new = '/';
             $redirect->type = '301';
             $redirect->regex = false;
             $redirect->active = (bool) Configuration::get('MGRT_AUTOACTIVE');
             $redirect->add();
             // Multishop
             $db->execute('INSERT INTO `' . _DB_PREFIX_ . 'redirect_shop` (`id_redirect`, `id_shop`) VALUES (' . (int) $redirect->id . ',' . (int) $id_shop . ')');
             // Update counter
             $t = (int) Configuration::get('MGRT_COUNTER');
             Configuration::updateValue('MGRT_COUNTER', $t + 1);
         }
         if (!empty($exist)) {
             // Test exist in current shop, if not adding it
             $exist_shop = $db->getRow('SELECT * FROM ' . _DB_PREFIX_ . 'redirect_shop WHERE id_shop = ' . (int) $id_shop . ' AND id_redirect = ' . (int) $exist['id_redirect']);
             if (empty($exist_shop)) {
                 $db->execute('INSERT INTO `' . _DB_PREFIX_ . 'redirect_shop` (`id_redirect`, `id_shop`) VALUES (' . (int) $exist['id_redirect'] . ',' . (int) $id_shop . ')');
             }
         }
     } else {
         // We try to find some concordance in available pages
         // considering link rewrite and manipulated url structures
         $rewrite = Tools::isSubmit('rewrite') ? Tools::getValue('rewrite') : null;
         if (empty($rewrite)) {
             $id = Tools::getValue('id_' . $page_name) ? (int) Tools::getValue('id_' . $page_name) : null;
             switch ($page_name) {
                 case 'manufacturer':
                     if (!empty($id)) {
                         $manufacturer = new Manufacturer($id, $id_lang);
                         $rewrite = $manufacturer->link_rewrite;
                     }
                     break;
                 case 'supplier':
                     if (!empty($id)) {
                         $supplier = new Supplier($id, $id_lang);
                         $rewrite = $supplier->link_rewrite;
                     }
                     break;
                 case 'cms':
                     if (!empty($id)) {
                         $link_rewrite = CMS::getUrlRewriteInformations($id);
                     }
                     if (isset($link_rewrite[$id_lang])) {
                         $rewrite = $link_rewrite[$id_lang]['link_rewrite'];
                     }
                     break;
                 case 'product':
                     if (!empty($id)) {
                         $product = new Product($id, false, $id_lang);
                         $rewrite = $product->link_rewrite;
                     }
                     break;
                 case 'category':
                     if (!empty($id)) {
                         $category = new Category($id, $id_lang, $id_shop);
                         $rewrite = $category->link_rewrite;
                     }
                     break;
                 case 'cms_category':
                     if (!empty($id)) {
                         $rewrite = CMSCategory::getLinkRewrite($id, $id_lang);
                     }
                     break;
                 default:
                     // UFO or Module ? Hard to find an equivalent.
                     if (strpos('module', $page_name)) {
                         $rewrite = $page_name;
                     } else {
                         $rewrite = $uri_var;
                     }
                     break;
             }
         }
         if (!empty($rewrite)) {
             // Preparing string
             $rewrite = str_replace('/', '-', $rewrite);
             $a = explode('-', $rewrite);
             $b = array();
             // Filtering, 3 words search maximum, with 3 letter minimum
             foreach ($a as $v) {
                 if (Tools::strlen($v) > 2) {
                     $b[] = $v;
                 }
                 if (count($b) > 2) {
                     break;
                 }
             }
             if (!empty($b)) {
                 $search = implode('%', $b);
                 $exist = $db->executeS('SELECT r.id_redirect FROM ' . _DB_PREFIX_ . 'redirect r, ' . _DB_PREFIX_ . 'redirect_shop rs WHERE rs.id_redirect = r.id_redirect AND rs.id_shop = ' . (int) $id_shop . ' AND r.regex = 0 AND r.old LIKE "/%' . pSQL($search) . '"');
                 if (!empty($exist)) {
                     // Filling unactive empty settled redirect with new best match value
                     foreach ($exist as $value) {
                         $db->execute('UPDATE ' . _DB_PREFIX_ . 'redirect SET new = "' . pSQL($uri_var) . '" WHERE id_redirect = ' . (int) $value['id_redirect']);
                     }
                 }
             }
         }
     }
 }
コード例 #2
0
ファイル: Link.php プロジェクト: TheTypoMaster/neonflexible
 /**
  * Create a link to a CMS page
  *
  * @param mixed $cms CMS object (can be an ID CMS, but deprecated)
  * @param string $alias
  * @param bool $ssl
  * @param int $id_lang
  * @return string
  */
 public function getCMSLink($cms, $alias = null, $ssl = null, $id_lang = null, $id_shop = null)
 {
     if (!$id_lang) {
         $id_lang = Context::getContext()->language->id;
     }
     $url = $this->getBaseLink($id_shop, $ssl) . $this->getLangLink($id_lang, null, $id_shop);
     $dispatcher = Dispatcher::getInstance();
     if (!is_object($cms)) {
         $cms = new CMS($cms, $id_lang);
         if (!Validate::isLoadedObject($cms) && !$dispatcher->hasKeyword('cms_rule', $id_lang, 'meta_keywords', $id_shop) && !$dispatcher->hasKeyword('cms_rule', $id_lang, 'meta_title', $id_shop)) {
             return $url . $dispatcher->createUrl('cms_rule', $id_lang, array('id' => (int) $cms, 'rewrite' => (string) $alias), $this->allow, '', $id_shop);
         }
         $cms = new CMS($cms, $id_lang);
     }
     // Set available keywords
     $params = array();
     $params['id'] = $cms->id;
     $params['rewrite'] = !$alias ? is_array($cms->link_rewrite) ? $cms->link_rewrite[(int) $id_lang] : $cms->link_rewrite : $alias;
     $params['meta_keywords'] = '';
     if (isset($cms->meta_keywords) && !empty($cms->meta_keywords)) {
         $params['meta_keywords'] = is_array($cms->meta_keywords) ? Tools::str2url($cms->meta_keywords[(int) $id_lang]) : Tools::str2url($cms->meta_keywords);
     }
     $params['meta_title'] = '';
     if (isset($cms->meta_title) && !empty($cms->meta_title)) {
         $params['meta_title'] = is_array($cms->meta_title) ? Tools::str2url($cms->meta_title[(int) $id_lang]) : Tools::str2url($cms->meta_title);
     }
     $params['category_cms_rewrite'] = '';
     if (isset($cms->id_cms_category) && !empty($cms->id_cms_category)) {
         $params['category_cms_rewrite'] = Tools::str2url(CMSCategory::getLinkRewrite($cms->id_cms_category, $id_lang));
     }
     return $url . $dispatcher->createUrl('cms_rule', $id_lang, $params, $this->allow, '', $id_shop);
 }