Completed
Push — master ( 67bb37...e20777 )
by Michael
02:35
created

seo_url.php ➔ adslight_seo_cat()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 10

Duplication

Lines 17
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 1
dl 17
loc 17
rs 9.4285
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 29 and the first side effect is on line 32.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/*
3
-------------------------------------------------------------------------
4
                     ADSLIGHT 2 : Module for Xoops
5
6
        Redesigned and ameliorate By Luc Bizet user at www.frxoops.org
7
        Started with the Classifieds module and made MANY changes
8
        Website : http://www.luc-bizet.fr
9
        Contact : [email protected]
10
-------------------------------------------------------------------------
11
             Original credits below Version History
12
##########################################################################
13
#                    Classified Module for Xoops                         #
14
#  By John Mordo user jlm69 at www.xoops.org and www.jlmzone.com         #
15
#      Started with the MyAds module and made MANY changes               #
16
##########################################################################
17
 Original Author: Pascal Le Boustouller
18
 Author Website : [email protected]
19
 Licence Type   : GPL
20
-------------------------------------------------------------------------
21
*/
22
/////////////////////////////////////
23
// AdsLight UrlRewrite By Nikita   //
24
// http://www.aideordi.com         //
25
/////////////////////////////////////
26
27
// defined('XOOPS_ROOT_PATH') || exit('XOOPS root path not defined');
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
28
29
define('REAL_MODULE_NAME', 'adslight');
30
define('SEO_MODULE_NAME', 'annonces');
31
32
ob_start('seo_urls');
33
34
/**
35
 * @param $s
36
 *
37
 * @return mixed
38
 */
39
function seo_urls($s)
40
{
41
    $XPS_URL = str_replace('/', '\/', quotemeta(XOOPS_URL));
42
    $s       = adslight_absolutize($s); // Fix URLs and HTML.
43
44
    $module_name = REAL_MODULE_NAME;
45
46
    $search = array(
47
        // Search URLs of modules' directry.
48
        '/<(a|meta)([^>]*)(href|url)=([\'\"]{0,1})' . $XPS_URL . '\/modules\/' . $module_name . '\/(viewcats.php)([^>\'\"]*)([\'\"]{1})([^>]*)>/i',
49
        '/<(a|meta)([^>]*)(href|url)=([\'\"]{0,1})' . $XPS_URL . '\/modules\/' . $module_name . '\/(viewads.php)([^>\'\"]*)([\'\"]{1})([^>]*)>/i',
50
        '/<(a|meta)([^>]*)(href|url)=([\'\"]{0,1})' . $XPS_URL . '\/modules\/' . $module_name . '\/(index.php)([^>\'\"]*)([\'\"]{1})([^>]*)>/i',
51
        //    '/<(a|meta)([^>]*)(href|url)=([\'\"]{0,1})'.$XPS_URL.'\/modules\/'.$module_name.'\/()([^>\'\"]*)([\'\"]{1})([^>]*)>/i',
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
52
53
    );
54
    $s      = preg_replace_callback($search, 'replace_links', $s);
55
56
    return $s;
57
}
58
59
/**
60
 * @param $matches
61
 *
62
 * @return string
63
 */
64
function replace_links($matches)
65
{
66
    $req_string = array();
67
    $add_to_url = '';
68
    switch ($matches[5]) {
69 View Code Duplication
        case 'viewcats.php':
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
70
//            $add_to_url = '';
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
71
            $req_string = $matches[6];
72
            if (!empty($matches[6])) {
73
                //              replacing cid=x
74
                if (preg_match('/cid=([0-9]+)/', $matches[6], $mvars)) {
75
                    $add_to_url = 'c' . $mvars[1] . '/' . adslight_seo_cat($mvars[1]) . '.html';
76
                    $req_string = preg_replace('/cid=[0-9]+/', '', $matches[6]);
77
                } else {
78
                    return $matches['0'];
79
                }
80
            }
81
            break;
82 View Code Duplication
        case 'viewads.php':
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
83
//            $add_to_url = '';
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
84
            $req_string = $matches[6];
85
            if (!empty($matches[6])) {
86
                //              replacing lid=x
87
                if (preg_match('/lid=([0-9]+)/', $matches[6], $mvars)) {
88
                    $add_to_url = 'p' . $mvars[1] . '/' . adslight_seo_titre($mvars[1]) . '.html';
89
                    $req_string = preg_replace('/lid=[0-9]+/', '', $matches[6]);
90
                } else {
91
                    return $matches['0'];
92
                }
93
            }
94
            break;
95
96
        default:
97
            break;
98
    }
99
    if ($req_string === '?') {
100
        $req_string = '';
101
    }
102
    $ret = '<' . $matches[1] . $matches[2] . $matches[3] . '=' . $matches[4] . XOOPS_URL . '/' . SEO_MODULE_NAME . '/' . $add_to_url . $req_string . $matches[7] . $matches[8] . '>';
103
104
    return $ret;
105
}
106
107
/**
108
 * @param $cid
109
 *
110
 * @return mixed|string
111
 */
