diff --git a/system/url/seo_url.php b/system/url/seo_url.php index 4b0da69..5c6febc 100644 --- a/system/url/seo_url.php +++ b/system/url/seo_url.php @@ -17,41 +17,41 @@ class SystemUrlSeoUrl extends Controller { } public function index() { - // Add rewrite to url class - if ($this->config->get('config_seo_url')) { - $this->url->addRewrite($this); - } + // Add rewrite to url class + if ($this->config->get('config_seo_url')) { + $this->url->addRewrite($this); + } + + $match = array(); - $match = array(); - - // Decode URL - if (isset($this->request->get['_route_'])) { - //$parts = explode('/', $this->request->get['_route_']); - $parts = array($this->request->get['_route_']); + // Decode URL + if (isset($this->request->get['_route_'])) { + $parts_count = explode('/', $this->request->get['_route_']); + $parts = array($this->request->get['_route_']); - foreach ($parts as $part) { - if(defined('USE_DB_CONFIG') && USE_DB_CONFIG == true) - $query = $this->db->query("SELECT * FROM url_alias WHERE keyword = '" . $this->db->escape($part) . "'"); + foreach ($parts as $part) { + if(defined('USE_DB_CONFIG') && USE_DB_CONFIG == true) + $query = $this->db->query("SELECT * FROM url_alias WHERE keyword = '" . $this->db->escape($part) . "'"); - if (isset($query) && $query != false && $query->num_rows === 1) { + if (isset($query) && $query != false && $query->num_rows === 1) { - //$url = explode('=', $query->row['query']); + //$url = explode('=', $query->row['query']); - if($query->row['get'] != "") { - $a = explode(',', $query->row['get']); + if($query->row['get'] != "") { + $a = explode(',', $query->row['get']); + + foreach($a as $value) { + $b = explode('=', $value); + $_GET[$b[0]] = $b[1]; + } + } - foreach($a as $value) { - $b = explode('=', $value); - $_GET[$b[0]] = $b[1]; - } - } - //var_dump($query->row['query']); - $this->request->get['route'] = $query->row['query']; + $this->request->get['route'] = $query->row['query']; - } elseif (defined('ROUTES') && is_array(ROUTES)) { - $rotas = ROUTES; + } elseif (defined('ROUTES') && is_array(ROUTES)) { + $rotas = ROUTES; - if(isset($rotas[$part])){ + if(isset($rotas[$part])){ $this->request->get['route'] = $rotas[$part]; } else { @@ -64,11 +64,20 @@ class SystemUrlSeoUrl extends Controller { $preg = str_replace(array_keys($this->regType), array_values($this->regType), $preg); if((@preg_match("/". $preg . "/", $parts[0], $match))) { - unset($match[0]); - $match = $this->request->clean($match); + $countTree = explode("/", $match[0]); + + if(count($countTree) == count($parts_count)){ + + unset($match[0]); + + $match = $this->request->clean($match); + + $pagina = $page; + + break; + } - $pagina = $page; } } } @@ -81,87 +90,93 @@ class SystemUrlSeoUrl extends Controller { } } else { - $this->request->get['route'] = $this->notfound; - } - } - - - if (isset($this->request->get['route'])) { - return $this->forward($this->request->get['route'], $match); - } - } - } - - public function rewrite($link) { - if ($this->config->get('config_seo_url')) { - $url_data = parse_url(str_replace('&', '&', $link)); - - $url = ''; - - $data = array(); - - parse_str($url_data['query'], $data); - - $joinRegex = implode("|", array_keys($this->regType)); - - foreach ($data as $key => $value) { - - if (isset($data['route'])) { - if(defined('USE_DB_CONFIG') && USE_DB_CONFIG == true) - $query = $this->db->query("SELECT * FROM url_alias WHERE `query` = '" . $this->db->escape($value) . "'"); - if (isset($query) && $query->num_rows && $query->num_rows != NULL) { - $url .= '/' . $query->row['keyword']; - } elseif (defined('ROUTES') && is_array(ROUTES)) { - $arV = array_search($value, ROUTES); - if($arV){ + $this->request->get['route'] = $this->notfound; + } + } - if(isRegularExpression($arV)){ //check is a valid wildcard - unset($data['route']); - $str = $arV; + if (isset($this->request->get['route'])) { + return $this->forward($this->request->get['route'], $match); + } + } + } - foreach($data as $replace){ - $str = preg_replace('/('.$joinRegex.')/', $replace, $str, 1); - } + public function rewrite($link) { + if ($this->config->get('config_seo_url')) { + $url_data = parse_url(str_replace('&', '&', $link)); + + $url = ''; - $url .= '/' .($str); + $data = array(); - $data = false; + parse_str($url_data['query'], $data); + + $joinRegex = implode("|", array_keys($this->regType)); + + foreach ($data as $key => $value) { + + if (isset($data['route'])) { + if(defined('USE_DB_CONFIG') && USE_DB_CONFIG == true) + $query = $this->db->query("SELECT * FROM url_alias WHERE `query` = '" . $this->db->escape($value) . "'"); + if (isset($query) && $query->num_rows && $query->num_rows != NULL) { + $url .= '/' . $query->row['keyword']; + } elseif (defined('ROUTES') && is_array(ROUTES)) { + //$arV = array_search($value, ROUTES); + foreach(ROUTES as $query => $page) { + if($page == $value){ + if(isRegularExpression($query)){ + unset($data['route']); + $qnt = substr_count($query, '%'); + if(count($data) == $qnt) { + $str = $query; + + foreach($data as $replace){ + $str = preg_replace('/('.$joinRegex.')/', $replace, $str, 1); + } + + $url .= '/' .($str); + + $data = false; + + break; + } + } else { + $url .= '/' . (array_search($value, ROUTES)); + } - } else { - $url .= '/' . (array_search($value, ROUTES)); } } + + } + unset($data[$key]); + } + + } + + if ($url) { + unset($data['route']); + + $query = ''; + + if ($data) { + foreach ($data as $key => $value) { + $query .= '&' . $key . '=' . $value; } - unset($data[$key]); - } - - } - - if ($url) { - unset($data['route']); - - $query = ''; - - if ($data) { - foreach ($data as $key => $value) { - $query .= '&' . $key . '=' . $value; - } - - if ($query) { - $query = '?' . trim($query, '&'); - } - } - - return $url_data['scheme'] . '://' . $url_data['host'] . (isset($url_data['port']) ? ':' . $url_data['port'] : '') . str_replace('/index.php', '', $url_data['path']) . $url . $query; - } else { - return $link; - } - } else { - return $link; - } - } + + if ($query) { + $query = '?' . trim($query, '&'); + } + } + + return $url_data['scheme'] . '://' . $url_data['host'] . (isset($url_data['port']) ? ':' . $url_data['port'] : '') . str_replace('/index.php', '', $url_data['path']) . $url . $query; + } else { + return $link; + } + } else { + return $link; + } + } } function isRegularExpression($string) {