adslight_seo_cat()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
c 0
b 0
f 0
dl 0
loc 12
rs 10
cc 2
nc 2
nop 1
1
<?php declare(strict_types=1);
2
/*
3
 * You may not change or alter any portion of this comment or credits
4
 * of supporting developers from this source code or any supporting source code
5
 * which is considered copyrighted (c) material of the original comment or credit authors.
6
 *
7
 * This program is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
 */
11
12
/**
13
 * @copyright    XOOPS Project (https://xoops.org)
14
 * @license      GNU GPL 2.0 or later (https://www.gnu.org/licenses/gpl-2.0.html)
15
 * @author       XOOPS Development Team
16
 * @author       Pascal Le Boustouller: original author ([email protected])
17
 * @author       Luc Bizet (www.frxoops.org)
18
 * @author       jlm69 (www.jlmzone.com)
19
 * @author       mamba (www.xoops.org)
20
 */
21
/////////////////////////////////////
22
// AdsLight UrlRewrite By Nikita   //
23
// https://www.aideordi.com         //
24
/////////////////////////////////////
25
26
define('REAL_MODULE_NAME', 'adslight');
27
define('SEO_MODULE_NAME', 'annonces');
28
29
ob_start('seo_urls');
30
31
/**
32
 * @param $s
33
 *
34
 * @return string|string[]|null
35
 */
36
function seo_urls($s)
37
{
38
    $XPS_URL = str_replace('/', '\/', quotemeta(XOOPS_URL));
39
    $s       = adslight_absolutize($s); // Fix URLs and HTML.
40
41
    $module_name = REAL_MODULE_NAME;
42
43
    $search = [
44
        // Search URLs of modules' directry.
45
        '/<(a|meta)([^>]*)(href|url)=([\'\"]{0,1})' . $XPS_URL . '\/modules\/' . $module_name . '\/(viewcats.php)([^>\'\"]*)([\'\"]{1})([^>]*)>/i',
46
        '/<(a|meta)([^>]*)(href|url)=([\'\"]{0,1})' . $XPS_URL . '\/modules\/' . $module_name . '\/(viewads.php)([^>\'\"]*)([\'\"]{1})([^>]*)>/i',
47
        '/<(a|meta)([^>]*)(href|url)=([\'\"]{0,1})' . $XPS_URL . '\/modules\/' . $module_name . '\/(index.php)([^>\'\"]*)([\'\"]{1})([^>]*)>/i',
48
        //    '/<(a|meta)([^>]*)(href|url)=([\'\"]{0,1})'.$XPS_URL.'\/modules\/'.$module_name.'\/()([^>\'\"]*)([\'\"]{1})([^>]*)>/i',
49
    ];
50
51
    return preg_replace_callback($search, 'replaceLinks', $s);
52
}
53
54
/**
55
 * @param $matches
56
 * @return string
57
 */
58
function replaceLinks($matches): string
59
{
60
    $req_string = [];
61
    $add_to_url = '';
62
    switch ($matches[5]) {
63
        case 'viewcats.php':
64
            //            $add_to_url = '';
65
            $req_string = $matches[6];
66
            if (!empty($matches[6])) {
67
                //              replacing cid=x
68
                if (preg_match('/cid=(\d+)/', $matches[6], $mvars)) {
69
                    $add_to_url = 'c' . $mvars[1] . '/' . adslight_seo_cat($mvars[1]) . '.html';
70
                    $req_string = preg_replace('/cid=\d+/', '', $matches[6]);
71
                } else {
72
                    return $matches['0'];
73
                }
74
            }
75
            break;
76
        case 'viewads.php':
77
            //            $add_to_url = '';
78
            $req_string = $matches[6];
79
            if (!empty($matches[6])) {
80
                //              replacing lid=x
81
                if (preg_match('/lid=(\d+)/', $matches[6], $mvars)) {
82
                    $add_to_url = 'p' . $mvars[1] . '/' . adslight_seo_titre($mvars[1]) . '.html';
83
                    $req_string = preg_replace('/lid=\d+/', '', $matches[6]);
84
                } else {
85
                    return $matches['0'];
86
                }
87
            }
88
            break;
89
        default:
90
            break;
91
    }
92
    if ('?' === $req_string) {
93
        $req_string = '';
94
    }
95
96
    return '<' . $matches[1] . $matches[2] . $matches[3] . '=' . $matches[4] . XOOPS_URL . '/' . SEO_MODULE_NAME . '/' . $add_to_url . $req_string . $matches[7] . $matches[8] . '>';
97
}
98
99
/**
100
 * @param $cid
101
 *
102
 * @return string|array<string>|null
103
 */
