-
Notifications
You must be signed in to change notification settings - Fork 0
/
url_rewriter.php
417 lines (290 loc) · 10.1 KB
/
url_rewriter.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
<?php
/*
Модуль "ЧПУ для ShopCMS"
2009-2011 (c) http://trickywebs.org.ua/
Техническая поддержка - soulmare@gmail.com
Лицензия - MIT http://www.opensource.org/licenses/mit-license.php
*/
//error_reporting(E_ALL);
//ini_set('display_errors', 1);
// Корневые разделы ЧПУ ссылок
define('FU_CATALOG_ROOT', 'catalog');
define('FU_NEWS_ROOT', 'news');
define('FU_PAGES_ROOT', 'pages');
define('FU_ROOT_DIR', dirname(__FILE__));
define('FU_OPTION_ID', 0);
// Access items by human readable URLs
if(isset($_GET['uri'])) {
$uri = $_GET['uri'];
$uriFor = isset($_GET['uriFor']) && $_GET['uriFor'] ? $_GET['uriFor'] : 'product';
// Try to load item by URI
$uri = mysql_real_escape_string($uri);
switch($uriFor) {
// Product
case 'product':
$sql = "SELECT productID
FROM ".PRODUCTS_TABLE."
WHERE uri = '$uri'
LIMIT 1";
if($row = query_first($sql)) {
$_GET['productID'] = $row[0];
} else // 404 not found
$_GET['categoryID'] = -1;
break;
// Category
case 'category':
// Offset
if(preg_match('|(.+)_offset_(\d+)$|', $uri, $matches)) {
$uri = $matches[1];
$offset = $matches[2];
} elseif(preg_match('|(.+)_show_all.html$|', $uri, $matches)) {
$uri = $matches[1];
$offset = 1;
$showAll = 'yes';
} else
$offset = 1;
$sql = "SELECT categoryID
FROM ".CATEGORIES_TABLE."
WHERE uri = '$uri'
LIMIT 1";
if($row = query_first($sql)) {
$_GET['categoryID'] = $row[0];
if(isset($showAll))
$_GET['show_all'] = $showAll;
else {
$_GET['offset'] = $offset;
}
} else // 404 not found
$_GET['categoryID'] = -1;
break;
// News
case 'news':
$sql = "SELECT NID
FROM ".NEWS_TABLE."
WHERE uri = '$uri'
LIMIT 1";
if($row = query_first($sql)) {
$_GET['fullnews'] = $row[0];
} else // 404 not found
$_GET['categoryID'] = -1;
break;
// Pages
case 'pages':
$sql = "SELECT aux_page_ID
FROM ".AUX_PAGES_TABLE."
WHERE uri = '$uri'
LIMIT 1";
if($row = query_first($sql)) {
$_GET['show_aux_page'] = $row[0];
} else // 404 not found
$_GET['categoryID'] = -1;
break;
}
//check_www_prefix();
// Launch engine
} else { // No URI
// Access by ID must be redirected to URI if present
if((isset($_GET['productID']) || isset($_GET['categoryID']) || isset($_GET['fullnews']) || isset($_GET['show_aux_page'])) && !isset($_GET['search']) && !isset($_GET['discuss']) && !isset($_GET['advanced_search_in_category']) && !isset($_GET['sort']) && !isset($_GET['sent']) && !isset($_GET['search_with_change_category_ability']) && ($_SERVER['REQUEST_METHOD'] == 'GET')) {
if(isset($_GET['productID'])) { // product
$itemId = (int) $_GET['productID'];
$sql = "SELECT p.uri, p.uri_opt_val, c.uri AS 'categorySlug', p.categoryID
FROM ".PRODUCTS_TABLE." p
LEFT JOIN ".CATEGORIES_TABLE." c
ON p.categoryID = c.categoryID
WHERE productID = '$itemId'
LIMIT 1";
} elseif(isset($_GET['categoryID'])) { // category
$itemId = (int) $_GET['categoryID'];
$sql = "SELECT uri
FROM ".CATEGORIES_TABLE."
WHERE categoryID = '$itemId'
LIMIT 1";
} elseif(isset($_GET['fullnews'])) { // news
$itemId = (int) $_GET['fullnews'];
$sql = "SELECT uri
FROM ".NEWS_TABLE."
WHERE NID = '$itemId'
LIMIT 1";
} elseif(isset($_GET['show_aux_page'])) { // pages
$itemId = (int) $_GET['show_aux_page'];
$sql = "SELECT uri
FROM ".AUX_PAGES_TABLE."
WHERE aux_page_ID = '$itemId'
LIMIT 1";
}
// Get URI by ID
if($queryResult = mysql_query($sql)) {
if($row = mysql_fetch_row($queryResult)) {
// Item has valid URI, redirect to it
if($itemURI = $row[0]) {
// Load engine settings
if(!defined('CONF_FU_DEMO_MODE'))
define('CONF_FU_DEMO_MODE', 1);
// Generate new URL
$newUrl = 'http://'.CONF_SHOP_URL.'/';
if(isset($_GET['productID'])) { // Product
$newUrl .= FU_CATALOG_ROOT;
$categorySlug = $row[2] ? $row[2] : 'category_'.$row[3];
$newUrl .= '/'.$categorySlug.'/';
$newUrl .= FU_OPTION_ID ? fu_get_option_slug($_GET['productID']).'/'.urlencode($itemURI) : urlencode($itemURI);
} elseif(isset($_GET['categoryID'])) { // Category
$newUrl .= FU_CATALOG_ROOT;
$newUrl .= '/'.urlencode($itemURI);
} elseif(isset($_GET['fullnews'])) { // News
$newUrl .= FU_NEWS_ROOT;
$newUrl .= '/'.urlencode($itemURI);
} elseif(isset($_GET['show_aux_page'])) { // Pages
$newUrl .= FU_PAGES_ROOT;
$newUrl .= '/'.urlencode($itemURI);
}
header('Content-Type: text/html; charset=utf-8');
header ('HTTP/1.1 301 Moved Permanently');
header ('Location: '.$newUrl);
?>
<html>
<head>
<title>301 Moved Permanently<title>
</head>
<body>
<h1>301 Moved Permanently</h1>
<h2>Страница переместилась</h2>
<p>Если браузер не проследовал автоматически, пожалуйста перейдите на новый адрес этой страницы <a href="<?php echo $newUrl?>"><?php echo $newUrl?></a></p>
<p><small>Страница сгенирирована модулем "ЧПУ УРЛ для ShopCMS". Разработка модуля <a href="http://trickywebs.org.ua/">trickywebs.org.ua</a></small></p>
<body>
</html>
<?php
die();
}
}
}
}
//check_www_prefix();
// Launch engine
}
function fu_make_url($obj) {
global $fc;
// Check if it's a correct object
if(isset($obj['categoryID'])) {
if(isset($obj['productID'])) { // Product
if($obj['uri']) { // Rewrite URL
// Get category slug
if(isset($fc[$obj['categoryID']]) && $fc[$obj['categoryID']]['uri'])
$pCatSlug = $fc[$obj['categoryID']]['uri'];
else
$pCatSlug = 'category_'.$obj['categoryID'];
// Get option slug
if(-1 == $obj['uri_opt_val']) { // undefined - define option slug
// Get slug and update slug field
$pOptSlug = fu_get_option_slug($obj['productID']);
} else // Slug already defined, just use it
$pOptSlug = $obj['uri_opt_val'];
$url = FU_OPTION_ID ? FU_CATALOG_ROOT.'/'.$pCatSlug.'/'.$pOptSlug.'/'.$obj['uri'] : FU_CATALOG_ROOT.'/'.$pCatSlug.'/'.$obj['uri'];
} else { // Common URL
$url = "product_{$obj['productID']}.html";
}
} else { // Category
if($obj['uri']) { // Rewrite URL
$url = FU_CATALOG_ROOT.'/'.$obj['uri'];
} else { // Common URL
$url = "category_{$obj['categoryID']}.html";
}
}
} else
$url = '';
return $url;
}
function fu_make_url_news($obj) {
// Check object
if(isset($obj['NID'])) {
if($obj['uri']) { // Rewrite URL
$url = FU_NEWS_ROOT.'/'.$obj['uri'];
} else { // Common URL
$url = "show_news_{$obj['NID']}.html";
}
} else
$url = '';
return $url;
}
function fu_make_url_pages($pageId) {
$pagesUriTable = get_pages_uri_table();
// Check object
if(isset($pagesUriTable[$pageId]) && $pagesUriTable[$pageId]) {
$url = FU_PAGES_ROOT . '/' . $pagesUriTable[$pageId];
} else
$url = "page_$pageId.html";
return $url;
}
// Get associated array of pages' URIs
function get_pages_uri_table() {
global $pagesUriTable;
if(isset($pagesUriTable) && $pagesUriTable) return $pagesUriTable;
else $pagesUriTable = Array();
$sql = 'SELECT aux_page_ID, uri
FROM ' . AUX_PAGES_TABLE;
$result = db_query($sql);
while($row = db_fetch_assoc($result))
$pagesUriTable[$row['aux_page_ID']] = $row['uri'];
return $pagesUriTable;
}
// Get slug and update slug field
function fu_get_option_slug($productID) {
// Get option value
$productID = (int) $productID;
$sql = "SELECT povvt.option_value AS optionValue
FROM ".PRODUCTS_OPTIONS_SET_TABLE." AS post
LEFT JOIN ".PRODUCTS_OPTIONS_VALUES_VARIANTS_TABLE." AS povvt
ON povvt.variantID=post.variantID
WHERE povvt.optionID=".FU_OPTION_ID." AND post.productID='$productID'
UNION
SELECT povt.option_value AS optionValue
FROM ".PRODUCT_OPTIONS_VALUES_TABLE." AS povt
LEFT JOIN ".PRODUCT_OPTIONS_TABLE." AS pot
ON pot.optionID = povt.optionID
WHERE povt.productID = '$productID'
LIMIT 1";
$q = db_query($sql);
if($row = db_fetch_assoc($q)) {
$optionValue = urlencode(strtolower(htmlspecialchars_decode(fu_translit($row['optionValue']))));
} else
$optionValue = '';
if(!$optionValue) $optionValue = 'unknown';
// Update slug field
$optionValueSql = mysql_real_escape_string($optionValue);
$sql = "UPDATE ".PRODUCTS_TABLE."
SET uri_opt_val = '$optionValueSql'
WHERE productID = '$productID'
LIMIT 1";
db_query($sql);
return $optionValue;
}
// from http://www.softtime.ru/scripts/translit.php
function fu_translit($st) {
// Сначала заменяем "односимвольные" фонемы.
$st=strtr($st,"абвгдеёзийклмнопрстуфхъыэ_",
"abvgdeeziyklmnoprstufh'iei");
$st=strtr($st,"АБВГДЕЁЗИЙКЛМНОПРСТУФХЪЫЭ_",
"ABVGDEEZIYKLMNOPRSTUFH'IEI");
// Затем - "многосимвольные".
$st=strtr($st,
array(
"ж"=>"zh", "ц"=>"ts", "ч"=>"ch", "ш"=>"sh",
"щ"=>"shch","ь"=>"", "ю"=>"yu", "я"=>"ya",
"Ж"=>"ZH", "Ц"=>"TS", "Ч"=>"CH", "Ш"=>"SH",
"Щ"=>"SHCH","Ь"=>"", "Ю"=>"YU", "Я"=>"YA",
"ї"=>"i", "Ї"=>"Yi", "є"=>"ie", "Є"=>"Ye"
)
);
// Возвращаем результат.
return $st;
}
function query_first($sql) {
if($queryResult = mysql_query($sql))
if($row = mysql_fetch_row($queryResult))
return $row;
}
function query_first_assoc($sql) {
if($queryResult = db_query($sql))
if($row = db_fetch_assoc($queryResult))
return $row;
}
?>