Advertisement
Guest User

Untitled

a guest
Oct 26th, 2013
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.10 KB | None | 0 0
  1. <?php
  2. class ControllerCommonSeoPro extends Controller {
  3.     public function index() {
  4.         // Add rewrite to url class
  5.         if ($this->config->get('config_seo_url')) {
  6.             $this->url->addRewrite($this);
  7.         } else {
  8.             return;
  9.         }
  10.  
  11.         // Decode URL
  12.         if (!isset($this->request->get['_route_'])) {
  13.             $this->validate();
  14.         } else {
  15.             $route = $this->request->get['_route_'];
  16.             unset($this->request->get['_route_']);
  17.             $parts = explode('/', trim(utf8_strtolower($route), '/'));
  18.             list($last_part) = explode('.', array_pop($parts));
  19.             array_push($parts, $last_part);
  20.  
  21.             $keyword_in = array_map(array($this->db, 'escape'), $parts);
  22.             $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE keyword IN ('" . implode("', '", $keyword_in) . "')");
  23.  
  24.             if ($query->num_rows == sizeof($parts)) {
  25.                 $queries = array();
  26.                 foreach ($query->rows as $row) {
  27.                     $queries[utf8_strtolower($row['keyword'])] = $row['query'];
  28.                 }
  29.  
  30.                 reset($parts);
  31.                 foreach ($parts as $part) {
  32.                     $url = explode('=', $queries[$part], 2);
  33.  
  34.                     if ($url[0] == 'category_id') {
  35.                         if (!isset($this->request->get['path'])) {
  36.                             $this->request->get['path'] = $url[1];
  37.                         } else {
  38.                             $this->request->get['path'] .= '_' . $url[1];
  39.                         }
  40.                     } else {
  41.                         $this->request->get[$url[0]] = $url[1];
  42.                     }
  43.                 }
  44.             } else {
  45.                 $this->request->get['route'] = 'error/not_found';
  46.             }
  47.  
  48.             if (isset($this->request->get['product_id'])) {
  49.                 $this->request->get['route'] = 'product/product';
  50.                 if (!isset($this->request->get['path'])) {
  51.                     $path = $this->getPathByProduct($this->request->get['product_id']);
  52.                     if ($path) $this->request->get['path'] = $path;
  53.                 }
  54.             } elseif (isset($this->request->get['path'])) {
  55.                 $this->request->get['route'] = 'product/category';
  56.             } elseif (isset($this->request->get['manufacturer_id'])) {
  57.                 $this->request->get['route'] = 'product/manufacturer/info';
  58.             } elseif (isset($this->request->get['information_id'])) {
  59.                 $this->request->get['route'] = 'information/information';
  60.             }
  61.  
  62.             $this->validate();
  63.  
  64.             if (isset($this->request->get['route'])) {
  65.                 return $this->forward($this->request->get['route']);
  66.             }
  67.         }
  68.     }
  69.  
  70.     public function rewrite($link) {
  71.         if (!$this->config->get('config_seo_url')) return $link;
  72.  
  73.         $seo_url = '';
  74.  
  75.         $component = parse_url(str_replace('&amp;', '&', $link));
  76.  
  77.         $data = array();
  78.         parse_str($component['query'], $data);
  79.  
  80.         $route = $data['route'];
  81.         unset($data['route']);
  82.  
  83.         switch ($route) {
  84.             case 'product/product':
  85.                 if (isset($data['product_id'])) {
  86.                     $tmp = $data;
  87.                     $data = array();
  88.                     if ($this->config->get('config_seo_url_include_path')) {
  89.                         $data['path'] = $this->getPathByProduct($tmp['product_id']);
  90.                         if (!$data['path']) return $link;
  91.                     }
  92.                     $data['product_id'] = $tmp['product_id'];
  93.                     if (isset($tmp['tracking'])) {
  94.                         $data['tracking'] = $tmp['tracking'];
  95.                     }
  96.                 }
  97.                 break;
  98.  
  99.             case 'product/category':
  100.                 if (isset($data['path'])) {
  101.                     $category = explode('_', $data['path']);
  102.                     $category = end($category);
  103.                     $data['path'] = $this->getPathByCategory($category);
  104.                     if (!$data['path']) return $link;
  105.                 }
  106.                 break;
  107.  
  108.             case 'product/product/review':
  109.             case 'information/information/info':
  110.                 return $link;
  111.                 break;
  112.  
  113.             default:
  114.                 break;
  115.         }
  116.  
  117.         if ($component['scheme'] == 'https') {
  118.             $link = $this->config->get('config_ssl');
  119.         } else {
  120.             $link = $this->config->get('config_url');
  121.         }
  122.  
  123.         $link .= 'index.php?route=' . $route;
  124.  
  125.         if (count($data)) {
  126.             $link .= '&amp;' . urldecode(http_build_query($data, '', '&amp;'));
  127.         }
  128.  
  129.         $queries = array();
  130.         foreach ($data as $key => $value) {
  131.             switch ($key) {
  132.                 case 'product_id':
  133.                 case 'manufacturer_id':
  134.                 case 'category_id':
  135.                 case 'information_id':
  136.                     $queries[] = $key . '=' . $value;
  137.                     unset($data[$key]);
  138.                     $postfix = 1;
  139.                     break;
  140.  
  141.                 case 'path':
  142.                     $categories = explode('_', $value);
  143.                     foreach ($categories as $category) {
  144.                         $queries[] = 'category_id=' . $category;
  145.                     }
  146.                     unset($data[$key]);
  147.                     break;
  148.  
  149.                 default:
  150.                     break;
  151.             }
  152.         }
  153.  
  154.         if (!empty($queries)) {
  155.             $query_in = array_map(array($this->db, 'escape'), $queries);
  156.             $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE `query` IN ('" . implode("', '", $query_in) . "')");
  157.  
  158.             if ($query->num_rows == count($queries)) {
  159.                 $aliases = array();
  160.                 foreach ($query->rows as $row) {
  161.                     $aliases[$row['query']] = $row['keyword'];
  162.                 }
  163.                 foreach ($queries as $query) {
  164.                     $seo_url .= '/' . rawurlencode($aliases[$query]);
  165.                 }
  166.             }
  167.         }
  168.  
  169.         if ($seo_url == '') return $link;
  170.  
  171.         $seo_url = trim($seo_url, '/');
  172.  
  173.         if ($component['scheme'] == 'https') {
  174.             $seo_url = $this->config->get('config_ssl') . $seo_url;
  175.         } else {
  176.             $seo_url = $this->config->get('config_url') . $seo_url;
  177.         }
  178.  
  179.         if (isset($postfix)) {
  180.             $seo_url .= trim($this->config->get('config_seo_url_postfix'));
  181.         } else {
  182.             $seo_url .= '/';
  183.         }
  184.  
  185.         if (count($data)) {
  186.             $seo_url .= '?' . urldecode(http_build_query($data, '', '&amp;'));
  187.         }
  188.  
  189.         return $seo_url;
  190.     }
  191.  
  192.     private function getPathByProduct($product_id) {
  193.         $product_id = (int)$product_id;
  194.         if ($product_id < 1) return false;
  195.  
  196.         static $path = null;
  197.         if (!is_array($path)) {
  198.             $path = $this->cache->get('product.seopath');
  199.             if (!is_array($path)) $path = array();
  200.         }
  201.  
  202.         if (!isset($path[$product_id])) {
  203.             $query = $this->db->query("SELECT category_id FROM " . DB_PREFIX . "product_to_category WHERE product_id = '" . $product_id . "' ORDER BY main_category DESC LIMIT 1");
  204.  
  205.             $path[$product_id] = $this->getPathByCategory($query->num_rows ? (int)$query->row['category_id'] : 0);
  206.  
  207.             $this->cache->set('product.seopath', $path);
  208.         }
  209.  
  210.         return $path[$product_id];
  211.     }
  212.  
  213.     private function getPathByCategory($category_id) {
  214.         $category_id = (int)$category_id;
  215.         if ($category_id < 1) return false;
  216.  
  217.         static $path = null;
  218.         if (!is_array($path)) {
  219.             $path = $this->cache->get('category.seopath');
  220.             if (!is_array($path)) $path = array();
  221.         }
  222.  
  223.         if (!isset($path[$category_id])) {
  224.             $max_level = 10;
  225.  
  226.             $sql = "SELECT CONCAT_WS('_'";
  227.             for ($i = $max_level-1; $i >= 0; --$i) {
  228.                 $sql .= ",t$i.category_id";
  229.             }
  230.             $sql .= ") AS path FROM " . DB_PREFIX . "category t0";
  231.             for ($i = 1; $i < $max_level; ++$i) {
  232.                 $sql .= " LEFT JOIN " . DB_PREFIX . "category t$i ON (t$i.category_id = t" . ($i-1) . ".parent_id)";
  233.             }
  234.             $sql .= " WHERE t0.category_id = '" . $category_id . "'";
  235.  
  236.             $query = $this->db->query($sql);
  237.  
  238.             $path[$category_id] = $query->num_rows ? $query->row['path'] : false;
  239.  
  240.             $this->cache->set('category.seopath', $path);
  241.         }
  242.  
  243.         return $path[$category_id];
  244.     }
  245.  
  246.     private function validate() {
  247.         if (empty($this->request->get['route']) || $this->request->get['route'] == 'error/not_found') {
  248.             return;
  249.         }
  250.  
  251.         if (isset($this->request->server['HTTP_X_REQUESTED_WITH']) && strtolower($this->request->server['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
  252.             return;
  253.         }
  254.  
  255.         if (isset($this->request->server['HTTPS']) && (($this->request->server['HTTPS'] == 'on') || ($this->request->server['HTTPS'] == '1'))) {
  256.             $url = str_replace('&amp;', '&', $this->config->get('config_ssl') . ltrim($this->request->server['REQUEST_URI'], '/'));
  257.             $seo = str_replace('&amp;', '&', $this->url->link($this->request->get['route'], $this->getQueryString(array('route')), 'SSL'));
  258.         } else {
  259.             $url = str_replace('&amp;', '&', $this->config->get('config_url') . ltrim($this->request->server['REQUEST_URI'], '/'));
  260.             $seo = str_replace('&amp;', '&', $this->url->link($this->request->get['route'], $this->getQueryString(array('route')), 'NONSSL'));
  261.         }
  262.  
  263.         if (rawurldecode($url) != rawurldecode($seo)) {
  264.             header($this->request->server['SERVER_PROTOCOL'] . ' 301 Moved Permanently');
  265.  
  266.             $this->response->redirect($seo);
  267.         }
  268.     }
  269.  
  270.     private function getQueryString($exclude = array()) {
  271.         if (!is_array($exclude)) {
  272.             $exclude = array();
  273.         }
  274.  
  275.         return urldecode(http_build_query(array_diff_key($this->request->get, array_flip($exclude))));
  276.     }
  277. }
  278. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement