This repository has been archived by the owner on Apr 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 82
/
seo.php
244 lines (180 loc) · 7.76 KB
/
seo.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
<?php
/**
* @package Arastta eCommerce
* @copyright Copyright (C) 2015 Arastta Association. All rights reserved. (arastta.org)
* @credits See CREDITS.txt for credits and other copyright notices.
* @license GNU General Public License version 3; see LICENSE.txt
*/
class Seo extends Object {
protected $db;
protected $config;
protected $request;
protected $session;
protected $language;
public function __construct($registry = '') {
if (empty($registry)) {
return;
}
$this->db = $registry->get('db');
$this->config = $registry->get('config');
$this->request = $registry->get('request');
$this->session = $registry->get('session');
$this->language = $registry->get('language');
}
public function getAlias($id, $type = 'product') {
$id = intval($id);
if (empty($id)) {
return '';
}
static $rows = array('product' => array(), 'category' => array(), 'manufacturer' => array(), 'information' => array());
if (!isset($rows[$type][$id])) {
$query = $type.'_id='.$id;
$results = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE query = '" . $this->db->escape($query) . "'");
if ($results->num_rows) {
$tmp = '';
$found = '';
// Get the language id
$lang_id = $this->getLanguageId();
foreach ($results->rows as $result) {
if (!$result['keyword']) {
continue;
}
$tmp = $result['keyword'];
if ($result['language_id'] != $lang_id) {
continue;
}
$found = $result['keyword'];
break;
}
$alias = !empty($found) ? $found : $tmp;
}
else {
$alias = $type.'-'.$id;
}
$rows[$type][$id] = $alias;
}
return $rows[$type][$id];
}
public function getAliasQuery($keyword) {
$query = false;
$results = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE keyword = '" . $this->db->escape($keyword) . "'");
if ($results->num_rows) {
$tmp = '';
$found = '';
// Get the language id
$lang_id = $this->getLanguageId();
foreach ($results->rows as $result) {
if (!$result['query']) {
continue;
}
$tmp = $result['query'];
if ($result['language_id'] != $lang_id) {
continue;
}
$found = $result['query'];
break;
}
$query = !empty($found) ? $found : $tmp;
}
elseif (strpos($keyword, '-')) {
$tmp = explode('-', $keyword);
$routes = array('product', 'category', 'manufacturer', 'information');
if (!empty($tmp[0]) && in_array($tmp[0], $routes) && !empty($tmp[1]) && is_numeric($tmp[1])) {
$query = $tmp[0].'_id='.$tmp[1];
}
}
return $query;
}
public function getLanguageId() {
static $data = array();
if ($this->config->get('config_seo_translate')) {
$code = $this->session->data['language'];
}
else {
$code = $this->config->get('config_language');
}
if (!isset($data[$code])) {
$sql = $this->db->query("SELECT language_id FROM `" . DB_PREFIX . "language` WHERE status = '1' AND code = '" . $this->db->escape($code) . "' LIMIT 1");
if ($sql->num_rows) {
$data[$code] = $sql->row['language_id'];
}
else {
$data[$code] = 1;
}
}
return $data[$code];
}
public function generateAlias($title, $id = null, $language_id = null) {
$title = html_entity_decode($title, ENT_QUOTES, 'UTF-8');
$alias = $this->safeAlias($title);
if ($id && $language_id) {
$count = 0;
$baseAlias = $alias;
while ($this->db->query("SELECT COUNT(*) AS count FROM " . DB_PREFIX . "url_alias WHERE keyword = '" . $this->db->escape($alias) . "' AND language_id = '" . (int)$language_id . "'")->row['count'] > 0) {
if ($count == 0) {
$baseAlias = ($alias = $alias . '-' . $id) . '-';
} else {
$alias = $baseAlias . $count;
}
$count++;
}
}
return $alias;
}
// From Joomla.Platform
public function safeAlias($string) {
// Replace double byte whitespaces by single byte (East Asian languages)
$str = preg_replace('/\xE3\x80\x80/', ' ', $string);
// Remove any '-' from the string as they will be used as concatenator.
// Would be great to let the spaces in but only Firefox is friendly with this
$str = str_replace('-', ' ', $str);
// Replace forbidden characters by whitespaces
$str = preg_replace('#[:\#\*"@+=;!><&\.%()\]\/\'\\\\|\[]#', "\x20", $str);
// Delete all '?'
$str = str_replace('?', '', $str);
// Trim white spaces at beginning and end of alias and make lowercase
$str = trim(utf8_strtolower($str));
// Remove any duplicate whitespace and replace whitespaces by hyphens
$str = preg_replace('#\x20+#', '-', $str);
return $str;
}
public function getCategoryIdBySortOrder($product_id) {
static $data = array();
if (!isset($data[$product_id])) {
$data[$product_id] = null;
$sql = "SELECT pc.category_id FROM " . DB_PREFIX . "product_to_category AS pc";
$sql .= " LEFT JOIN " . DB_PREFIX . "category AS c ON (pc.category_id = c.category_id)";
$sql .= " LEFT JOIN " . DB_PREFIX . "category_to_store c2s ON (c.category_id = c2s.category_id)";
$sql .= " WHERE pc.product_id = '" . (int)$product_id . "'";
$sql .= " AND c2s.store_id = '" . (int)$this->config->get('config_store_id') . "'";
$sql .= " AND c.status = '1'";
$sql .= " ORDER BY c.sort_order";
$sql .= " LIMIT 1";
$query = $this->db->query($sql);
if (!$query->num_rows) {
return $data[$product_id];
}
$data[$product_id] = $query->row['category_id'];
}
return $data[$product_id];
}
public function getParentCategoriesIds($category_id = 0) {
static $data = array();
if (!isset($data[$category_id])) {
$data[$category_id] = array();
$sql = "SELECT c.parent_id FROM " . DB_PREFIX . "category c";
$sql .= " LEFT JOIN " . DB_PREFIX . "category_to_store c2s ON (c.category_id = c2s.category_id)";
$sql .= " WHERE c.category_id = '" . (int)$category_id . "'";
$sql .= " AND c2s.store_id = '" . (int)$this->config->get('config_store_id') . "'";
$sql .= " ORDER BY c.sort_order";
$sql .= " LIMIT 1";
$query = $this->db->query($sql);
if (!$query->num_rows) {
return $data[$category_id];
}
$data[$category_id][] = $query->row['parent_id'];
$data[$category_id] = array_merge($this->getParentCategoriesIds($query->row['parent_id']), $data[$category_id]);
}
return $data[$category_id];
}
}