104
function adslight_seo_cat($cid)
105
{
106
    /** @var \XoopsMySQLDatabase $xoopsDB */
107
    $xoopsDB = \XoopsDatabaseFactory::getDatabaseConnection();
108
    $sql     = ' SELECT title FROM ' . $xoopsDB->prefix('adslight_categories') . ' WHERE cid = ' . $cid . ' ';
109
    $result = $xoopsDB->query($sql);
110
    if (!$xoopsDB->isResultSet($result)) {
111
        \trigger_error("Query Failed! SQL: $sql- Error: " . $xoopsDB->error(), E_USER_ERROR);
112
    }
113
    $res     = $xoopsDB->fetchArray($result);
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type boolean; however, parameter $result of XoopsMySQLDatabase::fetchArray() does only seem to accept mysqli_result, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

113
    $res     = $xoopsDB->fetchArray(/** @scrutinizer ignore-type */ $result);
Loading history...
114
115
    return adslight_seo_title($res['title']);
116
}
117
118
/**
119
 * @param $lid
120
 *
121
 * @return string|array<string>|null
122
 */
123
function adslight_seo_titre($lid)
124
{
125
    /** @var \XoopsMySQLDatabase $xoopsDB */
126
    $xoopsDB = \XoopsDatabaseFactory::getDatabaseConnection();
127
    $sql     = ' SELECT title FROM ' . $xoopsDB->prefix('adslight_listing') . ' WHERE lid = ' . $lid . ' ';
128
    $result = $xoopsDB->query($sql);
129
    if (!$xoopsDB->isResultSet($result)) {
130
        \trigger_error("Query Failed! SQL: $sql- Error: " . $xoopsDB->error(), E_USER_ERROR);
131
    }
132
    $res     = $xoopsDB->fetchArray($result);
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type boolean; however, parameter $result of XoopsMySQLDatabase::fetchArray() does only seem to accept mysqli_result, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

132
    $res     = $xoopsDB->fetchArray(/** @scrutinizer ignore-type */ $result);
Loading history...
133
134
    return adslight_seo_title($res['title']);
135
}
136
137
/**
138
 * @param string $title
139
 * @param bool   $withExt
140
 *
141
 * @return string|array<string>|null
142
 */
