diff --git a/system/url/seo_url.php b/system/url/seo_url.php index 5eb7f93..3121a96 100644 --- a/system/url/seo_url.php +++ b/system/url/seo_url.php @@ -15,206 +15,210 @@ use Phacil\Framework\Request; class SystemUrlSeoUrl extends ControllerController { - /** - * - * @var string - */ - private $notfound = 'error/not_found'; - - /** - * - * @var string[] - */ - private $regType = array( - "%d" => '(\\d{1,})', - "%w" => '(\\w{1,})', - "%a" => '([[:ascii:]]{1,})', - "%" => '(.*)'); - - /** - * @param Registry $registry - * @return void - */ - public function __construct(\Phacil\Framework\Registry $registry) - { - parent::__construct($registry); - - $this->notfound = Config::NOT_FOUND() ?: $this->notfound; - - } - - /** - * @return Action|void - * @todo - */ - public function index() { - // Add rewrite to url class - if ($this->config->get('config_seo_url')) { - $this->url->addRewrite($this); - } + /** + * + * @var string + */ + private $notfound = 'error/not_found'; + + /** + * + * @var string[] + */ + private $regType = array( + "%d" => '(\\d{1,})', + "%w" => '(\\w{1,})', + "%a" => '([[:ascii:]]{1,})', + "%" => '(.*)'); + + /** + * @param Registry $registry + * @return void + */ + public function __construct(\Phacil\Framework\Registry $registry) + { + parent::__construct($registry); + + $this->notfound = Config::NOT_FOUND() ?: $this->notfound; + + } + + /** + * @return Action|void + * @todo + */ + public function index() { + // Add rewrite to url class + if ($this->config->get('config_seo_url')) { + $this->url->addRewrite($this); + } - $match = array(); + $match = array(); - // Decode URL - if (Request::GET('_route_')) { - $parts_count = explode('/', Request::GET('_route_')); - $parts = array(Request::GET('_route_')); - - foreach ($parts as $part) { - if(Config::USE_DB_CONFIG()){ - $query = $this->db->query()->select()->setTable("url_alias"); - $query->where()->eq('keyword', $part); - $query = $query->load(); - - if (isset($query) && $query != false && $query->getNumRows() === 1) { - $row = $query->getRow(); - - if(!empty($row->getValue('get'))) { - $a = explode(',', $row->getValue('get')); - - foreach($a as $value) { - $b = explode('=', $value); - $_GET[$b[0]] = $b[1]; - Request::GET($b[0], $b[1]); - } - } - - Request::GET('route', $row->getValue('query')); - } - } elseif (Config::ROUTES() && is_array(Config::ROUTES())) { - $rotas = Config::ROUTES(); - - if(isset($rotas[$part])){ - Request::GET('route', $rotas[$part]); - } else { - - foreach($rotas as $key => $page) { + // Decode URL + if (Request::GET('_route_')) { + $parts_count = explode('/', Request::GET('_route_')); + $parts = array(Request::GET('_route_')); + + foreach ($parts as $part) { + if(Config::USE_DB_CONFIG()){ + $query = $this->db->query()->select()->setTable("url_alias"); + $query->where()->eq('keyword', $part); + $query = $query->load(); + } + + if (isset($query) && $query != false && $query->getNumRows() === 1) { + $row = $query->getRow(); + + if(!empty($row->getValue('get'))) { + $a = explode(',', $row->getValue('get')); + + foreach($a as $value) { + $b = explode('=', $value); + $_GET[$b[0]] = $b[1]; + Request::GET($b[0], $b[1]); + } + } + + Request::GET('route', $row->getValue('query')); + } elseif (Config::ROUTES() && is_array(Config::ROUTES())) { + $rotas = Config::ROUTES(); + + if(isset($rotas[$part])){ + Request::GET('route', $rotas[$part]); + } else { + + foreach($rotas as $key => $page) { + + if(isRegularExpression($key)) { + + $preg = preg_quote($key, "/"); + + $preg = str_replace(array_keys($this->regType), array_values($this->regType), $preg); + + if((@preg_match("/". $preg . "/", $parts[0], $match))) { + + $countTree = explode("/", $match[0]); + + if(count($countTree) == count($parts_count)){ - if(isRegularExpression($key)) { + unset($match[0]); + + $match = $this->request->clean($match); - $preg = preg_quote($key, "/"); + $pagina = $page; + + break; + } + + } + } + } + + if(isset($pagina)) { + Request::GET('route', $pagina); + } else { + Request::GET('route', $this->notfound); + } + } + + } else { + Request::GET('route', $this->notfound); + } + } + + + if (Request::GET('route')) { + return $this->forward(Request::GET('route'), $match); + } + } + } - $preg = str_replace(array_keys($this->regType), array_values($this->regType), $preg); + /** + * @param string $link + * @return string + * @todo + */ + public function rewrite($link) { + if ($this->config->get('config_seo_url')) { + $url_data = parse_url(str_replace('&', '&', $link)); - if((@preg_match("/". $preg . "/", $parts[0], $match))) { + $url = ''; - $countTree = explode("/", $match[0]); + $data = array(); - if(count($countTree) == count($parts_count)){ + parse_str($url_data['query'], $data); - unset($match[0]); + $joinRegex = implode("|", array_keys($this->regType)); - $match = $this->request->clean($match); + foreach ($data as $key => $value) { - $pagina = $page; + if (isset($data['route'])) { + if(Config::USE_DB_CONFIG()){ + $query = $this->db->query()->select()->from("url_alias"); + $query->where()->eq('query', $value)->end(); + $query = $query->load(); + } + + if (isset($query) && $query->getNumRows() > 0) { + $url .= '/' . $query->getRow()->getValue('keyword'); + } elseif (Config::ROUTES() && is_array(Config::ROUTES())) { + //$arV = array_search($value, ROUTES); + foreach(Config::ROUTES() as $query => $page) { + if($page == $value){ + if(isRegularExpression($query)){ + unset($data['route']); + $qnt = substr_count($query, '%'); + if(count($data) == $qnt) { + $str = $query; - break; - } - - } - } - } - - if(isset($pagina)) { - Request::GET('route', $pagina); - } else { - Request::GET('route', $this->notfound); - } - } - - } else { - Request::GET('route', $this->notfound); - } - } + foreach($data as $replace){ + $str = preg_replace('/('.$joinRegex.')/', $replace, $str, 1); + } + $url .= '/' .($str); - if (Request::GET('route')) { - return $this->forward(Request::GET('route'), $match); - } - } - } + $data = false; - /** - * @param string $link - * @return string - * @todo - */ - public function rewrite($link) { - if ($this->config->get('config_seo_url')) { - $url_data = parse_url(str_replace('&', '&', $link)); + break; + } + } else { + $url .= '/' . (array_search($value, Config::ROUTES())); + } - $url = ''; + } - $data = array(); + } - parse_str($url_data['query'], $data); + } + unset($data[$key]); + } - $joinRegex = implode("|", array_keys($this->regType)); + } - foreach ($data as $key => $value) { + if ($url) { + unset($data['route']); - if (isset($data['route'])) { - if(Config::USE_DB_CONFIG()) - $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 (Config::ROUTES() && is_array(Config::ROUTES())) { - //$arV = array_search($value, ROUTES); - foreach(Config::ROUTES() as $query => $page) { - if($page == $value){ - if(isRegularExpression($query)){ - unset($data['route']); - $qnt = substr_count($query, '%'); - if(count($data) == $qnt) { - $str = $query; + $query = ''; - foreach($data as $replace){ - $str = preg_replace('/('.$joinRegex.')/', $replace, $str, 1); - } - - $url .= '/' .($str); - - $data = false; - - break; - } - } else { - $url .= '/' . (array_search($value, Config::ROUTES())); - } - - } - - } - - } - 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 ($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; + } + } } /** @@ -222,6 +226,6 @@ class SystemUrlSeoUrl extends ControllerController { * @return bool */ function isRegularExpression($string) { - return (strpos($string, '%') !== false); + return (strpos($string, '%') !== false); }