112 View Code Duplication
function adslight_seo_cat($cid)
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
113
{
114
    global $xoopsDB;
115
    $db     = XoopsDatabaseFactory::getDatabaseConnection();
116
    $query  = '
117
        SELECT
118
            title
119
        FROM
120
            ' . $xoopsDB->prefix('adslight_categories') . '
121
        WHERE
122
            cid = ' . $cid . '';
123
    $result = $db->query($query);
124
    $res    = $db->fetchArray($result);
125
    $ret    = adslight_seo_title($res['title']);
126
127
    return $ret;
128
}
129
130
/**
131
 * @param $lid
132
 *
133
 * @return mixed|string
134
 */
135 View Code Duplication
function adslight_seo_titre($lid)
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
136
{
137
    global $xoopsDB;
138
    $db     = XoopsDatabaseFactory::getDatabaseConnection();
139
    $query  = '
140
        SELECT
141
            title
142
        FROM
143
            ' . $xoopsDB->prefix('adslight_listing') . '
144
        WHERE
145
            lid = ' . $lid . '';
146
    $result = $db->query($query);
147
    $res    = $db->fetchArray($result);
148
    $ret    = adslight_seo_title($res['title']);
149
150
    return $ret;
151
}
152
153
/**
154
 * @param string $title
155
 * @param bool   $withExt
156
 *
157
 * @return mixed|string
158
 */
159
function adslight_seo_title($title = '', $withExt = false)
160
{
161
    /**
162
     * if XOOPS ML is present, let's sanitize the title with the current language
163
     */
164
    $myts = MyTextSanitizer::getInstance();
165
    if (method_exists($myts, 'formatForML')) {
166
        $title = $myts->formatForML($title);
167
    }
168
169
    // Transformation de la chaine en minuscule
170
    // Codage de la chaine afin d'�viter les erreurs 500 en cas de caract�res impr�vus
171
    $title = rawurlencode(strtolower($title));
172
173
    // Transformation des ponctuations
174
    //                 Tab     Space      !        "        #        %        &        '        (        )        ,        /        :        ;        <        =        >        ?        @        [        \        ]        ^        {        |        }        ~       .                 +
175
    $pattern = array(
176
        '/%09/',
177
        '/%20/',
178
        '/%21/',
179
        '/%22/',
180
        '/%23/',
181
        '/%25/',
182
        '/%26/',
183
        '/%27/',
184
        '/%28/',
185
        '/%29/',
186
        '/%2C/',
187
        '/%2F/',
188
        '/%3A/',
189
        '/%3B/',
190
        '/%3C/',
191
        '/%3D/',
192
        '/%3E/',
193
        '/%3F/',
194
        '/%40/',
195
        '/%5B/',
196
        '/%5C/',
197
        '/%5D/',
198
        '/%5E/',
199
        '/%7B/',
200
        '/%7C/',
201
        '/%7D/',
202
        '/%7E/',
203
        "/\./",
204
        '/%2A/',
205
        '/%2B/',
206
        '/quot/'
207
    );
208
    $rep_pat = array(
209
        '-',
210
        '-',
211
        '',
212
        '',
213
        '',
214
        '-100',
215
        '',
216
        '-',
217
        '',
218
        '',
219
        '',
220
        '-',
221
        '',
222
        '',
223
        '',
224
        '-',
225
        '',
226
        '',
227
        '-at-',
228
        '',
229
        '-',
230
        '',
231
        '-',
232
        '',
233
        '-',
234
        '',
235
        '-',
236
        '',
237
        '',
238
        '+',
239
        ''
240
    );
241
    $title   = preg_replace($pattern, $rep_pat, $title);
242
243
    // Transformation des caract�res accentu�s
244
    //                  °        è        é        ê        ë        ç        à        â        ä        î        ï        ù        ü        û        ô        ö
245
    $pattern = array(
246
        '/%B0/',
247
        '/%E8/',
248
        '/%E9/',
249
        '/%EA/',
250
        '/%EB/',
251
        '/%E7/',
252
        '/%E0/',
253
        '/%E2/',
254
        '/%E4/',
255
        '/%EE/',
256
        '/%EF/',
257
        '/%F9/',
258
        '/%FC/',
259
        '/%FB/',
260
        '/%F4/',
261
        '/%F6/',
262
        '/%E3%A8/',
263
        '/%E3%A9/',
264
        '/%E3%A0/',
265
        '/%E3%AA/',
266
        '/%E3%A2/',
267
        '/a%80%9C/',
268
        '/a%80%9D/',
269
        '/%E3%A7/'
270
    );
271
    $rep_pat = array(
272
        '-',
273
        'e',
274
        'e',
275
        'e',
276
        'e',
277
        'c',
278
        'a',
279
        'a',
280
        'a',
281
        'i',
282
        'i',
283
        'u',
284
        'u',
285
        'u',
286
        'o',
287
        'o',
288
        'e',
289
        'e',
290
        'a',
291
        'e',
292
        'a',
293
        '-',
294
        '-',
295
        'c'
296
    );
297
    $title   = preg_replace($pattern, $rep_pat, $title);
298
299
    if (count($title) > 0) {
300
        if ($withExt) {
301
            $title .= '.html';
302
        }
303
304
        return $title;
305
    } else {
306
        return '';
307
    }
308
}
309
310
/**
311
 * @param $s
312
 *
313
 * @return mixed
314
 */
315
function adslight_absolutize($s)
0 ignored issues
show
Coding Style introduced by
adslight_absolutize uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
316
{
317
    if (preg_match('/\/$/', $_SERVER['REQUEST_URI'])) {
318
        $req_dir = preg_replace('/\/$/', '', $_SERVER['REQUEST_URI']);
319
        $req_php = '';
0 ignored issues
show
Unused Code introduced by
$req_php is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
320
    } else {
321
        $req_dir = dirname($_SERVER['REQUEST_URI']);
322
        $req_php = preg_replace('/.*(\/[a-zA-Z0-9_\-]+)\.php.*/', '\\1.php', $_SERVER['REQUEST_URI']);
0 ignored issues
show
Unused Code introduced by
$req_php is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
323
    }
324
    $req_dir = ($req_dir === "\\" || $req_dir === '/') ? '' : $req_dir;
325
    $dir_arr = explode('/', $req_dir);
326
    $m       = count($dir_arr) - 1;
327
    $d1      = @str_replace('/' . $dir_arr[$m], '', $req_dir);
328
    $d2      = @str_replace('/' . $dir_arr[$m - 1], '', $d1);
329
    $d3      = @str_replace('/' . $dir_arr[$m - 2], '', $d2);
330
    $d4      = @str_replace('/' . $dir_arr[$m - 3], '', $d3);
331
    $d5      = @str_replace('/' . $dir_arr[$m - 4], '', $d4);
0 ignored issues
show
Unused Code introduced by
$d5 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
332
    $host    = 'http://' . $_SERVER['HTTP_HOST'];
333
    $in      = array(
334
        '/<([^>\?\&]*)(href|src|action|background|window\.location)=([^\"\' >]+)([^>]*)>/i',
335
        '/<([^>\?\&]*)(href|src|action|background|window\.location)=([\"\']{1})\.\.\/\.\.\/\.\.\/([^\"\']*)([\"\']{1})([^>]*)>/i',
336
        '/<([^>\?\&]*)(href|src|action|background|window\.location)=([\"\']{1})\.\.\/\.\.\/([^\"\']*)([\"\']{1})([^>]*)>/i',
337
        '/<([^>\?\&]*)(href|src|action|background|window\.location)=([\"\']{1})\.\.\/([^\"\']*)([\"\']{1})([^>]*)>/i',
338
        '/<([^>\?\&]*)(href|src|action|background|window\.location)=([\"\']{1})\/([^\"\']*)([\"\']{1})([^>]*)>/i',
339
        '/<([^>\?\&]*)(href|src|action|background|window\.location)=([\"\']{1})\?([^\"\']*)([\"\']{1})([^>]*)>/i'//This dir
340
        ,
341
        '/<([^>\?\&]*)(href|src|action|background|window\.location)=([\"\']{1})([^#]{1}[^\/\"\'>]*)([\"\']{1})([^>]*)>/i',
342
        '/<([^>\?\&]*)(href|src|action|background|window\.location)=([\"\']{1})(?:\.\/)?([^\"\'\/:]*\/*)?([^\"\'\/:]*\/*)?([^\"\'\/:]*\/*)?([a-zA-Z0-9_\-]+)\.([^\"\'>]*)([\"\']{1})([^>]*)>/i',
343
        '/[^"\'a-zA-Z_0-9](window\.open|url)\(([\"\']{0,1})(?:\.\/)?([^\"\'\/]*)\.([^\"\'\/]+)([\"\']*)([^\)]*)/i',
344
        '/<meta([^>]*)url=([a-zA-Z0-9_\-]+)\.([^\"\'>]*)([\"\']{1})([^>]*)>/i'
345
    );
346
    $out     = array(
347
        '<\\1\\2="\\3"\\4>',
348
        '<\\1\\2=\\3' . $host . $d3 . '/\\4\\5\\6>',
349
        '<\\1\\2=\\3' . $host . $d2 . '/\\4\\5\\6>',
350
        '<\\1\\2=\\3' . $host . $d1 . '/\\4\\5\\6>',
351
        '<\\1\\2=\\3' . $host . '/\\4\\5\\6>',
352
        '<\\1\\2=\\3' . $host . $_SERVER['PHP_SELF'] . '?\\4\\5\\6>'//This dir.
353
        ,
354
        '<\\1\\2=\\3' . $host . $req_dir . '/\\4\\5\\6\\7>',
355
        '<\\1\\2=\\3' . $host . $req_dir . '/\\4\\5\\6\\7.\\8\\9\\10>',
356
        '$1($2' . $host . $req_dir . '/$3.$4$5$6',
357
        '<meta$1url=' . $host . $req_dir . '/$2.$3$4$5>'
358
    );
359
    $s       = preg_replace($in, $out, $s);
360
361
    return $s;
362
}
363