1: <?php
2: namespace Opencart\Catalog\Controller\Startup;
3: /**
4: * Class SeoUrl
5: *
6: * @package Opencart\Catalog\Controller\Startup
7: */
8: class SeoUrl extends \Opencart\System\Engine\Controller {
9: /**
10: * @return null
11: */
12: public function index() {
13: // Add rewrite to URL class
14: if ($this->config->get('config_seo_url')) {
15: $this->url->addRewrite($this);
16:
17: $this->load->model('design/seo_url');
18:
19: // Decode URL
20: if (isset($this->request->get['_route_'])) {
21: $parts = explode('/', $this->request->get['_route_']);
22:
23: // remove any empty arrays from trailing
24: if (oc_strlen(end($parts)) == 0) {
25: array_pop($parts);
26: }
27:
28: foreach ($parts as $key => $value) {
29: $seo_url_info = $this->model_design_seo_url->getSeoUrlByKeyword($value);
30:
31: if ($seo_url_info) {
32: $this->request->get[$seo_url_info['key']] = html_entity_decode($seo_url_info['value'], ENT_QUOTES, 'UTF-8');
33:
34: unset($parts[$key]);
35: }
36: }
37:
38: if (!isset($this->request->get['route'])) {
39: $this->request->get['route'] = $this->config->get('action_default');
40: }
41:
42: if ($parts) {
43: $this->request->get['route'] = $this->config->get('action_error');
44: }
45: }
46: }
47:
48: return null;
49: }
50:
51: /**
52: * Rewrite
53: *
54: * @param string $link
55: *
56: * @return string
57: */
58: public function rewrite(string $link): string {
59: $url_info = parse_url(str_replace('&amp;', '&', $link));
60:
61: // Build the url
62: $url = '';
63:
64: if ($url_info['scheme']) {
65: $url .= $url_info['scheme'];
66: }
67:
68: $url .= '://';
69:
70: if ($url_info['host']) {
71: $url .= $url_info['host'];
72: }
73:
74: if (isset($url_info['port'])) {
75: $url .= ':' . $url_info['port'];
76: }
77:
78: parse_str($url_info['query'], $query);
79:
80: // Start changing the URL query into a path
81: $paths = [];
82:
83: // Parse the query into its separate parts
84: $parts = explode('&', $url_info['query']);
85:
86: foreach ($parts as $part) {
87: $pair = explode('=', $part);
88:
89: if (isset($pair[0])) {
90: $key = (string)$pair[0];
91: }
92:
93: if (isset($pair[1])) {
94: $value = (string)$pair[1];
95: } else {
96: $value = '';
97: }
98:
99: $result = $this->model_design_seo_url->getSeoUrlByKeyValue((string)$key, (string)$value);
100:
101: if ($result) {
102: $paths[] = $result;
103:
104: unset($query[$key]);
105: }
106: }
107:
108: $sort_order = [];
109:
110: foreach ($paths as $key => $value) {
111: $sort_order[$key] = $value['sort_order'];
112: }
113:
114: array_multisort($sort_order, SORT_ASC, $paths);
115:
116: // Build the path
117: $url .= str_replace('/index.php', '', $url_info['path']);
118:
119: foreach ($paths as $result) {
120: $url .= '/' . $result['keyword'];
121: }
122:
123: // Rebuild the URL query
124: if ($query) {
125: $url .= '?' . str_replace(['%2F'], ['/'], http_build_query($query));
126: }
127:
128: return $url;
129: }
130: }
131: