forked from XoopsModules25x/adslight
-
Notifications
You must be signed in to change notification settings - Fork 1
/
seo_url.php
345 lines (323 loc) · 10.9 KB
/
seo_url.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
<?php declare(strict_types=1);
/*
* You may not change or alter any portion of this comment or credits
* of supporting developers from this source code or any supporting source code
* which is considered copyrighted (c) material of the original comment or credit authors.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
/**
* @copyright XOOPS Project (https://xoops.org)
* @license GNU GPL 2.0 or later (https://www.gnu.org/licenses/gpl-2.0.html)
* @author XOOPS Development Team
* @author Pascal Le Boustouller: original author ([email protected])
* @author Luc Bizet (www.frxoops.org)
* @author jlm69 (www.jlmzone.com)
* @author mamba (www.xoops.org)
*/
/////////////////////////////////////
// AdsLight UrlRewrite By Nikita //
// https://www.aideordi.com //
/////////////////////////////////////
define('REAL_MODULE_NAME', 'adslight');
define('SEO_MODULE_NAME', 'annonces');
ob_start('seo_urls');
/**
* @param $s
*
* @return string|string[]|null
*/
function seo_urls($s)
{
$XPS_URL = str_replace('/', '\/', quotemeta(XOOPS_URL));
$s = adslight_absolutize($s); // Fix URLs and HTML.
$module_name = REAL_MODULE_NAME;
$search = [
// Search URLs of modules' directry.
'/<(a|meta)([^>]*)(href|url)=([\'\"]{0,1})' . $XPS_URL . '\/modules\/' . $module_name . '\/(viewcats.php)([^>\'\"]*)([\'\"]{1})([^>]*)>/i',
'/<(a|meta)([^>]*)(href|url)=([\'\"]{0,1})' . $XPS_URL . '\/modules\/' . $module_name . '\/(viewads.php)([^>\'\"]*)([\'\"]{1})([^>]*)>/i',
'/<(a|meta)([^>]*)(href|url)=([\'\"]{0,1})' . $XPS_URL . '\/modules\/' . $module_name . '\/(index.php)([^>\'\"]*)([\'\"]{1})([^>]*)>/i',
// '/<(a|meta)([^>]*)(href|url)=([\'\"]{0,1})'.$XPS_URL.'\/modules\/'.$module_name.'\/()([^>\'\"]*)([\'\"]{1})([^>]*)>/i',
];
return preg_replace_callback($search, 'replaceLinks', $s);
}
/**
* @param $matches
* @return string
*/
function replaceLinks($matches): string
{
$req_string = [];
$add_to_url = '';
switch ($matches[5]) {
case 'viewcats.php':
// $add_to_url = '';
$req_string = $matches[6];
if (!empty($matches[6])) {
// replacing cid=x
if (preg_match('/cid=(\d+)/', $matches[6], $mvars)) {
$add_to_url = 'c' . $mvars[1] . '/' . adslight_seo_cat($mvars[1]) . '.html';
$req_string = preg_replace('/cid=\d+/', '', $matches[6]);
} else {
return $matches['0'];
}
}
break;
case 'viewads.php':
// $add_to_url = '';
$req_string = $matches[6];
if (!empty($matches[6])) {
// replacing lid=x
if (preg_match('/lid=(\d+)/', $matches[6], $mvars)) {
$add_to_url = 'p' . $mvars[1] . '/' . adslight_seo_titre($mvars[1]) . '.html';
$req_string = preg_replace('/lid=\d+/', '', $matches[6]);
} else {
return $matches['0'];
}
}
break;
default:
break;
}
if ('?' === $req_string) {
$req_string = '';
}
return '<' . $matches[1] . $matches[2] . $matches[3] . '=' . $matches[4] . XOOPS_URL . '/' . SEO_MODULE_NAME . '/' . $add_to_url . $req_string . $matches[7] . $matches[8] . '>';
}
/**
* @param $cid
*
* @return string|array<string>|null
*/
function adslight_seo_cat($cid)
{
/** @var \XoopsMySQLDatabase $xoopsDB */
$xoopsDB = \XoopsDatabaseFactory::getDatabaseConnection();
$sql = ' SELECT title FROM ' . $xoopsDB->prefix('adslight_categories') . ' WHERE cid = ' . $cid . ' ';
$result = $xoopsDB->query($sql);
if (!$xoopsDB->isResultSet($result)) {
\trigger_error("Query Failed! SQL: $sql- Error: " . $xoopsDB->error(), E_USER_ERROR);
}
$res = $xoopsDB->fetchArray($result);
return adslight_seo_title($res['title']);
}
/**
* @param $lid
*
* @return string|array<string>|null
*/
function adslight_seo_titre($lid)
{
/** @var \XoopsMySQLDatabase $xoopsDB */
$xoopsDB = \XoopsDatabaseFactory::getDatabaseConnection();
$sql = ' SELECT title FROM ' . $xoopsDB->prefix('adslight_listing') . ' WHERE lid = ' . $lid . ' ';
$result = $xoopsDB->query($sql);
if (!$xoopsDB->isResultSet($result)) {
\trigger_error("Query Failed! SQL: $sql- Error: " . $xoopsDB->error(), E_USER_ERROR);
}
$res = $xoopsDB->fetchArray($result);
return adslight_seo_title($res['title']);
}
/**
* @param string $title
* @param bool $withExt
*
* @return string|array<string>|null
*/
function adslight_seo_title($title = '', $withExt = false)
{
/**
* if XOOPS ML is present, let's sanitize the title with the current language
*/
$myts = \MyTextSanitizer::getInstance();
if (method_exists($myts, 'formatForML')) {
$title = $myts->formatForML($title);
}
// Transformation de la chaine en minuscule
// String encoding to avoid 500 errors in case of unforeseen characters
$title = rawurlencode(mb_strtolower($title));
// Transformation des ponctuations
// Tab Space ! " # % & ' ( ) , / : ; < = > ? @ [ \ ] ^ { | } ~ . +
$pattern = [
'/%09/', // Tab
'/%20/', // Space
'/%21/', // !
'/%22/', // "
'/%23/', // #
'/%25/', // %
'/%26/', // &
'/%27/', // '
'/%28/', // (
'/%29/', // )
'/%2C/', // ,
'/%2F/', // /
'/%3A/', // :
'/%3B/', // ;
'/%3C/', // <
'/%3D/', // =
'/%3E/', // >
'/%3F/', // ?
'/%40/', // @
'/%5B/', // [
'/%5C/', // \
'/%5D/', // ]
'/%5E/', // ^
'/%7B/', // {
'/%7C/', // |
'/%7D/', // }
'/%7E/', // ~
'/\./', // .
'/%2A/',
'/%2B/',
'/quot/',
];
$rep_pat = [
'-',
'-',
'',
'',
'',
'-100',
'',
'-',
'',
'',
'',
'-',
'',
'',
'',
'-',
'',
'',
'-at-',
'',
'-',
'',
'-',
'',
'-',
'',
'-',
'',
'',
'+',
'',
];
$title = preg_replace($pattern, $rep_pat, $title);
// Transformation of characters with accents
// ° è é ê ë ç à â ä î ï ù ü û ô ö
$pattern = [
'/%B0/', // °
'/%E8/', // è
'/%E9/', // é
'/%EA/', // ê
'/%EB/', // ë
'/%E7/', // ç
'/%E0/', // à
'/%E2/', // â
'/%E4/', // ä
'/%EE/', // î
'/%EF/', // ï
'/%F9/', // ù
'/%FC/', // ü
'/%FB/', // û
'/%F4/', // ô
'/%F6/', // ö
'/%E3%A8/',
'/%E3%A9/',
'/%E3%A0/',
'/%E3%AA/',
'/%E3%A2/',
'/a%80%9C/',
'/a%80%9D/',
'/%E3%A7/',
];
$rep_pat = [
'-',
'e',
'e',
'e',
'e',
'c',
'a',
'a',
'a',
'i',
'i',
'u',
'u',
'u',
'o',
'o',
'e',
'e',
'a',
'e',
'a',
'-',
'-',
'c',
];
$title = preg_replace($pattern, $rep_pat, $title);
if (count($title) > 0) {
if ($withExt) {
$title .= '.html';
}
return $title;
}
return '';
}
/**
* @param $s
*
* @return string|string[]|null
*/
function adslight_absolutize($s)
{
if (preg_match('/\/$/', $_SERVER['REQUEST_URI'])) {
$req_dir = preg_replace('/\/$/', '', $_SERVER['REQUEST_URI']);
$req_php = '';
} else {
$req_dir = dirname($_SERVER['REQUEST_URI']);
$req_php = preg_replace('/.*(\/[a-zA-Z0-9_\-]+)\.php.*/', '\\1.php', $_SERVER['REQUEST_URI']);
}
$req_dir = '\\' === $req_dir || '/' === $req_dir ? '' : $req_dir;
$dir_arr = explode('/', $req_dir);
$m = count($dir_arr) - 1;
$d1 = @str_replace('/' . $dir_arr[$m], '', $req_dir);
$d2 = @str_replace('/' . $dir_arr[$m - 1], '', $d1);
$d3 = @str_replace('/' . $dir_arr[$m - 2], '', $d2);
$d4 = @str_replace('/' . $dir_arr[$m - 3], '', $d3);
$d5 = @str_replace('/' . $dir_arr[$m - 4], '', $d4);
$host = 'http://' . $_SERVER['HTTP_HOST'];
$in = [
'/<([^>\?\&]*)(href|src|action|background|window\.location)=([^\"\' >]+)([^>]*)>/i',
'/<([^>\?\&]*)(href|src|action|background|window\.location)=([\"\']{1})\.\.\/\.\.\/\.\.\/([^\"\']*)([\"\']{1})([^>]*)>/i',
'/<([^>\?\&]*)(href|src|action|background|window\.location)=([\"\']{1})\.\.\/\.\.\/([^\"\']*)([\"\']{1})([^>]*)>/i',
'/<([^>\?\&]*)(href|src|action|background|window\.location)=([\"\']{1})\.\.\/([^\"\']*)([\"\']{1})([^>]*)>/i',
'/<([^>\?\&]*)(href|src|action|background|window\.location)=([\"\']{1})\/([^\"\']*)([\"\']{1})([^>]*)>/i',
'/<([^>\?\&]*)(href|src|action|background|window\.location)=([\"\']{1})\?([^\"\']*)([\"\']{1})([^>]*)>/i'//This dir
,
'/<([^>\?\&]*)(href|src|action|background|window\.location)=([\"\']{1})([^#]{1}[^\/\"\'>]*)([\"\']{1})([^>]*)>/i',
'/<([^>\?\&]*)(href|src|action|background|window\.location)=([\"\']{1})(?:\.\/)?([^\"\'\/:]*\/*)?([^\"\'\/:]*\/*)?([^\"\'\/:]*\/*)?([a-zA-Z0-9_\-]+)\.([^\"\'>]*)([\"\']{1})([^>]*)>/i',
'/[^"\'a-zA-Z_0-9](window\.open|url)\(([\"\']{0,1})(?:\.\/)?([^\"\'\/]*)\.([^\"\'\/]+)([\"\']*)([^\)]*)/i',
'/<meta([^>]*)url=([a-zA-Z0-9_\-]+)\.([^\"\'>]*)([\"\']{1})([^>]*)>/i',
];
$out = [
'<\\1\\2="\\3"\\4>',
'<\\1\\2=\\3' . $host . $d3 . '/\\4\\5\\6>',
'<\\1\\2=\\3' . $host . $d2 . '/\\4\\5\\6>',
'<\\1\\2=\\3' . $host . $d1 . '/\\4\\5\\6>',
'<\\1\\2=\\3' . $host . '/\\4\\5\\6>',
'<\\1\\2=\\3' . $host . $_SERVER['SCRIPT_NAME'] . '?\\4\\5\\6>'//This dir.
,
'<\\1\\2=\\3' . $host . $req_dir . '/\\4\\5\\6\\7>',
'<\\1\\2=\\3' . $host . $req_dir . '/\\4\\5\\6\\7.\\8\\9\\10>',
'$1($2' . $host . $req_dir . '/$3.$4$5$6',
'<meta$1url=' . $host . $req_dir . '/$2.$3$4$5>',
];
return preg_replace($in, $out, $s);
}