Guest User

seo_url

a guest
Aug 5th, 2015
447
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.73 KB | None | 0 0
  1. <?php
  2. class ControllerCommonSeoUrl 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.         }
  8.  
  9.         // Decode URL
  10.         if (isset($this->request->get['_route_'])) {
  11.             $parts = explode('/', $this->request->get['_route_']);
  12.  
  13.             // remove any empty arrays from trailing
  14.             if (utf8_strlen(end($parts)) == 0) {
  15.                 array_pop($parts);
  16.             }
  17.  
  18.             foreach ($parts as $part) {
  19.                 $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE keyword = '" . $this->db->escape($part) . "'");
  20.  
  21.                 if ($query->num_rows) {
  22.                     $url = explode('=', $query->row['query']);
  23.  
  24.                     if ($url[0] == 'product_id') {
  25.                         $this->request->get['product_id'] = $url[1];
  26.                     }
  27.  
  28.                     if ($url[0] == 'category_id') {
  29.                         if (!isset($this->request->get['path'])) {
  30.                             $this->request->get['path'] = $url[1];
  31.                         } else {
  32.                             $this->request->get['path'] .= '_' . $url[1];
  33.                         }
  34.                     }
  35.  
  36.                     if ($url[0] == 'manufacturer_id') {
  37.                         $this->request->get['manufacturer_id'] = $url[1];
  38.                     }
  39.  
  40.                     if ($url[0] == 'information_id') {
  41.                         $this->request->get['information_id'] = $url[1];
  42.                     }
  43.  
  44.                     if ($query->row['query'] && $url[0] != 'information_id' && $url[0] != 'manufacturer_id' && $url[0] != 'category_id' && $url[0] != 'product_id') {
  45.                         $this->request->get['route'] = $query->row['query'];
  46.                     }
  47.                 } else {
  48.                     $this->request->get['route'] = 'error/not_found';
  49.  
  50.                     break;
  51.                 }
  52.             }
  53.  
  54.             if (!isset($this->request->get['route'])) {
  55.                 if (isset($this->request->get['product_id'])) {
  56.                     $this->request->get['route'] = 'product/product';
  57.                 } elseif (isset($this->request->get['path'])) {
  58.                     $this->request->get['route'] = 'product/category';
  59.                 } elseif (isset($this->request->get['manufacturer_id'])) {
  60.                     $this->request->get['route'] = 'product/manufacturer/info';
  61.                 } elseif (isset($this->request->get['information_id'])) {
  62.                     $this->request->get['route'] = 'information/information';
  63.                 }
  64.             }
  65.                          else {
  66.                             $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE keyword = '" . $this->db->escape($this->request->get['_route_']) . "'");
  67.                             if ($query->num_rows) {
  68.                                 $this->request->get['route'] = $query->row['query'];
  69.                             }
  70.                       }
  71.             if (isset($this->request->get['route'])) {
  72.                 return new Action($this->request->get['route']);
  73.             }
  74.         }
  75.     }
  76.  
  77.     public function rewrite($link) {
  78.         $url_info = parse_url(str_replace('&amp;', '&', $link));
  79.  
  80.         $url = '';
  81.  
  82.         $data = array();
  83.  
  84.         parse_str($url_info['query'], $data);
  85.  
  86.         foreach ($data as $key => $value) {
  87.             if (isset($data['route'])) {
  88.                 if (($data['route'] == 'product/product' && $key == 'product_id') || (($data['route'] == 'product/manufacturer/info' || $data['route'] == 'product/product') && $key == 'manufacturer_id') || ($data['route'] == 'information/information' && $key == 'information_id')) {
  89.                     $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE `query` = '" . $this->db->escape($key . '=' . (int)$value) . "'");
  90.  
  91.                     if ($query->num_rows && $query->row['keyword']) {
  92.                         $url .= '/' . $query->row['keyword'];
  93.  
  94.                         unset($data[$key]);
  95.                     }
  96.                 } elseif ($key == 'path') {
  97.                     $categories = explode('_', $value);
  98.  
  99.                     foreach ($categories as $category) {
  100.                         $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE `query` = 'category_id=" . (int)$category . "'");
  101.  
  102.                         if ($query->num_rows && $query->row['keyword']) {
  103.                             $url .= '/' . $query->row['keyword'];
  104.                         } else {
  105.                             $url = '';
  106.  
  107.                             break;
  108.                         }
  109.                     }
  110.  
  111.                     unset($data[$key]);
  112.                 }
  113.             }
  114.                         else {
  115.                         $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE `query` = '" . $this->db->escape($data['route']) . "'");
  116.  
  117.                         if ($query->num_rows) {
  118.                             $url .= '/' . $query->row['keyword'];
  119.  
  120.                             unset($data[$key]);
  121.                         }
  122.                    }
  123.         }
  124.  
  125.         if ($url) {
  126.             unset($data['route']);
  127.  
  128.             $query = '';
  129.  
  130.             if ($data) {
  131.                 foreach ($data as $key => $value) {
  132.                     $query .= '&' . rawurlencode((string)$key) . '=' . rawurlencode((string)$value);
  133.                 }
  134.  
  135.                 if ($query) {
  136.                     $query = '?' . str_replace('&', '&amp;', trim($query, '&'));
  137.                 }
  138.             }
  139.  
  140.             return $url_info['scheme'] . '://' . $url_info['host'] . (isset($url_info['port']) ? ':' . $url_info['port'] : '') . str_replace('/index.php', '', $url_info['path']) . $url . $query;
  141.         } else {
  142.             return $link;
  143.         }
  144.     }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment