seo_url.php (4322B)
1 <?php 2 class ControllerStartupSeoUrl 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 . "seo_url WHERE keyword = '" . $this->db->escape($part) . "' AND store_id = '" . (int)$this->config->get('config_store_id') . "'"); 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 } 66 } 67 68 public function rewrite($link) { 69 $url_info = parse_url(str_replace('&', '&', $link)); 70 71 $url = ''; 72 73 $data = array(); 74 75 parse_str($url_info['query'], $data); 76 77 foreach ($data as $key => $value) { 78 if (isset($data['route'])) { 79 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')) { 80 $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "seo_url WHERE `query` = '" . $this->db->escape($key . '=' . (int)$value) . "' AND store_id = '" . (int)$this->config->get('config_store_id') . "' AND language_id = '" . (int)$this->config->get('config_language_id') . "'"); 81 82 if ($query->num_rows && $query->row['keyword']) { 83 $url .= '/' . $query->row['keyword']; 84 85 unset($data[$key]); 86 } 87 } elseif ($key == 'path') { 88 $categories = explode('_', $value); 89 90 foreach ($categories as $category) { 91 $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "seo_url WHERE `query` = 'category_id=" . (int)$category . "' AND store_id = '" . (int)$this->config->get('config_store_id') . "' AND language_id = '" . (int)$this->config->get('config_language_id') . "'"); 92 93 if ($query->num_rows && $query->row['keyword']) { 94 $url .= '/' . $query->row['keyword']; 95 } else { 96 $url = ''; 97 98 break; 99 } 100 } 101 102 unset($data[$key]); 103 } 104 } 105 } 106 107 if ($url) { 108 unset($data['route']); 109 110 $query = ''; 111 112 if ($data) { 113 foreach ($data as $key => $value) { 114 $query .= '&' . rawurlencode((string)$key) . '=' . rawurlencode((is_array($value) ? http_build_query($value) : (string)$value)); 115 } 116 117 if ($query) { 118 $query = '?' . str_replace('&', '&', trim($query, '&')); 119 } 120 } 121 122 return $url_info['scheme'] . '://' . $url_info['host'] . (isset($url_info['port']) ? ':' . $url_info['port'] : '') . str_replace('/index.php', '', $url_info['path']) . $url . $query; 123 } else { 124 return $link; 125 } 126 } 127 }