143
function adslight_seo_title($title = '', $withExt = false)
144
{
145
    /**
146
     * if XOOPS ML is present, let's sanitize the title with the current language
147
     */
148
    $myts = \MyTextSanitizer::getInstance();
149
    if (method_exists($myts, 'formatForML')) {
150
        $title = $myts->formatForML($title);
151
    }
152
153
    // Transformation de la chaine en minuscule
154
    // String encoding to avoid 500 errors in case of unforeseen characters
155
    $title = rawurlencode(mb_strtolower($title));
156
157
    // Transformation des ponctuations
158
    //                 Tab     Space      !        "        #        %        &        '        (        )        ,        /        :        ;        <        =        >        ?        @        [        \        ]        ^        {        |        }        ~       .                 +
159
    $pattern = [
160
        '/%09/', // Tab
161
        '/%20/', // Space
162
        '/%21/', // !
163
        '/%22/', // "
164
        '/%23/', // #
165
        '/%25/', // %
166
        '/%26/', // &
167
        '/%27/', // '
168
        '/%28/', // (
169
        '/%29/', // )
170
        '/%2C/', // ,
171
        '/%2F/', // /
172
        '/%3A/', // :
173
        '/%3B/', // ;
174
        '/%3C/', // <
175
        '/%3D/', // =
176
        '/%3E/', // >
177
        '/%3F/', // ?
178
        '/%40/', // @
179
        '/%5B/', // [
180
        '/%5C/', // \
181
        '/%5D/', // ]
182
        '/%5E/', // ^
183
        '/%7B/', // {
184
        '/%7C/', // |
185
        '/%7D/', // }
186
        '/%7E/', // ~
187
        '/\./', // .
188
        '/%2A/',
189
        '/%2B/',
190
        '/quot/',
191
    ];
192
    $rep_pat = [
193
        '-',
194
        '-',
195
        '',
196
        '',
197
        '',
198
        '-100',
199
        '',
200
        '-',
201
        '',
202
        '',
203
        '',
204
        '-',
205
        '',
206
        '',
207
        '',
208
        '-',
209
        '',
210
        '',
211
        '-at-',
212
        '',
213
        '-',
214
        '',
215
        '-',
216
        '',
217
        '-',
218
        '',
219
        '-',
220
        '',
221
        '',
222
        '+',
223
        '',
224
    ];
225
    $title   = preg_replace($pattern, $rep_pat, $title);
226
227
    // Transformation of characters with accents
228
    //                  °        è        é        ê        ë        ç        à        â        ä        î        ï        ù        ü        û        ô        ö
229
    $pattern = [
230
        '/%B0/',        // °
231
        '/%E8/',        // è
232
        '/%E9/',        // é
233
        '/%EA/',        // ê
234
        '/%EB/',        // ë
235
        '/%E7/',        // ç
236
        '/%E0/',        // à
237
        '/%E2/',        // â
238
        '/%E4/',        // ä
239
        '/%EE/',        // î
240
        '/%EF/',        // ï
241
        '/%F9/',        // ù
242
        '/%FC/',        // ü
243
        '/%FB/',        // û
244
        '/%F4/',        // ô
245
        '/%F6/',        // ö
246
        '/%E3%A8/',
247
        '/%E3%A9/',
248
        '/%E3%A0/',
249
        '/%E3%AA/',
250
        '/%E3%A2/',
251
        '/a%80%9C/',
252
        '/a%80%9D/',
253
        '/%E3%A7/',
254
    ];
255
    $rep_pat = [
256
        '-',
257
        'e',
258
        'e',
259
        'e',
260
        'e',
261
        'c',
262
        'a',
263
        'a',
264
        'a',
265
        'i',
266
        'i',
267
        'u',
268
        'u',
269
        'u',
270
        'o',
271
        'o',
272
        'e',
273
        'e',
274
        'a',
275
        'e',
276
        'a',
277
        '-',
278
        '-',
279
        'c',
280
    ];
281
    $title   = preg_replace($pattern, $rep_pat, $title);
282
283
    if (count($title) > 0) {
0 ignored issues
show
Bug introduced by
$title of type string is incompatible with the type Countable|array expected by parameter $value of count(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

283
    if (count(/** @scrutinizer ignore-type */ $title) > 0) {
Loading history...
284
        if ($withExt) {
285
            $title .= '.html';
286
        }
287
288
        return $title;
289
    }
290
291
    return '';
292
}
293
294
/**
295
 * @param $s
296
 *
297
 * @return string|string[]|null
298
 */
299
function adslight_absolutize($s)
300
{
301
    if (preg_match('/\/$/', $_SERVER['REQUEST_URI'])) {
302
        $req_dir = preg_replace('/\/$/', '', $_SERVER['REQUEST_URI']);
303
        $req_php = '';
0 ignored issues
show
Unused Code introduced by
The assignment to $req_php is dead and can be removed.
Loading history...
304
    } else {
305
        $req_dir = dirname($_SERVER['REQUEST_URI']);
306
        $req_php = preg_replace('/.*(\/[a-zA-Z0-9_\-]+)\.php.*/', '\\1.php', $_SERVER['REQUEST_URI']);
307
    }
308
    $req_dir = '\\' === $req_dir || '/' === $req_dir ? '' : $req_dir;
309
    $dir_arr = explode('/', $req_dir);
310
    $m       = count($dir_arr) - 1;
311
    $d1      = @str_replace('/' . $dir_arr[$m], '', $req_dir);
312
    $d2      = @str_replace('/' . $dir_arr[$m - 1], '', $d1);
313
    $d3      = @str_replace('/' . $dir_arr[$m - 2], '', $d2);
314
    $d4      = @str_replace('/' . $dir_arr[$m - 3], '', $d3);
315
    $d5      = @str_replace('/' . $dir_arr[$m - 4], '', $d4);
0 ignored issues
show
Unused Code introduced by
The assignment to $d5 is dead and can be removed.
Loading history...
316
    $host    = 'http://' . $_SERVER['HTTP_HOST'];
317
    $in      = [
318
        '/<([^>\?\&]*)(href|src|action|background|window\.location)=([^\"\' >]+)([^>]*)>/i',
319
        '/<([^>\?\&]*)(href|src|action|background|window\.location)=([\"\']{1})\.\.\/\.\.\/\.\.\/([^\"\']*)([\"\']{1})([^>]*)>/i',
320
        '/<([^>\?\&]*)(href|src|action|background|window\.location)=([\"\']{1})\.\.\/\.\.\/([^\"\']*)([\"\']{1})([^>]*)>/i',
321
        '/<([^>\?\&]*)(href|src|action|background|window\.location)=([\"\']{1})\.\.\/([^\"\']*)([\"\']{1})([^>]*)>/i',
322
        '/<([^>\?\&]*)(href|src|action|background|window\.location)=([\"\']{1})\/([^\"\']*)([\"\']{1})([^>]*)>/i',
323
        '/<([^>\?\&]*)(href|src|action|background|window\.location)=([\"\']{1})\?([^\"\']*)([\"\']{1})([^>]*)>/i'//This dir
324
        ,
325
        '/<([^>\?\&]*)(href|src|action|background|window\.location)=([\"\']{1})([^#]{1}[^\/\"\'>]*)([\"\']{1})([^>]*)>/i',
326
        '/<([^>\?\&]*)(href|src|action|background|window\.location)=([\"\']{1})(?:\.\/)?([^\"\'\/:]*\/*)?([^\"\'\/:]*\/*)?([^\"\'\/:]*\/*)?([a-zA-Z0-9_\-]+)\.([^\"\'>]*)([\"\']{1})([^>]*)>/i',
327
        '/[^"\'a-zA-Z_0-9](window\.open|url)\(([\"\']{0,1})(?:\.\/)?([^\"\'\/]*)\.([^\"\'\/]+)([\"\']*)([^\)]*)/i',
328
        '/<meta([^>]*)url=([a-zA-Z0-9_\-]+)\.([^\"\'>]*)([\"\']{1})([^>]*)>/i',
329
    ];
330
    $out     = [
331
        '<\\1\\2="\\3"\\4>',
332
        '<\\1\\2=\\3' . $host . $d3 . '/\\4\\5\\6>',
333
        '<\\1\\2=\\3' . $host . $d2 . '/\\4\\5\\6>',
334
        '<\\1\\2=\\3' . $host . $d1 . '/\\4\\5\\6>',
335
        '<\\1\\2=\\3' . $host . '/\\4\\5\\6>',
336
        '<\\1\\2=\\3' . $host . $_SERVER['SCRIPT_NAME'] . '?\\4\\5\\6>'//This dir.
337
        ,
338
        '<\\1\\2=\\3' . $host . $req_dir . '/\\4\\5\\6\\7>',
339
        '<\\1\\2=\\3' . $host . $req_dir . '/\\4\\5\\6\\7.\\8\\9\\10>',
340
        '$1($2' . $host . $req_dir . '/$3.$4$5$6',
341
        '<meta$1url=' . $host . $req_dir . '/$2.$3$4$5>',
342
    ];
343
344
    return preg_replace($in, $out, $s);
345
}
346